blob: 8b64fe0201f25509fab7c870bbaf2aea9852e013 [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"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000029#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "handle.h"
31#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "primitive.h"
David Brazdild26a4112015-11-10 11:07:31 +000038#include "utils/array_ref.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000039
40namespace art {
41
David Brazdil1abb4192015-02-17 18:33:36 +000042class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010046class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000048class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000051class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000052class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000054class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010056class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010057class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000059class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010060class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062
Mathieu Chartier736b5602015-09-02 14:54:11 -070063namespace mirror {
64class DexCache;
65} // namespace mirror
66
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067static const int kDefaultNumberOfBlocks = 8;
68static const int kDefaultNumberOfSuccessors = 2;
69static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010070static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010071static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000072static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Roland Levillain5b5b9312016-03-22 14:57:31 +000074// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
75static constexpr int32_t kMaxIntShiftDistance = 0x1f;
76// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
77static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000078
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010079static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070080static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010081
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010082static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
83
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060084static constexpr uint32_t kNoDexPc = -1;
85
Dave Allison20dfc792014-06-16 20:44:29 -070086enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070087 // All types.
88 kCondEQ, // ==
89 kCondNE, // !=
90 // Signed integers and floating-point numbers.
91 kCondLT, // <
92 kCondLE, // <=
93 kCondGT, // >
94 kCondGE, // >=
95 // Unsigned integers.
96 kCondB, // <
97 kCondBE, // <=
98 kCondA, // >
99 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -0700100};
101
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000102enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000103 kAnalysisSkipped,
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
David Brazdildee58d62016-04-07 09:54:26 +0000430 uint16_t GetNumberOfInVRegs() const {
431 return number_of_in_vregs_;
432 }
433
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100434 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100435 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100436 return number_of_vregs_ - number_of_in_vregs_;
437 }
438
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100439 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100440 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100441 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100442
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100443 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100444 return linear_order_;
445 }
446
Mark Mendell1152c922015-04-24 17:06:35 -0400447 bool HasBoundsChecks() const {
448 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800449 }
450
Mark Mendell1152c922015-04-24 17:06:35 -0400451 void SetHasBoundsChecks(bool value) {
452 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800453 }
454
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100455 bool ShouldGenerateConstructorBarrier() const {
456 return should_generate_constructor_barrier_;
457 }
458
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000459 bool IsDebuggable() const { return debuggable_; }
460
David Brazdil8d5b8b22015-03-24 10:51:52 +0000461 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000462 // already, it is created and inserted into the graph. This method is only for
463 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600464 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000465
466 // TODO: This is problematic for the consistency of reference type propagation
467 // because it can be created anytime after the pass and thus it will be left
468 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600469 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000470
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600471 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
472 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000473 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600474 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
475 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000476 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600477 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
478 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000479 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600480 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
481 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000482 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000483
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100484 HCurrentMethod* GetCurrentMethod();
485
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100486 const DexFile& GetDexFile() const {
487 return dex_file_;
488 }
489
490 uint32_t GetMethodIdx() const {
491 return method_idx_;
492 }
493
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100494 InvokeType GetInvokeType() const {
495 return invoke_type_;
496 }
497
Mark Mendellc4701932015-04-10 13:18:51 -0400498 InstructionSet GetInstructionSet() const {
499 return instruction_set_;
500 }
501
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000502 bool IsCompilingOsr() const { return osr_; }
503
David Brazdil77a48ae2015-09-15 12:34:04 +0000504 bool HasTryCatch() const { return has_try_catch_; }
505 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100506
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000507 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
508 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
509
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100510 ArtMethod* GetArtMethod() const { return art_method_; }
511 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
512
Mark Mendellf6529172015-11-17 11:16:56 -0500513 // Returns an instruction with the opposite boolean value from 'cond'.
514 // The instruction has been inserted into the graph, either as a constant, or
515 // before cursor.
516 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
517
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000518 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
519
David Brazdil2d7352b2015-04-20 14:52:42 +0100520 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000521 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100522 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000523
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000524 template <class InstructionType, typename ValueType>
525 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600526 ArenaSafeMap<ValueType, InstructionType*>* cache,
527 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000528 // Try to find an existing constant of the given value.
529 InstructionType* constant = nullptr;
530 auto cached_constant = cache->find(value);
531 if (cached_constant != cache->end()) {
532 constant = cached_constant->second;
533 }
534
535 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100536 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000537 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600538 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000539 cache->Overwrite(value, constant);
540 InsertConstant(constant);
541 }
542 return constant;
543 }
544
David Brazdil8d5b8b22015-03-24 10:51:52 +0000545 void InsertConstant(HConstant* instruction);
546
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000547 // Cache a float constant into the graph. This method should only be
548 // called by the SsaBuilder when creating "equivalent" instructions.
549 void CacheFloatConstant(HFloatConstant* constant);
550
551 // See CacheFloatConstant comment.
552 void CacheDoubleConstant(HDoubleConstant* constant);
553
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000554 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000555
556 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100557 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000558
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100559 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100560 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000561
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100562 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100563 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100564
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000565 HBasicBlock* entry_block_;
566 HBasicBlock* exit_block_;
567
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100568 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100569 uint16_t maximum_number_of_out_vregs_;
570
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100571 // The number of virtual registers in this method. Contains the parameters.
572 uint16_t number_of_vregs_;
573
574 // The number of virtual registers used by parameters of this method.
575 uint16_t number_of_in_vregs_;
576
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000577 // Number of vreg size slots that the temporaries use (used in baseline compiler).
578 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100579
Mark Mendell1152c922015-04-24 17:06:35 -0400580 // Has bounds checks. We can totally skip BCE if it's false.
581 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800582
David Brazdil77a48ae2015-09-15 12:34:04 +0000583 // Flag whether there are any try/catch blocks in the graph. We will skip
584 // try/catch-related passes if false.
585 bool has_try_catch_;
586
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000587 // Flag whether there are any irreducible loops in the graph.
588 bool has_irreducible_loops_;
589
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000590 // Indicates whether the graph should be compiled in a way that
591 // ensures full debuggability. If false, we can apply more
592 // aggressive optimizations that may limit the level of debugging.
593 const bool debuggable_;
594
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000595 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000596 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000597
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100598 // The dex file from which the method is from.
599 const DexFile& dex_file_;
600
601 // The method index in the dex file.
602 const uint32_t method_idx_;
603
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100604 // If inlined, this encodes how the callee is being invoked.
605 const InvokeType invoke_type_;
606
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100607 // Whether the graph has been transformed to SSA form. Only used
608 // in debug mode to ensure we are not using properties only valid
609 // for non-SSA form (like the number of temporaries).
610 bool in_ssa_form_;
611
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100612 const bool should_generate_constructor_barrier_;
613
Mathieu Chartiere401d142015-04-22 13:56:20 -0700614 const InstructionSet instruction_set_;
615
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000616 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000617 HNullConstant* cached_null_constant_;
618 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000619 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000620 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000621 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000622
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100623 HCurrentMethod* cached_current_method_;
624
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100625 // The ArtMethod this graph is for. Note that for AOT, it may be null,
626 // for example for methods whose declaring class could not be resolved
627 // (such as when the superclass could not be found).
628 ArtMethod* art_method_;
629
David Brazdil4833f5a2015-12-16 10:37:39 +0000630 // Keep the RTI of inexact Object to avoid having to pass stack handle
631 // collection pointer to passes which may create NullConstant.
632 ReferenceTypeInfo inexact_object_rti_;
633
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000634 // Whether we are compiling this graph for on stack replacement: this will
635 // make all loops seen as irreducible and emit special stack maps to mark
636 // compiled code entries which the interpreter can directly jump to.
637 const bool osr_;
638
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000639 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100640 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000641 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000642 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000643 DISALLOW_COPY_AND_ASSIGN(HGraph);
644};
645
Vladimir Markof9f64412015-09-02 14:05:49 +0100646class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000647 public:
648 HLoopInformation(HBasicBlock* header, HGraph* graph)
649 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100650 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000651 irreducible_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100652 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100653 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000654 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100655 back_edges_.reserve(kDefaultNumberOfBackEdges);
656 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100657
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000658 bool IsIrreducible() const { return irreducible_; }
659
660 void Dump(std::ostream& os);
661
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100662 HBasicBlock* GetHeader() const {
663 return header_;
664 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000665
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000666 void SetHeader(HBasicBlock* block) {
667 header_ = block;
668 }
669
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100670 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
671 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
672 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
673
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000674 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100675 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000676 }
677
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100678 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100679 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100680 }
681
David Brazdil46e2a392015-03-16 17:31:52 +0000682 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100683 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100684 }
685
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000686 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100687 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000688 }
689
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100690 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100691
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100692 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100693 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100694 }
695
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100696 // Returns the lifetime position of the back edge that has the
697 // greatest lifetime position.
698 size_t GetLifetimeEnd() const;
699
700 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100701 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100702 }
703
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000704 // Finds blocks that are part of this loop.
705 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100706
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100707 // Returns whether this loop information contains `block`.
708 // Note that this loop information *must* be populated before entering this function.
709 bool Contains(const HBasicBlock& block) const;
710
711 // Returns whether this loop information is an inner loop of `other`.
712 // Note that `other` *must* be populated before entering this function.
713 bool IsIn(const HLoopInformation& other) const;
714
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800715 // Returns true if instruction is not defined within this loop.
716 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700717
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100718 const ArenaBitVector& GetBlocks() const { return blocks_; }
719
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000720 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000721 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000722
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000723 void ClearAllBlocks() {
724 blocks_.ClearAllBits();
725 }
726
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000727 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100728 // Internal recursive implementation of `Populate`.
729 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100730 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100731
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000732 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100733 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000734 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100735 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100736 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000737
738 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
739};
740
David Brazdilec16f792015-08-19 15:04:01 +0100741// Stores try/catch information for basic blocks.
742// Note that HGraph is constructed so that catch blocks cannot simultaneously
743// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100744class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100745 public:
746 // Try block information constructor.
747 explicit TryCatchInformation(const HTryBoundary& try_entry)
748 : try_entry_(&try_entry),
749 catch_dex_file_(nullptr),
750 catch_type_index_(DexFile::kDexNoIndex16) {
751 DCHECK(try_entry_ != nullptr);
752 }
753
754 // Catch block information constructor.
755 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
756 : try_entry_(nullptr),
757 catch_dex_file_(&dex_file),
758 catch_type_index_(catch_type_index) {}
759
760 bool IsTryBlock() const { return try_entry_ != nullptr; }
761
762 const HTryBoundary& GetTryEntry() const {
763 DCHECK(IsTryBlock());
764 return *try_entry_;
765 }
766
767 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
768
769 bool IsCatchAllTypeIndex() const {
770 DCHECK(IsCatchBlock());
771 return catch_type_index_ == DexFile::kDexNoIndex16;
772 }
773
774 uint16_t GetCatchTypeIndex() const {
775 DCHECK(IsCatchBlock());
776 return catch_type_index_;
777 }
778
779 const DexFile& GetCatchDexFile() const {
780 DCHECK(IsCatchBlock());
781 return *catch_dex_file_;
782 }
783
784 private:
785 // One of possibly several TryBoundary instructions entering the block's try.
786 // Only set for try blocks.
787 const HTryBoundary* try_entry_;
788
789 // Exception type information. Only set for catch blocks.
790 const DexFile* catch_dex_file_;
791 const uint16_t catch_type_index_;
792};
793
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100794static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100795static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100796
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000797// A block in a method. Contains the list of instructions represented
798// as a double linked list. Each block knows its predecessors and
799// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100800
Vladimir Markof9f64412015-09-02 14:05:49 +0100801class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000802 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600803 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000804 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000805 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
806 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000807 loop_information_(nullptr),
808 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000809 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100810 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100811 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100812 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000813 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000814 try_catch_information_(nullptr) {
815 predecessors_.reserve(kDefaultNumberOfPredecessors);
816 successors_.reserve(kDefaultNumberOfSuccessors);
817 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
818 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000819
Vladimir Marko60584552015-09-03 13:35:12 +0000820 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100821 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000822 }
823
Vladimir Marko60584552015-09-03 13:35:12 +0000824 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100825 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000826 }
827
David Brazdild26a4112015-11-10 11:07:31 +0000828 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
829 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
830
Vladimir Marko60584552015-09-03 13:35:12 +0000831 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
832 return ContainsElement(successors_, block, start_from);
833 }
834
835 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100836 return dominated_blocks_;
837 }
838
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100839 bool IsEntryBlock() const {
840 return graph_->GetEntryBlock() == this;
841 }
842
843 bool IsExitBlock() const {
844 return graph_->GetExitBlock() == this;
845 }
846
David Brazdil46e2a392015-03-16 17:31:52 +0000847 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000848 bool IsSingleTryBoundary() const;
849
850 // Returns true if this block emits nothing but a jump.
851 bool IsSingleJump() const {
852 HLoopInformation* loop_info = GetLoopInformation();
853 return (IsSingleGoto() || IsSingleTryBoundary())
854 // Back edges generate a suspend check.
855 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
856 }
David Brazdil46e2a392015-03-16 17:31:52 +0000857
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000858 void AddBackEdge(HBasicBlock* back_edge) {
859 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000860 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000861 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100862 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000863 loop_information_->AddBackEdge(back_edge);
864 }
865
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000866 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000867 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000868
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100869 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000870 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600871 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000872
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000873 HBasicBlock* GetDominator() const { return dominator_; }
874 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000875 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
876
877 void RemoveDominatedBlock(HBasicBlock* block) {
878 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100879 }
Vladimir Marko60584552015-09-03 13:35:12 +0000880
881 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
882 ReplaceElement(dominated_blocks_, existing, new_block);
883 }
884
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100885 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000886
887 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100888 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000889 }
890
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100891 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
892 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100893 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100894 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100895 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
896 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000897
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000898 HInstruction* GetFirstInstructionDisregardMoves() const;
899
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000900 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000901 successors_.push_back(block);
902 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000903 }
904
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100905 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
906 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100907 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000908 new_block->predecessors_.push_back(this);
909 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000910 }
911
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000912 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
913 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000914 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000915 new_block->successors_.push_back(this);
916 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000917 }
918
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100919 // Insert `this` between `predecessor` and `successor. This method
920 // preserves the indicies, and will update the first edge found between
921 // `predecessor` and `successor`.
922 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
923 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100924 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000925 successor->predecessors_[predecessor_index] = this;
926 predecessor->successors_[successor_index] = this;
927 successors_.push_back(successor);
928 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100929 }
930
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100931 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000932 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100933 }
934
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000935 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000936 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000937 }
938
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100939 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000940 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100941 }
942
943 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000944 predecessors_.push_back(block);
945 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100946 }
947
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100948 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000949 DCHECK_EQ(predecessors_.size(), 2u);
950 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100951 }
952
David Brazdil769c9e52015-04-27 13:54:09 +0100953 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000954 DCHECK_EQ(successors_.size(), 2u);
955 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100956 }
957
David Brazdilfc6a86a2015-06-26 10:33:45 +0000958 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000959 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100960 }
961
David Brazdilfc6a86a2015-06-26 10:33:45 +0000962 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000963 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100964 }
965
David Brazdilfc6a86a2015-06-26 10:33:45 +0000966 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000967 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100968 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000969 }
970
971 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000972 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100973 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000974 }
975
976 // Returns whether the first occurrence of `predecessor` in the list of
977 // predecessors is at index `idx`.
978 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100979 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000980 return GetPredecessorIndexOf(predecessor) == idx;
981 }
982
David Brazdild7558da2015-09-22 13:04:14 +0100983 // Create a new block between this block and its predecessors. The new block
984 // is added to the graph, all predecessor edges are relinked to it and an edge
985 // is created to `this`. Returns the new empty block. Reverse post order or
986 // loop and try/catch information are not updated.
987 HBasicBlock* CreateImmediateDominator();
988
David Brazdilfc6a86a2015-06-26 10:33:45 +0000989 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100990 // created, latter block. Note that this method will add the block to the
991 // graph, create a Goto at the end of the former block and will create an edge
992 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100993 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000994 HBasicBlock* SplitBefore(HInstruction* cursor);
995
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000996 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000997 // created block. Note that this method just updates raw block information,
998 // like predecessors, successors, dominators, and instruction list. It does not
999 // update the graph, reverse post order, loop information, nor make sure the
1000 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001001 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1002
1003 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1004 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001005
1006 // Merge `other` at the end of `this`. Successors and dominated blocks of
1007 // `other` are changed to be successors and dominated blocks of `this`. Note
1008 // that this method does not update the graph, reverse post order, loop
1009 // information, nor make sure the blocks are consistent (for example ending
1010 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001011 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001012
1013 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1014 // of `this` are moved to `other`.
1015 // Note that this method does not update the graph, reverse post order, loop
1016 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001017 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001018 void ReplaceWith(HBasicBlock* other);
1019
David Brazdil2d7352b2015-04-20 14:52:42 +01001020 // Merge `other` at the end of `this`. This method updates loops, reverse post
1021 // order, links to predecessors, successors, dominators and deletes the block
1022 // from the graph. The two blocks must be successive, i.e. `this` the only
1023 // predecessor of `other` and vice versa.
1024 void MergeWith(HBasicBlock* other);
1025
1026 // Disconnects `this` from all its predecessors, successors and dominator,
1027 // removes it from all loops it is included in and eventually from the graph.
1028 // The block must not dominate any other block. Predecessors and successors
1029 // are safely updated.
1030 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001031
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001032 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001033 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001034 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001035 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001036 // Replace instruction `initial` with `replacement` within this block.
1037 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1038 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001039 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001040 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001041 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001042 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1043 // instruction list. With 'ensure_safety' set to true, it verifies that the
1044 // instruction is not in use and removes it from the use lists of its inputs.
1045 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1046 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001047 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001048
1049 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001050 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001051 }
1052
Roland Levillain6b879dd2014-09-22 17:13:44 +01001053 bool IsLoopPreHeaderFirstPredecessor() const {
1054 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001055 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001056 }
1057
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001058 bool IsFirstPredecessorBackEdge() const {
1059 DCHECK(IsLoopHeader());
1060 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1061 }
1062
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001063 HLoopInformation* GetLoopInformation() const {
1064 return loop_information_;
1065 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001066
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001067 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001068 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001069 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001070 void SetInLoop(HLoopInformation* info) {
1071 if (IsLoopHeader()) {
1072 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001073 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001074 loop_information_ = info;
1075 } else if (loop_information_->Contains(*info->GetHeader())) {
1076 // Block is currently part of an outer loop. Make it part of this inner loop.
1077 // Note that a non loop header having a loop information means this loop information
1078 // has already been populated
1079 loop_information_ = info;
1080 } else {
1081 // Block is part of an inner loop. Do not update the loop information.
1082 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1083 // at this point, because this method is being called while populating `info`.
1084 }
1085 }
1086
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001087 // Raw update of the loop information.
1088 void SetLoopInformation(HLoopInformation* info) {
1089 loop_information_ = info;
1090 }
1091
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001092 bool IsInLoop() const { return loop_information_ != nullptr; }
1093
David Brazdilec16f792015-08-19 15:04:01 +01001094 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1095
1096 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1097 try_catch_information_ = try_catch_information;
1098 }
1099
1100 bool IsTryBlock() const {
1101 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1102 }
1103
1104 bool IsCatchBlock() const {
1105 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1106 }
David Brazdilffee3d32015-07-06 11:48:53 +01001107
1108 // Returns the try entry that this block's successors should have. They will
1109 // be in the same try, unless the block ends in a try boundary. In that case,
1110 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001111 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001112
David Brazdild7558da2015-09-22 13:04:14 +01001113 bool HasThrowingInstructions() const;
1114
David Brazdila4b8c212015-05-07 09:59:30 +01001115 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001116 bool Dominates(HBasicBlock* block) const;
1117
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001118 size_t GetLifetimeStart() const { return lifetime_start_; }
1119 size_t GetLifetimeEnd() const { return lifetime_end_; }
1120
1121 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1122 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1123
David Brazdil8d5b8b22015-03-24 10:51:52 +00001124 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001125 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001126 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001127 bool HasSinglePhi() const;
1128
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001129 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001130 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001131 ArenaVector<HBasicBlock*> predecessors_;
1132 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001133 HInstructionList instructions_;
1134 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001135 HLoopInformation* loop_information_;
1136 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001137 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001138 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001139 // The dex program counter of the first instruction of this block.
1140 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001141 size_t lifetime_start_;
1142 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001143 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001144
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001145 friend class HGraph;
1146 friend class HInstruction;
1147
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001148 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1149};
1150
David Brazdilb2bd1c52015-03-25 11:17:37 +00001151// Iterates over the LoopInformation of all loops which contain 'block'
1152// from the innermost to the outermost.
1153class HLoopInformationOutwardIterator : public ValueObject {
1154 public:
1155 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1156 : current_(block.GetLoopInformation()) {}
1157
1158 bool Done() const { return current_ == nullptr; }
1159
1160 void Advance() {
1161 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001162 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001163 }
1164
1165 HLoopInformation* Current() const {
1166 DCHECK(!Done());
1167 return current_;
1168 }
1169
1170 private:
1171 HLoopInformation* current_;
1172
1173 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1174};
1175
Alexandre Ramesef20f712015-06-09 10:29:30 +01001176#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001177 M(Above, Condition) \
1178 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001179 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001180 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001181 M(ArrayGet, Instruction) \
1182 M(ArrayLength, Instruction) \
1183 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001184 M(Below, Condition) \
1185 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001186 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001187 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001188 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001189 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001190 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001191 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001192 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001193 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001194 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001195 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001196 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001197 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001198 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001199 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001200 M(Exit, Instruction) \
1201 M(FloatConstant, Constant) \
1202 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001203 M(GreaterThan, Condition) \
1204 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001205 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001206 M(InstanceFieldGet, Instruction) \
1207 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001208 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001209 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001210 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001211 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001212 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001213 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001214 M(LessThan, Condition) \
1215 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001216 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001217 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001218 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001219 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001220 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001221 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001222 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001223 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001224 M(Neg, UnaryOperation) \
1225 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001226 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001227 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001228 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001229 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001230 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001231 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001232 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001233 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001234 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001235 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001236 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001237 M(Return, Instruction) \
1238 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001239 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001240 M(Shl, BinaryOperation) \
1241 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001242 M(StaticFieldGet, Instruction) \
1243 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001244 M(UnresolvedInstanceFieldGet, Instruction) \
1245 M(UnresolvedInstanceFieldSet, Instruction) \
1246 M(UnresolvedStaticFieldGet, Instruction) \
1247 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001248 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001249 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001250 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001251 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001252 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001253 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001254 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001255 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001256
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001257/*
1258 * Instructions, shared across several (not all) architectures.
1259 */
1260#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1261#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1262#else
1263#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001264 M(BitwiseNegatedRight, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001265 M(MultiplyAccumulate, Instruction)
1266#endif
1267
Vladimir Markob4536b72015-11-24 13:45:23 +00001268#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001269#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001270#else
1271#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1272 M(ArmDexCacheArraysBase, Instruction)
1273#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001274
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001275#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001276#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001277#else
1278#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001279 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001280 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001281#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001282
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001283#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1284
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001285#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1286
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001287#ifndef ART_ENABLE_CODEGEN_x86
1288#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1289#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001290#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1291 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001292 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001293 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001294 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001295#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001296
1297#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1298
1299#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1300 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001301 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001302 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1303 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001304 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001305 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001306 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1307 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1308
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001309#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1310 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001311 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001312 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001313 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001314 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001315
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001316#define FOR_EACH_INSTRUCTION(M) \
1317 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1318 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1319
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001320#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001321FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1322#undef FORWARD_DECLARATION
1323
Roland Levillainccc07a92014-09-16 14:48:16 +01001324#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001325 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001326 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001327 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001328 return other->Is##type(); \
1329 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001330 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001331
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001332#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1333 bool Is##type() const { return As##type() != nullptr; } \
1334 const H##type* As##type() const { return this; } \
1335 H##type* As##type() { return this; }
1336
David Brazdiled596192015-01-23 10:39:45 +00001337template <typename T> class HUseList;
1338
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001339template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001340class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001341 public:
David Brazdiled596192015-01-23 10:39:45 +00001342 HUseListNode* GetPrevious() const { return prev_; }
1343 HUseListNode* GetNext() const { return next_; }
1344 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001345 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001346 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001347
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001348 private:
David Brazdiled596192015-01-23 10:39:45 +00001349 HUseListNode(T user, size_t index)
1350 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1351
1352 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001353 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001354 HUseListNode<T>* prev_;
1355 HUseListNode<T>* next_;
1356
1357 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001358
1359 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1360};
1361
David Brazdiled596192015-01-23 10:39:45 +00001362template <typename T>
1363class HUseList : public ValueObject {
1364 public:
1365 HUseList() : first_(nullptr) {}
1366
1367 void Clear() {
1368 first_ = nullptr;
1369 }
1370
1371 // Adds a new entry at the beginning of the use list and returns
1372 // the newly created node.
1373 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001374 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001375 if (IsEmpty()) {
1376 first_ = new_node;
1377 } else {
1378 first_->prev_ = new_node;
1379 new_node->next_ = first_;
1380 first_ = new_node;
1381 }
1382 return new_node;
1383 }
1384
1385 HUseListNode<T>* GetFirst() const {
1386 return first_;
1387 }
1388
1389 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001390 DCHECK(node != nullptr);
1391 DCHECK(Contains(node));
1392
David Brazdiled596192015-01-23 10:39:45 +00001393 if (node->prev_ != nullptr) {
1394 node->prev_->next_ = node->next_;
1395 }
1396 if (node->next_ != nullptr) {
1397 node->next_->prev_ = node->prev_;
1398 }
1399 if (node == first_) {
1400 first_ = node->next_;
1401 }
1402 }
1403
David Brazdil1abb4192015-02-17 18:33:36 +00001404 bool Contains(const HUseListNode<T>* node) const {
1405 if (node == nullptr) {
1406 return false;
1407 }
1408 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1409 if (current == node) {
1410 return true;
1411 }
1412 }
1413 return false;
1414 }
1415
David Brazdiled596192015-01-23 10:39:45 +00001416 bool IsEmpty() const {
1417 return first_ == nullptr;
1418 }
1419
1420 bool HasOnlyOneUse() const {
1421 return first_ != nullptr && first_->next_ == nullptr;
1422 }
1423
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001424 size_t SizeSlow() const {
1425 size_t count = 0;
1426 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1427 ++count;
1428 }
1429 return count;
1430 }
1431
David Brazdiled596192015-01-23 10:39:45 +00001432 private:
1433 HUseListNode<T>* first_;
1434};
1435
1436template<typename T>
1437class HUseIterator : public ValueObject {
1438 public:
1439 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1440
1441 bool Done() const { return current_ == nullptr; }
1442
1443 void Advance() {
1444 DCHECK(!Done());
1445 current_ = current_->GetNext();
1446 }
1447
1448 HUseListNode<T>* Current() const {
1449 DCHECK(!Done());
1450 return current_;
1451 }
1452
1453 private:
1454 HUseListNode<T>* current_;
1455
1456 friend class HValue;
1457};
1458
David Brazdil1abb4192015-02-17 18:33:36 +00001459// This class is used by HEnvironment and HInstruction classes to record the
1460// instructions they use and pointers to the corresponding HUseListNodes kept
1461// by the used instructions.
1462template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001463class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001464 public:
1465 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1466 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1467
1468 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1469 : instruction_(old_record.instruction_), use_node_(use_node) {
1470 DCHECK(instruction_ != nullptr);
1471 DCHECK(use_node_ != nullptr);
1472 DCHECK(old_record.use_node_ == nullptr);
1473 }
1474
1475 HInstruction* GetInstruction() const { return instruction_; }
1476 HUseListNode<T>* GetUseNode() const { return use_node_; }
1477
1478 private:
1479 // Instruction used by the user.
1480 HInstruction* instruction_;
1481
1482 // Corresponding entry in the use list kept by 'instruction_'.
1483 HUseListNode<T>* use_node_;
1484};
1485
Aart Bik854a02b2015-07-14 16:07:00 -07001486/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001487 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001488 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001489 * For write/read dependences on fields/arrays, the dependence analysis uses
1490 * type disambiguation (e.g. a float field write cannot modify the value of an
1491 * integer field read) and the access type (e.g. a reference array write cannot
1492 * modify the value of a reference field read [although it may modify the
1493 * reference fetch prior to reading the field, which is represented by its own
1494 * write/read dependence]). The analysis makes conservative points-to
1495 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1496 * the same, and any reference read depends on any reference read without
1497 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001498 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001499 * The internal representation uses 38-bit and is described in the table below.
1500 * The first line indicates the side effect, and for field/array accesses the
1501 * second line indicates the type of the access (in the order of the
1502 * Primitive::Type enum).
1503 * The two numbered lines below indicate the bit position in the bitfield (read
1504 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001505 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001506 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1507 * +-------------+---------+---------+--------------+---------+---------+
1508 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1509 * | 3 |333333322|222222221| 1 |111111110|000000000|
1510 * | 7 |654321098|765432109| 8 |765432109|876543210|
1511 *
1512 * Note that, to ease the implementation, 'changes' bits are least significant
1513 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001514 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001515class SideEffects : public ValueObject {
1516 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001517 SideEffects() : flags_(0) {}
1518
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001519 static SideEffects None() {
1520 return SideEffects(0);
1521 }
1522
1523 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001524 return SideEffects(kAllChangeBits | kAllDependOnBits);
1525 }
1526
1527 static SideEffects AllChanges() {
1528 return SideEffects(kAllChangeBits);
1529 }
1530
1531 static SideEffects AllDependencies() {
1532 return SideEffects(kAllDependOnBits);
1533 }
1534
1535 static SideEffects AllExceptGCDependency() {
1536 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1537 }
1538
1539 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001540 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001541 }
1542
Aart Bik34c3ba92015-07-20 14:08:59 -07001543 static SideEffects AllWrites() {
1544 return SideEffects(kAllWrites);
1545 }
1546
1547 static SideEffects AllReads() {
1548 return SideEffects(kAllReads);
1549 }
1550
1551 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1552 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001553 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001554 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001555 }
1556
Aart Bik854a02b2015-07-14 16:07:00 -07001557 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001558 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001559 }
1560
Aart Bik34c3ba92015-07-20 14:08:59 -07001561 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1562 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001563 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001564 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001565 }
1566
1567 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001568 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001569 }
1570
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001571 static SideEffects CanTriggerGC() {
1572 return SideEffects(1ULL << kCanTriggerGCBit);
1573 }
1574
1575 static SideEffects DependsOnGC() {
1576 return SideEffects(1ULL << kDependsOnGCBit);
1577 }
1578
Aart Bik854a02b2015-07-14 16:07:00 -07001579 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001580 SideEffects Union(SideEffects other) const {
1581 return SideEffects(flags_ | other.flags_);
1582 }
1583
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001584 SideEffects Exclusion(SideEffects other) const {
1585 return SideEffects(flags_ & ~other.flags_);
1586 }
1587
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001588 void Add(SideEffects other) {
1589 flags_ |= other.flags_;
1590 }
1591
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001592 bool Includes(SideEffects other) const {
1593 return (other.flags_ & flags_) == other.flags_;
1594 }
1595
1596 bool HasSideEffects() const {
1597 return (flags_ & kAllChangeBits);
1598 }
1599
1600 bool HasDependencies() const {
1601 return (flags_ & kAllDependOnBits);
1602 }
1603
1604 // Returns true if there are no side effects or dependencies.
1605 bool DoesNothing() const {
1606 return flags_ == 0;
1607 }
1608
Aart Bik854a02b2015-07-14 16:07:00 -07001609 // Returns true if something is written.
1610 bool DoesAnyWrite() const {
1611 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001612 }
1613
Aart Bik854a02b2015-07-14 16:07:00 -07001614 // Returns true if something is read.
1615 bool DoesAnyRead() const {
1616 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001617 }
1618
Aart Bik854a02b2015-07-14 16:07:00 -07001619 // Returns true if potentially everything is written and read
1620 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001621 bool DoesAllReadWrite() const {
1622 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1623 }
1624
Aart Bik854a02b2015-07-14 16:07:00 -07001625 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001626 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001627 }
1628
Roland Levillain0d5a2812015-11-13 10:07:31 +00001629 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001630 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001631 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1632 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001633 }
1634
1635 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001636 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001637 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001638 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001639 for (int s = kLastBit; s >= 0; s--) {
1640 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1641 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1642 // This is a bit for the GC side effect.
1643 if (current_bit_is_set) {
1644 flags += "GC";
1645 }
Aart Bik854a02b2015-07-14 16:07:00 -07001646 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001647 } else {
1648 // This is a bit for the array/field analysis.
1649 // The underscore character stands for the 'can trigger GC' bit.
1650 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1651 if (current_bit_is_set) {
1652 flags += kDebug[s];
1653 }
1654 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1655 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1656 flags += "|";
1657 }
1658 }
Aart Bik854a02b2015-07-14 16:07:00 -07001659 }
1660 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001661 }
1662
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001663 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001664
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001665 private:
1666 static constexpr int kFieldArrayAnalysisBits = 9;
1667
1668 static constexpr int kFieldWriteOffset = 0;
1669 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1670 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1671 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1672
1673 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1674
1675 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1676 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1677 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1678 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1679
1680 static constexpr int kLastBit = kDependsOnGCBit;
1681 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1682
1683 // Aliases.
1684
1685 static_assert(kChangeBits == kDependOnBits,
1686 "the 'change' bits should match the 'depend on' bits.");
1687
1688 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1689 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1690 static constexpr uint64_t kAllWrites =
1691 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1692 static constexpr uint64_t kAllReads =
1693 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001694
Aart Bik854a02b2015-07-14 16:07:00 -07001695 // Translates type to bit flag.
1696 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1697 CHECK_NE(type, Primitive::kPrimVoid);
1698 const uint64_t one = 1;
1699 const int shift = type; // 0-based consecutive enum
1700 DCHECK_LE(kFieldWriteOffset, shift);
1701 DCHECK_LT(shift, kArrayWriteOffset);
1702 return one << (type + offset);
1703 }
1704
1705 // Private constructor on direct flags value.
1706 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1707
1708 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001709};
1710
David Brazdiled596192015-01-23 10:39:45 +00001711// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001712class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001713 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001714 HEnvironment(ArenaAllocator* arena,
1715 size_t number_of_vregs,
1716 const DexFile& dex_file,
1717 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001718 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001719 InvokeType invoke_type,
1720 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001721 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1722 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001723 parent_(nullptr),
1724 dex_file_(dex_file),
1725 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001726 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001727 invoke_type_(invoke_type),
1728 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001729 }
1730
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001731 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001732 : HEnvironment(arena,
1733 to_copy.Size(),
1734 to_copy.GetDexFile(),
1735 to_copy.GetMethodIdx(),
1736 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001737 to_copy.GetInvokeType(),
1738 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001739
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001740 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001741 if (parent_ != nullptr) {
1742 parent_->SetAndCopyParentChain(allocator, parent);
1743 } else {
1744 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1745 parent_->CopyFrom(parent);
1746 if (parent->GetParent() != nullptr) {
1747 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1748 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001749 }
David Brazdiled596192015-01-23 10:39:45 +00001750 }
1751
Vladimir Marko71bf8092015-09-15 15:33:14 +01001752 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001753 void CopyFrom(HEnvironment* environment);
1754
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001755 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1756 // input to the loop phi instead. This is for inserting instructions that
1757 // require an environment (like HDeoptimization) in the loop pre-header.
1758 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001759
1760 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001761 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001762 }
1763
1764 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001765 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001766 }
1767
David Brazdil1abb4192015-02-17 18:33:36 +00001768 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001769
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001770 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001771
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001772 HEnvironment* GetParent() const { return parent_; }
1773
1774 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001775 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001776 }
1777
1778 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001779 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001780 }
1781
1782 uint32_t GetDexPc() const {
1783 return dex_pc_;
1784 }
1785
1786 uint32_t GetMethodIdx() const {
1787 return method_idx_;
1788 }
1789
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001790 InvokeType GetInvokeType() const {
1791 return invoke_type_;
1792 }
1793
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001794 const DexFile& GetDexFile() const {
1795 return dex_file_;
1796 }
1797
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001798 HInstruction* GetHolder() const {
1799 return holder_;
1800 }
1801
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001802
1803 bool IsFromInlinedInvoke() const {
1804 return GetParent() != nullptr;
1805 }
1806
David Brazdiled596192015-01-23 10:39:45 +00001807 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001808 // Record instructions' use entries of this environment for constant-time removal.
1809 // It should only be called by HInstruction when a new environment use is added.
1810 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1811 DCHECK(env_use->GetUser() == this);
1812 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001813 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001814 }
David Brazdiled596192015-01-23 10:39:45 +00001815
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001816 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1817 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001818 HEnvironment* parent_;
1819 const DexFile& dex_file_;
1820 const uint32_t method_idx_;
1821 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001822 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001823
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001824 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001825 HInstruction* const holder_;
1826
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001827 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001828
1829 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1830};
1831
Vladimir Markof9f64412015-09-02 14:05:49 +01001832class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001833 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001834 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001835 : previous_(nullptr),
1836 next_(nullptr),
1837 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001838 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001839 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001840 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001841 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001842 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001843 locations_(nullptr),
1844 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001845 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001846 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001847 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1848 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1849 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001850
Dave Allison20dfc792014-06-16 20:44:29 -07001851 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001852
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001853#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001854 enum InstructionKind {
1855 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1856 };
1857#undef DECLARE_KIND
1858
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001859 HInstruction* GetNext() const { return next_; }
1860 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001861
Calin Juravle77520bc2015-01-12 18:45:46 +00001862 HInstruction* GetNextDisregardingMoves() const;
1863 HInstruction* GetPreviousDisregardingMoves() const;
1864
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001865 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001866 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001867 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001868 bool IsInBlock() const { return block_ != nullptr; }
1869 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001870 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1871 bool IsIrreducibleLoopHeaderPhi() const {
1872 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1873 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001874
Roland Levillain6b879dd2014-09-22 17:13:44 +01001875 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001876 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001877
1878 virtual void Accept(HGraphVisitor* visitor) = 0;
1879 virtual const char* DebugName() const = 0;
1880
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001881 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001882 void SetRawInputAt(size_t index, HInstruction* input) {
1883 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1884 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001885
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001886 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001887
1888 uint32_t GetDexPc() const { return dex_pc_; }
1889
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001890 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001891
Roland Levillaine161a2a2014-10-03 12:45:18 +01001892 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001893 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001894
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001895 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001896 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001897
Calin Juravle10e244f2015-01-26 18:54:32 +00001898 // Does not apply for all instructions, but having this at top level greatly
1899 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001900 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001901 virtual bool CanBeNull() const {
1902 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1903 return true;
1904 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001905
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001906 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001907 return false;
1908 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001909
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001910 virtual bool IsActualObject() const {
1911 return GetType() == Primitive::kPrimNot;
1912 }
1913
Calin Juravle2e768302015-07-28 14:41:11 +00001914 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001915
Calin Juravle61d544b2015-02-23 16:46:57 +00001916 ReferenceTypeInfo GetReferenceTypeInfo() const {
1917 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001918 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
1919 GetPackedFlag<kFlagReferenceTypeIsExact>());;
Calin Juravle61d544b2015-02-23 16:46:57 +00001920 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001921
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001922 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001923 DCHECK(user != nullptr);
1924 HUseListNode<HInstruction*>* use =
1925 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1926 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001927 }
1928
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001929 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001930 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001931 HUseListNode<HEnvironment*>* env_use =
1932 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1933 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001934 }
1935
David Brazdil1abb4192015-02-17 18:33:36 +00001936 void RemoveAsUserOfInput(size_t input) {
1937 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1938 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1939 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001940
David Brazdil1abb4192015-02-17 18:33:36 +00001941 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1942 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001943
David Brazdiled596192015-01-23 10:39:45 +00001944 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1945 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001946 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001947 bool HasOnlyOneNonEnvironmentUse() const {
1948 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1949 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001950
Roland Levillain6c82d402014-10-13 16:10:27 +01001951 // Does this instruction strictly dominate `other_instruction`?
1952 // Returns false if this instruction and `other_instruction` are the same.
1953 // Aborts if this instruction and `other_instruction` are both phis.
1954 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001955
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001956 int GetId() const { return id_; }
1957 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001958
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001959 int GetSsaIndex() const { return ssa_index_; }
1960 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1961 bool HasSsaIndex() const { return ssa_index_ != -1; }
1962
1963 bool HasEnvironment() const { return environment_ != nullptr; }
1964 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001965 // Set the `environment_` field. Raw because this method does not
1966 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001967 void SetRawEnvironment(HEnvironment* environment) {
1968 DCHECK(environment_ == nullptr);
1969 DCHECK_EQ(environment->GetHolder(), this);
1970 environment_ = environment;
1971 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001972
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001973 void RemoveEnvironment();
1974
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001975 // Set the environment of this instruction, copying it from `environment`. While
1976 // copying, the uses lists are being updated.
1977 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001978 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001979 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001980 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001981 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001982 if (environment->GetParent() != nullptr) {
1983 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1984 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001985 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001986
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001987 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1988 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001989 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001990 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001991 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001992 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001993 if (environment->GetParent() != nullptr) {
1994 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1995 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001996 }
1997
Nicolas Geoffray39468442014-09-02 15:17:15 +01001998 // Returns the number of entries in the environment. Typically, that is the
1999 // number of dex registers in a method. It could be more in case of inlining.
2000 size_t EnvironmentSize() const;
2001
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002002 LocationSummary* GetLocations() const { return locations_; }
2003 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002004
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002005 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002006 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002007
Alexandre Rames188d4312015-04-09 18:30:21 +01002008 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2009 // uses of this instruction by `other` are *not* updated.
2010 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2011 ReplaceWith(other);
2012 other->ReplaceInput(this, use_index);
2013 }
2014
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002015 // Move `this` instruction before `cursor`.
2016 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002017
Vladimir Markofb337ea2015-11-25 15:25:10 +00002018 // Move `this` before its first user and out of any loops. If there is no
2019 // out-of-loop user that dominates all other users, move the instruction
2020 // to the end of the out-of-loop common dominator of the user's blocks.
2021 //
2022 // This can be used only on non-throwing instructions with no side effects that
2023 // have at least one use but no environment uses.
2024 void MoveBeforeFirstUserAndOutOfLoops();
2025
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002026#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002027 bool Is##type() const; \
2028 const H##type* As##type() const; \
2029 H##type* As##type();
2030
2031 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2032#undef INSTRUCTION_TYPE_CHECK
2033
2034#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002035 bool Is##type() const { return (As##type() != nullptr); } \
2036 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002037 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002038 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002039#undef INSTRUCTION_TYPE_CHECK
2040
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002041 // Returns whether the instruction can be moved within the graph.
2042 virtual bool CanBeMoved() const { return false; }
2043
2044 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002045 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002046 return false;
2047 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002048
2049 // Returns whether any data encoded in the two instructions is equal.
2050 // This method does not look at the inputs. Both instructions must be
2051 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002052 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002053 return false;
2054 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002055
2056 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002057 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002058 // 2) Their inputs are identical.
2059 bool Equals(HInstruction* other) const;
2060
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002061 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2062 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2063 // the virtual function because the __attribute__((__pure__)) doesn't really
2064 // apply the strong requirement for virtual functions, preventing optimizations.
2065 InstructionKind GetKind() const PURE;
2066 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002067
2068 virtual size_t ComputeHashCode() const {
2069 size_t result = GetKind();
2070 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2071 result = (result * 31) + InputAt(i)->GetId();
2072 }
2073 return result;
2074 }
2075
2076 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002077 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002078 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002079
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002080 size_t GetLifetimePosition() const { return lifetime_position_; }
2081 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2082 LiveInterval* GetLiveInterval() const { return live_interval_; }
2083 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2084 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2085
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002086 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2087
2088 // Returns whether the code generation of the instruction will require to have access
2089 // to the current method. Such instructions are:
2090 // (1): Instructions that require an environment, as calling the runtime requires
2091 // to walk the stack and have the current method stored at a specific stack address.
2092 // (2): Object literals like classes and strings, that are loaded from the dex cache
2093 // fields of the current method.
2094 bool NeedsCurrentMethod() const {
2095 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2096 }
2097
Vladimir Markodc151b22015-10-15 18:02:30 +01002098 // Returns whether the code generation of the instruction will require to have access
2099 // to the dex cache of the current method's declaring class via the current method.
2100 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002101
Mark Mendellc4701932015-04-10 13:18:51 -04002102 // Does this instruction have any use in an environment before
2103 // control flow hits 'other'?
2104 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2105
2106 // Remove all references to environment uses of this instruction.
2107 // The caller must ensure that this is safe to do.
2108 void RemoveEnvironmentUsers();
2109
Vladimir Markoa1de9182016-02-25 11:37:38 +00002110 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2111 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002112
David Brazdil1abb4192015-02-17 18:33:36 +00002113 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002114 // If set, the machine code for this instruction is assumed to be generated by
2115 // its users. Used by liveness analysis to compute use positions accordingly.
2116 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2117 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2118 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2119 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2120
David Brazdil1abb4192015-02-17 18:33:36 +00002121 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2122 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2123
Vladimir Markoa1de9182016-02-25 11:37:38 +00002124 uint32_t GetPackedFields() const {
2125 return packed_fields_;
2126 }
2127
2128 template <size_t flag>
2129 bool GetPackedFlag() const {
2130 return (packed_fields_ & (1u << flag)) != 0u;
2131 }
2132
2133 template <size_t flag>
2134 void SetPackedFlag(bool value = true) {
2135 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2136 }
2137
2138 template <typename BitFieldType>
2139 typename BitFieldType::value_type GetPackedField() const {
2140 return BitFieldType::Decode(packed_fields_);
2141 }
2142
2143 template <typename BitFieldType>
2144 void SetPackedField(typename BitFieldType::value_type value) {
2145 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2146 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2147 }
2148
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002149 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002150 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2151
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002152 HInstruction* previous_;
2153 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002154 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002155 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002156
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002157 // An instruction gets an id when it is added to the graph.
2158 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002159 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002160 int id_;
2161
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002162 // When doing liveness analysis, instructions that have uses get an SSA index.
2163 int ssa_index_;
2164
Vladimir Markoa1de9182016-02-25 11:37:38 +00002165 // Packed fields.
2166 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002167
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002168 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002169 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002170
2171 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002172 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002173
Nicolas Geoffray39468442014-09-02 15:17:15 +01002174 // The environment associated with this instruction. Not null if the instruction
2175 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002176 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002177
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002178 // Set by the code generator.
2179 LocationSummary* locations_;
2180
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002181 // Set by the liveness analysis.
2182 LiveInterval* live_interval_;
2183
2184 // Set by the liveness analysis, this is the position in a linear
2185 // order of blocks where this instruction's live interval start.
2186 size_t lifetime_position_;
2187
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002188 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002189
Vladimir Markoa1de9182016-02-25 11:37:38 +00002190 // The reference handle part of the reference type info.
2191 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002192 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002193 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002194
David Brazdil1abb4192015-02-17 18:33:36 +00002195 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002196 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002197 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002198 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002199 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002200
2201 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2202};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002203std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002204
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002205class HInputIterator : public ValueObject {
2206 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002207 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002208
2209 bool Done() const { return index_ == instruction_->InputCount(); }
2210 HInstruction* Current() const { return instruction_->InputAt(index_); }
2211 void Advance() { index_++; }
2212
2213 private:
2214 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002215 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002216
2217 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2218};
2219
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002220class HInstructionIterator : public ValueObject {
2221 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002222 explicit HInstructionIterator(const HInstructionList& instructions)
2223 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002224 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002225 }
2226
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002227 bool Done() const { return instruction_ == nullptr; }
2228 HInstruction* Current() const { return instruction_; }
2229 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002230 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002231 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002232 }
2233
2234 private:
2235 HInstruction* instruction_;
2236 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002237
2238 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002239};
2240
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002241class HBackwardInstructionIterator : public ValueObject {
2242 public:
2243 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2244 : instruction_(instructions.last_instruction_) {
2245 next_ = Done() ? nullptr : instruction_->GetPrevious();
2246 }
2247
2248 bool Done() const { return instruction_ == nullptr; }
2249 HInstruction* Current() const { return instruction_; }
2250 void Advance() {
2251 instruction_ = next_;
2252 next_ = Done() ? nullptr : instruction_->GetPrevious();
2253 }
2254
2255 private:
2256 HInstruction* instruction_;
2257 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002258
2259 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002260};
2261
Vladimir Markof9f64412015-09-02 14:05:49 +01002262template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002263class HTemplateInstruction: public HInstruction {
2264 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002265 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002266 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002267 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002268
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002269 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002270
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002271 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002272 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2273 DCHECK_LT(i, N);
2274 return inputs_[i];
2275 }
David Brazdil1abb4192015-02-17 18:33:36 +00002276
2277 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002278 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002279 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002280 }
2281
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002282 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002283 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002284
2285 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002286};
2287
Vladimir Markof9f64412015-09-02 14:05:49 +01002288// HTemplateInstruction specialization for N=0.
2289template<>
2290class HTemplateInstruction<0>: public HInstruction {
2291 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002292 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002293 : HInstruction(side_effects, dex_pc) {}
2294
Vladimir Markof9f64412015-09-02 14:05:49 +01002295 virtual ~HTemplateInstruction() {}
2296
2297 size_t InputCount() const OVERRIDE { return 0; }
2298
2299 protected:
2300 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2301 LOG(FATAL) << "Unreachable";
2302 UNREACHABLE();
2303 }
2304
2305 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2306 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2307 LOG(FATAL) << "Unreachable";
2308 UNREACHABLE();
2309 }
2310
2311 private:
2312 friend class SsaBuilder;
2313};
2314
Dave Allison20dfc792014-06-16 20:44:29 -07002315template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002316class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002317 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002318 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002319 : HTemplateInstruction<N>(side_effects, dex_pc) {
2320 this->template SetPackedField<TypeField>(type);
2321 }
Dave Allison20dfc792014-06-16 20:44:29 -07002322 virtual ~HExpression() {}
2323
Vladimir Markoa1de9182016-02-25 11:37:38 +00002324 Primitive::Type GetType() const OVERRIDE {
2325 return TypeField::Decode(this->GetPackedFields());
2326 }
Dave Allison20dfc792014-06-16 20:44:29 -07002327
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002328 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002329 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2330 static constexpr size_t kFieldTypeSize =
2331 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2332 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2333 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2334 "Too many packed fields.");
2335 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002336};
2337
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002338// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2339// instruction that branches to the exit block.
2340class HReturnVoid : public HTemplateInstruction<0> {
2341 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002342 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2343 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002344
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002345 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002346
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002347 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002348
2349 private:
2350 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2351};
2352
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002353// Represents dex's RETURN opcodes. A HReturn is a control flow
2354// instruction that branches to the exit block.
2355class HReturn : public HTemplateInstruction<1> {
2356 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002357 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2358 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002359 SetRawInputAt(0, value);
2360 }
2361
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002362 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002363
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002364 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002365
2366 private:
2367 DISALLOW_COPY_AND_ASSIGN(HReturn);
2368};
2369
David Brazdildee58d62016-04-07 09:54:26 +00002370class HPhi : public HInstruction {
2371 public:
2372 HPhi(ArenaAllocator* arena,
2373 uint32_t reg_number,
2374 size_t number_of_inputs,
2375 Primitive::Type type,
2376 uint32_t dex_pc = kNoDexPc)
2377 : HInstruction(SideEffects::None(), dex_pc),
2378 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2379 reg_number_(reg_number) {
2380 SetPackedField<TypeField>(ToPhiType(type));
2381 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2382 // Phis are constructed live and marked dead if conflicting or unused.
2383 // Individual steps of SsaBuilder should assume that if a phi has been
2384 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2385 SetPackedFlag<kFlagIsLive>(true);
2386 SetPackedFlag<kFlagCanBeNull>(true);
2387 }
2388
2389 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2390 static Primitive::Type ToPhiType(Primitive::Type type) {
2391 return Primitive::PrimitiveKind(type);
2392 }
2393
2394 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2395
2396 size_t InputCount() const OVERRIDE { return inputs_.size(); }
2397
2398 void AddInput(HInstruction* input);
2399 void RemoveInputAt(size_t index);
2400
2401 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2402 void SetType(Primitive::Type new_type) {
2403 // Make sure that only valid type changes occur. The following are allowed:
2404 // (1) int -> float/ref (primitive type propagation),
2405 // (2) long -> double (primitive type propagation).
2406 DCHECK(GetType() == new_type ||
2407 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2408 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2409 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2410 SetPackedField<TypeField>(new_type);
2411 }
2412
2413 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2414 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2415
2416 uint32_t GetRegNumber() const { return reg_number_; }
2417
2418 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2419 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2420 bool IsDead() const { return !IsLive(); }
2421 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2422
2423 bool IsVRegEquivalentOf(HInstruction* other) const {
2424 return other != nullptr
2425 && other->IsPhi()
2426 && other->AsPhi()->GetBlock() == GetBlock()
2427 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2428 }
2429
2430 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2431 // An equivalent phi is a phi having the same dex register and type.
2432 // It assumes that phis with the same dex register are adjacent.
2433 HPhi* GetNextEquivalentPhiWithSameType() {
2434 HInstruction* next = GetNext();
2435 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2436 if (next->GetType() == GetType()) {
2437 return next->AsPhi();
2438 }
2439 next = next->GetNext();
2440 }
2441 return nullptr;
2442 }
2443
2444 DECLARE_INSTRUCTION(Phi);
2445
2446 protected:
2447 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
2448 return inputs_[index];
2449 }
2450
2451 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2452 inputs_[index] = input;
2453 }
2454
2455 private:
2456 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2457 static constexpr size_t kFieldTypeSize =
2458 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2459 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2460 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2461 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2462 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2463 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2464
2465 ArenaVector<HUserRecord<HInstruction*> > inputs_;
2466 const uint32_t reg_number_;
2467
2468 DISALLOW_COPY_AND_ASSIGN(HPhi);
2469};
2470
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002471// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002472// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002473// exit block.
2474class HExit : public HTemplateInstruction<0> {
2475 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002476 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002477
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002478 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002479
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002480 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002481
2482 private:
2483 DISALLOW_COPY_AND_ASSIGN(HExit);
2484};
2485
2486// Jumps from one block to another.
2487class HGoto : public HTemplateInstruction<0> {
2488 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002489 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002490
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002491 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002492
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002493 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002494 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002495 }
2496
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002497 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002498
2499 private:
2500 DISALLOW_COPY_AND_ASSIGN(HGoto);
2501};
2502
Roland Levillain9867bc72015-08-05 10:21:34 +01002503class HConstant : public HExpression<0> {
2504 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002505 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2506 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002507
2508 bool CanBeMoved() const OVERRIDE { return true; }
2509
Roland Levillain1a653882016-03-18 18:05:57 +00002510 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002511 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002512 // Is this constant 0 in the arithmetic sense?
2513 virtual bool IsArithmeticZero() const { return false; }
2514 // Is this constant a 0-bit pattern?
2515 virtual bool IsZeroBitPattern() const { return false; }
2516 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002517 virtual bool IsOne() const { return false; }
2518
David Brazdil77a48ae2015-09-15 12:34:04 +00002519 virtual uint64_t GetValueAsUint64() const = 0;
2520
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002521 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002522
2523 private:
2524 DISALLOW_COPY_AND_ASSIGN(HConstant);
2525};
2526
2527class HNullConstant : public HConstant {
2528 public:
2529 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2530 return true;
2531 }
2532
David Brazdil77a48ae2015-09-15 12:34:04 +00002533 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2534
Roland Levillain9867bc72015-08-05 10:21:34 +01002535 size_t ComputeHashCode() const OVERRIDE { return 0; }
2536
Roland Levillain1a653882016-03-18 18:05:57 +00002537 // The null constant representation is a 0-bit pattern.
2538 virtual bool IsZeroBitPattern() const { return true; }
2539
Roland Levillain9867bc72015-08-05 10:21:34 +01002540 DECLARE_INSTRUCTION(NullConstant);
2541
2542 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002543 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002544
2545 friend class HGraph;
2546 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2547};
2548
2549// Constants of the type int. Those can be from Dex instructions, or
2550// synthesized (for example with the if-eqz instruction).
2551class HIntConstant : public HConstant {
2552 public:
2553 int32_t GetValue() const { return value_; }
2554
David Brazdil9f389d42015-10-01 14:32:56 +01002555 uint64_t GetValueAsUint64() const OVERRIDE {
2556 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2557 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002558
Roland Levillain9867bc72015-08-05 10:21:34 +01002559 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002560 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002561 return other->AsIntConstant()->value_ == value_;
2562 }
2563
2564 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2565
2566 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002567 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2568 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002569 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2570
Roland Levillain1a653882016-03-18 18:05:57 +00002571 // Integer constants are used to encode Boolean values as well,
2572 // where 1 means true and 0 means false.
2573 bool IsTrue() const { return GetValue() == 1; }
2574 bool IsFalse() const { return GetValue() == 0; }
2575
Roland Levillain9867bc72015-08-05 10:21:34 +01002576 DECLARE_INSTRUCTION(IntConstant);
2577
2578 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002579 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2580 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2581 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2582 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002583
2584 const int32_t value_;
2585
2586 friend class HGraph;
2587 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2588 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2589 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2590};
2591
2592class HLongConstant : public HConstant {
2593 public:
2594 int64_t GetValue() const { return value_; }
2595
David Brazdil77a48ae2015-09-15 12:34:04 +00002596 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2597
Roland Levillain9867bc72015-08-05 10:21:34 +01002598 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002599 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002600 return other->AsLongConstant()->value_ == value_;
2601 }
2602
2603 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2604
2605 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002606 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2607 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002608 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2609
2610 DECLARE_INSTRUCTION(LongConstant);
2611
2612 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002613 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2614 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002615
2616 const int64_t value_;
2617
2618 friend class HGraph;
2619 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2620};
Dave Allison20dfc792014-06-16 20:44:29 -07002621
Roland Levillain31dd3d62016-02-16 12:21:02 +00002622class HFloatConstant : public HConstant {
2623 public:
2624 float GetValue() const { return value_; }
2625
2626 uint64_t GetValueAsUint64() const OVERRIDE {
2627 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2628 }
2629
2630 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2631 DCHECK(other->IsFloatConstant()) << other->DebugName();
2632 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2633 }
2634
2635 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2636
2637 bool IsMinusOne() const OVERRIDE {
2638 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2639 }
Roland Levillain1a653882016-03-18 18:05:57 +00002640 bool IsArithmeticZero() const OVERRIDE {
2641 return std::fpclassify(value_) == FP_ZERO;
2642 }
2643 bool IsArithmeticPositiveZero() const {
2644 return IsArithmeticZero() && !std::signbit(value_);
2645 }
2646 bool IsArithmeticNegativeZero() const {
2647 return IsArithmeticZero() && std::signbit(value_);
2648 }
2649 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002650 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002651 }
2652 bool IsOne() const OVERRIDE {
2653 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2654 }
2655 bool IsNaN() const {
2656 return std::isnan(value_);
2657 }
2658
2659 DECLARE_INSTRUCTION(FloatConstant);
2660
2661 private:
2662 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2663 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2664 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2665 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2666
2667 const float value_;
2668
2669 // Only the SsaBuilder and HGraph can create floating-point constants.
2670 friend class SsaBuilder;
2671 friend class HGraph;
2672 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2673};
2674
2675class HDoubleConstant : public HConstant {
2676 public:
2677 double GetValue() const { return value_; }
2678
2679 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2680
2681 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2682 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2683 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2684 }
2685
2686 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2687
2688 bool IsMinusOne() const OVERRIDE {
2689 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2690 }
Roland Levillain1a653882016-03-18 18:05:57 +00002691 bool IsArithmeticZero() const OVERRIDE {
2692 return std::fpclassify(value_) == FP_ZERO;
2693 }
2694 bool IsArithmeticPositiveZero() const {
2695 return IsArithmeticZero() && !std::signbit(value_);
2696 }
2697 bool IsArithmeticNegativeZero() const {
2698 return IsArithmeticZero() && std::signbit(value_);
2699 }
2700 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002701 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002702 }
2703 bool IsOne() const OVERRIDE {
2704 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2705 }
2706 bool IsNaN() const {
2707 return std::isnan(value_);
2708 }
2709
2710 DECLARE_INSTRUCTION(DoubleConstant);
2711
2712 private:
2713 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2714 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2715 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2716 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2717
2718 const double value_;
2719
2720 // Only the SsaBuilder and HGraph can create floating-point constants.
2721 friend class SsaBuilder;
2722 friend class HGraph;
2723 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2724};
2725
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002726// Conditional branch. A block ending with an HIf instruction must have
2727// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002728class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002729 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002730 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2731 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002732 SetRawInputAt(0, input);
2733 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002734
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002735 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002736
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002737 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002738 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002739 }
2740
2741 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002742 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002743 }
2744
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002745 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002746
2747 private:
2748 DISALLOW_COPY_AND_ASSIGN(HIf);
2749};
2750
David Brazdilfc6a86a2015-06-26 10:33:45 +00002751
2752// Abstract instruction which marks the beginning and/or end of a try block and
2753// links it to the respective exception handlers. Behaves the same as a Goto in
2754// non-exceptional control flow.
2755// Normal-flow successor is stored at index zero, exception handlers under
2756// higher indices in no particular order.
2757class HTryBoundary : public HTemplateInstruction<0> {
2758 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002759 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002760 kEntry,
2761 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002762 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002763 };
2764
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002765 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002766 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2767 SetPackedField<BoundaryKindField>(kind);
2768 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002769
2770 bool IsControlFlow() const OVERRIDE { return true; }
2771
2772 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002773 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002774
David Brazdild26a4112015-11-10 11:07:31 +00002775 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2776 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2777 }
2778
David Brazdilfc6a86a2015-06-26 10:33:45 +00002779 // Returns whether `handler` is among its exception handlers (non-zero index
2780 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002781 bool HasExceptionHandler(const HBasicBlock& handler) const {
2782 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002783 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002784 }
2785
2786 // If not present already, adds `handler` to its block's list of exception
2787 // handlers.
2788 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002789 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002790 GetBlock()->AddSuccessor(handler);
2791 }
2792 }
2793
Vladimir Markoa1de9182016-02-25 11:37:38 +00002794 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2795 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002796
David Brazdilffee3d32015-07-06 11:48:53 +01002797 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2798
David Brazdilfc6a86a2015-06-26 10:33:45 +00002799 DECLARE_INSTRUCTION(TryBoundary);
2800
2801 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002802 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2803 static constexpr size_t kFieldBoundaryKindSize =
2804 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2805 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2806 kFieldBoundaryKind + kFieldBoundaryKindSize;
2807 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2808 "Too many packed fields.");
2809 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002810
2811 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2812};
2813
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002814// Deoptimize to interpreter, upon checking a condition.
2815class HDeoptimize : public HTemplateInstruction<1> {
2816 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002817 // We set CanTriggerGC to prevent any intermediate address to be live
2818 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002819 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002820 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002821 SetRawInputAt(0, cond);
2822 }
2823
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002824 bool CanBeMoved() const OVERRIDE { return true; }
2825 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2826 return true;
2827 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002828 bool NeedsEnvironment() const OVERRIDE { return true; }
2829 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002830
2831 DECLARE_INSTRUCTION(Deoptimize);
2832
2833 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002834 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2835};
2836
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002837// Represents the ArtMethod that was passed as a first argument to
2838// the method. It is used by instructions that depend on it, like
2839// instructions that work with the dex cache.
2840class HCurrentMethod : public HExpression<0> {
2841 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002842 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2843 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002844
2845 DECLARE_INSTRUCTION(CurrentMethod);
2846
2847 private:
2848 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2849};
2850
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002851// Fetches an ArtMethod from the virtual table or the interface method table
2852// of a class.
2853class HClassTableGet : public HExpression<1> {
2854 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002855 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002856 kVTable,
2857 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002858 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002859 };
2860 HClassTableGet(HInstruction* cls,
2861 Primitive::Type type,
2862 TableKind kind,
2863 size_t index,
2864 uint32_t dex_pc)
2865 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002866 index_(index) {
2867 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002868 SetRawInputAt(0, cls);
2869 }
2870
2871 bool CanBeMoved() const OVERRIDE { return true; }
2872 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2873 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002874 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002875 }
2876
Vladimir Markoa1de9182016-02-25 11:37:38 +00002877 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002878 size_t GetIndex() const { return index_; }
2879
2880 DECLARE_INSTRUCTION(ClassTableGet);
2881
2882 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002883 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2884 static constexpr size_t kFieldTableKindSize =
2885 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2886 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2887 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2888 "Too many packed fields.");
2889 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2890
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002891 // The index of the ArtMethod in the table.
2892 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002893
2894 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2895};
2896
Mark Mendellfe57faa2015-09-18 09:26:15 -04002897// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2898// have one successor for each entry in the switch table, and the final successor
2899// will be the block containing the next Dex opcode.
2900class HPackedSwitch : public HTemplateInstruction<1> {
2901 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002902 HPackedSwitch(int32_t start_value,
2903 uint32_t num_entries,
2904 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002905 uint32_t dex_pc = kNoDexPc)
2906 : HTemplateInstruction(SideEffects::None(), dex_pc),
2907 start_value_(start_value),
2908 num_entries_(num_entries) {
2909 SetRawInputAt(0, input);
2910 }
2911
2912 bool IsControlFlow() const OVERRIDE { return true; }
2913
2914 int32_t GetStartValue() const { return start_value_; }
2915
Vladimir Marko211c2112015-09-24 16:52:33 +01002916 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002917
2918 HBasicBlock* GetDefaultBlock() const {
2919 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002920 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002921 }
2922 DECLARE_INSTRUCTION(PackedSwitch);
2923
2924 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002925 const int32_t start_value_;
2926 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002927
2928 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2929};
2930
Roland Levillain88cb1752014-10-20 16:36:47 +01002931class HUnaryOperation : public HExpression<1> {
2932 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002933 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2934 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002935 SetRawInputAt(0, input);
2936 }
2937
2938 HInstruction* GetInput() const { return InputAt(0); }
2939 Primitive::Type GetResultType() const { return GetType(); }
2940
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002941 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002942 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002943 return true;
2944 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002945
Roland Levillain31dd3d62016-02-16 12:21:02 +00002946 // Try to statically evaluate `this` and return a HConstant
2947 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002948 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002949 HConstant* TryStaticEvaluation() const;
2950
2951 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002952 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2953 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002954 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2955 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002956
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002957 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002958
2959 private:
2960 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2961};
2962
Dave Allison20dfc792014-06-16 20:44:29 -07002963class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002964 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002965 HBinaryOperation(Primitive::Type result_type,
2966 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002967 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002968 SideEffects side_effects = SideEffects::None(),
2969 uint32_t dex_pc = kNoDexPc)
2970 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002971 SetRawInputAt(0, left);
2972 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002973 }
2974
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002975 HInstruction* GetLeft() const { return InputAt(0); }
2976 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002977 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002978
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002979 virtual bool IsCommutative() const { return false; }
2980
2981 // Put constant on the right.
2982 // Returns whether order is changed.
2983 bool OrderInputsWithConstantOnTheRight() {
2984 HInstruction* left = InputAt(0);
2985 HInstruction* right = InputAt(1);
2986 if (left->IsConstant() && !right->IsConstant()) {
2987 ReplaceInput(right, 0);
2988 ReplaceInput(left, 1);
2989 return true;
2990 }
2991 return false;
2992 }
2993
2994 // Order inputs by instruction id, but favor constant on the right side.
2995 // This helps GVN for commutative ops.
2996 void OrderInputs() {
2997 DCHECK(IsCommutative());
2998 HInstruction* left = InputAt(0);
2999 HInstruction* right = InputAt(1);
3000 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3001 return;
3002 }
3003 if (OrderInputsWithConstantOnTheRight()) {
3004 return;
3005 }
3006 // Order according to instruction id.
3007 if (left->GetId() > right->GetId()) {
3008 ReplaceInput(right, 0);
3009 ReplaceInput(left, 1);
3010 }
3011 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003012
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003013 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003014 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003015 return true;
3016 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003017
Roland Levillain31dd3d62016-02-16 12:21:02 +00003018 // Try to statically evaluate `this` and return a HConstant
3019 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003020 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003021 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003022
3023 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003024 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3025 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003026 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3027 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003028 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003029 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3030 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003031 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3032 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003033 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3034 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003035 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003036 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3037 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003038
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003039 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003040 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003041 HConstant* GetConstantRight() const;
3042
3043 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003044 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003045 HInstruction* GetLeastConstantLeft() const;
3046
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003047 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003048
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003049 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003050 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3051};
3052
Mark Mendellc4701932015-04-10 13:18:51 -04003053// The comparison bias applies for floating point operations and indicates how NaN
3054// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003055enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003056 kNoBias, // bias is not applicable (i.e. for long operation)
3057 kGtBias, // return 1 for NaN comparisons
3058 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003059 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003060};
3061
Roland Levillain31dd3d62016-02-16 12:21:02 +00003062std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3063
Dave Allison20dfc792014-06-16 20:44:29 -07003064class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003065 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003066 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003067 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3068 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3069 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003070
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003071 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003072 // `instruction`, and disregard moves in between.
3073 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003074
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003075 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003076
3077 virtual IfCondition GetCondition() const = 0;
3078
Mark Mendellc4701932015-04-10 13:18:51 -04003079 virtual IfCondition GetOppositeCondition() const = 0;
3080
Vladimir Markoa1de9182016-02-25 11:37:38 +00003081 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003082 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3083
Vladimir Markoa1de9182016-02-25 11:37:38 +00003084 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3085 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003086
3087 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003088 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003089 }
3090
Roland Levillain4fa13f62015-07-06 18:11:54 +01003091 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003092 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003093 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003094 if (if_cond == kCondNE) {
3095 return true;
3096 } else if (if_cond == kCondEQ) {
3097 return false;
3098 }
3099 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003100 }
3101
3102 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003103 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003104 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003105 if (if_cond == kCondEQ) {
3106 return true;
3107 } else if (if_cond == kCondNE) {
3108 return false;
3109 }
3110 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003111 }
3112
Roland Levillain31dd3d62016-02-16 12:21:02 +00003113 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003114 // Needed if we merge a HCompare into a HCondition.
3115 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3116 static constexpr size_t kFieldComparisonBiasSize =
3117 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3118 static constexpr size_t kNumberOfConditionPackedBits =
3119 kFieldComparisonBias + kFieldComparisonBiasSize;
3120 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3121 using ComparisonBiasField =
3122 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3123
Roland Levillain31dd3d62016-02-16 12:21:02 +00003124 template <typename T>
3125 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3126
3127 template <typename T>
3128 int32_t CompareFP(T x, T y) const {
3129 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3130 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3131 // Handle the bias.
3132 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3133 }
3134
3135 // Return an integer constant containing the result of a condition evaluated at compile time.
3136 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3137 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3138 }
3139
Dave Allison20dfc792014-06-16 20:44:29 -07003140 private:
3141 DISALLOW_COPY_AND_ASSIGN(HCondition);
3142};
3143
3144// Instruction to check if two inputs are equal to each other.
3145class HEqual : public HCondition {
3146 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003147 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3148 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003149
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003150 bool IsCommutative() const OVERRIDE { return true; }
3151
Vladimir Marko9e23df52015-11-10 17:14:35 +00003152 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3153 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003154 return MakeConstantCondition(true, GetDexPc());
3155 }
3156 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3157 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3158 }
3159 // In the following Evaluate methods, a HCompare instruction has
3160 // been merged into this HEqual instruction; evaluate it as
3161 // `Compare(x, y) == 0`.
3162 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3163 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3164 GetDexPc());
3165 }
3166 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3167 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3168 }
3169 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3170 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003171 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003172
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003173 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003174
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003175 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003176 return kCondEQ;
3177 }
3178
Mark Mendellc4701932015-04-10 13:18:51 -04003179 IfCondition GetOppositeCondition() const OVERRIDE {
3180 return kCondNE;
3181 }
3182
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003183 private:
Aart Bike9f37602015-10-09 11:15:55 -07003184 template <typename T> bool Compute(T x, T y) const { return x == y; }
3185
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003186 DISALLOW_COPY_AND_ASSIGN(HEqual);
3187};
3188
Dave Allison20dfc792014-06-16 20:44:29 -07003189class HNotEqual : public HCondition {
3190 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003191 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3192 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003193
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003194 bool IsCommutative() const OVERRIDE { return true; }
3195
Vladimir Marko9e23df52015-11-10 17:14:35 +00003196 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3197 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003198 return MakeConstantCondition(false, GetDexPc());
3199 }
3200 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3201 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3202 }
3203 // In the following Evaluate methods, a HCompare instruction has
3204 // been merged into this HNotEqual instruction; evaluate it as
3205 // `Compare(x, y) != 0`.
3206 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3207 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3208 }
3209 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3210 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3211 }
3212 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3213 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003214 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003215
Dave Allison20dfc792014-06-16 20:44:29 -07003216 DECLARE_INSTRUCTION(NotEqual);
3217
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003218 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003219 return kCondNE;
3220 }
3221
Mark Mendellc4701932015-04-10 13:18:51 -04003222 IfCondition GetOppositeCondition() const OVERRIDE {
3223 return kCondEQ;
3224 }
3225
Dave Allison20dfc792014-06-16 20:44:29 -07003226 private:
Aart Bike9f37602015-10-09 11:15:55 -07003227 template <typename T> bool Compute(T x, T y) const { return x != y; }
3228
Dave Allison20dfc792014-06-16 20:44:29 -07003229 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3230};
3231
3232class HLessThan : public HCondition {
3233 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003234 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3235 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003236
Roland Levillain9867bc72015-08-05 10:21:34 +01003237 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003238 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003239 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003240 // In the following Evaluate methods, a HCompare instruction has
3241 // been merged into this HLessThan instruction; evaluate it as
3242 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003243 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003244 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3245 }
3246 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3247 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3248 }
3249 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3250 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003251 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003252
Dave Allison20dfc792014-06-16 20:44:29 -07003253 DECLARE_INSTRUCTION(LessThan);
3254
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003255 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003256 return kCondLT;
3257 }
3258
Mark Mendellc4701932015-04-10 13:18:51 -04003259 IfCondition GetOppositeCondition() const OVERRIDE {
3260 return kCondGE;
3261 }
3262
Dave Allison20dfc792014-06-16 20:44:29 -07003263 private:
Aart Bike9f37602015-10-09 11:15:55 -07003264 template <typename T> bool Compute(T x, T y) const { return x < y; }
3265
Dave Allison20dfc792014-06-16 20:44:29 -07003266 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3267};
3268
3269class HLessThanOrEqual : public HCondition {
3270 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003271 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3272 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003273
Roland Levillain9867bc72015-08-05 10:21:34 +01003274 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003275 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003276 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003277 // In the following Evaluate methods, a HCompare instruction has
3278 // been merged into this HLessThanOrEqual instruction; evaluate it as
3279 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003280 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003281 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3282 }
3283 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3284 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3285 }
3286 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3287 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003288 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003289
Dave Allison20dfc792014-06-16 20:44:29 -07003290 DECLARE_INSTRUCTION(LessThanOrEqual);
3291
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003292 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003293 return kCondLE;
3294 }
3295
Mark Mendellc4701932015-04-10 13:18:51 -04003296 IfCondition GetOppositeCondition() const OVERRIDE {
3297 return kCondGT;
3298 }
3299
Dave Allison20dfc792014-06-16 20:44:29 -07003300 private:
Aart Bike9f37602015-10-09 11:15:55 -07003301 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3302
Dave Allison20dfc792014-06-16 20:44:29 -07003303 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3304};
3305
3306class HGreaterThan : public HCondition {
3307 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003308 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3309 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003310
Roland Levillain9867bc72015-08-05 10:21:34 +01003311 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003312 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003313 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003314 // In the following Evaluate methods, a HCompare instruction has
3315 // been merged into this HGreaterThan instruction; evaluate it as
3316 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003317 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003318 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3319 }
3320 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3321 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3322 }
3323 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3324 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003325 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003326
Dave Allison20dfc792014-06-16 20:44:29 -07003327 DECLARE_INSTRUCTION(GreaterThan);
3328
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003329 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003330 return kCondGT;
3331 }
3332
Mark Mendellc4701932015-04-10 13:18:51 -04003333 IfCondition GetOppositeCondition() const OVERRIDE {
3334 return kCondLE;
3335 }
3336
Dave Allison20dfc792014-06-16 20:44:29 -07003337 private:
Aart Bike9f37602015-10-09 11:15:55 -07003338 template <typename T> bool Compute(T x, T y) const { return x > y; }
3339
Dave Allison20dfc792014-06-16 20:44:29 -07003340 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3341};
3342
3343class HGreaterThanOrEqual : public HCondition {
3344 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003345 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3346 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003347
Roland Levillain9867bc72015-08-05 10:21:34 +01003348 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003349 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003350 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003351 // In the following Evaluate methods, a HCompare instruction has
3352 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3353 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003354 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003355 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3356 }
3357 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3358 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3359 }
3360 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3361 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003362 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003363
Dave Allison20dfc792014-06-16 20:44:29 -07003364 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3365
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003366 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003367 return kCondGE;
3368 }
3369
Mark Mendellc4701932015-04-10 13:18:51 -04003370 IfCondition GetOppositeCondition() const OVERRIDE {
3371 return kCondLT;
3372 }
3373
Dave Allison20dfc792014-06-16 20:44:29 -07003374 private:
Aart Bike9f37602015-10-09 11:15:55 -07003375 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3376
Dave Allison20dfc792014-06-16 20:44:29 -07003377 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3378};
3379
Aart Bike9f37602015-10-09 11:15:55 -07003380class HBelow : public HCondition {
3381 public:
3382 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3383 : HCondition(first, second, dex_pc) {}
3384
3385 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003386 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003387 }
3388 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003389 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3390 }
3391 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3392 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3393 LOG(FATAL) << DebugName() << " is not defined for float values";
3394 UNREACHABLE();
3395 }
3396 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3397 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3398 LOG(FATAL) << DebugName() << " is not defined for double values";
3399 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003400 }
3401
3402 DECLARE_INSTRUCTION(Below);
3403
3404 IfCondition GetCondition() const OVERRIDE {
3405 return kCondB;
3406 }
3407
3408 IfCondition GetOppositeCondition() const OVERRIDE {
3409 return kCondAE;
3410 }
3411
3412 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003413 template <typename T> bool Compute(T x, T y) const {
3414 return MakeUnsigned(x) < MakeUnsigned(y);
3415 }
Aart Bike9f37602015-10-09 11:15:55 -07003416
3417 DISALLOW_COPY_AND_ASSIGN(HBelow);
3418};
3419
3420class HBelowOrEqual : public HCondition {
3421 public:
3422 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3423 : HCondition(first, second, dex_pc) {}
3424
3425 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003426 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003427 }
3428 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003429 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3430 }
3431 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3432 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3433 LOG(FATAL) << DebugName() << " is not defined for float values";
3434 UNREACHABLE();
3435 }
3436 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3437 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3438 LOG(FATAL) << DebugName() << " is not defined for double values";
3439 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003440 }
3441
3442 DECLARE_INSTRUCTION(BelowOrEqual);
3443
3444 IfCondition GetCondition() const OVERRIDE {
3445 return kCondBE;
3446 }
3447
3448 IfCondition GetOppositeCondition() const OVERRIDE {
3449 return kCondA;
3450 }
3451
3452 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003453 template <typename T> bool Compute(T x, T y) const {
3454 return MakeUnsigned(x) <= MakeUnsigned(y);
3455 }
Aart Bike9f37602015-10-09 11:15:55 -07003456
3457 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3458};
3459
3460class HAbove : public HCondition {
3461 public:
3462 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3463 : HCondition(first, second, dex_pc) {}
3464
3465 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003466 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003467 }
3468 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003469 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3470 }
3471 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3472 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3473 LOG(FATAL) << DebugName() << " is not defined for float values";
3474 UNREACHABLE();
3475 }
3476 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3477 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3478 LOG(FATAL) << DebugName() << " is not defined for double values";
3479 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003480 }
3481
3482 DECLARE_INSTRUCTION(Above);
3483
3484 IfCondition GetCondition() const OVERRIDE {
3485 return kCondA;
3486 }
3487
3488 IfCondition GetOppositeCondition() const OVERRIDE {
3489 return kCondBE;
3490 }
3491
3492 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003493 template <typename T> bool Compute(T x, T y) const {
3494 return MakeUnsigned(x) > MakeUnsigned(y);
3495 }
Aart Bike9f37602015-10-09 11:15:55 -07003496
3497 DISALLOW_COPY_AND_ASSIGN(HAbove);
3498};
3499
3500class HAboveOrEqual : public HCondition {
3501 public:
3502 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3503 : HCondition(first, second, dex_pc) {}
3504
3505 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003506 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003507 }
3508 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003509 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3510 }
3511 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3512 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3513 LOG(FATAL) << DebugName() << " is not defined for float values";
3514 UNREACHABLE();
3515 }
3516 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3517 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3518 LOG(FATAL) << DebugName() << " is not defined for double values";
3519 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003520 }
3521
3522 DECLARE_INSTRUCTION(AboveOrEqual);
3523
3524 IfCondition GetCondition() const OVERRIDE {
3525 return kCondAE;
3526 }
3527
3528 IfCondition GetOppositeCondition() const OVERRIDE {
3529 return kCondB;
3530 }
3531
3532 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003533 template <typename T> bool Compute(T x, T y) const {
3534 return MakeUnsigned(x) >= MakeUnsigned(y);
3535 }
Aart Bike9f37602015-10-09 11:15:55 -07003536
3537 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3538};
Dave Allison20dfc792014-06-16 20:44:29 -07003539
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003540// Instruction to check how two inputs compare to each other.
3541// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3542class HCompare : public HBinaryOperation {
3543 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003544 // Note that `comparison_type` is the type of comparison performed
3545 // between the comparison's inputs, not the type of the instantiated
3546 // HCompare instruction (which is always Primitive::kPrimInt).
3547 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003548 HInstruction* first,
3549 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003550 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003551 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003552 : HBinaryOperation(Primitive::kPrimInt,
3553 first,
3554 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003555 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003556 dex_pc) {
3557 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003558 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3559 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003560 }
3561
Roland Levillain9867bc72015-08-05 10:21:34 +01003562 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003563 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3564
3565 template <typename T>
3566 int32_t ComputeFP(T x, T y) const {
3567 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3568 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3569 // Handle the bias.
3570 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3571 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003572
Roland Levillain9867bc72015-08-05 10:21:34 +01003573 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003574 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3575 // reach this code path when processing a freshly built HIR
3576 // graph. However HCompare integer instructions can be synthesized
3577 // by the instruction simplifier to implement IntegerCompare and
3578 // IntegerSignum intrinsics, so we have to handle this case.
3579 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003580 }
3581 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003582 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3583 }
3584 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3585 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3586 }
3587 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3588 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003589 }
3590
Calin Juravleddb7df22014-11-25 20:56:51 +00003591 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003592 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003593 }
3594
Vladimir Markoa1de9182016-02-25 11:37:38 +00003595 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003596
Roland Levillain31dd3d62016-02-16 12:21:02 +00003597 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003598 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003599 bool IsGtBias() const {
3600 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003601 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003602 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003603
Roland Levillain1693a1f2016-03-15 14:57:31 +00003604 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3605 // Comparisons do not require a runtime call in any back end.
3606 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003607 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003608
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003609 DECLARE_INSTRUCTION(Compare);
3610
Roland Levillain31dd3d62016-02-16 12:21:02 +00003611 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003612 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3613 static constexpr size_t kFieldComparisonBiasSize =
3614 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3615 static constexpr size_t kNumberOfComparePackedBits =
3616 kFieldComparisonBias + kFieldComparisonBiasSize;
3617 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3618 using ComparisonBiasField =
3619 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3620
Roland Levillain31dd3d62016-02-16 12:21:02 +00003621 // Return an integer constant containing the result of a comparison evaluated at compile time.
3622 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3623 DCHECK(value == -1 || value == 0 || value == 1) << value;
3624 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3625 }
3626
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003627 private:
3628 DISALLOW_COPY_AND_ASSIGN(HCompare);
3629};
3630
David Brazdil6de19382016-01-08 17:37:10 +00003631class HNewInstance : public HExpression<2> {
3632 public:
3633 HNewInstance(HInstruction* cls,
3634 HCurrentMethod* current_method,
3635 uint32_t dex_pc,
3636 uint16_t type_index,
3637 const DexFile& dex_file,
3638 bool can_throw,
3639 bool finalizable,
3640 QuickEntrypointEnum entrypoint)
3641 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3642 type_index_(type_index),
3643 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003644 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003645 SetPackedFlag<kFlagCanThrow>(can_throw);
3646 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003647 SetRawInputAt(0, cls);
3648 SetRawInputAt(1, current_method);
3649 }
3650
3651 uint16_t GetTypeIndex() const { return type_index_; }
3652 const DexFile& GetDexFile() const { return dex_file_; }
3653
3654 // Calls runtime so needs an environment.
3655 bool NeedsEnvironment() const OVERRIDE { return true; }
3656
3657 // It may throw when called on type that's not instantiable/accessible.
3658 // It can throw OOME.
3659 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003660 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>() || true; }
David Brazdil6de19382016-01-08 17:37:10 +00003661
Vladimir Markoa1de9182016-02-25 11:37:38 +00003662 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003663
3664 bool CanBeNull() const OVERRIDE { return false; }
3665
3666 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3667
3668 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3669 entrypoint_ = entrypoint;
3670 }
3671
3672 bool IsStringAlloc() const;
3673
3674 DECLARE_INSTRUCTION(NewInstance);
3675
3676 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003677 static constexpr size_t kFlagCanThrow = kNumberOfExpressionPackedBits;
3678 static constexpr size_t kFlagFinalizable = kFlagCanThrow + 1;
3679 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3680 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3681 "Too many packed fields.");
3682
David Brazdil6de19382016-01-08 17:37:10 +00003683 const uint16_t type_index_;
3684 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003685 QuickEntrypointEnum entrypoint_;
3686
3687 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3688};
3689
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003690enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003691#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3692 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003693#include "intrinsics_list.h"
3694 kNone,
3695 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3696#undef INTRINSICS_LIST
3697#undef OPTIMIZING_INTRINSICS
3698};
3699std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3700
Agi Csaki05f20562015-08-19 14:58:14 -07003701enum IntrinsicNeedsEnvironmentOrCache {
3702 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3703 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003704};
3705
Aart Bik5d75afe2015-12-14 11:57:01 -08003706enum IntrinsicSideEffects {
3707 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3708 kReadSideEffects, // Intrinsic may read heap memory.
3709 kWriteSideEffects, // Intrinsic may write heap memory.
3710 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3711};
3712
3713enum IntrinsicExceptions {
3714 kNoThrow, // Intrinsic does not throw any exceptions.
3715 kCanThrow // Intrinsic may throw exceptions.
3716};
3717
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003718class HInvoke : public HInstruction {
3719 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003720 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003721
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003722 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003723
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003724 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003725 SetRawInputAt(index, argument);
3726 }
3727
Roland Levillain3e3d7332015-04-28 11:00:54 +01003728 // Return the number of arguments. This number can be lower than
3729 // the number of inputs returned by InputCount(), as some invoke
3730 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3731 // inputs at the end of their list of inputs.
3732 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3733
Vladimir Markoa1de9182016-02-25 11:37:38 +00003734 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003735
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003736 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003737 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003738
Vladimir Markoa1de9182016-02-25 11:37:38 +00003739 InvokeType GetOriginalInvokeType() const {
3740 return GetPackedField<OriginalInvokeTypeField>();
3741 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003742
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003743 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003744 return intrinsic_;
3745 }
3746
Aart Bik5d75afe2015-12-14 11:57:01 -08003747 void SetIntrinsic(Intrinsics intrinsic,
3748 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3749 IntrinsicSideEffects side_effects,
3750 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003751
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003752 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003753 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003754 }
3755
Vladimir Markoa1de9182016-02-25 11:37:38 +00003756 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003757
3758 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3759
3760 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3761 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3762 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003763
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003764 uint32_t* GetIntrinsicOptimizations() {
3765 return &intrinsic_optimizations_;
3766 }
3767
3768 const uint32_t* GetIntrinsicOptimizations() const {
3769 return &intrinsic_optimizations_;
3770 }
3771
3772 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3773
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003774 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003775
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003776 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003777 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3778 static constexpr size_t kFieldOriginalInvokeTypeSize =
3779 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3780 static constexpr size_t kFieldReturnType =
3781 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3782 static constexpr size_t kFieldReturnTypeSize =
3783 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3784 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3785 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3786 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3787 using OriginalInvokeTypeField =
3788 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3789 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3790
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003791 HInvoke(ArenaAllocator* arena,
3792 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003793 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003794 Primitive::Type return_type,
3795 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003796 uint32_t dex_method_index,
3797 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003798 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003799 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003800 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003801 inputs_(number_of_arguments + number_of_other_inputs,
3802 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003803 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003804 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003805 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003806 SetPackedField<ReturnTypeField>(return_type);
3807 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3808 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003809 }
3810
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003811 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003812 return inputs_[index];
3813 }
3814
David Brazdil1abb4192015-02-17 18:33:36 +00003815 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003816 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003817 }
3818
Vladimir Markoa1de9182016-02-25 11:37:38 +00003819 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003820
Roland Levillain3e3d7332015-04-28 11:00:54 +01003821 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003822 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003823 const uint32_t dex_method_index_;
3824 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003825
3826 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3827 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003828
3829 private:
3830 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3831};
3832
Calin Juravle175dc732015-08-25 15:42:32 +01003833class HInvokeUnresolved : public HInvoke {
3834 public:
3835 HInvokeUnresolved(ArenaAllocator* arena,
3836 uint32_t number_of_arguments,
3837 Primitive::Type return_type,
3838 uint32_t dex_pc,
3839 uint32_t dex_method_index,
3840 InvokeType invoke_type)
3841 : HInvoke(arena,
3842 number_of_arguments,
3843 0u /* number_of_other_inputs */,
3844 return_type,
3845 dex_pc,
3846 dex_method_index,
3847 invoke_type) {
3848 }
3849
3850 DECLARE_INSTRUCTION(InvokeUnresolved);
3851
3852 private:
3853 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3854};
3855
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003856class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003857 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003858 // Requirements of this method call regarding the class
3859 // initialization (clinit) check of its declaring class.
3860 enum class ClinitCheckRequirement {
3861 kNone, // Class already initialized.
3862 kExplicit, // Static call having explicit clinit check as last input.
3863 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003864 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003865 };
3866
Vladimir Marko58155012015-08-19 12:49:41 +00003867 // Determines how to load the target ArtMethod*.
3868 enum class MethodLoadKind {
3869 // Use a String init ArtMethod* loaded from Thread entrypoints.
3870 kStringInit,
3871
3872 // Use the method's own ArtMethod* loaded by the register allocator.
3873 kRecursive,
3874
3875 // Use ArtMethod* at a known address, embed the direct address in the code.
3876 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3877 kDirectAddress,
3878
3879 // Use ArtMethod* at an address that will be known at link time, embed the direct
3880 // address in the code. If the image is relocatable, emit .patch_oat entry.
3881 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3882 // the image relocatable or not.
3883 kDirectAddressWithFixup,
3884
Vladimir Markoa1de9182016-02-25 11:37:38 +00003885 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003886 // Used when we need to use the dex cache, for example for invoke-static that
3887 // may cause class initialization (the entry may point to a resolution method),
3888 // and we know that we can access the dex cache arrays using a PC-relative load.
3889 kDexCachePcRelative,
3890
3891 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3892 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3893 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3894 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3895 kDexCacheViaMethod,
3896 };
3897
3898 // Determines the location of the code pointer.
3899 enum class CodePtrLocation {
3900 // Recursive call, use local PC-relative call instruction.
3901 kCallSelf,
3902
3903 // Use PC-relative call instruction patched at link time.
3904 // Used for calls within an oat file, boot->boot or app->app.
3905 kCallPCRelative,
3906
3907 // Call to a known target address, embed the direct address in code.
3908 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3909 kCallDirect,
3910
3911 // Call to a target address that will be known at link time, embed the direct
3912 // address in code. If the image is relocatable, emit .patch_oat entry.
3913 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3914 // the image relocatable or not.
3915 kCallDirectWithFixup,
3916
3917 // Use code pointer from the ArtMethod*.
3918 // Used when we don't know the target code. This is also the last-resort-kind used when
3919 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3920 kCallArtMethod,
3921 };
3922
3923 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003924 MethodLoadKind method_load_kind;
3925 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003926 // The method load data holds
3927 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3928 // Note that there are multiple string init methods, each having its own offset.
3929 // - the method address for kDirectAddress
3930 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003931 uint64_t method_load_data;
3932 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003933 };
3934
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003935 HInvokeStaticOrDirect(ArenaAllocator* arena,
3936 uint32_t number_of_arguments,
3937 Primitive::Type return_type,
3938 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003939 uint32_t method_index,
3940 MethodReference target_method,
3941 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003942 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003943 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003944 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003945 : HInvoke(arena,
3946 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003947 // There is potentially one extra argument for the HCurrentMethod node, and
3948 // potentially one other if the clinit check is explicit, and potentially
3949 // one other if the method is a string factory.
3950 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003951 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003952 return_type,
3953 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003954 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003955 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003956 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003957 dispatch_info_(dispatch_info) {
3958 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3959 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3960 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003961
Vladimir Markodc151b22015-10-15 18:02:30 +01003962 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003963 bool had_current_method_input = HasCurrentMethodInput();
3964 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3965
3966 // Using the current method is the default and once we find a better
3967 // method load kind, we should not go back to using the current method.
3968 DCHECK(had_current_method_input || !needs_current_method_input);
3969
3970 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003971 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3972 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003973 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003974 dispatch_info_ = dispatch_info;
3975 }
3976
Vladimir Markoc53c0792015-11-19 15:48:33 +00003977 void AddSpecialInput(HInstruction* input) {
3978 // We allow only one special input.
3979 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3980 DCHECK(InputCount() == GetSpecialInputIndex() ||
3981 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3982 InsertInputAt(GetSpecialInputIndex(), input);
3983 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003984
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003985 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003986 // We access the method via the dex cache so we can't do an implicit null check.
3987 // TODO: for intrinsics we can generate implicit null checks.
3988 return false;
3989 }
3990
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003991 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003992 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003993 }
3994
Vladimir Markoc53c0792015-11-19 15:48:33 +00003995 // Get the index of the special input, if any.
3996 //
David Brazdil6de19382016-01-08 17:37:10 +00003997 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3998 // method pointer; otherwise there may be one platform-specific special input,
3999 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004000 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004001 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004002
Vladimir Markoa1de9182016-02-25 11:37:38 +00004003 InvokeType GetOptimizedInvokeType() const {
4004 return GetPackedField<OptimizedInvokeTypeField>();
4005 }
4006
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004007 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004008 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004009 }
4010
Vladimir Marko58155012015-08-19 12:49:41 +00004011 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4012 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4013 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004014 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004015 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004016 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004017 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004018 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4019 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004020 bool HasCurrentMethodInput() const {
4021 // This function can be called only after the invoke has been fully initialized by the builder.
4022 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004023 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004024 return true;
4025 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004026 DCHECK(InputCount() == GetSpecialInputIndex() ||
4027 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004028 return false;
4029 }
4030 }
Vladimir Marko58155012015-08-19 12:49:41 +00004031 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
4032 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004033 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00004034
4035 int32_t GetStringInitOffset() const {
4036 DCHECK(IsStringInit());
4037 return dispatch_info_.method_load_data;
4038 }
4039
4040 uint64_t GetMethodAddress() const {
4041 DCHECK(HasMethodAddress());
4042 return dispatch_info_.method_load_data;
4043 }
4044
4045 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004046 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004047 return dispatch_info_.method_load_data;
4048 }
4049
4050 uint64_t GetDirectCodePtr() const {
4051 DCHECK(HasDirectCodePtr());
4052 return dispatch_info_.direct_code_ptr;
4053 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004054
Vladimir Markoa1de9182016-02-25 11:37:38 +00004055 ClinitCheckRequirement GetClinitCheckRequirement() const {
4056 return GetPackedField<ClinitCheckRequirementField>();
4057 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004058
Roland Levillain4c0eb422015-04-24 16:43:49 +01004059 // Is this instruction a call to a static method?
4060 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004061 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004062 }
4063
Vladimir Markofbb184a2015-11-13 14:47:00 +00004064 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4065 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4066 // instruction; only relevant for static calls with explicit clinit check.
4067 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004068 DCHECK(IsStaticWithExplicitClinitCheck());
4069 size_t last_input_index = InputCount() - 1;
4070 HInstruction* last_input = InputAt(last_input_index);
4071 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004072 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004073 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004074 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004075 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004076 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004077 }
4078
4079 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004080 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004081 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004082 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004083 }
4084
4085 // Is this a call to a static method whose declaring class has an
4086 // implicit intialization check requirement?
4087 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004088 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004089 }
4090
Vladimir Markob554b5a2015-11-06 12:57:55 +00004091 // Does this method load kind need the current method as an input?
4092 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4093 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4094 }
4095
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004096 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004097
Roland Levillain4c0eb422015-04-24 16:43:49 +01004098 protected:
4099 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
4100 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
4101 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
4102 HInstruction* input = input_record.GetInstruction();
4103 // `input` is the last input of a static invoke marked as having
4104 // an explicit clinit check. It must either be:
4105 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4106 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4107 DCHECK(input != nullptr);
4108 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
4109 }
4110 return input_record;
4111 }
4112
Vladimir Markoc53c0792015-11-19 15:48:33 +00004113 void InsertInputAt(size_t index, HInstruction* input);
4114 void RemoveInputAt(size_t index);
4115
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004116 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004117 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4118 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4119 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4120 static constexpr size_t kFieldClinitCheckRequirement =
4121 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4122 static constexpr size_t kFieldClinitCheckRequirementSize =
4123 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4124 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4125 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4126 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4127 "Too many packed fields.");
4128 using OptimizedInvokeTypeField =
4129 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4130 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4131 kFieldClinitCheckRequirement,
4132 kFieldClinitCheckRequirementSize>;
4133
Vladimir Marko58155012015-08-19 12:49:41 +00004134 // The target method may refer to different dex file or method index than the original
4135 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4136 // in derived class to increase visibility.
4137 MethodReference target_method_;
4138 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004139
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004140 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004141};
Vladimir Markof64242a2015-12-01 14:58:23 +00004142std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004143std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004144
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004145class HInvokeVirtual : public HInvoke {
4146 public:
4147 HInvokeVirtual(ArenaAllocator* arena,
4148 uint32_t number_of_arguments,
4149 Primitive::Type return_type,
4150 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004151 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004152 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004153 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004154 vtable_index_(vtable_index) {}
4155
Calin Juravle641547a2015-04-21 22:08:51 +01004156 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004157 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004158 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004159 }
4160
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004161 uint32_t GetVTableIndex() const { return vtable_index_; }
4162
4163 DECLARE_INSTRUCTION(InvokeVirtual);
4164
4165 private:
4166 const uint32_t vtable_index_;
4167
4168 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4169};
4170
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004171class HInvokeInterface : public HInvoke {
4172 public:
4173 HInvokeInterface(ArenaAllocator* arena,
4174 uint32_t number_of_arguments,
4175 Primitive::Type return_type,
4176 uint32_t dex_pc,
4177 uint32_t dex_method_index,
4178 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004179 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004180 imt_index_(imt_index) {}
4181
Calin Juravle641547a2015-04-21 22:08:51 +01004182 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004183 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004184 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004185 }
4186
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004187 uint32_t GetImtIndex() const { return imt_index_; }
4188 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4189
4190 DECLARE_INSTRUCTION(InvokeInterface);
4191
4192 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004193 const uint32_t imt_index_;
4194
4195 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4196};
4197
Roland Levillain88cb1752014-10-20 16:36:47 +01004198class HNeg : public HUnaryOperation {
4199 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004200 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004201 : HUnaryOperation(result_type, input, dex_pc) {
4202 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4203 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004204
Roland Levillain9867bc72015-08-05 10:21:34 +01004205 template <typename T> T Compute(T x) const { return -x; }
4206
4207 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004208 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004209 }
4210 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004211 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004212 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004213 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4214 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4215 }
4216 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4217 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4218 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004219
Roland Levillain88cb1752014-10-20 16:36:47 +01004220 DECLARE_INSTRUCTION(Neg);
4221
4222 private:
4223 DISALLOW_COPY_AND_ASSIGN(HNeg);
4224};
4225
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004226class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004227 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004228 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004229 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004230 uint32_t dex_pc,
4231 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004232 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004233 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004234 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004235 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004236 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004237 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004238 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004239 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004240 }
4241
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004242 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004243 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004244
4245 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004246 bool NeedsEnvironment() const OVERRIDE { return true; }
4247
Mingyao Yang0c365e62015-03-31 15:09:29 -07004248 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4249 bool CanThrow() const OVERRIDE { return true; }
4250
Calin Juravle10e244f2015-01-26 18:54:32 +00004251 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004252
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004253 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4254
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004255 DECLARE_INSTRUCTION(NewArray);
4256
4257 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004258 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004259 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004260 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004261
4262 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4263};
4264
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004265class HAdd : public HBinaryOperation {
4266 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004267 HAdd(Primitive::Type result_type,
4268 HInstruction* left,
4269 HInstruction* right,
4270 uint32_t dex_pc = kNoDexPc)
4271 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004272
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004273 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004274
Roland Levillain9867bc72015-08-05 10:21:34 +01004275 template <typename T> T Compute(T x, T y) const { return x + y; }
4276
4277 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004278 return GetBlock()->GetGraph()->GetIntConstant(
4279 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004280 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004281 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004282 return GetBlock()->GetGraph()->GetLongConstant(
4283 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004284 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004285 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4286 return GetBlock()->GetGraph()->GetFloatConstant(
4287 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4288 }
4289 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4290 return GetBlock()->GetGraph()->GetDoubleConstant(
4291 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4292 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004293
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004294 DECLARE_INSTRUCTION(Add);
4295
4296 private:
4297 DISALLOW_COPY_AND_ASSIGN(HAdd);
4298};
4299
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004300class HSub : public HBinaryOperation {
4301 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004302 HSub(Primitive::Type result_type,
4303 HInstruction* left,
4304 HInstruction* right,
4305 uint32_t dex_pc = kNoDexPc)
4306 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004307
Roland Levillain9867bc72015-08-05 10:21:34 +01004308 template <typename T> T Compute(T x, T y) const { return x - y; }
4309
4310 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004311 return GetBlock()->GetGraph()->GetIntConstant(
4312 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004313 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004314 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004315 return GetBlock()->GetGraph()->GetLongConstant(
4316 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004317 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004318 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4319 return GetBlock()->GetGraph()->GetFloatConstant(
4320 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4321 }
4322 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4323 return GetBlock()->GetGraph()->GetDoubleConstant(
4324 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4325 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004326
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004327 DECLARE_INSTRUCTION(Sub);
4328
4329 private:
4330 DISALLOW_COPY_AND_ASSIGN(HSub);
4331};
4332
Calin Juravle34bacdf2014-10-07 20:23:36 +01004333class HMul : public HBinaryOperation {
4334 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004335 HMul(Primitive::Type result_type,
4336 HInstruction* left,
4337 HInstruction* right,
4338 uint32_t dex_pc = kNoDexPc)
4339 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004340
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004341 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004342
Roland Levillain9867bc72015-08-05 10:21:34 +01004343 template <typename T> T Compute(T x, T y) const { return x * y; }
4344
4345 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004346 return GetBlock()->GetGraph()->GetIntConstant(
4347 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004348 }
4349 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004350 return GetBlock()->GetGraph()->GetLongConstant(
4351 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004352 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004353 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4354 return GetBlock()->GetGraph()->GetFloatConstant(
4355 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4356 }
4357 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4358 return GetBlock()->GetGraph()->GetDoubleConstant(
4359 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4360 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004361
4362 DECLARE_INSTRUCTION(Mul);
4363
4364 private:
4365 DISALLOW_COPY_AND_ASSIGN(HMul);
4366};
4367
Calin Juravle7c4954d2014-10-28 16:57:40 +00004368class HDiv : public HBinaryOperation {
4369 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004370 HDiv(Primitive::Type result_type,
4371 HInstruction* left,
4372 HInstruction* right,
4373 uint32_t dex_pc)
4374 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004375
Roland Levillain9867bc72015-08-05 10:21:34 +01004376 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004377 T ComputeIntegral(T x, T y) const {
4378 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004379 // Our graph structure ensures we never have 0 for `y` during
4380 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004381 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004382 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004383 return (y == -1) ? -x : x / y;
4384 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004385
Roland Levillain31dd3d62016-02-16 12:21:02 +00004386 template <typename T>
4387 T ComputeFP(T x, T y) const {
4388 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4389 return x / y;
4390 }
4391
Roland Levillain9867bc72015-08-05 10:21:34 +01004392 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004393 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004394 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004395 }
4396 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004397 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004398 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4399 }
4400 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4401 return GetBlock()->GetGraph()->GetFloatConstant(
4402 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4403 }
4404 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4405 return GetBlock()->GetGraph()->GetDoubleConstant(
4406 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004407 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004408
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004409 static SideEffects SideEffectsForArchRuntimeCalls() {
4410 // The generated code can use a runtime call.
4411 return SideEffects::CanTriggerGC();
4412 }
4413
Calin Juravle7c4954d2014-10-28 16:57:40 +00004414 DECLARE_INSTRUCTION(Div);
4415
4416 private:
4417 DISALLOW_COPY_AND_ASSIGN(HDiv);
4418};
4419
Calin Juravlebacfec32014-11-14 15:54:36 +00004420class HRem : public HBinaryOperation {
4421 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004422 HRem(Primitive::Type result_type,
4423 HInstruction* left,
4424 HInstruction* right,
4425 uint32_t dex_pc)
4426 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004427
Roland Levillain9867bc72015-08-05 10:21:34 +01004428 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004429 T ComputeIntegral(T x, T y) const {
4430 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004431 // Our graph structure ensures we never have 0 for `y` during
4432 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004433 DCHECK_NE(y, 0);
4434 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4435 return (y == -1) ? 0 : x % y;
4436 }
4437
Roland Levillain31dd3d62016-02-16 12:21:02 +00004438 template <typename T>
4439 T ComputeFP(T x, T y) const {
4440 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4441 return std::fmod(x, y);
4442 }
4443
Roland Levillain9867bc72015-08-05 10:21:34 +01004444 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004445 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004446 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004447 }
4448 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004449 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004450 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004451 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004452 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4453 return GetBlock()->GetGraph()->GetFloatConstant(
4454 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4455 }
4456 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4457 return GetBlock()->GetGraph()->GetDoubleConstant(
4458 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4459 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004460
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004461 static SideEffects SideEffectsForArchRuntimeCalls() {
4462 return SideEffects::CanTriggerGC();
4463 }
4464
Calin Juravlebacfec32014-11-14 15:54:36 +00004465 DECLARE_INSTRUCTION(Rem);
4466
4467 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004468 DISALLOW_COPY_AND_ASSIGN(HRem);
4469};
4470
Calin Juravled0d48522014-11-04 16:40:20 +00004471class HDivZeroCheck : public HExpression<1> {
4472 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004473 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4474 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004475 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004476 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004477 SetRawInputAt(0, value);
4478 }
4479
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004480 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4481
Calin Juravled0d48522014-11-04 16:40:20 +00004482 bool CanBeMoved() const OVERRIDE { return true; }
4483
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004484 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004485 return true;
4486 }
4487
4488 bool NeedsEnvironment() const OVERRIDE { return true; }
4489 bool CanThrow() const OVERRIDE { return true; }
4490
Calin Juravled0d48522014-11-04 16:40:20 +00004491 DECLARE_INSTRUCTION(DivZeroCheck);
4492
4493 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004494 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4495};
4496
Calin Juravle9aec02f2014-11-18 23:06:35 +00004497class HShl : public HBinaryOperation {
4498 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004499 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004500 HInstruction* value,
4501 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004502 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004503 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4504 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4505 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004506 }
4507
Roland Levillain5b5b9312016-03-22 14:57:31 +00004508 template <typename T>
4509 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4510 return value << (distance & max_shift_distance);
4511 }
4512
4513 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004514 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004515 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004516 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004517 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004518 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004519 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004520 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004521 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4522 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4523 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4524 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004525 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004526 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4527 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004528 LOG(FATAL) << DebugName() << " is not defined for float values";
4529 UNREACHABLE();
4530 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004531 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4532 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004533 LOG(FATAL) << DebugName() << " is not defined for double values";
4534 UNREACHABLE();
4535 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004536
4537 DECLARE_INSTRUCTION(Shl);
4538
4539 private:
4540 DISALLOW_COPY_AND_ASSIGN(HShl);
4541};
4542
4543class HShr : public HBinaryOperation {
4544 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004545 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004546 HInstruction* value,
4547 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004548 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004549 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4550 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4551 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004552 }
4553
Roland Levillain5b5b9312016-03-22 14:57:31 +00004554 template <typename T>
4555 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4556 return value >> (distance & max_shift_distance);
4557 }
4558
4559 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004560 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004561 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004562 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004563 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004564 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004565 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004566 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004567 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4568 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4569 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4570 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004571 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004572 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4573 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004574 LOG(FATAL) << DebugName() << " is not defined for float values";
4575 UNREACHABLE();
4576 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004577 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4578 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004579 LOG(FATAL) << DebugName() << " is not defined for double values";
4580 UNREACHABLE();
4581 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004582
4583 DECLARE_INSTRUCTION(Shr);
4584
4585 private:
4586 DISALLOW_COPY_AND_ASSIGN(HShr);
4587};
4588
4589class HUShr : public HBinaryOperation {
4590 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004591 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004592 HInstruction* value,
4593 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004594 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004595 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4596 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4597 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004598 }
4599
Roland Levillain5b5b9312016-03-22 14:57:31 +00004600 template <typename T>
4601 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4602 typedef typename std::make_unsigned<T>::type V;
4603 V ux = static_cast<V>(value);
4604 return static_cast<T>(ux >> (distance & max_shift_distance));
4605 }
4606
4607 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004608 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004609 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004610 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004611 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004612 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004613 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004614 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004615 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4616 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4617 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4618 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004619 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004620 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4621 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004622 LOG(FATAL) << DebugName() << " is not defined for float values";
4623 UNREACHABLE();
4624 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004625 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4626 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004627 LOG(FATAL) << DebugName() << " is not defined for double values";
4628 UNREACHABLE();
4629 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004630
4631 DECLARE_INSTRUCTION(UShr);
4632
4633 private:
4634 DISALLOW_COPY_AND_ASSIGN(HUShr);
4635};
4636
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004637class HAnd : public HBinaryOperation {
4638 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004639 HAnd(Primitive::Type result_type,
4640 HInstruction* left,
4641 HInstruction* right,
4642 uint32_t dex_pc = kNoDexPc)
4643 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004644
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004645 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004646
Roland Levillaine53bd812016-02-24 14:54:18 +00004647 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004648
4649 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004650 return GetBlock()->GetGraph()->GetIntConstant(
4651 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004652 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004653 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004654 return GetBlock()->GetGraph()->GetLongConstant(
4655 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004656 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004657 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4658 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4659 LOG(FATAL) << DebugName() << " is not defined for float values";
4660 UNREACHABLE();
4661 }
4662 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4663 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4664 LOG(FATAL) << DebugName() << " is not defined for double values";
4665 UNREACHABLE();
4666 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004667
4668 DECLARE_INSTRUCTION(And);
4669
4670 private:
4671 DISALLOW_COPY_AND_ASSIGN(HAnd);
4672};
4673
4674class HOr : public HBinaryOperation {
4675 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004676 HOr(Primitive::Type result_type,
4677 HInstruction* left,
4678 HInstruction* right,
4679 uint32_t dex_pc = kNoDexPc)
4680 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004681
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004682 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004683
Roland Levillaine53bd812016-02-24 14:54:18 +00004684 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004685
4686 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004687 return GetBlock()->GetGraph()->GetIntConstant(
4688 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004689 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004690 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004691 return GetBlock()->GetGraph()->GetLongConstant(
4692 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004693 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004694 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4695 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4696 LOG(FATAL) << DebugName() << " is not defined for float values";
4697 UNREACHABLE();
4698 }
4699 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4700 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4701 LOG(FATAL) << DebugName() << " is not defined for double values";
4702 UNREACHABLE();
4703 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004704
4705 DECLARE_INSTRUCTION(Or);
4706
4707 private:
4708 DISALLOW_COPY_AND_ASSIGN(HOr);
4709};
4710
4711class HXor : public HBinaryOperation {
4712 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004713 HXor(Primitive::Type result_type,
4714 HInstruction* left,
4715 HInstruction* right,
4716 uint32_t dex_pc = kNoDexPc)
4717 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004718
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004719 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004720
Roland Levillaine53bd812016-02-24 14:54:18 +00004721 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004722
4723 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004724 return GetBlock()->GetGraph()->GetIntConstant(
4725 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004726 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004727 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004728 return GetBlock()->GetGraph()->GetLongConstant(
4729 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004730 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004731 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4732 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4733 LOG(FATAL) << DebugName() << " is not defined for float values";
4734 UNREACHABLE();
4735 }
4736 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4737 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4738 LOG(FATAL) << DebugName() << " is not defined for double values";
4739 UNREACHABLE();
4740 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004741
4742 DECLARE_INSTRUCTION(Xor);
4743
4744 private:
4745 DISALLOW_COPY_AND_ASSIGN(HXor);
4746};
4747
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004748class HRor : public HBinaryOperation {
4749 public:
4750 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004751 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004752 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4753 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004754 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004755
Roland Levillain5b5b9312016-03-22 14:57:31 +00004756 template <typename T>
4757 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4758 typedef typename std::make_unsigned<T>::type V;
4759 V ux = static_cast<V>(value);
4760 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004761 return static_cast<T>(ux);
4762 } else {
4763 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004764 return static_cast<T>(ux >> (distance & max_shift_value)) |
4765 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004766 }
4767 }
4768
Roland Levillain5b5b9312016-03-22 14:57:31 +00004769 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004770 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004771 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004772 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004773 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004774 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004775 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004776 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004777 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4778 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4779 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4780 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004781 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004782 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4783 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004784 LOG(FATAL) << DebugName() << " is not defined for float values";
4785 UNREACHABLE();
4786 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004787 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4788 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004789 LOG(FATAL) << DebugName() << " is not defined for double values";
4790 UNREACHABLE();
4791 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004792
4793 DECLARE_INSTRUCTION(Ror);
4794
4795 private:
4796 DISALLOW_COPY_AND_ASSIGN(HRor);
4797};
4798
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004799// The value of a parameter in this method. Its location depends on
4800// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004801class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004802 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004803 HParameterValue(const DexFile& dex_file,
4804 uint16_t type_index,
4805 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004806 Primitive::Type parameter_type,
4807 bool is_this = false)
4808 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004809 dex_file_(dex_file),
4810 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004811 index_(index) {
4812 SetPackedFlag<kFlagIsThis>(is_this);
4813 SetPackedFlag<kFlagCanBeNull>(!is_this);
4814 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004815
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004816 const DexFile& GetDexFile() const { return dex_file_; }
4817 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004818 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004819 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004820
Vladimir Markoa1de9182016-02-25 11:37:38 +00004821 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4822 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004823
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004824 DECLARE_INSTRUCTION(ParameterValue);
4825
4826 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004827 // Whether or not the parameter value corresponds to 'this' argument.
4828 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4829 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4830 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4831 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4832 "Too many packed fields.");
4833
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004834 const DexFile& dex_file_;
4835 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004836 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004837 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004838 const uint8_t index_;
4839
4840 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4841};
4842
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004843class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004844 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004845 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4846 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004847
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004848 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004849 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004850 return true;
4851 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004852
Roland Levillain9867bc72015-08-05 10:21:34 +01004853 template <typename T> T Compute(T x) const { return ~x; }
4854
4855 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004856 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004857 }
4858 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004859 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004860 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004861 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4862 LOG(FATAL) << DebugName() << " is not defined for float values";
4863 UNREACHABLE();
4864 }
4865 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4866 LOG(FATAL) << DebugName() << " is not defined for double values";
4867 UNREACHABLE();
4868 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004869
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004870 DECLARE_INSTRUCTION(Not);
4871
4872 private:
4873 DISALLOW_COPY_AND_ASSIGN(HNot);
4874};
4875
David Brazdil66d126e2015-04-03 16:02:44 +01004876class HBooleanNot : public HUnaryOperation {
4877 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004878 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4879 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004880
4881 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004882 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004883 return true;
4884 }
4885
Roland Levillain9867bc72015-08-05 10:21:34 +01004886 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004887 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004888 return !x;
4889 }
4890
Roland Levillain9867bc72015-08-05 10:21:34 +01004891 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004892 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004893 }
4894 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4895 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004896 UNREACHABLE();
4897 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004898 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4899 LOG(FATAL) << DebugName() << " is not defined for float values";
4900 UNREACHABLE();
4901 }
4902 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4903 LOG(FATAL) << DebugName() << " is not defined for double values";
4904 UNREACHABLE();
4905 }
David Brazdil66d126e2015-04-03 16:02:44 +01004906
4907 DECLARE_INSTRUCTION(BooleanNot);
4908
4909 private:
4910 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4911};
4912
Roland Levillaindff1f282014-11-05 14:15:05 +00004913class HTypeConversion : public HExpression<1> {
4914 public:
4915 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004916 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004917 : HExpression(result_type,
4918 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4919 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004920 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004921 // Invariant: We should never generate a conversion to a Boolean value.
4922 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004923 }
4924
4925 HInstruction* GetInput() const { return InputAt(0); }
4926 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4927 Primitive::Type GetResultType() const { return GetType(); }
4928
4929 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004930 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004931
Mark Mendelle82549b2015-05-06 10:55:34 -04004932 // Try to statically evaluate the conversion and return a HConstant
4933 // containing the result. If the input cannot be converted, return nullptr.
4934 HConstant* TryStaticEvaluation() const;
4935
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004936 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4937 Primitive::Type result_type) {
4938 // Some architectures may not require the 'GC' side effects, but at this point
4939 // in the compilation process we do not know what architecture we will
4940 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004941 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4942 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004943 return SideEffects::CanTriggerGC();
4944 }
4945 return SideEffects::None();
4946 }
4947
Roland Levillaindff1f282014-11-05 14:15:05 +00004948 DECLARE_INSTRUCTION(TypeConversion);
4949
4950 private:
4951 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4952};
4953
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004954static constexpr uint32_t kNoRegNumber = -1;
4955
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004956class HNullCheck : public HExpression<1> {
4957 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004958 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4959 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004960 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004961 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004962 SetRawInputAt(0, value);
4963 }
4964
Calin Juravle10e244f2015-01-26 18:54:32 +00004965 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004966 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004967 return true;
4968 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004969
Calin Juravle10e244f2015-01-26 18:54:32 +00004970 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004971
Calin Juravle10e244f2015-01-26 18:54:32 +00004972 bool CanThrow() const OVERRIDE { return true; }
4973
4974 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004975
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004976
4977 DECLARE_INSTRUCTION(NullCheck);
4978
4979 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004980 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4981};
4982
4983class FieldInfo : public ValueObject {
4984 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004985 FieldInfo(MemberOffset field_offset,
4986 Primitive::Type field_type,
4987 bool is_volatile,
4988 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004989 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004990 const DexFile& dex_file,
4991 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004992 : field_offset_(field_offset),
4993 field_type_(field_type),
4994 is_volatile_(is_volatile),
4995 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004996 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004997 dex_file_(dex_file),
4998 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004999
5000 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005001 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005002 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005003 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005004 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005005 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005006 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005007
5008 private:
5009 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005010 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005011 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005012 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005013 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005014 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005015 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005016};
5017
5018class HInstanceFieldGet : public HExpression<1> {
5019 public:
5020 HInstanceFieldGet(HInstruction* value,
5021 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005022 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005023 bool is_volatile,
5024 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005025 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005026 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005027 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005028 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005029 : HExpression(field_type,
5030 SideEffects::FieldReadOfType(field_type, is_volatile),
5031 dex_pc),
5032 field_info_(field_offset,
5033 field_type,
5034 is_volatile,
5035 field_idx,
5036 declaring_class_def_index,
5037 dex_file,
5038 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005039 SetRawInputAt(0, value);
5040 }
5041
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005042 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005043
5044 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5045 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
5046 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005047 }
5048
Calin Juravle641547a2015-04-21 22:08:51 +01005049 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5050 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005051 }
5052
5053 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005054 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5055 }
5056
Calin Juravle52c48962014-12-16 17:02:57 +00005057 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005058 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005059 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005060 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005061
5062 DECLARE_INSTRUCTION(InstanceFieldGet);
5063
5064 private:
5065 const FieldInfo field_info_;
5066
5067 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5068};
5069
5070class HInstanceFieldSet : public HTemplateInstruction<2> {
5071 public:
5072 HInstanceFieldSet(HInstruction* object,
5073 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005074 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005075 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005076 bool is_volatile,
5077 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005078 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005079 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005080 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005081 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005082 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5083 dex_pc),
5084 field_info_(field_offset,
5085 field_type,
5086 is_volatile,
5087 field_idx,
5088 declaring_class_def_index,
5089 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005090 dex_cache) {
5091 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005092 SetRawInputAt(0, object);
5093 SetRawInputAt(1, value);
5094 }
5095
Calin Juravle641547a2015-04-21 22:08:51 +01005096 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5097 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005098 }
5099
Calin Juravle52c48962014-12-16 17:02:57 +00005100 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005101 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005102 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005103 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005104 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005105 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5106 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005107
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005108 DECLARE_INSTRUCTION(InstanceFieldSet);
5109
5110 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005111 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5112 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5113 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5114 "Too many packed fields.");
5115
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005116 const FieldInfo field_info_;
5117
5118 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5119};
5120
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005121class HArrayGet : public HExpression<2> {
5122 public:
Aart Bik18b36ab2016-04-13 16:41:35 -07005123 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type, uint32_t dex_pc)
5124 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005125 SetRawInputAt(0, array);
5126 SetRawInputAt(1, index);
5127 }
5128
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005129 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005130 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005131 return true;
5132 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005133 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005134 // TODO: We can be smarter here.
5135 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5136 // which generates the implicit null check. There are cases when these can be removed
5137 // to produce better code. If we ever add optimizations to do so we should allow an
5138 // implicit check here (as long as the address falls in the first page).
5139 return false;
5140 }
5141
David Brazdil4833f5a2015-12-16 10:37:39 +00005142 bool IsEquivalentOf(HArrayGet* other) const {
5143 bool result = (GetDexPc() == other->GetDexPc());
5144 if (kIsDebugBuild && result) {
5145 DCHECK_EQ(GetBlock(), other->GetBlock());
5146 DCHECK_EQ(GetArray(), other->GetArray());
5147 DCHECK_EQ(GetIndex(), other->GetIndex());
5148 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005149 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005150 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005151 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5152 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005153 }
5154 }
5155 return result;
5156 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005157
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005158 HInstruction* GetArray() const { return InputAt(0); }
5159 HInstruction* GetIndex() const { return InputAt(1); }
5160
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005161 DECLARE_INSTRUCTION(ArrayGet);
5162
5163 private:
5164 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5165};
5166
5167class HArraySet : public HTemplateInstruction<3> {
5168 public:
5169 HArraySet(HInstruction* array,
5170 HInstruction* index,
5171 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005172 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005173 uint32_t dex_pc)
5174 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005175 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5176 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5177 SetPackedFlag<kFlagValueCanBeNull>(true);
5178 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005179 SetRawInputAt(0, array);
5180 SetRawInputAt(1, index);
5181 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005182 // Make a best guess now, may be refined during SSA building.
5183 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005184 }
5185
Calin Juravle77520bc2015-01-12 18:45:46 +00005186 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005187 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005188 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005189 }
5190
Mingyao Yang81014cb2015-06-02 03:16:27 -07005191 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005192 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005193
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005194 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005195 // TODO: Same as for ArrayGet.
5196 return false;
5197 }
5198
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005199 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005200 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005201 }
5202
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005203 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005204 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005205 }
5206
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005207 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005208 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005209 }
5210
Vladimir Markoa1de9182016-02-25 11:37:38 +00005211 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5212 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5213 bool StaticTypeOfArrayIsObjectArray() const {
5214 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5215 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005216
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005217 HInstruction* GetArray() const { return InputAt(0); }
5218 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005219 HInstruction* GetValue() const { return InputAt(2); }
5220
5221 Primitive::Type GetComponentType() const {
5222 // The Dex format does not type floating point index operations. Since the
5223 // `expected_component_type_` is set during building and can therefore not
5224 // be correct, we also check what is the value type. If it is a floating
5225 // point type, we must use that type.
5226 Primitive::Type value_type = GetValue()->GetType();
5227 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5228 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005229 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005230 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005231
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005232 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005233 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005234 }
5235
Aart Bik18b36ab2016-04-13 16:41:35 -07005236 void ComputeSideEffects() {
5237 Primitive::Type type = GetComponentType();
5238 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5239 SideEffectsForArchRuntimeCalls(type)));
5240 }
5241
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005242 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5243 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5244 }
5245
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005246 DECLARE_INSTRUCTION(ArraySet);
5247
5248 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005249 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5250 static constexpr size_t kFieldExpectedComponentTypeSize =
5251 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5252 static constexpr size_t kFlagNeedsTypeCheck =
5253 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5254 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005255 // Cached information for the reference_type_info_ so that codegen
5256 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005257 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5258 static constexpr size_t kNumberOfArraySetPackedBits =
5259 kFlagStaticTypeOfArrayIsObjectArray + 1;
5260 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5261 using ExpectedComponentTypeField =
5262 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005263
5264 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5265};
5266
5267class HArrayLength : public HExpression<1> {
5268 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005269 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005270 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005271 // Note that arrays do not change length, so the instruction does not
5272 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005273 SetRawInputAt(0, array);
5274 }
5275
Calin Juravle77520bc2015-01-12 18:45:46 +00005276 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005277 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005278 return true;
5279 }
Calin Juravle641547a2015-04-21 22:08:51 +01005280 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5281 return obj == InputAt(0);
5282 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005283
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005284 DECLARE_INSTRUCTION(ArrayLength);
5285
5286 private:
5287 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5288};
5289
5290class HBoundsCheck : public HExpression<2> {
5291 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005292 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5293 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005294 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005295 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005296 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005297 SetRawInputAt(0, index);
5298 SetRawInputAt(1, length);
5299 }
5300
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005301 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005302 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005303 return true;
5304 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005305
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005306 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005307
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005308 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005309
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005310 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005311
5312 DECLARE_INSTRUCTION(BoundsCheck);
5313
5314 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005315 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5316};
5317
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005318class HSuspendCheck : public HTemplateInstruction<0> {
5319 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005320 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005321 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005322
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005323 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005324 return true;
5325 }
5326
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005327 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5328 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005329
5330 DECLARE_INSTRUCTION(SuspendCheck);
5331
5332 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005333 // Only used for code generation, in order to share the same slow path between back edges
5334 // of a same loop.
5335 SlowPathCode* slow_path_;
5336
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005337 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5338};
5339
David Srbecky0cf44932015-12-09 14:09:59 +00005340// Pseudo-instruction which provides the native debugger with mapping information.
5341// It ensures that we can generate line number and local variables at this point.
5342class HNativeDebugInfo : public HTemplateInstruction<0> {
5343 public:
5344 explicit HNativeDebugInfo(uint32_t dex_pc)
5345 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5346
5347 bool NeedsEnvironment() const OVERRIDE {
5348 return true;
5349 }
5350
5351 DECLARE_INSTRUCTION(NativeDebugInfo);
5352
5353 private:
5354 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5355};
5356
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005357/**
5358 * Instruction to load a Class object.
5359 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005360class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005361 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005362 HLoadClass(HCurrentMethod* current_method,
5363 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005364 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005365 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005366 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005367 bool needs_access_check,
5368 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005369 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005370 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005371 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005372 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005373 // Referrers class should not need access check. We never inline unverified
5374 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005375 DCHECK(!is_referrers_class || !needs_access_check);
5376
5377 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5378 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5379 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5380 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005381 SetRawInputAt(0, current_method);
5382 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005383
5384 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005385
5386 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005387 // Note that we don't need to test for generate_clinit_check_.
5388 // Whether or not we need to generate the clinit check is processed in
5389 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005390 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005391 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005392 }
5393
5394 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5395
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005396 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005397 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005398
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005399 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005400 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005401 }
5402
Calin Juravle0ba218d2015-05-19 18:46:01 +01005403 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005404 // The entrypoint the code generator is going to call does not do
5405 // clinit of the class.
5406 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005407 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005408 }
5409
5410 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005411 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005412 (!IsReferrersClass() && !IsInDexCache()) ||
5413 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005414 }
5415
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005416
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005417 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005418 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005419 }
5420
Calin Juravleacf735c2015-02-12 15:25:22 +00005421 ReferenceTypeInfo GetLoadedClassRTI() {
5422 return loaded_class_rti_;
5423 }
5424
5425 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5426 // Make sure we only set exact types (the loaded class should never be merged).
5427 DCHECK(rti.IsExact());
5428 loaded_class_rti_ = rti;
5429 }
5430
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005431 const DexFile& GetDexFile() { return dex_file_; }
5432
Vladimir Markoa1de9182016-02-25 11:37:38 +00005433 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005434
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005435 static SideEffects SideEffectsForArchRuntimeCalls() {
5436 return SideEffects::CanTriggerGC();
5437 }
5438
Vladimir Markoa1de9182016-02-25 11:37:38 +00005439 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5440 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5441 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5442 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005443
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005444 DECLARE_INSTRUCTION(LoadClass);
5445
5446 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005447 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5448 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5449 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005450 // Whether this instruction must generate the initialization check.
5451 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005452 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5453 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5454 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5455
5456 const uint16_t type_index_;
5457 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005458
Calin Juravleacf735c2015-02-12 15:25:22 +00005459 ReferenceTypeInfo loaded_class_rti_;
5460
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005461 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5462};
5463
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005464class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005465 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005466 // Determines how to load the String.
5467 enum class LoadKind {
5468 // Use boot image String* address that will be known at link time.
5469 // Used for boot image strings referenced by boot image code in non-PIC mode.
5470 kBootImageLinkTimeAddress,
5471
5472 // Use PC-relative boot image String* address that will be known at link time.
5473 // Used for boot image strings referenced by boot image code in PIC mode.
5474 kBootImageLinkTimePcRelative,
5475
5476 // Use a known boot image String* address, embedded in the code by the codegen.
5477 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5478 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5479 // GetIncludePatchInformation().
5480 kBootImageAddress,
5481
5482 // Load from the resolved strings array at an absolute address.
5483 // Used for strings outside the boot image referenced by JIT-compiled code.
5484 kDexCacheAddress,
5485
5486 // Load from resolved strings array in the dex cache using a PC-relative load.
5487 // Used for strings outside boot image when we know that we can access
5488 // the dex cache arrays using a PC-relative load.
5489 kDexCachePcRelative,
5490
5491 // Load from resolved strings array accessed through the class loaded from
5492 // the compiled method's own ArtMethod*. This is the default access type when
5493 // all other types are unavailable.
5494 kDexCacheViaMethod,
5495
5496 kLast = kDexCacheViaMethod
5497 };
5498
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005499 HLoadString(HCurrentMethod* current_method,
5500 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005501 const DexFile& dex_file,
5502 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005503 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005504 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005505 SetPackedFlag<kFlagIsInDexCache>(false);
5506 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5507 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005508 SetRawInputAt(0, current_method);
5509 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005510
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005511 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5512 DCHECK(HasAddress(load_kind));
5513 load_data_.address = address;
5514 SetLoadKindInternal(load_kind);
5515 }
5516
5517 void SetLoadKindWithStringReference(LoadKind load_kind,
5518 const DexFile& dex_file,
5519 uint32_t string_index) {
5520 DCHECK(HasStringReference(load_kind));
5521 load_data_.ref.dex_file = &dex_file;
5522 string_index_ = string_index;
5523 SetLoadKindInternal(load_kind);
5524 }
5525
5526 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5527 const DexFile& dex_file,
5528 uint32_t element_index) {
5529 DCHECK(HasDexCacheReference(load_kind));
5530 load_data_.ref.dex_file = &dex_file;
5531 load_data_.ref.dex_cache_element_index = element_index;
5532 SetLoadKindInternal(load_kind);
5533 }
5534
5535 LoadKind GetLoadKind() const {
5536 return GetPackedField<LoadKindField>();
5537 }
5538
5539 const DexFile& GetDexFile() const;
5540
5541 uint32_t GetStringIndex() const {
5542 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5543 return string_index_;
5544 }
5545
5546 uint32_t GetDexCacheElementOffset() const;
5547
5548 uint64_t GetAddress() const {
5549 DCHECK(HasAddress(GetLoadKind()));
5550 return load_data_.address;
5551 }
5552
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005553 bool CanBeMoved() const OVERRIDE { return true; }
5554
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005555 bool InstructionDataEquals(HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005556
5557 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5558
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005559 // Will call the runtime if we need to load the string through
5560 // the dex cache and the string is not guaranteed to be there yet.
5561 bool NeedsEnvironment() const OVERRIDE {
5562 LoadKind load_kind = GetLoadKind();
5563 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5564 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5565 load_kind == LoadKind::kBootImageAddress) {
5566 return false;
5567 }
5568 return !IsInDexCache();
5569 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005570
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005571 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5572 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5573 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005574
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005575 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005576 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005577
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005578 static SideEffects SideEffectsForArchRuntimeCalls() {
5579 return SideEffects::CanTriggerGC();
5580 }
5581
Vladimir Markoa1de9182016-02-25 11:37:38 +00005582 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5583
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005584 void MarkInDexCache() {
5585 SetPackedFlag<kFlagIsInDexCache>(true);
5586 DCHECK(!NeedsEnvironment());
5587 RemoveEnvironment();
5588 }
5589
5590 size_t InputCount() const OVERRIDE {
5591 return (InputAt(0) != nullptr) ? 1u : 0u;
5592 }
5593
5594 void AddSpecialInput(HInstruction* special_input);
5595
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005596 DECLARE_INSTRUCTION(LoadString);
5597
5598 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005599 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005600 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5601 static constexpr size_t kFieldLoadKindSize =
5602 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5603 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005604 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005605 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005606
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005607 static bool HasStringReference(LoadKind load_kind) {
5608 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5609 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5610 load_kind == LoadKind::kDexCacheViaMethod;
5611 }
5612
5613 static bool HasAddress(LoadKind load_kind) {
5614 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5615 }
5616
5617 static bool HasDexCacheReference(LoadKind load_kind) {
5618 return load_kind == LoadKind::kDexCachePcRelative;
5619 }
5620
5621 void SetLoadKindInternal(LoadKind load_kind);
5622
5623 // String index serves also as the hash code and it's also needed for slow-paths,
5624 // so it must not be overwritten with other load data.
5625 uint32_t string_index_;
5626
5627 union {
5628 struct {
5629 const DexFile* dex_file; // For string reference and dex cache reference.
5630 uint32_t dex_cache_element_index; // Only for dex cache reference.
5631 } ref;
5632 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5633 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005634
5635 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5636};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005637std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5638
5639// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5640inline const DexFile& HLoadString::GetDexFile() const {
5641 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5642 << GetLoadKind();
5643 return *load_data_.ref.dex_file;
5644}
5645
5646// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5647inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5648 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5649 return load_data_.ref.dex_cache_element_index;
5650}
5651
5652// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5653inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5654 // The special input is used for PC-relative loads on some architectures.
5655 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5656 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
5657 DCHECK(InputAt(0) == nullptr);
5658 SetRawInputAt(0u, special_input);
5659 special_input->AddUseAt(this, 0);
5660}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005661
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005662/**
5663 * Performs an initialization check on its Class object input.
5664 */
5665class HClinitCheck : public HExpression<1> {
5666 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005667 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005668 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005669 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005670 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5671 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005672 SetRawInputAt(0, constant);
5673 }
5674
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005675 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005676 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005677 return true;
5678 }
5679
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005680 bool NeedsEnvironment() const OVERRIDE {
5681 // May call runtime to initialize the class.
5682 return true;
5683 }
5684
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005685 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005686
5687 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5688
5689 DECLARE_INSTRUCTION(ClinitCheck);
5690
5691 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005692 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5693};
5694
5695class HStaticFieldGet : public HExpression<1> {
5696 public:
5697 HStaticFieldGet(HInstruction* cls,
5698 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005699 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005700 bool is_volatile,
5701 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005702 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005703 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005704 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005705 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005706 : HExpression(field_type,
5707 SideEffects::FieldReadOfType(field_type, is_volatile),
5708 dex_pc),
5709 field_info_(field_offset,
5710 field_type,
5711 is_volatile,
5712 field_idx,
5713 declaring_class_def_index,
5714 dex_file,
5715 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005716 SetRawInputAt(0, cls);
5717 }
5718
Calin Juravle52c48962014-12-16 17:02:57 +00005719
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005720 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005721
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005722 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005723 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5724 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005725 }
5726
5727 size_t ComputeHashCode() const OVERRIDE {
5728 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5729 }
5730
Calin Juravle52c48962014-12-16 17:02:57 +00005731 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005732 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5733 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005734 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005735
5736 DECLARE_INSTRUCTION(StaticFieldGet);
5737
5738 private:
5739 const FieldInfo field_info_;
5740
5741 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5742};
5743
5744class HStaticFieldSet : public HTemplateInstruction<2> {
5745 public:
5746 HStaticFieldSet(HInstruction* cls,
5747 HInstruction* value,
5748 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005749 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005750 bool is_volatile,
5751 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005752 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005753 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005754 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005755 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005756 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5757 dex_pc),
5758 field_info_(field_offset,
5759 field_type,
5760 is_volatile,
5761 field_idx,
5762 declaring_class_def_index,
5763 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005764 dex_cache) {
5765 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005766 SetRawInputAt(0, cls);
5767 SetRawInputAt(1, value);
5768 }
5769
Calin Juravle52c48962014-12-16 17:02:57 +00005770 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005771 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5772 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005773 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005774
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005775 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005776 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5777 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005778
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005779 DECLARE_INSTRUCTION(StaticFieldSet);
5780
5781 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005782 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5783 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5784 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5785 "Too many packed fields.");
5786
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005787 const FieldInfo field_info_;
5788
5789 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5790};
5791
Calin Juravlee460d1d2015-09-29 04:52:17 +01005792class HUnresolvedInstanceFieldGet : public HExpression<1> {
5793 public:
5794 HUnresolvedInstanceFieldGet(HInstruction* obj,
5795 Primitive::Type field_type,
5796 uint32_t field_index,
5797 uint32_t dex_pc)
5798 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5799 field_index_(field_index) {
5800 SetRawInputAt(0, obj);
5801 }
5802
5803 bool NeedsEnvironment() const OVERRIDE { return true; }
5804 bool CanThrow() const OVERRIDE { return true; }
5805
5806 Primitive::Type GetFieldType() const { return GetType(); }
5807 uint32_t GetFieldIndex() const { return field_index_; }
5808
5809 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5810
5811 private:
5812 const uint32_t field_index_;
5813
5814 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5815};
5816
5817class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5818 public:
5819 HUnresolvedInstanceFieldSet(HInstruction* obj,
5820 HInstruction* value,
5821 Primitive::Type field_type,
5822 uint32_t field_index,
5823 uint32_t dex_pc)
5824 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005825 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005826 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005827 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005828 SetRawInputAt(0, obj);
5829 SetRawInputAt(1, value);
5830 }
5831
5832 bool NeedsEnvironment() const OVERRIDE { return true; }
5833 bool CanThrow() const OVERRIDE { return true; }
5834
Vladimir Markoa1de9182016-02-25 11:37:38 +00005835 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005836 uint32_t GetFieldIndex() const { return field_index_; }
5837
5838 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5839
5840 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005841 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5842 static constexpr size_t kFieldFieldTypeSize =
5843 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5844 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5845 kFieldFieldType + kFieldFieldTypeSize;
5846 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5847 "Too many packed fields.");
5848 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5849
Calin Juravlee460d1d2015-09-29 04:52:17 +01005850 const uint32_t field_index_;
5851
5852 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5853};
5854
5855class HUnresolvedStaticFieldGet : public HExpression<0> {
5856 public:
5857 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5858 uint32_t field_index,
5859 uint32_t dex_pc)
5860 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5861 field_index_(field_index) {
5862 }
5863
5864 bool NeedsEnvironment() const OVERRIDE { return true; }
5865 bool CanThrow() const OVERRIDE { return true; }
5866
5867 Primitive::Type GetFieldType() const { return GetType(); }
5868 uint32_t GetFieldIndex() const { return field_index_; }
5869
5870 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5871
5872 private:
5873 const uint32_t field_index_;
5874
5875 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5876};
5877
5878class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5879 public:
5880 HUnresolvedStaticFieldSet(HInstruction* value,
5881 Primitive::Type field_type,
5882 uint32_t field_index,
5883 uint32_t dex_pc)
5884 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005885 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005886 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005887 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005888 SetRawInputAt(0, value);
5889 }
5890
5891 bool NeedsEnvironment() const OVERRIDE { return true; }
5892 bool CanThrow() const OVERRIDE { return true; }
5893
Vladimir Markoa1de9182016-02-25 11:37:38 +00005894 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005895 uint32_t GetFieldIndex() const { return field_index_; }
5896
5897 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5898
5899 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005900 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5901 static constexpr size_t kFieldFieldTypeSize =
5902 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5903 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5904 kFieldFieldType + kFieldFieldTypeSize;
5905 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5906 "Too many packed fields.");
5907 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5908
Calin Juravlee460d1d2015-09-29 04:52:17 +01005909 const uint32_t field_index_;
5910
5911 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5912};
5913
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005914// Implement the move-exception DEX instruction.
5915class HLoadException : public HExpression<0> {
5916 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005917 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5918 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005919
David Brazdilbbd733e2015-08-18 17:48:17 +01005920 bool CanBeNull() const OVERRIDE { return false; }
5921
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005922 DECLARE_INSTRUCTION(LoadException);
5923
5924 private:
5925 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5926};
5927
David Brazdilcb1c0552015-08-04 16:22:25 +01005928// Implicit part of move-exception which clears thread-local exception storage.
5929// Must not be removed because the runtime expects the TLS to get cleared.
5930class HClearException : public HTemplateInstruction<0> {
5931 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005932 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5933 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005934
5935 DECLARE_INSTRUCTION(ClearException);
5936
5937 private:
5938 DISALLOW_COPY_AND_ASSIGN(HClearException);
5939};
5940
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005941class HThrow : public HTemplateInstruction<1> {
5942 public:
5943 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005944 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005945 SetRawInputAt(0, exception);
5946 }
5947
5948 bool IsControlFlow() const OVERRIDE { return true; }
5949
5950 bool NeedsEnvironment() const OVERRIDE { return true; }
5951
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005952 bool CanThrow() const OVERRIDE { return true; }
5953
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005954
5955 DECLARE_INSTRUCTION(Throw);
5956
5957 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005958 DISALLOW_COPY_AND_ASSIGN(HThrow);
5959};
5960
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005961/**
5962 * Implementation strategies for the code generator of a HInstanceOf
5963 * or `HCheckCast`.
5964 */
5965enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005966 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005967 kExactCheck, // Can do a single class compare.
5968 kClassHierarchyCheck, // Can just walk the super class chain.
5969 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5970 kInterfaceCheck, // No optimization yet when checking against an interface.
5971 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005972 kArrayCheck, // No optimization yet when checking against a generic array.
5973 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005974};
5975
Roland Levillain86503782016-02-11 19:07:30 +00005976std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5977
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005978class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005979 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005980 HInstanceOf(HInstruction* object,
5981 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005982 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005983 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005984 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005985 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005986 dex_pc) {
5987 SetPackedField<TypeCheckKindField>(check_kind);
5988 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005989 SetRawInputAt(0, object);
5990 SetRawInputAt(1, constant);
5991 }
5992
5993 bool CanBeMoved() const OVERRIDE { return true; }
5994
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005995 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005996 return true;
5997 }
5998
5999 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006000 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006001 }
6002
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006003 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006004 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6005 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6006 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6007 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006008
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006009 static bool CanCallRuntime(TypeCheckKind check_kind) {
6010 // Mips currently does runtime calls for any other checks.
6011 return check_kind != TypeCheckKind::kExactCheck;
6012 }
6013
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006014 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006015 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006016 }
6017
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006018 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006019
6020 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006021 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6022 static constexpr size_t kFieldTypeCheckKindSize =
6023 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6024 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6025 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6026 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6027 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006028
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006029 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6030};
6031
Calin Juravleb1498f62015-02-16 13:13:29 +00006032class HBoundType : public HExpression<1> {
6033 public:
David Brazdilf5552582015-12-27 13:36:12 +00006034 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006035 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006036 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6037 SetPackedFlag<kFlagUpperCanBeNull>(true);
6038 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006039 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006040 SetRawInputAt(0, input);
6041 }
6042
David Brazdilf5552582015-12-27 13:36:12 +00006043 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006044 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006045 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006046 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006047
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006048 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006049 DCHECK(GetUpperCanBeNull() || !can_be_null);
6050 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006051 }
6052
Vladimir Markoa1de9182016-02-25 11:37:38 +00006053 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006054
Calin Juravleb1498f62015-02-16 13:13:29 +00006055 DECLARE_INSTRUCTION(BoundType);
6056
6057 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006058 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6059 // is false then CanBeNull() cannot be true).
6060 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6061 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6062 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6063 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6064
Calin Juravleb1498f62015-02-16 13:13:29 +00006065 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006066 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6067 // It is used to bound the type in cases like:
6068 // if (x instanceof ClassX) {
6069 // // uper_bound_ will be ClassX
6070 // }
David Brazdilf5552582015-12-27 13:36:12 +00006071 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006072
6073 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6074};
6075
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006076class HCheckCast : public HTemplateInstruction<2> {
6077 public:
6078 HCheckCast(HInstruction* object,
6079 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006080 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006081 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006082 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6083 SetPackedField<TypeCheckKindField>(check_kind);
6084 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006085 SetRawInputAt(0, object);
6086 SetRawInputAt(1, constant);
6087 }
6088
6089 bool CanBeMoved() const OVERRIDE { return true; }
6090
6091 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6092 return true;
6093 }
6094
6095 bool NeedsEnvironment() const OVERRIDE {
6096 // Instruction may throw a CheckCastError.
6097 return true;
6098 }
6099
6100 bool CanThrow() const OVERRIDE { return true; }
6101
Vladimir Markoa1de9182016-02-25 11:37:38 +00006102 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6103 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6104 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6105 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006106
6107 DECLARE_INSTRUCTION(CheckCast);
6108
6109 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006110 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6111 static constexpr size_t kFieldTypeCheckKindSize =
6112 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6113 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6114 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6115 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6116 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006117
6118 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006119};
6120
Calin Juravle27df7582015-04-17 19:12:31 +01006121class HMemoryBarrier : public HTemplateInstruction<0> {
6122 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006123 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006124 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006125 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6126 SetPackedField<BarrierKindField>(barrier_kind);
6127 }
Calin Juravle27df7582015-04-17 19:12:31 +01006128
Vladimir Markoa1de9182016-02-25 11:37:38 +00006129 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006130
6131 DECLARE_INSTRUCTION(MemoryBarrier);
6132
6133 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006134 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6135 static constexpr size_t kFieldBarrierKindSize =
6136 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6137 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6138 kFieldBarrierKind + kFieldBarrierKindSize;
6139 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6140 "Too many packed fields.");
6141 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006142
6143 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6144};
6145
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006146class HMonitorOperation : public HTemplateInstruction<1> {
6147 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006148 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006149 kEnter,
6150 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006151 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006152 };
6153
6154 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006155 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006156 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6157 dex_pc) {
6158 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006159 SetRawInputAt(0, object);
6160 }
6161
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006162 // Instruction may go into runtime, so we need an environment.
6163 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006164
6165 bool CanThrow() const OVERRIDE {
6166 // Verifier guarantees that monitor-exit cannot throw.
6167 // This is important because it allows the HGraphBuilder to remove
6168 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6169 return IsEnter();
6170 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006171
Vladimir Markoa1de9182016-02-25 11:37:38 +00006172 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6173 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006174
6175 DECLARE_INSTRUCTION(MonitorOperation);
6176
Calin Juravle52c48962014-12-16 17:02:57 +00006177 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006178 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6179 static constexpr size_t kFieldOperationKindSize =
6180 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6181 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6182 kFieldOperationKind + kFieldOperationKindSize;
6183 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6184 "Too many packed fields.");
6185 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006186
6187 private:
6188 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6189};
6190
David Brazdil74eb1b22015-12-14 11:44:01 +00006191class HSelect : public HExpression<3> {
6192 public:
6193 HSelect(HInstruction* condition,
6194 HInstruction* true_value,
6195 HInstruction* false_value,
6196 uint32_t dex_pc)
6197 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6198 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6199
6200 // First input must be `true_value` or `false_value` to allow codegens to
6201 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6202 // that architectures which implement HSelect as a conditional move also
6203 // will not need to invert the condition.
6204 SetRawInputAt(0, false_value);
6205 SetRawInputAt(1, true_value);
6206 SetRawInputAt(2, condition);
6207 }
6208
6209 HInstruction* GetFalseValue() const { return InputAt(0); }
6210 HInstruction* GetTrueValue() const { return InputAt(1); }
6211 HInstruction* GetCondition() const { return InputAt(2); }
6212
6213 bool CanBeMoved() const OVERRIDE { return true; }
6214 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6215
6216 bool CanBeNull() const OVERRIDE {
6217 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6218 }
6219
6220 DECLARE_INSTRUCTION(Select);
6221
6222 private:
6223 DISALLOW_COPY_AND_ASSIGN(HSelect);
6224};
6225
Vladimir Markof9f64412015-09-02 14:05:49 +01006226class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006227 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006228 MoveOperands(Location source,
6229 Location destination,
6230 Primitive::Type type,
6231 HInstruction* instruction)
6232 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006233
6234 Location GetSource() const { return source_; }
6235 Location GetDestination() const { return destination_; }
6236
6237 void SetSource(Location value) { source_ = value; }
6238 void SetDestination(Location value) { destination_ = value; }
6239
6240 // The parallel move resolver marks moves as "in-progress" by clearing the
6241 // destination (but not the source).
6242 Location MarkPending() {
6243 DCHECK(!IsPending());
6244 Location dest = destination_;
6245 destination_ = Location::NoLocation();
6246 return dest;
6247 }
6248
6249 void ClearPending(Location dest) {
6250 DCHECK(IsPending());
6251 destination_ = dest;
6252 }
6253
6254 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006255 DCHECK(source_.IsValid() || destination_.IsInvalid());
6256 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006257 }
6258
6259 // True if this blocks a move from the given location.
6260 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006261 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006262 }
6263
6264 // A move is redundant if it's been eliminated, if its source and
6265 // destination are the same, or if its destination is unneeded.
6266 bool IsRedundant() const {
6267 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6268 }
6269
6270 // We clear both operands to indicate move that's been eliminated.
6271 void Eliminate() {
6272 source_ = destination_ = Location::NoLocation();
6273 }
6274
6275 bool IsEliminated() const {
6276 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6277 return source_.IsInvalid();
6278 }
6279
Alexey Frunze4dda3372015-06-01 18:31:49 -07006280 Primitive::Type GetType() const { return type_; }
6281
Nicolas Geoffray90218252015-04-15 11:56:51 +01006282 bool Is64BitMove() const {
6283 return Primitive::Is64BitType(type_);
6284 }
6285
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006286 HInstruction* GetInstruction() const { return instruction_; }
6287
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006288 private:
6289 Location source_;
6290 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006291 // The type this move is for.
6292 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006293 // The instruction this move is assocatied with. Null when this move is
6294 // for moving an input in the expected locations of user (including a phi user).
6295 // This is only used in debug mode, to ensure we do not connect interval siblings
6296 // in the same parallel move.
6297 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006298};
6299
Roland Levillainc9285912015-12-18 10:38:42 +00006300std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6301
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006302static constexpr size_t kDefaultNumberOfMoves = 4;
6303
6304class HParallelMove : public HTemplateInstruction<0> {
6305 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006306 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006307 : HTemplateInstruction(SideEffects::None(), dex_pc),
6308 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6309 moves_.reserve(kDefaultNumberOfMoves);
6310 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006311
Nicolas Geoffray90218252015-04-15 11:56:51 +01006312 void AddMove(Location source,
6313 Location destination,
6314 Primitive::Type type,
6315 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006316 DCHECK(source.IsValid());
6317 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006318 if (kIsDebugBuild) {
6319 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006320 for (const MoveOperands& move : moves_) {
6321 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006322 // Special case the situation where the move is for the spill slot
6323 // of the instruction.
6324 if ((GetPrevious() == instruction)
6325 || ((GetPrevious() == nullptr)
6326 && instruction->IsPhi()
6327 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006328 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006329 << "Doing parallel moves for the same instruction.";
6330 } else {
6331 DCHECK(false) << "Doing parallel moves for the same instruction.";
6332 }
6333 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006334 }
6335 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006336 for (const MoveOperands& move : moves_) {
6337 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006338 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006339 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006340 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006341 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006342 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006343 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006344 }
6345
Vladimir Marko225b6462015-09-28 12:17:40 +01006346 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006347 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006348 }
6349
Vladimir Marko225b6462015-09-28 12:17:40 +01006350 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006351
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006352 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006353
6354 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006355 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006356
6357 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6358};
6359
Mark Mendell0616ae02015-04-17 12:49:27 -04006360} // namespace art
6361
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006362#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6363#include "nodes_shared.h"
6364#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006365#ifdef ART_ENABLE_CODEGEN_arm
6366#include "nodes_arm.h"
6367#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006368#ifdef ART_ENABLE_CODEGEN_arm64
6369#include "nodes_arm64.h"
6370#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006371#ifdef ART_ENABLE_CODEGEN_x86
6372#include "nodes_x86.h"
6373#endif
6374
6375namespace art {
6376
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006377class HGraphVisitor : public ValueObject {
6378 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006379 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6380 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006381
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006382 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006383 virtual void VisitBasicBlock(HBasicBlock* block);
6384
Roland Levillain633021e2014-10-01 14:12:25 +01006385 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006386 void VisitInsertionOrder();
6387
Roland Levillain633021e2014-10-01 14:12:25 +01006388 // Visit the graph following dominator tree reverse post-order.
6389 void VisitReversePostOrder();
6390
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006391 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006392
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006393 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006394#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006395 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6396
6397 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6398
6399#undef DECLARE_VISIT_INSTRUCTION
6400
6401 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006402 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006403
6404 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6405};
6406
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006407class HGraphDelegateVisitor : public HGraphVisitor {
6408 public:
6409 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6410 virtual ~HGraphDelegateVisitor() {}
6411
6412 // Visit functions that delegate to to super class.
6413#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006414 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006415
6416 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6417
6418#undef DECLARE_VISIT_INSTRUCTION
6419
6420 private:
6421 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6422};
6423
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006424class HInsertionOrderIterator : public ValueObject {
6425 public:
6426 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6427
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006428 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006429 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006430 void Advance() { ++index_; }
6431
6432 private:
6433 const HGraph& graph_;
6434 size_t index_;
6435
6436 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6437};
6438
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006439class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006440 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006441 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6442 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006443 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006444 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006445
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006446 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6447 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006448 void Advance() { ++index_; }
6449
6450 private:
6451 const HGraph& graph_;
6452 size_t index_;
6453
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006454 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006455};
6456
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006457class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006458 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006459 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006460 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006461 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006462 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006463 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006464
6465 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006466 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006467 void Advance() { --index_; }
6468
6469 private:
6470 const HGraph& graph_;
6471 size_t index_;
6472
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006473 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006474};
6475
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006476class HLinearPostOrderIterator : public ValueObject {
6477 public:
6478 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006479 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006480
6481 bool Done() const { return index_ == 0; }
6482
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006483 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006484
6485 void Advance() {
6486 --index_;
6487 DCHECK_GE(index_, 0U);
6488 }
6489
6490 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006491 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006492 size_t index_;
6493
6494 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6495};
6496
6497class HLinearOrderIterator : public ValueObject {
6498 public:
6499 explicit HLinearOrderIterator(const HGraph& graph)
6500 : order_(graph.GetLinearOrder()), index_(0) {}
6501
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006502 bool Done() const { return index_ == order_.size(); }
6503 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006504 void Advance() { ++index_; }
6505
6506 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006507 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006508 size_t index_;
6509
6510 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6511};
6512
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006513// Iterator over the blocks that art part of the loop. Includes blocks part
6514// of an inner loop. The order in which the blocks are iterated is on their
6515// block id.
6516class HBlocksInLoopIterator : public ValueObject {
6517 public:
6518 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6519 : blocks_in_loop_(info.GetBlocks()),
6520 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6521 index_(0) {
6522 if (!blocks_in_loop_.IsBitSet(index_)) {
6523 Advance();
6524 }
6525 }
6526
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006527 bool Done() const { return index_ == blocks_.size(); }
6528 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006529 void Advance() {
6530 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006531 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006532 if (blocks_in_loop_.IsBitSet(index_)) {
6533 break;
6534 }
6535 }
6536 }
6537
6538 private:
6539 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006540 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006541 size_t index_;
6542
6543 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6544};
6545
Mingyao Yang3584bce2015-05-19 16:01:59 -07006546// Iterator over the blocks that art part of the loop. Includes blocks part
6547// of an inner loop. The order in which the blocks are iterated is reverse
6548// post order.
6549class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6550 public:
6551 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6552 : blocks_in_loop_(info.GetBlocks()),
6553 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6554 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006555 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006556 Advance();
6557 }
6558 }
6559
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006560 bool Done() const { return index_ == blocks_.size(); }
6561 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006562 void Advance() {
6563 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006564 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6565 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006566 break;
6567 }
6568 }
6569 }
6570
6571 private:
6572 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006573 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006574 size_t index_;
6575
6576 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6577};
6578
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006579inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006580 if (constant->IsIntConstant()) {
6581 return constant->AsIntConstant()->GetValue();
6582 } else if (constant->IsLongConstant()) {
6583 return constant->AsLongConstant()->GetValue();
6584 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006585 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006586 return 0;
6587 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006588}
6589
Vladimir Marko58155012015-08-19 12:49:41 +00006590inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6591 // For the purposes of the compiler, the dex files must actually be the same object
6592 // if we want to safely treat them as the same. This is especially important for JIT
6593 // as custom class loaders can open the same underlying file (or memory) multiple
6594 // times and provide different class resolution but no two class loaders should ever
6595 // use the same DexFile object - doing so is an unsupported hack that can lead to
6596 // all sorts of weird failures.
6597 return &lhs == &rhs;
6598}
6599
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006600#define INSTRUCTION_TYPE_CHECK(type, super) \
6601 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6602 inline const H##type* HInstruction::As##type() const { \
6603 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6604 } \
6605 inline H##type* HInstruction::As##type() { \
6606 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6607 }
6608
6609 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6610#undef INSTRUCTION_TYPE_CHECK
6611
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006612// Create space in `blocks` for adding `number_of_new_blocks` entries
6613// starting at location `at`. Blocks after `at` are moved accordingly.
6614inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6615 size_t number_of_new_blocks,
6616 size_t after) {
6617 DCHECK_LT(after, blocks->size());
6618 size_t old_size = blocks->size();
6619 size_t new_size = old_size + number_of_new_blocks;
6620 blocks->resize(new_size);
6621 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6622}
6623
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006624} // namespace art
6625
6626#endif // ART_COMPILER_OPTIMIZING_NODES_H_