blob: 7fc39cbadde832bb2074f54cbb8a9e6bf398384e [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
Vladimir Marko27bb86e2016-04-14 18:07:55 +0100172 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000173 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 Bik1f7624c2016-04-13 21:17:17 +00001554 : SideEffects(TypeFlagWithAlias(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 Bik1f7624c2016-04-13 21:17:17 +00001558 return SideEffects(TypeFlagWithAlias(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 Bik1f7624c2016-04-13 21:17:17 +00001564 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001565 }
1566
1567 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik1f7624c2016-04-13 21:17:17 +00001568 return SideEffects(TypeFlagWithAlias(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 Bik1f7624c2016-04-13 21:17:17 +00001695 // Work around the fact that HIR aliases I/F and J/D.
1696 // TODO: remove this interceptor once HIR types are clean
1697 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1698 switch (type) {
1699 case Primitive::kPrimInt:
1700 case Primitive::kPrimFloat:
1701 return TypeFlag(Primitive::kPrimInt, offset) |
1702 TypeFlag(Primitive::kPrimFloat, offset);
1703 case Primitive::kPrimLong:
1704 case Primitive::kPrimDouble:
1705 return TypeFlag(Primitive::kPrimLong, offset) |
1706 TypeFlag(Primitive::kPrimDouble, offset);
1707 default:
1708 return TypeFlag(type, offset);
1709 }
1710 }
1711
Aart Bik854a02b2015-07-14 16:07:00 -07001712 // Translates type to bit flag.
1713 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1714 CHECK_NE(type, Primitive::kPrimVoid);
1715 const uint64_t one = 1;
1716 const int shift = type; // 0-based consecutive enum
1717 DCHECK_LE(kFieldWriteOffset, shift);
1718 DCHECK_LT(shift, kArrayWriteOffset);
1719 return one << (type + offset);
1720 }
1721
1722 // Private constructor on direct flags value.
1723 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1724
1725 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001726};
1727
David Brazdiled596192015-01-23 10:39:45 +00001728// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001729class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001730 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001731 HEnvironment(ArenaAllocator* arena,
1732 size_t number_of_vregs,
1733 const DexFile& dex_file,
1734 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001735 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001736 InvokeType invoke_type,
1737 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001738 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1739 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001740 parent_(nullptr),
1741 dex_file_(dex_file),
1742 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001743 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001744 invoke_type_(invoke_type),
1745 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001746 }
1747
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001748 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001749 : HEnvironment(arena,
1750 to_copy.Size(),
1751 to_copy.GetDexFile(),
1752 to_copy.GetMethodIdx(),
1753 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001754 to_copy.GetInvokeType(),
1755 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001756
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001757 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001758 if (parent_ != nullptr) {
1759 parent_->SetAndCopyParentChain(allocator, parent);
1760 } else {
1761 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1762 parent_->CopyFrom(parent);
1763 if (parent->GetParent() != nullptr) {
1764 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1765 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001766 }
David Brazdiled596192015-01-23 10:39:45 +00001767 }
1768
Vladimir Marko71bf8092015-09-15 15:33:14 +01001769 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001770 void CopyFrom(HEnvironment* environment);
1771
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001772 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1773 // input to the loop phi instead. This is for inserting instructions that
1774 // require an environment (like HDeoptimization) in the loop pre-header.
1775 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001776
1777 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001778 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001779 }
1780
1781 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001782 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001783 }
1784
David Brazdil1abb4192015-02-17 18:33:36 +00001785 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001786
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001787 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001788
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001789 HEnvironment* GetParent() const { return parent_; }
1790
1791 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001792 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001793 }
1794
1795 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001796 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001797 }
1798
1799 uint32_t GetDexPc() const {
1800 return dex_pc_;
1801 }
1802
1803 uint32_t GetMethodIdx() const {
1804 return method_idx_;
1805 }
1806
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001807 InvokeType GetInvokeType() const {
1808 return invoke_type_;
1809 }
1810
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001811 const DexFile& GetDexFile() const {
1812 return dex_file_;
1813 }
1814
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001815 HInstruction* GetHolder() const {
1816 return holder_;
1817 }
1818
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001819
1820 bool IsFromInlinedInvoke() const {
1821 return GetParent() != nullptr;
1822 }
1823
David Brazdiled596192015-01-23 10:39:45 +00001824 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001825 // Record instructions' use entries of this environment for constant-time removal.
1826 // It should only be called by HInstruction when a new environment use is added.
1827 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1828 DCHECK(env_use->GetUser() == this);
1829 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001830 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001831 }
David Brazdiled596192015-01-23 10:39:45 +00001832
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001833 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1834 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001835 HEnvironment* parent_;
1836 const DexFile& dex_file_;
1837 const uint32_t method_idx_;
1838 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001839 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001840
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001841 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001842 HInstruction* const holder_;
1843
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001844 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001845
1846 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1847};
1848
Vladimir Markof9f64412015-09-02 14:05:49 +01001849class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001850 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001851 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001852 : previous_(nullptr),
1853 next_(nullptr),
1854 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001855 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001856 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001857 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001858 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001859 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001860 locations_(nullptr),
1861 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001862 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001863 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001864 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1865 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1866 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001867
Dave Allison20dfc792014-06-16 20:44:29 -07001868 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001869
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001870#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001871 enum InstructionKind {
1872 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1873 };
1874#undef DECLARE_KIND
1875
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001876 HInstruction* GetNext() const { return next_; }
1877 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001878
Calin Juravle77520bc2015-01-12 18:45:46 +00001879 HInstruction* GetNextDisregardingMoves() const;
1880 HInstruction* GetPreviousDisregardingMoves() const;
1881
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001882 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001883 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001884 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001885 bool IsInBlock() const { return block_ != nullptr; }
1886 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001887 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1888 bool IsIrreducibleLoopHeaderPhi() const {
1889 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1890 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001891
Roland Levillain6b879dd2014-09-22 17:13:44 +01001892 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001893 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001894
1895 virtual void Accept(HGraphVisitor* visitor) = 0;
1896 virtual const char* DebugName() const = 0;
1897
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001898 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001899 void SetRawInputAt(size_t index, HInstruction* input) {
1900 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1901 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001902
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001903 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001904
1905 uint32_t GetDexPc() const { return dex_pc_; }
1906
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001907 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001908
Roland Levillaine161a2a2014-10-03 12:45:18 +01001909 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001910 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001911
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001912 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001913 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001914
Calin Juravle10e244f2015-01-26 18:54:32 +00001915 // Does not apply for all instructions, but having this at top level greatly
1916 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001917 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001918 virtual bool CanBeNull() const {
1919 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1920 return true;
1921 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001922
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001923 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001924 return false;
1925 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001926
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001927 virtual bool IsActualObject() const {
1928 return GetType() == Primitive::kPrimNot;
1929 }
1930
Calin Juravle2e768302015-07-28 14:41:11 +00001931 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001932
Calin Juravle61d544b2015-02-23 16:46:57 +00001933 ReferenceTypeInfo GetReferenceTypeInfo() const {
1934 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001935 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko27bb86e2016-04-14 18:07:55 +01001936 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001937 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001938
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001939 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001940 DCHECK(user != nullptr);
1941 HUseListNode<HInstruction*>* use =
1942 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1943 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001944 }
1945
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001946 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001947 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001948 HUseListNode<HEnvironment*>* env_use =
1949 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1950 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001951 }
1952
David Brazdil1abb4192015-02-17 18:33:36 +00001953 void RemoveAsUserOfInput(size_t input) {
1954 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1955 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1956 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001957
David Brazdil1abb4192015-02-17 18:33:36 +00001958 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1959 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001960
David Brazdiled596192015-01-23 10:39:45 +00001961 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1962 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001963 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001964 bool HasOnlyOneNonEnvironmentUse() const {
1965 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1966 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001967
Roland Levillain6c82d402014-10-13 16:10:27 +01001968 // Does this instruction strictly dominate `other_instruction`?
1969 // Returns false if this instruction and `other_instruction` are the same.
1970 // Aborts if this instruction and `other_instruction` are both phis.
1971 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001972
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001973 int GetId() const { return id_; }
1974 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001975
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001976 int GetSsaIndex() const { return ssa_index_; }
1977 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1978 bool HasSsaIndex() const { return ssa_index_ != -1; }
1979
1980 bool HasEnvironment() const { return environment_ != nullptr; }
1981 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001982 // Set the `environment_` field. Raw because this method does not
1983 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001984 void SetRawEnvironment(HEnvironment* environment) {
1985 DCHECK(environment_ == nullptr);
1986 DCHECK_EQ(environment->GetHolder(), this);
1987 environment_ = environment;
1988 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001989
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001990 void RemoveEnvironment();
1991
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001992 // Set the environment of this instruction, copying it from `environment`. While
1993 // copying, the uses lists are being updated.
1994 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001995 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001996 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001997 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001998 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001999 if (environment->GetParent() != nullptr) {
2000 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2001 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002002 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002003
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002004 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2005 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002006 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002007 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002008 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002009 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002010 if (environment->GetParent() != nullptr) {
2011 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2012 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002013 }
2014
Nicolas Geoffray39468442014-09-02 15:17:15 +01002015 // Returns the number of entries in the environment. Typically, that is the
2016 // number of dex registers in a method. It could be more in case of inlining.
2017 size_t EnvironmentSize() const;
2018
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002019 LocationSummary* GetLocations() const { return locations_; }
2020 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002021
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002022 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002023 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002024
Alexandre Rames188d4312015-04-09 18:30:21 +01002025 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2026 // uses of this instruction by `other` are *not* updated.
2027 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2028 ReplaceWith(other);
2029 other->ReplaceInput(this, use_index);
2030 }
2031
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002032 // Move `this` instruction before `cursor`.
2033 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002034
Vladimir Markofb337ea2015-11-25 15:25:10 +00002035 // Move `this` before its first user and out of any loops. If there is no
2036 // out-of-loop user that dominates all other users, move the instruction
2037 // to the end of the out-of-loop common dominator of the user's blocks.
2038 //
2039 // This can be used only on non-throwing instructions with no side effects that
2040 // have at least one use but no environment uses.
2041 void MoveBeforeFirstUserAndOutOfLoops();
2042
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002043#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002044 bool Is##type() const; \
2045 const H##type* As##type() const; \
2046 H##type* As##type();
2047
2048 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2049#undef INSTRUCTION_TYPE_CHECK
2050
2051#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002052 bool Is##type() const { return (As##type() != nullptr); } \
2053 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002054 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002055 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002056#undef INSTRUCTION_TYPE_CHECK
2057
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002058 // Returns whether the instruction can be moved within the graph.
2059 virtual bool CanBeMoved() const { return false; }
2060
2061 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002062 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002063 return false;
2064 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002065
2066 // Returns whether any data encoded in the two instructions is equal.
2067 // This method does not look at the inputs. Both instructions must be
2068 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002069 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002070 return false;
2071 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002072
2073 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002074 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002075 // 2) Their inputs are identical.
2076 bool Equals(HInstruction* other) const;
2077
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002078 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2079 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2080 // the virtual function because the __attribute__((__pure__)) doesn't really
2081 // apply the strong requirement for virtual functions, preventing optimizations.
2082 InstructionKind GetKind() const PURE;
2083 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002084
2085 virtual size_t ComputeHashCode() const {
2086 size_t result = GetKind();
2087 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2088 result = (result * 31) + InputAt(i)->GetId();
2089 }
2090 return result;
2091 }
2092
2093 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002094 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002095 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002096
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002097 size_t GetLifetimePosition() const { return lifetime_position_; }
2098 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2099 LiveInterval* GetLiveInterval() const { return live_interval_; }
2100 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2101 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2102
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002103 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2104
2105 // Returns whether the code generation of the instruction will require to have access
2106 // to the current method. Such instructions are:
2107 // (1): Instructions that require an environment, as calling the runtime requires
2108 // to walk the stack and have the current method stored at a specific stack address.
2109 // (2): Object literals like classes and strings, that are loaded from the dex cache
2110 // fields of the current method.
2111 bool NeedsCurrentMethod() const {
2112 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2113 }
2114
Vladimir Markodc151b22015-10-15 18:02:30 +01002115 // Returns whether the code generation of the instruction will require to have access
2116 // to the dex cache of the current method's declaring class via the current method.
2117 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002118
Mark Mendellc4701932015-04-10 13:18:51 -04002119 // Does this instruction have any use in an environment before
2120 // control flow hits 'other'?
2121 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2122
2123 // Remove all references to environment uses of this instruction.
2124 // The caller must ensure that this is safe to do.
2125 void RemoveEnvironmentUsers();
2126
Vladimir Markoa1de9182016-02-25 11:37:38 +00002127 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2128 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002129
David Brazdil1abb4192015-02-17 18:33:36 +00002130 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002131 // If set, the machine code for this instruction is assumed to be generated by
2132 // its users. Used by liveness analysis to compute use positions accordingly.
2133 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2134 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2135 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2136 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2137
David Brazdil1abb4192015-02-17 18:33:36 +00002138 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2139 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2140
Vladimir Markoa1de9182016-02-25 11:37:38 +00002141 uint32_t GetPackedFields() const {
2142 return packed_fields_;
2143 }
2144
2145 template <size_t flag>
2146 bool GetPackedFlag() const {
2147 return (packed_fields_ & (1u << flag)) != 0u;
2148 }
2149
2150 template <size_t flag>
2151 void SetPackedFlag(bool value = true) {
2152 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2153 }
2154
2155 template <typename BitFieldType>
2156 typename BitFieldType::value_type GetPackedField() const {
2157 return BitFieldType::Decode(packed_fields_);
2158 }
2159
2160 template <typename BitFieldType>
2161 void SetPackedField(typename BitFieldType::value_type value) {
2162 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2163 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2164 }
2165
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002166 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002167 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2168
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002169 HInstruction* previous_;
2170 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002171 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002172 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002173
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002174 // An instruction gets an id when it is added to the graph.
2175 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002176 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002177 int id_;
2178
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002179 // When doing liveness analysis, instructions that have uses get an SSA index.
2180 int ssa_index_;
2181
Vladimir Markoa1de9182016-02-25 11:37:38 +00002182 // Packed fields.
2183 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002184
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002185 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002186 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002187
2188 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002189 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002190
Nicolas Geoffray39468442014-09-02 15:17:15 +01002191 // The environment associated with this instruction. Not null if the instruction
2192 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002193 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002194
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002195 // Set by the code generator.
2196 LocationSummary* locations_;
2197
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002198 // Set by the liveness analysis.
2199 LiveInterval* live_interval_;
2200
2201 // Set by the liveness analysis, this is the position in a linear
2202 // order of blocks where this instruction's live interval start.
2203 size_t lifetime_position_;
2204
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002205 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002206
Vladimir Markoa1de9182016-02-25 11:37:38 +00002207 // The reference handle part of the reference type info.
2208 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002209 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002210 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002211
David Brazdil1abb4192015-02-17 18:33:36 +00002212 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002213 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002214 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002215 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002216 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002217
2218 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2219};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002220std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002221
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002222class HInputIterator : public ValueObject {
2223 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002224 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002225
2226 bool Done() const { return index_ == instruction_->InputCount(); }
2227 HInstruction* Current() const { return instruction_->InputAt(index_); }
2228 void Advance() { index_++; }
2229
2230 private:
2231 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002232 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002233
2234 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2235};
2236
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002237class HInstructionIterator : public ValueObject {
2238 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002239 explicit HInstructionIterator(const HInstructionList& instructions)
2240 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002241 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002242 }
2243
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002244 bool Done() const { return instruction_ == nullptr; }
2245 HInstruction* Current() const { return instruction_; }
2246 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002247 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002248 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002249 }
2250
2251 private:
2252 HInstruction* instruction_;
2253 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002254
2255 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002256};
2257
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002258class HBackwardInstructionIterator : public ValueObject {
2259 public:
2260 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2261 : instruction_(instructions.last_instruction_) {
2262 next_ = Done() ? nullptr : instruction_->GetPrevious();
2263 }
2264
2265 bool Done() const { return instruction_ == nullptr; }
2266 HInstruction* Current() const { return instruction_; }
2267 void Advance() {
2268 instruction_ = next_;
2269 next_ = Done() ? nullptr : instruction_->GetPrevious();
2270 }
2271
2272 private:
2273 HInstruction* instruction_;
2274 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002275
2276 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002277};
2278
Vladimir Markof9f64412015-09-02 14:05:49 +01002279template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002280class HTemplateInstruction: public HInstruction {
2281 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002282 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002283 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002284 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002285
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002286 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002287
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002288 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002289 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2290 DCHECK_LT(i, N);
2291 return inputs_[i];
2292 }
David Brazdil1abb4192015-02-17 18:33:36 +00002293
2294 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002295 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002296 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002297 }
2298
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002299 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002300 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002301
2302 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002303};
2304
Vladimir Markof9f64412015-09-02 14:05:49 +01002305// HTemplateInstruction specialization for N=0.
2306template<>
2307class HTemplateInstruction<0>: public HInstruction {
2308 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002309 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002310 : HInstruction(side_effects, dex_pc) {}
2311
Vladimir Markof9f64412015-09-02 14:05:49 +01002312 virtual ~HTemplateInstruction() {}
2313
2314 size_t InputCount() const OVERRIDE { return 0; }
2315
2316 protected:
2317 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2318 LOG(FATAL) << "Unreachable";
2319 UNREACHABLE();
2320 }
2321
2322 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2323 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2324 LOG(FATAL) << "Unreachable";
2325 UNREACHABLE();
2326 }
2327
2328 private:
2329 friend class SsaBuilder;
2330};
2331
Dave Allison20dfc792014-06-16 20:44:29 -07002332template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002333class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002334 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002335 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002336 : HTemplateInstruction<N>(side_effects, dex_pc) {
2337 this->template SetPackedField<TypeField>(type);
2338 }
Dave Allison20dfc792014-06-16 20:44:29 -07002339 virtual ~HExpression() {}
2340
Vladimir Markoa1de9182016-02-25 11:37:38 +00002341 Primitive::Type GetType() const OVERRIDE {
2342 return TypeField::Decode(this->GetPackedFields());
2343 }
Dave Allison20dfc792014-06-16 20:44:29 -07002344
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002345 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002346 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2347 static constexpr size_t kFieldTypeSize =
2348 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2349 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2350 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2351 "Too many packed fields.");
2352 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002353};
2354
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002355// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2356// instruction that branches to the exit block.
2357class HReturnVoid : public HTemplateInstruction<0> {
2358 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002359 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2360 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002361
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002362 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002363
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002364 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002365
2366 private:
2367 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2368};
2369
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002370// Represents dex's RETURN opcodes. A HReturn is a control flow
2371// instruction that branches to the exit block.
2372class HReturn : public HTemplateInstruction<1> {
2373 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002374 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2375 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002376 SetRawInputAt(0, value);
2377 }
2378
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002379 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002380
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002381 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002382
2383 private:
2384 DISALLOW_COPY_AND_ASSIGN(HReturn);
2385};
2386
David Brazdildee58d62016-04-07 09:54:26 +00002387class HPhi : public HInstruction {
2388 public:
2389 HPhi(ArenaAllocator* arena,
2390 uint32_t reg_number,
2391 size_t number_of_inputs,
2392 Primitive::Type type,
2393 uint32_t dex_pc = kNoDexPc)
2394 : HInstruction(SideEffects::None(), dex_pc),
2395 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2396 reg_number_(reg_number) {
2397 SetPackedField<TypeField>(ToPhiType(type));
2398 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2399 // Phis are constructed live and marked dead if conflicting or unused.
2400 // Individual steps of SsaBuilder should assume that if a phi has been
2401 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2402 SetPackedFlag<kFlagIsLive>(true);
2403 SetPackedFlag<kFlagCanBeNull>(true);
2404 }
2405
2406 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2407 static Primitive::Type ToPhiType(Primitive::Type type) {
2408 return Primitive::PrimitiveKind(type);
2409 }
2410
2411 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2412
2413 size_t InputCount() const OVERRIDE { return inputs_.size(); }
2414
2415 void AddInput(HInstruction* input);
2416 void RemoveInputAt(size_t index);
2417
2418 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2419 void SetType(Primitive::Type new_type) {
2420 // Make sure that only valid type changes occur. The following are allowed:
2421 // (1) int -> float/ref (primitive type propagation),
2422 // (2) long -> double (primitive type propagation).
2423 DCHECK(GetType() == new_type ||
2424 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2425 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2426 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2427 SetPackedField<TypeField>(new_type);
2428 }
2429
2430 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2431 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2432
2433 uint32_t GetRegNumber() const { return reg_number_; }
2434
2435 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2436 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2437 bool IsDead() const { return !IsLive(); }
2438 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2439
2440 bool IsVRegEquivalentOf(HInstruction* other) const {
2441 return other != nullptr
2442 && other->IsPhi()
2443 && other->AsPhi()->GetBlock() == GetBlock()
2444 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2445 }
2446
2447 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2448 // An equivalent phi is a phi having the same dex register and type.
2449 // It assumes that phis with the same dex register are adjacent.
2450 HPhi* GetNextEquivalentPhiWithSameType() {
2451 HInstruction* next = GetNext();
2452 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2453 if (next->GetType() == GetType()) {
2454 return next->AsPhi();
2455 }
2456 next = next->GetNext();
2457 }
2458 return nullptr;
2459 }
2460
2461 DECLARE_INSTRUCTION(Phi);
2462
2463 protected:
2464 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
2465 return inputs_[index];
2466 }
2467
2468 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2469 inputs_[index] = input;
2470 }
2471
2472 private:
2473 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2474 static constexpr size_t kFieldTypeSize =
2475 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2476 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2477 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2478 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2479 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2480 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2481
2482 ArenaVector<HUserRecord<HInstruction*> > inputs_;
2483 const uint32_t reg_number_;
2484
2485 DISALLOW_COPY_AND_ASSIGN(HPhi);
2486};
2487
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002488// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002489// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002490// exit block.
2491class HExit : public HTemplateInstruction<0> {
2492 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002493 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002494
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002495 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002496
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002497 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002498
2499 private:
2500 DISALLOW_COPY_AND_ASSIGN(HExit);
2501};
2502
2503// Jumps from one block to another.
2504class HGoto : public HTemplateInstruction<0> {
2505 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002506 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002507
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002508 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002509
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002510 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002511 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002512 }
2513
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002514 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002515
2516 private:
2517 DISALLOW_COPY_AND_ASSIGN(HGoto);
2518};
2519
Roland Levillain9867bc72015-08-05 10:21:34 +01002520class HConstant : public HExpression<0> {
2521 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002522 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2523 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002524
2525 bool CanBeMoved() const OVERRIDE { return true; }
2526
Roland Levillain1a653882016-03-18 18:05:57 +00002527 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002528 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002529 // Is this constant 0 in the arithmetic sense?
2530 virtual bool IsArithmeticZero() const { return false; }
2531 // Is this constant a 0-bit pattern?
2532 virtual bool IsZeroBitPattern() const { return false; }
2533 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002534 virtual bool IsOne() const { return false; }
2535
David Brazdil77a48ae2015-09-15 12:34:04 +00002536 virtual uint64_t GetValueAsUint64() const = 0;
2537
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002538 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002539
2540 private:
2541 DISALLOW_COPY_AND_ASSIGN(HConstant);
2542};
2543
2544class HNullConstant : public HConstant {
2545 public:
2546 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2547 return true;
2548 }
2549
David Brazdil77a48ae2015-09-15 12:34:04 +00002550 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2551
Roland Levillain9867bc72015-08-05 10:21:34 +01002552 size_t ComputeHashCode() const OVERRIDE { return 0; }
2553
Roland Levillain1a653882016-03-18 18:05:57 +00002554 // The null constant representation is a 0-bit pattern.
2555 virtual bool IsZeroBitPattern() const { return true; }
2556
Roland Levillain9867bc72015-08-05 10:21:34 +01002557 DECLARE_INSTRUCTION(NullConstant);
2558
2559 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002560 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002561
2562 friend class HGraph;
2563 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2564};
2565
2566// Constants of the type int. Those can be from Dex instructions, or
2567// synthesized (for example with the if-eqz instruction).
2568class HIntConstant : public HConstant {
2569 public:
2570 int32_t GetValue() const { return value_; }
2571
David Brazdil9f389d42015-10-01 14:32:56 +01002572 uint64_t GetValueAsUint64() const OVERRIDE {
2573 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2574 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002575
Roland Levillain9867bc72015-08-05 10:21:34 +01002576 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002577 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002578 return other->AsIntConstant()->value_ == value_;
2579 }
2580
2581 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2582
2583 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002584 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2585 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002586 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2587
Roland Levillain1a653882016-03-18 18:05:57 +00002588 // Integer constants are used to encode Boolean values as well,
2589 // where 1 means true and 0 means false.
2590 bool IsTrue() const { return GetValue() == 1; }
2591 bool IsFalse() const { return GetValue() == 0; }
2592
Roland Levillain9867bc72015-08-05 10:21:34 +01002593 DECLARE_INSTRUCTION(IntConstant);
2594
2595 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002596 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2597 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2598 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2599 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002600
2601 const int32_t value_;
2602
2603 friend class HGraph;
2604 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2605 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2606 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2607};
2608
2609class HLongConstant : public HConstant {
2610 public:
2611 int64_t GetValue() const { return value_; }
2612
David Brazdil77a48ae2015-09-15 12:34:04 +00002613 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2614
Roland Levillain9867bc72015-08-05 10:21:34 +01002615 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002616 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002617 return other->AsLongConstant()->value_ == value_;
2618 }
2619
2620 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2621
2622 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002623 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2624 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002625 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2626
2627 DECLARE_INSTRUCTION(LongConstant);
2628
2629 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002630 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2631 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002632
2633 const int64_t value_;
2634
2635 friend class HGraph;
2636 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2637};
Dave Allison20dfc792014-06-16 20:44:29 -07002638
Roland Levillain31dd3d62016-02-16 12:21:02 +00002639class HFloatConstant : public HConstant {
2640 public:
2641 float GetValue() const { return value_; }
2642
2643 uint64_t GetValueAsUint64() const OVERRIDE {
2644 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2645 }
2646
2647 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2648 DCHECK(other->IsFloatConstant()) << other->DebugName();
2649 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2650 }
2651
2652 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2653
2654 bool IsMinusOne() const OVERRIDE {
2655 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2656 }
Roland Levillain1a653882016-03-18 18:05:57 +00002657 bool IsArithmeticZero() const OVERRIDE {
2658 return std::fpclassify(value_) == FP_ZERO;
2659 }
2660 bool IsArithmeticPositiveZero() const {
2661 return IsArithmeticZero() && !std::signbit(value_);
2662 }
2663 bool IsArithmeticNegativeZero() const {
2664 return IsArithmeticZero() && std::signbit(value_);
2665 }
2666 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002667 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002668 }
2669 bool IsOne() const OVERRIDE {
2670 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2671 }
2672 bool IsNaN() const {
2673 return std::isnan(value_);
2674 }
2675
2676 DECLARE_INSTRUCTION(FloatConstant);
2677
2678 private:
2679 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2680 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2681 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2682 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2683
2684 const float value_;
2685
2686 // Only the SsaBuilder and HGraph can create floating-point constants.
2687 friend class SsaBuilder;
2688 friend class HGraph;
2689 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2690};
2691
2692class HDoubleConstant : public HConstant {
2693 public:
2694 double GetValue() const { return value_; }
2695
2696 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2697
2698 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2699 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2700 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2701 }
2702
2703 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2704
2705 bool IsMinusOne() const OVERRIDE {
2706 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2707 }
Roland Levillain1a653882016-03-18 18:05:57 +00002708 bool IsArithmeticZero() const OVERRIDE {
2709 return std::fpclassify(value_) == FP_ZERO;
2710 }
2711 bool IsArithmeticPositiveZero() const {
2712 return IsArithmeticZero() && !std::signbit(value_);
2713 }
2714 bool IsArithmeticNegativeZero() const {
2715 return IsArithmeticZero() && std::signbit(value_);
2716 }
2717 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002718 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002719 }
2720 bool IsOne() const OVERRIDE {
2721 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2722 }
2723 bool IsNaN() const {
2724 return std::isnan(value_);
2725 }
2726
2727 DECLARE_INSTRUCTION(DoubleConstant);
2728
2729 private:
2730 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2731 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2732 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2733 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2734
2735 const double value_;
2736
2737 // Only the SsaBuilder and HGraph can create floating-point constants.
2738 friend class SsaBuilder;
2739 friend class HGraph;
2740 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2741};
2742
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002743// Conditional branch. A block ending with an HIf instruction must have
2744// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002745class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002746 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002747 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2748 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002749 SetRawInputAt(0, input);
2750 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002751
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002752 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002753
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002754 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002755 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002756 }
2757
2758 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002759 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002760 }
2761
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002762 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002763
2764 private:
2765 DISALLOW_COPY_AND_ASSIGN(HIf);
2766};
2767
David Brazdilfc6a86a2015-06-26 10:33:45 +00002768
2769// Abstract instruction which marks the beginning and/or end of a try block and
2770// links it to the respective exception handlers. Behaves the same as a Goto in
2771// non-exceptional control flow.
2772// Normal-flow successor is stored at index zero, exception handlers under
2773// higher indices in no particular order.
2774class HTryBoundary : public HTemplateInstruction<0> {
2775 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002776 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002777 kEntry,
2778 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002779 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002780 };
2781
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002782 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002783 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2784 SetPackedField<BoundaryKindField>(kind);
2785 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002786
2787 bool IsControlFlow() const OVERRIDE { return true; }
2788
2789 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002790 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002791
David Brazdild26a4112015-11-10 11:07:31 +00002792 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2793 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2794 }
2795
David Brazdilfc6a86a2015-06-26 10:33:45 +00002796 // Returns whether `handler` is among its exception handlers (non-zero index
2797 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002798 bool HasExceptionHandler(const HBasicBlock& handler) const {
2799 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002800 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002801 }
2802
2803 // If not present already, adds `handler` to its block's list of exception
2804 // handlers.
2805 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002806 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002807 GetBlock()->AddSuccessor(handler);
2808 }
2809 }
2810
Vladimir Markoa1de9182016-02-25 11:37:38 +00002811 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2812 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002813
David Brazdilffee3d32015-07-06 11:48:53 +01002814 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2815
David Brazdilfc6a86a2015-06-26 10:33:45 +00002816 DECLARE_INSTRUCTION(TryBoundary);
2817
2818 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002819 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2820 static constexpr size_t kFieldBoundaryKindSize =
2821 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2822 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2823 kFieldBoundaryKind + kFieldBoundaryKindSize;
2824 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2825 "Too many packed fields.");
2826 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002827
2828 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2829};
2830
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002831// Deoptimize to interpreter, upon checking a condition.
2832class HDeoptimize : public HTemplateInstruction<1> {
2833 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002834 // We set CanTriggerGC to prevent any intermediate address to be live
2835 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002836 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002837 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002838 SetRawInputAt(0, cond);
2839 }
2840
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002841 bool CanBeMoved() const OVERRIDE { return true; }
2842 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2843 return true;
2844 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002845 bool NeedsEnvironment() const OVERRIDE { return true; }
2846 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002847
2848 DECLARE_INSTRUCTION(Deoptimize);
2849
2850 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002851 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2852};
2853
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002854// Represents the ArtMethod that was passed as a first argument to
2855// the method. It is used by instructions that depend on it, like
2856// instructions that work with the dex cache.
2857class HCurrentMethod : public HExpression<0> {
2858 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002859 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2860 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002861
2862 DECLARE_INSTRUCTION(CurrentMethod);
2863
2864 private:
2865 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2866};
2867
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002868// Fetches an ArtMethod from the virtual table or the interface method table
2869// of a class.
2870class HClassTableGet : public HExpression<1> {
2871 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002872 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002873 kVTable,
2874 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002875 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002876 };
2877 HClassTableGet(HInstruction* cls,
2878 Primitive::Type type,
2879 TableKind kind,
2880 size_t index,
2881 uint32_t dex_pc)
2882 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002883 index_(index) {
2884 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002885 SetRawInputAt(0, cls);
2886 }
2887
2888 bool CanBeMoved() const OVERRIDE { return true; }
2889 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2890 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002891 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002892 }
2893
Vladimir Markoa1de9182016-02-25 11:37:38 +00002894 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002895 size_t GetIndex() const { return index_; }
2896
2897 DECLARE_INSTRUCTION(ClassTableGet);
2898
2899 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002900 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2901 static constexpr size_t kFieldTableKindSize =
2902 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2903 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2904 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2905 "Too many packed fields.");
2906 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2907
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002908 // The index of the ArtMethod in the table.
2909 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002910
2911 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2912};
2913
Mark Mendellfe57faa2015-09-18 09:26:15 -04002914// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2915// have one successor for each entry in the switch table, and the final successor
2916// will be the block containing the next Dex opcode.
2917class HPackedSwitch : public HTemplateInstruction<1> {
2918 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002919 HPackedSwitch(int32_t start_value,
2920 uint32_t num_entries,
2921 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002922 uint32_t dex_pc = kNoDexPc)
2923 : HTemplateInstruction(SideEffects::None(), dex_pc),
2924 start_value_(start_value),
2925 num_entries_(num_entries) {
2926 SetRawInputAt(0, input);
2927 }
2928
2929 bool IsControlFlow() const OVERRIDE { return true; }
2930
2931 int32_t GetStartValue() const { return start_value_; }
2932
Vladimir Marko211c2112015-09-24 16:52:33 +01002933 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002934
2935 HBasicBlock* GetDefaultBlock() const {
2936 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002937 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002938 }
2939 DECLARE_INSTRUCTION(PackedSwitch);
2940
2941 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002942 const int32_t start_value_;
2943 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002944
2945 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2946};
2947
Roland Levillain88cb1752014-10-20 16:36:47 +01002948class HUnaryOperation : public HExpression<1> {
2949 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002950 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2951 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002952 SetRawInputAt(0, input);
2953 }
2954
2955 HInstruction* GetInput() const { return InputAt(0); }
2956 Primitive::Type GetResultType() const { return GetType(); }
2957
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002958 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002959 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002960 return true;
2961 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002962
Roland Levillain31dd3d62016-02-16 12:21:02 +00002963 // Try to statically evaluate `this` and return a HConstant
2964 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002965 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002966 HConstant* TryStaticEvaluation() const;
2967
2968 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002969 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2970 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002971 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2972 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002973
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002974 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002975
2976 private:
2977 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2978};
2979
Dave Allison20dfc792014-06-16 20:44:29 -07002980class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002981 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002982 HBinaryOperation(Primitive::Type result_type,
2983 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002984 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002985 SideEffects side_effects = SideEffects::None(),
2986 uint32_t dex_pc = kNoDexPc)
2987 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002988 SetRawInputAt(0, left);
2989 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002990 }
2991
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002992 HInstruction* GetLeft() const { return InputAt(0); }
2993 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002994 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002995
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002996 virtual bool IsCommutative() const { return false; }
2997
2998 // Put constant on the right.
2999 // Returns whether order is changed.
3000 bool OrderInputsWithConstantOnTheRight() {
3001 HInstruction* left = InputAt(0);
3002 HInstruction* right = InputAt(1);
3003 if (left->IsConstant() && !right->IsConstant()) {
3004 ReplaceInput(right, 0);
3005 ReplaceInput(left, 1);
3006 return true;
3007 }
3008 return false;
3009 }
3010
3011 // Order inputs by instruction id, but favor constant on the right side.
3012 // This helps GVN for commutative ops.
3013 void OrderInputs() {
3014 DCHECK(IsCommutative());
3015 HInstruction* left = InputAt(0);
3016 HInstruction* right = InputAt(1);
3017 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3018 return;
3019 }
3020 if (OrderInputsWithConstantOnTheRight()) {
3021 return;
3022 }
3023 // Order according to instruction id.
3024 if (left->GetId() > right->GetId()) {
3025 ReplaceInput(right, 0);
3026 ReplaceInput(left, 1);
3027 }
3028 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003029
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003030 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003031 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003032 return true;
3033 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003034
Roland Levillain31dd3d62016-02-16 12:21:02 +00003035 // Try to statically evaluate `this` and return a HConstant
3036 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003037 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003038 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003039
3040 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003041 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3042 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003043 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3044 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003045 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003046 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3047 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003048 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3049 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003050 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3051 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003052 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003053 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3054 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003055
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003056 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003057 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003058 HConstant* GetConstantRight() const;
3059
3060 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003061 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003062 HInstruction* GetLeastConstantLeft() const;
3063
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003064 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003065
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003066 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003067 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3068};
3069
Mark Mendellc4701932015-04-10 13:18:51 -04003070// The comparison bias applies for floating point operations and indicates how NaN
3071// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003072enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003073 kNoBias, // bias is not applicable (i.e. for long operation)
3074 kGtBias, // return 1 for NaN comparisons
3075 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003076 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003077};
3078
Roland Levillain31dd3d62016-02-16 12:21:02 +00003079std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3080
Dave Allison20dfc792014-06-16 20:44:29 -07003081class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003082 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003083 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003084 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3085 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3086 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003087
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003088 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003089 // `instruction`, and disregard moves in between.
3090 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003091
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003092 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003093
3094 virtual IfCondition GetCondition() const = 0;
3095
Mark Mendellc4701932015-04-10 13:18:51 -04003096 virtual IfCondition GetOppositeCondition() const = 0;
3097
Vladimir Markoa1de9182016-02-25 11:37:38 +00003098 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003099 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3100
Vladimir Markoa1de9182016-02-25 11:37:38 +00003101 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3102 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003103
3104 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003105 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003106 }
3107
Roland Levillain4fa13f62015-07-06 18:11:54 +01003108 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003109 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003110 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003111 if (if_cond == kCondNE) {
3112 return true;
3113 } else if (if_cond == kCondEQ) {
3114 return false;
3115 }
3116 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003117 }
3118
3119 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003120 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003121 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003122 if (if_cond == kCondEQ) {
3123 return true;
3124 } else if (if_cond == kCondNE) {
3125 return false;
3126 }
3127 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003128 }
3129
Roland Levillain31dd3d62016-02-16 12:21:02 +00003130 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003131 // Needed if we merge a HCompare into a HCondition.
3132 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3133 static constexpr size_t kFieldComparisonBiasSize =
3134 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3135 static constexpr size_t kNumberOfConditionPackedBits =
3136 kFieldComparisonBias + kFieldComparisonBiasSize;
3137 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3138 using ComparisonBiasField =
3139 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3140
Roland Levillain31dd3d62016-02-16 12:21:02 +00003141 template <typename T>
3142 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3143
3144 template <typename T>
3145 int32_t CompareFP(T x, T y) const {
3146 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3147 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3148 // Handle the bias.
3149 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3150 }
3151
3152 // Return an integer constant containing the result of a condition evaluated at compile time.
3153 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3154 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3155 }
3156
Dave Allison20dfc792014-06-16 20:44:29 -07003157 private:
3158 DISALLOW_COPY_AND_ASSIGN(HCondition);
3159};
3160
3161// Instruction to check if two inputs are equal to each other.
3162class HEqual : public HCondition {
3163 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003164 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3165 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003166
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003167 bool IsCommutative() const OVERRIDE { return true; }
3168
Vladimir Marko9e23df52015-11-10 17:14:35 +00003169 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3170 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003171 return MakeConstantCondition(true, GetDexPc());
3172 }
3173 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3174 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3175 }
3176 // In the following Evaluate methods, a HCompare instruction has
3177 // been merged into this HEqual instruction; evaluate it as
3178 // `Compare(x, y) == 0`.
3179 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3180 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3181 GetDexPc());
3182 }
3183 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3184 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3185 }
3186 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3187 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003188 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003189
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003190 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003191
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003192 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003193 return kCondEQ;
3194 }
3195
Mark Mendellc4701932015-04-10 13:18:51 -04003196 IfCondition GetOppositeCondition() const OVERRIDE {
3197 return kCondNE;
3198 }
3199
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003200 private:
Aart Bike9f37602015-10-09 11:15:55 -07003201 template <typename T> bool Compute(T x, T y) const { return x == y; }
3202
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003203 DISALLOW_COPY_AND_ASSIGN(HEqual);
3204};
3205
Dave Allison20dfc792014-06-16 20:44:29 -07003206class HNotEqual : public HCondition {
3207 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003208 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3209 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003210
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003211 bool IsCommutative() const OVERRIDE { return true; }
3212
Vladimir Marko9e23df52015-11-10 17:14:35 +00003213 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3214 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003215 return MakeConstantCondition(false, GetDexPc());
3216 }
3217 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3218 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3219 }
3220 // In the following Evaluate methods, a HCompare instruction has
3221 // been merged into this HNotEqual instruction; evaluate it as
3222 // `Compare(x, y) != 0`.
3223 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3224 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3225 }
3226 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3227 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3228 }
3229 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3230 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003231 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003232
Dave Allison20dfc792014-06-16 20:44:29 -07003233 DECLARE_INSTRUCTION(NotEqual);
3234
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003235 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003236 return kCondNE;
3237 }
3238
Mark Mendellc4701932015-04-10 13:18:51 -04003239 IfCondition GetOppositeCondition() const OVERRIDE {
3240 return kCondEQ;
3241 }
3242
Dave Allison20dfc792014-06-16 20:44:29 -07003243 private:
Aart Bike9f37602015-10-09 11:15:55 -07003244 template <typename T> bool Compute(T x, T y) const { return x != y; }
3245
Dave Allison20dfc792014-06-16 20:44:29 -07003246 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3247};
3248
3249class HLessThan : public HCondition {
3250 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003251 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3252 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003253
Roland Levillain9867bc72015-08-05 10:21:34 +01003254 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003255 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003256 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003257 // In the following Evaluate methods, a HCompare instruction has
3258 // been merged into this HLessThan instruction; evaluate it as
3259 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003260 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003261 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3262 }
3263 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3264 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3265 }
3266 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3267 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003268 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003269
Dave Allison20dfc792014-06-16 20:44:29 -07003270 DECLARE_INSTRUCTION(LessThan);
3271
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003272 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003273 return kCondLT;
3274 }
3275
Mark Mendellc4701932015-04-10 13:18:51 -04003276 IfCondition GetOppositeCondition() const OVERRIDE {
3277 return kCondGE;
3278 }
3279
Dave Allison20dfc792014-06-16 20:44:29 -07003280 private:
Aart Bike9f37602015-10-09 11:15:55 -07003281 template <typename T> bool Compute(T x, T y) const { return x < y; }
3282
Dave Allison20dfc792014-06-16 20:44:29 -07003283 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3284};
3285
3286class HLessThanOrEqual : public HCondition {
3287 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003288 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3289 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003290
Roland Levillain9867bc72015-08-05 10:21:34 +01003291 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003292 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003293 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003294 // In the following Evaluate methods, a HCompare instruction has
3295 // been merged into this HLessThanOrEqual instruction; evaluate it as
3296 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003297 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003298 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3299 }
3300 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3301 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3302 }
3303 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3304 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003305 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003306
Dave Allison20dfc792014-06-16 20:44:29 -07003307 DECLARE_INSTRUCTION(LessThanOrEqual);
3308
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003309 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003310 return kCondLE;
3311 }
3312
Mark Mendellc4701932015-04-10 13:18:51 -04003313 IfCondition GetOppositeCondition() const OVERRIDE {
3314 return kCondGT;
3315 }
3316
Dave Allison20dfc792014-06-16 20:44:29 -07003317 private:
Aart Bike9f37602015-10-09 11:15:55 -07003318 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3319
Dave Allison20dfc792014-06-16 20:44:29 -07003320 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3321};
3322
3323class HGreaterThan : public HCondition {
3324 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003325 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3326 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003327
Roland Levillain9867bc72015-08-05 10:21:34 +01003328 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003329 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003330 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003331 // In the following Evaluate methods, a HCompare instruction has
3332 // been merged into this HGreaterThan instruction; evaluate it as
3333 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003334 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003335 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3336 }
3337 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3338 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3339 }
3340 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3341 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003342 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003343
Dave Allison20dfc792014-06-16 20:44:29 -07003344 DECLARE_INSTRUCTION(GreaterThan);
3345
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003346 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003347 return kCondGT;
3348 }
3349
Mark Mendellc4701932015-04-10 13:18:51 -04003350 IfCondition GetOppositeCondition() const OVERRIDE {
3351 return kCondLE;
3352 }
3353
Dave Allison20dfc792014-06-16 20:44:29 -07003354 private:
Aart Bike9f37602015-10-09 11:15:55 -07003355 template <typename T> bool Compute(T x, T y) const { return x > y; }
3356
Dave Allison20dfc792014-06-16 20:44:29 -07003357 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3358};
3359
3360class HGreaterThanOrEqual : public HCondition {
3361 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003362 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3363 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003364
Roland Levillain9867bc72015-08-05 10:21:34 +01003365 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003366 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003367 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003368 // In the following Evaluate methods, a HCompare instruction has
3369 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3370 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003371 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003372 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3373 }
3374 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3375 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3376 }
3377 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3378 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003379 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003380
Dave Allison20dfc792014-06-16 20:44:29 -07003381 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3382
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003383 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003384 return kCondGE;
3385 }
3386
Mark Mendellc4701932015-04-10 13:18:51 -04003387 IfCondition GetOppositeCondition() const OVERRIDE {
3388 return kCondLT;
3389 }
3390
Dave Allison20dfc792014-06-16 20:44:29 -07003391 private:
Aart Bike9f37602015-10-09 11:15:55 -07003392 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3393
Dave Allison20dfc792014-06-16 20:44:29 -07003394 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3395};
3396
Aart Bike9f37602015-10-09 11:15:55 -07003397class HBelow : public HCondition {
3398 public:
3399 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3400 : HCondition(first, second, dex_pc) {}
3401
3402 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003403 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003404 }
3405 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003406 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3407 }
3408 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3409 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3410 LOG(FATAL) << DebugName() << " is not defined for float values";
3411 UNREACHABLE();
3412 }
3413 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3414 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3415 LOG(FATAL) << DebugName() << " is not defined for double values";
3416 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003417 }
3418
3419 DECLARE_INSTRUCTION(Below);
3420
3421 IfCondition GetCondition() const OVERRIDE {
3422 return kCondB;
3423 }
3424
3425 IfCondition GetOppositeCondition() const OVERRIDE {
3426 return kCondAE;
3427 }
3428
3429 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003430 template <typename T> bool Compute(T x, T y) const {
3431 return MakeUnsigned(x) < MakeUnsigned(y);
3432 }
Aart Bike9f37602015-10-09 11:15:55 -07003433
3434 DISALLOW_COPY_AND_ASSIGN(HBelow);
3435};
3436
3437class HBelowOrEqual : public HCondition {
3438 public:
3439 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3440 : HCondition(first, second, dex_pc) {}
3441
3442 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003443 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003444 }
3445 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003446 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3447 }
3448 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3449 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3450 LOG(FATAL) << DebugName() << " is not defined for float values";
3451 UNREACHABLE();
3452 }
3453 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3454 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3455 LOG(FATAL) << DebugName() << " is not defined for double values";
3456 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003457 }
3458
3459 DECLARE_INSTRUCTION(BelowOrEqual);
3460
3461 IfCondition GetCondition() const OVERRIDE {
3462 return kCondBE;
3463 }
3464
3465 IfCondition GetOppositeCondition() const OVERRIDE {
3466 return kCondA;
3467 }
3468
3469 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003470 template <typename T> bool Compute(T x, T y) const {
3471 return MakeUnsigned(x) <= MakeUnsigned(y);
3472 }
Aart Bike9f37602015-10-09 11:15:55 -07003473
3474 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3475};
3476
3477class HAbove : public HCondition {
3478 public:
3479 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3480 : HCondition(first, second, dex_pc) {}
3481
3482 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003483 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003484 }
3485 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003486 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3487 }
3488 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3489 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3490 LOG(FATAL) << DebugName() << " is not defined for float values";
3491 UNREACHABLE();
3492 }
3493 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3494 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3495 LOG(FATAL) << DebugName() << " is not defined for double values";
3496 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003497 }
3498
3499 DECLARE_INSTRUCTION(Above);
3500
3501 IfCondition GetCondition() const OVERRIDE {
3502 return kCondA;
3503 }
3504
3505 IfCondition GetOppositeCondition() const OVERRIDE {
3506 return kCondBE;
3507 }
3508
3509 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003510 template <typename T> bool Compute(T x, T y) const {
3511 return MakeUnsigned(x) > MakeUnsigned(y);
3512 }
Aart Bike9f37602015-10-09 11:15:55 -07003513
3514 DISALLOW_COPY_AND_ASSIGN(HAbove);
3515};
3516
3517class HAboveOrEqual : public HCondition {
3518 public:
3519 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3520 : HCondition(first, second, dex_pc) {}
3521
3522 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003523 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003524 }
3525 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003526 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3527 }
3528 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3529 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3530 LOG(FATAL) << DebugName() << " is not defined for float values";
3531 UNREACHABLE();
3532 }
3533 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3534 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3535 LOG(FATAL) << DebugName() << " is not defined for double values";
3536 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003537 }
3538
3539 DECLARE_INSTRUCTION(AboveOrEqual);
3540
3541 IfCondition GetCondition() const OVERRIDE {
3542 return kCondAE;
3543 }
3544
3545 IfCondition GetOppositeCondition() const OVERRIDE {
3546 return kCondB;
3547 }
3548
3549 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003550 template <typename T> bool Compute(T x, T y) const {
3551 return MakeUnsigned(x) >= MakeUnsigned(y);
3552 }
Aart Bike9f37602015-10-09 11:15:55 -07003553
3554 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3555};
Dave Allison20dfc792014-06-16 20:44:29 -07003556
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003557// Instruction to check how two inputs compare to each other.
3558// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3559class HCompare : public HBinaryOperation {
3560 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003561 // Note that `comparison_type` is the type of comparison performed
3562 // between the comparison's inputs, not the type of the instantiated
3563 // HCompare instruction (which is always Primitive::kPrimInt).
3564 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003565 HInstruction* first,
3566 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003567 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003568 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003569 : HBinaryOperation(Primitive::kPrimInt,
3570 first,
3571 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003572 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003573 dex_pc) {
3574 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003575 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3576 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003577 }
3578
Roland Levillain9867bc72015-08-05 10:21:34 +01003579 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003580 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3581
3582 template <typename T>
3583 int32_t ComputeFP(T x, T y) const {
3584 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3585 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3586 // Handle the bias.
3587 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3588 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003589
Roland Levillain9867bc72015-08-05 10:21:34 +01003590 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003591 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3592 // reach this code path when processing a freshly built HIR
3593 // graph. However HCompare integer instructions can be synthesized
3594 // by the instruction simplifier to implement IntegerCompare and
3595 // IntegerSignum intrinsics, so we have to handle this case.
3596 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003597 }
3598 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003599 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3600 }
3601 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3602 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3603 }
3604 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3605 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003606 }
3607
Calin Juravleddb7df22014-11-25 20:56:51 +00003608 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003609 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003610 }
3611
Vladimir Markoa1de9182016-02-25 11:37:38 +00003612 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003613
Roland Levillain31dd3d62016-02-16 12:21:02 +00003614 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003615 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003616 bool IsGtBias() const {
3617 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003618 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003619 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003620
Roland Levillain1693a1f2016-03-15 14:57:31 +00003621 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3622 // Comparisons do not require a runtime call in any back end.
3623 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003624 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003625
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003626 DECLARE_INSTRUCTION(Compare);
3627
Roland Levillain31dd3d62016-02-16 12:21:02 +00003628 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003629 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3630 static constexpr size_t kFieldComparisonBiasSize =
3631 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3632 static constexpr size_t kNumberOfComparePackedBits =
3633 kFieldComparisonBias + kFieldComparisonBiasSize;
3634 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3635 using ComparisonBiasField =
3636 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3637
Roland Levillain31dd3d62016-02-16 12:21:02 +00003638 // Return an integer constant containing the result of a comparison evaluated at compile time.
3639 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3640 DCHECK(value == -1 || value == 0 || value == 1) << value;
3641 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3642 }
3643
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003644 private:
3645 DISALLOW_COPY_AND_ASSIGN(HCompare);
3646};
3647
David Brazdil6de19382016-01-08 17:37:10 +00003648class HNewInstance : public HExpression<2> {
3649 public:
3650 HNewInstance(HInstruction* cls,
3651 HCurrentMethod* current_method,
3652 uint32_t dex_pc,
3653 uint16_t type_index,
3654 const DexFile& dex_file,
3655 bool can_throw,
3656 bool finalizable,
3657 QuickEntrypointEnum entrypoint)
3658 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3659 type_index_(type_index),
3660 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003661 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003662 SetPackedFlag<kFlagCanThrow>(can_throw);
3663 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003664 SetRawInputAt(0, cls);
3665 SetRawInputAt(1, current_method);
3666 }
3667
3668 uint16_t GetTypeIndex() const { return type_index_; }
3669 const DexFile& GetDexFile() const { return dex_file_; }
3670
3671 // Calls runtime so needs an environment.
3672 bool NeedsEnvironment() const OVERRIDE { return true; }
3673
3674 // It may throw when called on type that's not instantiable/accessible.
3675 // It can throw OOME.
3676 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003677 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>() || true; }
David Brazdil6de19382016-01-08 17:37:10 +00003678
Vladimir Markoa1de9182016-02-25 11:37:38 +00003679 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003680
3681 bool CanBeNull() const OVERRIDE { return false; }
3682
3683 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3684
3685 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3686 entrypoint_ = entrypoint;
3687 }
3688
3689 bool IsStringAlloc() const;
3690
3691 DECLARE_INSTRUCTION(NewInstance);
3692
3693 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003694 static constexpr size_t kFlagCanThrow = kNumberOfExpressionPackedBits;
3695 static constexpr size_t kFlagFinalizable = kFlagCanThrow + 1;
3696 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3697 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3698 "Too many packed fields.");
3699
David Brazdil6de19382016-01-08 17:37:10 +00003700 const uint16_t type_index_;
3701 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003702 QuickEntrypointEnum entrypoint_;
3703
3704 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3705};
3706
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003707enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003708#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3709 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003710#include "intrinsics_list.h"
3711 kNone,
3712 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3713#undef INTRINSICS_LIST
3714#undef OPTIMIZING_INTRINSICS
3715};
3716std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3717
Agi Csaki05f20562015-08-19 14:58:14 -07003718enum IntrinsicNeedsEnvironmentOrCache {
3719 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3720 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003721};
3722
Aart Bik5d75afe2015-12-14 11:57:01 -08003723enum IntrinsicSideEffects {
3724 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3725 kReadSideEffects, // Intrinsic may read heap memory.
3726 kWriteSideEffects, // Intrinsic may write heap memory.
3727 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3728};
3729
3730enum IntrinsicExceptions {
3731 kNoThrow, // Intrinsic does not throw any exceptions.
3732 kCanThrow // Intrinsic may throw exceptions.
3733};
3734
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003735class HInvoke : public HInstruction {
3736 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003737 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003738
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003739 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003740
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003741 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003742 SetRawInputAt(index, argument);
3743 }
3744
Roland Levillain3e3d7332015-04-28 11:00:54 +01003745 // Return the number of arguments. This number can be lower than
3746 // the number of inputs returned by InputCount(), as some invoke
3747 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3748 // inputs at the end of their list of inputs.
3749 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3750
Vladimir Markoa1de9182016-02-25 11:37:38 +00003751 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003752
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003753 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003754 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003755
Vladimir Markoa1de9182016-02-25 11:37:38 +00003756 InvokeType GetOriginalInvokeType() const {
3757 return GetPackedField<OriginalInvokeTypeField>();
3758 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003759
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003760 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003761 return intrinsic_;
3762 }
3763
Aart Bik5d75afe2015-12-14 11:57:01 -08003764 void SetIntrinsic(Intrinsics intrinsic,
3765 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3766 IntrinsicSideEffects side_effects,
3767 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003768
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003769 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003770 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003771 }
3772
Vladimir Markoa1de9182016-02-25 11:37:38 +00003773 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003774
3775 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3776
3777 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3778 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3779 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003780
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003781 uint32_t* GetIntrinsicOptimizations() {
3782 return &intrinsic_optimizations_;
3783 }
3784
3785 const uint32_t* GetIntrinsicOptimizations() const {
3786 return &intrinsic_optimizations_;
3787 }
3788
3789 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3790
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003791 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003792
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003793 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003794 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3795 static constexpr size_t kFieldOriginalInvokeTypeSize =
3796 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3797 static constexpr size_t kFieldReturnType =
3798 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3799 static constexpr size_t kFieldReturnTypeSize =
3800 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3801 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3802 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3803 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3804 using OriginalInvokeTypeField =
3805 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3806 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3807
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003808 HInvoke(ArenaAllocator* arena,
3809 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003810 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003811 Primitive::Type return_type,
3812 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003813 uint32_t dex_method_index,
3814 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003815 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003816 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003817 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003818 inputs_(number_of_arguments + number_of_other_inputs,
3819 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003820 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003821 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003822 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003823 SetPackedField<ReturnTypeField>(return_type);
3824 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3825 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003826 }
3827
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003828 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003829 return inputs_[index];
3830 }
3831
David Brazdil1abb4192015-02-17 18:33:36 +00003832 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003833 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003834 }
3835
Vladimir Markoa1de9182016-02-25 11:37:38 +00003836 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003837
Roland Levillain3e3d7332015-04-28 11:00:54 +01003838 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003839 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003840 const uint32_t dex_method_index_;
3841 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003842
3843 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3844 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003845
3846 private:
3847 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3848};
3849
Calin Juravle175dc732015-08-25 15:42:32 +01003850class HInvokeUnresolved : public HInvoke {
3851 public:
3852 HInvokeUnresolved(ArenaAllocator* arena,
3853 uint32_t number_of_arguments,
3854 Primitive::Type return_type,
3855 uint32_t dex_pc,
3856 uint32_t dex_method_index,
3857 InvokeType invoke_type)
3858 : HInvoke(arena,
3859 number_of_arguments,
3860 0u /* number_of_other_inputs */,
3861 return_type,
3862 dex_pc,
3863 dex_method_index,
3864 invoke_type) {
3865 }
3866
3867 DECLARE_INSTRUCTION(InvokeUnresolved);
3868
3869 private:
3870 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3871};
3872
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003873class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003874 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003875 // Requirements of this method call regarding the class
3876 // initialization (clinit) check of its declaring class.
3877 enum class ClinitCheckRequirement {
3878 kNone, // Class already initialized.
3879 kExplicit, // Static call having explicit clinit check as last input.
3880 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003881 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003882 };
3883
Vladimir Marko58155012015-08-19 12:49:41 +00003884 // Determines how to load the target ArtMethod*.
3885 enum class MethodLoadKind {
3886 // Use a String init ArtMethod* loaded from Thread entrypoints.
3887 kStringInit,
3888
3889 // Use the method's own ArtMethod* loaded by the register allocator.
3890 kRecursive,
3891
3892 // Use ArtMethod* at a known address, embed the direct address in the code.
3893 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3894 kDirectAddress,
3895
3896 // Use ArtMethod* at an address that will be known at link time, embed the direct
3897 // address in the code. If the image is relocatable, emit .patch_oat entry.
3898 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3899 // the image relocatable or not.
3900 kDirectAddressWithFixup,
3901
Vladimir Markoa1de9182016-02-25 11:37:38 +00003902 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003903 // Used when we need to use the dex cache, for example for invoke-static that
3904 // may cause class initialization (the entry may point to a resolution method),
3905 // and we know that we can access the dex cache arrays using a PC-relative load.
3906 kDexCachePcRelative,
3907
3908 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3909 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3910 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3911 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3912 kDexCacheViaMethod,
3913 };
3914
3915 // Determines the location of the code pointer.
3916 enum class CodePtrLocation {
3917 // Recursive call, use local PC-relative call instruction.
3918 kCallSelf,
3919
3920 // Use PC-relative call instruction patched at link time.
3921 // Used for calls within an oat file, boot->boot or app->app.
3922 kCallPCRelative,
3923
3924 // Call to a known target address, embed the direct address in code.
3925 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3926 kCallDirect,
3927
3928 // Call to a target address that will be known at link time, embed the direct
3929 // address in code. If the image is relocatable, emit .patch_oat entry.
3930 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3931 // the image relocatable or not.
3932 kCallDirectWithFixup,
3933
3934 // Use code pointer from the ArtMethod*.
3935 // Used when we don't know the target code. This is also the last-resort-kind used when
3936 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3937 kCallArtMethod,
3938 };
3939
3940 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003941 MethodLoadKind method_load_kind;
3942 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003943 // The method load data holds
3944 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3945 // Note that there are multiple string init methods, each having its own offset.
3946 // - the method address for kDirectAddress
3947 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003948 uint64_t method_load_data;
3949 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003950 };
3951
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003952 HInvokeStaticOrDirect(ArenaAllocator* arena,
3953 uint32_t number_of_arguments,
3954 Primitive::Type return_type,
3955 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003956 uint32_t method_index,
3957 MethodReference target_method,
3958 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003959 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003960 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003961 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003962 : HInvoke(arena,
3963 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003964 // There is potentially one extra argument for the HCurrentMethod node, and
3965 // potentially one other if the clinit check is explicit, and potentially
3966 // one other if the method is a string factory.
3967 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003968 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003969 return_type,
3970 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003971 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003972 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003973 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003974 dispatch_info_(dispatch_info) {
3975 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3976 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3977 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003978
Vladimir Markodc151b22015-10-15 18:02:30 +01003979 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003980 bool had_current_method_input = HasCurrentMethodInput();
3981 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3982
3983 // Using the current method is the default and once we find a better
3984 // method load kind, we should not go back to using the current method.
3985 DCHECK(had_current_method_input || !needs_current_method_input);
3986
3987 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003988 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3989 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003990 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003991 dispatch_info_ = dispatch_info;
3992 }
3993
Vladimir Markoc53c0792015-11-19 15:48:33 +00003994 void AddSpecialInput(HInstruction* input) {
3995 // We allow only one special input.
3996 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3997 DCHECK(InputCount() == GetSpecialInputIndex() ||
3998 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3999 InsertInputAt(GetSpecialInputIndex(), input);
4000 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004001
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004002 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004003 // We access the method via the dex cache so we can't do an implicit null check.
4004 // TODO: for intrinsics we can generate implicit null checks.
4005 return false;
4006 }
4007
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004008 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004009 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004010 }
4011
Vladimir Markoc53c0792015-11-19 15:48:33 +00004012 // Get the index of the special input, if any.
4013 //
David Brazdil6de19382016-01-08 17:37:10 +00004014 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4015 // method pointer; otherwise there may be one platform-specific special input,
4016 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004017 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004018 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004019
Vladimir Markoa1de9182016-02-25 11:37:38 +00004020 InvokeType GetOptimizedInvokeType() const {
4021 return GetPackedField<OptimizedInvokeTypeField>();
4022 }
4023
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004024 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004025 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004026 }
4027
Vladimir Marko58155012015-08-19 12:49:41 +00004028 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4029 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4030 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004031 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004032 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004033 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004034 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004035 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4036 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004037 bool HasCurrentMethodInput() const {
4038 // This function can be called only after the invoke has been fully initialized by the builder.
4039 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004040 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004041 return true;
4042 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004043 DCHECK(InputCount() == GetSpecialInputIndex() ||
4044 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004045 return false;
4046 }
4047 }
Vladimir Marko58155012015-08-19 12:49:41 +00004048 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
4049 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004050 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00004051
4052 int32_t GetStringInitOffset() const {
4053 DCHECK(IsStringInit());
4054 return dispatch_info_.method_load_data;
4055 }
4056
4057 uint64_t GetMethodAddress() const {
4058 DCHECK(HasMethodAddress());
4059 return dispatch_info_.method_load_data;
4060 }
4061
4062 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004063 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004064 return dispatch_info_.method_load_data;
4065 }
4066
4067 uint64_t GetDirectCodePtr() const {
4068 DCHECK(HasDirectCodePtr());
4069 return dispatch_info_.direct_code_ptr;
4070 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004071
Vladimir Markoa1de9182016-02-25 11:37:38 +00004072 ClinitCheckRequirement GetClinitCheckRequirement() const {
4073 return GetPackedField<ClinitCheckRequirementField>();
4074 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004075
Roland Levillain4c0eb422015-04-24 16:43:49 +01004076 // Is this instruction a call to a static method?
4077 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004078 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004079 }
4080
Vladimir Markofbb184a2015-11-13 14:47:00 +00004081 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4082 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4083 // instruction; only relevant for static calls with explicit clinit check.
4084 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004085 DCHECK(IsStaticWithExplicitClinitCheck());
4086 size_t last_input_index = InputCount() - 1;
4087 HInstruction* last_input = InputAt(last_input_index);
4088 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004089 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004090 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004091 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004092 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004093 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004094 }
4095
4096 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004097 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004098 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004099 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004100 }
4101
4102 // Is this a call to a static method whose declaring class has an
4103 // implicit intialization check requirement?
4104 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004105 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004106 }
4107
Vladimir Markob554b5a2015-11-06 12:57:55 +00004108 // Does this method load kind need the current method as an input?
4109 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4110 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4111 }
4112
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004113 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004114
Roland Levillain4c0eb422015-04-24 16:43:49 +01004115 protected:
4116 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
4117 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
4118 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
4119 HInstruction* input = input_record.GetInstruction();
4120 // `input` is the last input of a static invoke marked as having
4121 // an explicit clinit check. It must either be:
4122 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4123 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4124 DCHECK(input != nullptr);
4125 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
4126 }
4127 return input_record;
4128 }
4129
Vladimir Markoc53c0792015-11-19 15:48:33 +00004130 void InsertInputAt(size_t index, HInstruction* input);
4131 void RemoveInputAt(size_t index);
4132
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004133 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004134 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4135 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4136 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4137 static constexpr size_t kFieldClinitCheckRequirement =
4138 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4139 static constexpr size_t kFieldClinitCheckRequirementSize =
4140 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4141 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4142 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4143 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4144 "Too many packed fields.");
4145 using OptimizedInvokeTypeField =
4146 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4147 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4148 kFieldClinitCheckRequirement,
4149 kFieldClinitCheckRequirementSize>;
4150
Vladimir Marko58155012015-08-19 12:49:41 +00004151 // The target method may refer to different dex file or method index than the original
4152 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4153 // in derived class to increase visibility.
4154 MethodReference target_method_;
4155 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004156
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004157 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004158};
Vladimir Markof64242a2015-12-01 14:58:23 +00004159std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004160std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004161
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004162class HInvokeVirtual : public HInvoke {
4163 public:
4164 HInvokeVirtual(ArenaAllocator* arena,
4165 uint32_t number_of_arguments,
4166 Primitive::Type return_type,
4167 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004168 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004169 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004170 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004171 vtable_index_(vtable_index) {}
4172
Calin Juravle641547a2015-04-21 22:08:51 +01004173 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004174 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004175 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004176 }
4177
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004178 uint32_t GetVTableIndex() const { return vtable_index_; }
4179
4180 DECLARE_INSTRUCTION(InvokeVirtual);
4181
4182 private:
4183 const uint32_t vtable_index_;
4184
4185 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4186};
4187
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004188class HInvokeInterface : public HInvoke {
4189 public:
4190 HInvokeInterface(ArenaAllocator* arena,
4191 uint32_t number_of_arguments,
4192 Primitive::Type return_type,
4193 uint32_t dex_pc,
4194 uint32_t dex_method_index,
4195 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004196 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004197 imt_index_(imt_index) {}
4198
Calin Juravle641547a2015-04-21 22:08:51 +01004199 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004200 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004201 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004202 }
4203
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004204 uint32_t GetImtIndex() const { return imt_index_; }
4205 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4206
4207 DECLARE_INSTRUCTION(InvokeInterface);
4208
4209 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004210 const uint32_t imt_index_;
4211
4212 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4213};
4214
Roland Levillain88cb1752014-10-20 16:36:47 +01004215class HNeg : public HUnaryOperation {
4216 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004217 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004218 : HUnaryOperation(result_type, input, dex_pc) {
4219 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4220 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004221
Roland Levillain9867bc72015-08-05 10:21:34 +01004222 template <typename T> T Compute(T x) const { return -x; }
4223
4224 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004225 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004226 }
4227 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004228 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004229 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004230 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4231 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4232 }
4233 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4234 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4235 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004236
Roland Levillain88cb1752014-10-20 16:36:47 +01004237 DECLARE_INSTRUCTION(Neg);
4238
4239 private:
4240 DISALLOW_COPY_AND_ASSIGN(HNeg);
4241};
4242
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004243class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004244 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004245 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004246 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004247 uint32_t dex_pc,
4248 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004249 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004250 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004251 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004252 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004253 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004254 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004255 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004256 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004257 }
4258
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004259 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004260 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004261
4262 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004263 bool NeedsEnvironment() const OVERRIDE { return true; }
4264
Mingyao Yang0c365e62015-03-31 15:09:29 -07004265 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4266 bool CanThrow() const OVERRIDE { return true; }
4267
Calin Juravle10e244f2015-01-26 18:54:32 +00004268 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004269
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004270 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4271
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004272 DECLARE_INSTRUCTION(NewArray);
4273
4274 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004275 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004276 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004277 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004278
4279 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4280};
4281
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004282class HAdd : public HBinaryOperation {
4283 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004284 HAdd(Primitive::Type result_type,
4285 HInstruction* left,
4286 HInstruction* right,
4287 uint32_t dex_pc = kNoDexPc)
4288 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004289
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004290 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004291
Roland Levillain9867bc72015-08-05 10:21:34 +01004292 template <typename T> T Compute(T x, T y) const { return x + y; }
4293
4294 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004295 return GetBlock()->GetGraph()->GetIntConstant(
4296 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004297 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004298 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004299 return GetBlock()->GetGraph()->GetLongConstant(
4300 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004301 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004302 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4303 return GetBlock()->GetGraph()->GetFloatConstant(
4304 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4305 }
4306 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4307 return GetBlock()->GetGraph()->GetDoubleConstant(
4308 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4309 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004310
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004311 DECLARE_INSTRUCTION(Add);
4312
4313 private:
4314 DISALLOW_COPY_AND_ASSIGN(HAdd);
4315};
4316
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004317class HSub : public HBinaryOperation {
4318 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004319 HSub(Primitive::Type result_type,
4320 HInstruction* left,
4321 HInstruction* right,
4322 uint32_t dex_pc = kNoDexPc)
4323 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004324
Roland Levillain9867bc72015-08-05 10:21:34 +01004325 template <typename T> T Compute(T x, T y) const { return x - y; }
4326
4327 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004328 return GetBlock()->GetGraph()->GetIntConstant(
4329 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004330 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004331 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004332 return GetBlock()->GetGraph()->GetLongConstant(
4333 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004334 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004335 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4336 return GetBlock()->GetGraph()->GetFloatConstant(
4337 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4338 }
4339 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4340 return GetBlock()->GetGraph()->GetDoubleConstant(
4341 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4342 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004343
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004344 DECLARE_INSTRUCTION(Sub);
4345
4346 private:
4347 DISALLOW_COPY_AND_ASSIGN(HSub);
4348};
4349
Calin Juravle34bacdf2014-10-07 20:23:36 +01004350class HMul : public HBinaryOperation {
4351 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004352 HMul(Primitive::Type result_type,
4353 HInstruction* left,
4354 HInstruction* right,
4355 uint32_t dex_pc = kNoDexPc)
4356 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004357
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004358 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004359
Roland Levillain9867bc72015-08-05 10:21:34 +01004360 template <typename T> T Compute(T x, T y) const { return x * y; }
4361
4362 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004363 return GetBlock()->GetGraph()->GetIntConstant(
4364 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004365 }
4366 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004367 return GetBlock()->GetGraph()->GetLongConstant(
4368 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004369 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004370 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4371 return GetBlock()->GetGraph()->GetFloatConstant(
4372 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4373 }
4374 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4375 return GetBlock()->GetGraph()->GetDoubleConstant(
4376 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4377 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004378
4379 DECLARE_INSTRUCTION(Mul);
4380
4381 private:
4382 DISALLOW_COPY_AND_ASSIGN(HMul);
4383};
4384
Calin Juravle7c4954d2014-10-28 16:57:40 +00004385class HDiv : public HBinaryOperation {
4386 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004387 HDiv(Primitive::Type result_type,
4388 HInstruction* left,
4389 HInstruction* right,
4390 uint32_t dex_pc)
4391 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004392
Roland Levillain9867bc72015-08-05 10:21:34 +01004393 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004394 T ComputeIntegral(T x, T y) const {
4395 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004396 // Our graph structure ensures we never have 0 for `y` during
4397 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004398 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004399 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004400 return (y == -1) ? -x : x / y;
4401 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004402
Roland Levillain31dd3d62016-02-16 12:21:02 +00004403 template <typename T>
4404 T ComputeFP(T x, T y) const {
4405 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4406 return x / y;
4407 }
4408
Roland Levillain9867bc72015-08-05 10:21:34 +01004409 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004410 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004411 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004412 }
4413 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004414 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004415 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4416 }
4417 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4418 return GetBlock()->GetGraph()->GetFloatConstant(
4419 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4420 }
4421 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4422 return GetBlock()->GetGraph()->GetDoubleConstant(
4423 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004424 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004425
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004426 static SideEffects SideEffectsForArchRuntimeCalls() {
4427 // The generated code can use a runtime call.
4428 return SideEffects::CanTriggerGC();
4429 }
4430
Calin Juravle7c4954d2014-10-28 16:57:40 +00004431 DECLARE_INSTRUCTION(Div);
4432
4433 private:
4434 DISALLOW_COPY_AND_ASSIGN(HDiv);
4435};
4436
Calin Juravlebacfec32014-11-14 15:54:36 +00004437class HRem : public HBinaryOperation {
4438 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004439 HRem(Primitive::Type result_type,
4440 HInstruction* left,
4441 HInstruction* right,
4442 uint32_t dex_pc)
4443 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004444
Roland Levillain9867bc72015-08-05 10:21:34 +01004445 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004446 T ComputeIntegral(T x, T y) const {
4447 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004448 // Our graph structure ensures we never have 0 for `y` during
4449 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004450 DCHECK_NE(y, 0);
4451 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4452 return (y == -1) ? 0 : x % y;
4453 }
4454
Roland Levillain31dd3d62016-02-16 12:21:02 +00004455 template <typename T>
4456 T ComputeFP(T x, T y) const {
4457 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4458 return std::fmod(x, y);
4459 }
4460
Roland Levillain9867bc72015-08-05 10:21:34 +01004461 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004462 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004463 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004464 }
4465 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004466 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004467 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004468 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004469 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4470 return GetBlock()->GetGraph()->GetFloatConstant(
4471 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4472 }
4473 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4474 return GetBlock()->GetGraph()->GetDoubleConstant(
4475 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4476 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004477
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004478 static SideEffects SideEffectsForArchRuntimeCalls() {
4479 return SideEffects::CanTriggerGC();
4480 }
4481
Calin Juravlebacfec32014-11-14 15:54:36 +00004482 DECLARE_INSTRUCTION(Rem);
4483
4484 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004485 DISALLOW_COPY_AND_ASSIGN(HRem);
4486};
4487
Calin Juravled0d48522014-11-04 16:40:20 +00004488class HDivZeroCheck : public HExpression<1> {
4489 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004490 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4491 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004492 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004493 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004494 SetRawInputAt(0, value);
4495 }
4496
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004497 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4498
Calin Juravled0d48522014-11-04 16:40:20 +00004499 bool CanBeMoved() const OVERRIDE { return true; }
4500
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004501 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004502 return true;
4503 }
4504
4505 bool NeedsEnvironment() const OVERRIDE { return true; }
4506 bool CanThrow() const OVERRIDE { return true; }
4507
Calin Juravled0d48522014-11-04 16:40:20 +00004508 DECLARE_INSTRUCTION(DivZeroCheck);
4509
4510 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004511 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4512};
4513
Calin Juravle9aec02f2014-11-18 23:06:35 +00004514class HShl : public HBinaryOperation {
4515 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004516 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004517 HInstruction* value,
4518 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004519 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004520 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4521 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4522 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004523 }
4524
Roland Levillain5b5b9312016-03-22 14:57:31 +00004525 template <typename T>
4526 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4527 return value << (distance & max_shift_distance);
4528 }
4529
4530 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004531 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004532 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004533 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004534 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004535 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004536 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004537 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004538 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4539 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4540 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4541 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004542 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004543 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4544 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004545 LOG(FATAL) << DebugName() << " is not defined for float values";
4546 UNREACHABLE();
4547 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004548 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4549 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004550 LOG(FATAL) << DebugName() << " is not defined for double values";
4551 UNREACHABLE();
4552 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004553
4554 DECLARE_INSTRUCTION(Shl);
4555
4556 private:
4557 DISALLOW_COPY_AND_ASSIGN(HShl);
4558};
4559
4560class HShr : public HBinaryOperation {
4561 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004562 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004563 HInstruction* value,
4564 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004565 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004566 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4567 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4568 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004569 }
4570
Roland Levillain5b5b9312016-03-22 14:57:31 +00004571 template <typename T>
4572 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4573 return value >> (distance & max_shift_distance);
4574 }
4575
4576 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004577 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004578 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004579 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004580 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004581 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004582 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004583 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004584 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4585 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4586 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4587 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004588 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004589 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4590 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004591 LOG(FATAL) << DebugName() << " is not defined for float values";
4592 UNREACHABLE();
4593 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004594 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4595 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004596 LOG(FATAL) << DebugName() << " is not defined for double values";
4597 UNREACHABLE();
4598 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004599
4600 DECLARE_INSTRUCTION(Shr);
4601
4602 private:
4603 DISALLOW_COPY_AND_ASSIGN(HShr);
4604};
4605
4606class HUShr : public HBinaryOperation {
4607 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004608 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004609 HInstruction* value,
4610 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004611 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004612 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4613 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4614 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004615 }
4616
Roland Levillain5b5b9312016-03-22 14:57:31 +00004617 template <typename T>
4618 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4619 typedef typename std::make_unsigned<T>::type V;
4620 V ux = static_cast<V>(value);
4621 return static_cast<T>(ux >> (distance & max_shift_distance));
4622 }
4623
4624 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004625 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004626 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004627 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004628 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004629 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004630 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004631 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004632 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4633 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4634 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4635 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004636 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004637 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4638 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004639 LOG(FATAL) << DebugName() << " is not defined for float values";
4640 UNREACHABLE();
4641 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004642 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4643 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004644 LOG(FATAL) << DebugName() << " is not defined for double values";
4645 UNREACHABLE();
4646 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004647
4648 DECLARE_INSTRUCTION(UShr);
4649
4650 private:
4651 DISALLOW_COPY_AND_ASSIGN(HUShr);
4652};
4653
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004654class HAnd : public HBinaryOperation {
4655 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004656 HAnd(Primitive::Type result_type,
4657 HInstruction* left,
4658 HInstruction* right,
4659 uint32_t dex_pc = kNoDexPc)
4660 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004661
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004662 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004663
Roland Levillaine53bd812016-02-24 14:54:18 +00004664 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004665
4666 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004667 return GetBlock()->GetGraph()->GetIntConstant(
4668 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004669 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004670 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004671 return GetBlock()->GetGraph()->GetLongConstant(
4672 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004673 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004674 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4675 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4676 LOG(FATAL) << DebugName() << " is not defined for float values";
4677 UNREACHABLE();
4678 }
4679 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4680 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4681 LOG(FATAL) << DebugName() << " is not defined for double values";
4682 UNREACHABLE();
4683 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004684
4685 DECLARE_INSTRUCTION(And);
4686
4687 private:
4688 DISALLOW_COPY_AND_ASSIGN(HAnd);
4689};
4690
4691class HOr : public HBinaryOperation {
4692 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004693 HOr(Primitive::Type result_type,
4694 HInstruction* left,
4695 HInstruction* right,
4696 uint32_t dex_pc = kNoDexPc)
4697 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004698
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004699 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004700
Roland Levillaine53bd812016-02-24 14:54:18 +00004701 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004702
4703 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004704 return GetBlock()->GetGraph()->GetIntConstant(
4705 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004706 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004707 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004708 return GetBlock()->GetGraph()->GetLongConstant(
4709 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004710 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004711 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4712 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4713 LOG(FATAL) << DebugName() << " is not defined for float values";
4714 UNREACHABLE();
4715 }
4716 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4717 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4718 LOG(FATAL) << DebugName() << " is not defined for double values";
4719 UNREACHABLE();
4720 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004721
4722 DECLARE_INSTRUCTION(Or);
4723
4724 private:
4725 DISALLOW_COPY_AND_ASSIGN(HOr);
4726};
4727
4728class HXor : public HBinaryOperation {
4729 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004730 HXor(Primitive::Type result_type,
4731 HInstruction* left,
4732 HInstruction* right,
4733 uint32_t dex_pc = kNoDexPc)
4734 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004735
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004736 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004737
Roland Levillaine53bd812016-02-24 14:54:18 +00004738 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004739
4740 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004741 return GetBlock()->GetGraph()->GetIntConstant(
4742 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004743 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004744 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004745 return GetBlock()->GetGraph()->GetLongConstant(
4746 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004747 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004748 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4749 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4750 LOG(FATAL) << DebugName() << " is not defined for float values";
4751 UNREACHABLE();
4752 }
4753 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4754 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4755 LOG(FATAL) << DebugName() << " is not defined for double values";
4756 UNREACHABLE();
4757 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004758
4759 DECLARE_INSTRUCTION(Xor);
4760
4761 private:
4762 DISALLOW_COPY_AND_ASSIGN(HXor);
4763};
4764
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004765class HRor : public HBinaryOperation {
4766 public:
4767 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004768 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004769 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4770 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004771 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004772
Roland Levillain5b5b9312016-03-22 14:57:31 +00004773 template <typename T>
4774 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4775 typedef typename std::make_unsigned<T>::type V;
4776 V ux = static_cast<V>(value);
4777 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004778 return static_cast<T>(ux);
4779 } else {
4780 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004781 return static_cast<T>(ux >> (distance & max_shift_value)) |
4782 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004783 }
4784 }
4785
Roland Levillain5b5b9312016-03-22 14:57:31 +00004786 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004787 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004788 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004789 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004790 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004791 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004792 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004793 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004794 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4795 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4796 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4797 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004798 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004799 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4800 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004801 LOG(FATAL) << DebugName() << " is not defined for float values";
4802 UNREACHABLE();
4803 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004804 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4805 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004806 LOG(FATAL) << DebugName() << " is not defined for double values";
4807 UNREACHABLE();
4808 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004809
4810 DECLARE_INSTRUCTION(Ror);
4811
4812 private:
4813 DISALLOW_COPY_AND_ASSIGN(HRor);
4814};
4815
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004816// The value of a parameter in this method. Its location depends on
4817// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004818class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004819 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004820 HParameterValue(const DexFile& dex_file,
4821 uint16_t type_index,
4822 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004823 Primitive::Type parameter_type,
4824 bool is_this = false)
4825 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004826 dex_file_(dex_file),
4827 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004828 index_(index) {
4829 SetPackedFlag<kFlagIsThis>(is_this);
4830 SetPackedFlag<kFlagCanBeNull>(!is_this);
4831 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004832
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004833 const DexFile& GetDexFile() const { return dex_file_; }
4834 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004835 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004836 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004837
Vladimir Markoa1de9182016-02-25 11:37:38 +00004838 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4839 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004840
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004841 DECLARE_INSTRUCTION(ParameterValue);
4842
4843 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004844 // Whether or not the parameter value corresponds to 'this' argument.
4845 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4846 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4847 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4848 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4849 "Too many packed fields.");
4850
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004851 const DexFile& dex_file_;
4852 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004853 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004854 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004855 const uint8_t index_;
4856
4857 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4858};
4859
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004860class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004861 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004862 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4863 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004864
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004865 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004866 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004867 return true;
4868 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004869
Roland Levillain9867bc72015-08-05 10:21:34 +01004870 template <typename T> T Compute(T x) const { return ~x; }
4871
4872 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004873 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004874 }
4875 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004876 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004877 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004878 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4879 LOG(FATAL) << DebugName() << " is not defined for float values";
4880 UNREACHABLE();
4881 }
4882 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4883 LOG(FATAL) << DebugName() << " is not defined for double values";
4884 UNREACHABLE();
4885 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004886
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004887 DECLARE_INSTRUCTION(Not);
4888
4889 private:
4890 DISALLOW_COPY_AND_ASSIGN(HNot);
4891};
4892
David Brazdil66d126e2015-04-03 16:02:44 +01004893class HBooleanNot : public HUnaryOperation {
4894 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004895 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4896 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004897
4898 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004899 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004900 return true;
4901 }
4902
Roland Levillain9867bc72015-08-05 10:21:34 +01004903 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004904 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004905 return !x;
4906 }
4907
Roland Levillain9867bc72015-08-05 10:21:34 +01004908 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004909 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004910 }
4911 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4912 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004913 UNREACHABLE();
4914 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004915 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4916 LOG(FATAL) << DebugName() << " is not defined for float values";
4917 UNREACHABLE();
4918 }
4919 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4920 LOG(FATAL) << DebugName() << " is not defined for double values";
4921 UNREACHABLE();
4922 }
David Brazdil66d126e2015-04-03 16:02:44 +01004923
4924 DECLARE_INSTRUCTION(BooleanNot);
4925
4926 private:
4927 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4928};
4929
Roland Levillaindff1f282014-11-05 14:15:05 +00004930class HTypeConversion : public HExpression<1> {
4931 public:
4932 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004933 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004934 : HExpression(result_type,
4935 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4936 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004937 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004938 // Invariant: We should never generate a conversion to a Boolean value.
4939 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004940 }
4941
4942 HInstruction* GetInput() const { return InputAt(0); }
4943 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4944 Primitive::Type GetResultType() const { return GetType(); }
4945
4946 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004947 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004948
Mark Mendelle82549b2015-05-06 10:55:34 -04004949 // Try to statically evaluate the conversion and return a HConstant
4950 // containing the result. If the input cannot be converted, return nullptr.
4951 HConstant* TryStaticEvaluation() const;
4952
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004953 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4954 Primitive::Type result_type) {
4955 // Some architectures may not require the 'GC' side effects, but at this point
4956 // in the compilation process we do not know what architecture we will
4957 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004958 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4959 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004960 return SideEffects::CanTriggerGC();
4961 }
4962 return SideEffects::None();
4963 }
4964
Roland Levillaindff1f282014-11-05 14:15:05 +00004965 DECLARE_INSTRUCTION(TypeConversion);
4966
4967 private:
4968 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4969};
4970
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004971static constexpr uint32_t kNoRegNumber = -1;
4972
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004973class HNullCheck : public HExpression<1> {
4974 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004975 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4976 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004977 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004978 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004979 SetRawInputAt(0, value);
4980 }
4981
Calin Juravle10e244f2015-01-26 18:54:32 +00004982 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004983 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004984 return true;
4985 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004986
Calin Juravle10e244f2015-01-26 18:54:32 +00004987 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004988
Calin Juravle10e244f2015-01-26 18:54:32 +00004989 bool CanThrow() const OVERRIDE { return true; }
4990
4991 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004992
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004993
4994 DECLARE_INSTRUCTION(NullCheck);
4995
4996 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004997 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4998};
4999
5000class FieldInfo : public ValueObject {
5001 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005002 FieldInfo(MemberOffset field_offset,
5003 Primitive::Type field_type,
5004 bool is_volatile,
5005 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005006 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005007 const DexFile& dex_file,
5008 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005009 : field_offset_(field_offset),
5010 field_type_(field_type),
5011 is_volatile_(is_volatile),
5012 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005013 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005014 dex_file_(dex_file),
5015 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005016
5017 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005018 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005019 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005020 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005021 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005022 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005023 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005024
5025 private:
5026 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005027 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005028 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005029 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005030 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005031 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005032 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005033};
5034
5035class HInstanceFieldGet : public HExpression<1> {
5036 public:
5037 HInstanceFieldGet(HInstruction* value,
5038 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005039 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005040 bool is_volatile,
5041 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005042 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005043 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005044 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005045 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005046 : HExpression(field_type,
5047 SideEffects::FieldReadOfType(field_type, is_volatile),
5048 dex_pc),
5049 field_info_(field_offset,
5050 field_type,
5051 is_volatile,
5052 field_idx,
5053 declaring_class_def_index,
5054 dex_file,
5055 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005056 SetRawInputAt(0, value);
5057 }
5058
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005059 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005060
5061 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5062 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
5063 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005064 }
5065
Calin Juravle641547a2015-04-21 22:08:51 +01005066 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5067 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005068 }
5069
5070 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005071 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5072 }
5073
Calin Juravle52c48962014-12-16 17:02:57 +00005074 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005075 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005076 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005077 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005078
5079 DECLARE_INSTRUCTION(InstanceFieldGet);
5080
5081 private:
5082 const FieldInfo field_info_;
5083
5084 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5085};
5086
5087class HInstanceFieldSet : public HTemplateInstruction<2> {
5088 public:
5089 HInstanceFieldSet(HInstruction* object,
5090 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005091 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005092 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005093 bool is_volatile,
5094 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005095 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005096 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005097 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005098 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005099 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5100 dex_pc),
5101 field_info_(field_offset,
5102 field_type,
5103 is_volatile,
5104 field_idx,
5105 declaring_class_def_index,
5106 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005107 dex_cache) {
5108 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005109 SetRawInputAt(0, object);
5110 SetRawInputAt(1, value);
5111 }
5112
Calin Juravle641547a2015-04-21 22:08:51 +01005113 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5114 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005115 }
5116
Calin Juravle52c48962014-12-16 17:02:57 +00005117 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005118 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005119 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005120 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005121 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005122 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5123 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005124
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005125 DECLARE_INSTRUCTION(InstanceFieldSet);
5126
5127 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005128 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5129 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5130 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5131 "Too many packed fields.");
5132
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005133 const FieldInfo field_info_;
5134
5135 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5136};
5137
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005138class HArrayGet : public HExpression<2> {
5139 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005140 HArrayGet(HInstruction* array,
5141 HInstruction* index,
5142 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005143 uint32_t dex_pc,
5144 SideEffects additional_side_effects = SideEffects::None())
5145 : HExpression(type,
5146 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00005147 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005148 SetRawInputAt(0, array);
5149 SetRawInputAt(1, index);
5150 }
5151
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005152 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005153 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005154 return true;
5155 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005156 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005157 // TODO: We can be smarter here.
5158 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5159 // which generates the implicit null check. There are cases when these can be removed
5160 // to produce better code. If we ever add optimizations to do so we should allow an
5161 // implicit check here (as long as the address falls in the first page).
5162 return false;
5163 }
5164
David Brazdil4833f5a2015-12-16 10:37:39 +00005165 bool IsEquivalentOf(HArrayGet* other) const {
5166 bool result = (GetDexPc() == other->GetDexPc());
5167 if (kIsDebugBuild && result) {
5168 DCHECK_EQ(GetBlock(), other->GetBlock());
5169 DCHECK_EQ(GetArray(), other->GetArray());
5170 DCHECK_EQ(GetIndex(), other->GetIndex());
5171 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005172 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005173 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005174 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5175 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005176 }
5177 }
5178 return result;
5179 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005180
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005181 HInstruction* GetArray() const { return InputAt(0); }
5182 HInstruction* GetIndex() const { return InputAt(1); }
5183
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005184 DECLARE_INSTRUCTION(ArrayGet);
5185
5186 private:
5187 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5188};
5189
5190class HArraySet : public HTemplateInstruction<3> {
5191 public:
5192 HArraySet(HInstruction* array,
5193 HInstruction* index,
5194 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005195 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005196 uint32_t dex_pc,
5197 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005198 : HTemplateInstruction(
Aart Bik1f7624c2016-04-13 21:17:17 +00005199 SideEffects::ArrayWriteOfType(expected_component_type).Union(
5200 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
5201 additional_side_effects),
5202 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005203 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5204 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5205 SetPackedFlag<kFlagValueCanBeNull>(true);
5206 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005207 SetRawInputAt(0, array);
5208 SetRawInputAt(1, index);
5209 SetRawInputAt(2, value);
5210 }
5211
Calin Juravle77520bc2015-01-12 18:45:46 +00005212 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005213 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005214 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005215 }
5216
Mingyao Yang81014cb2015-06-02 03:16:27 -07005217 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005218 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005219
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005220 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005221 // TODO: Same as for ArrayGet.
5222 return false;
5223 }
5224
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005225 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005226 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005227 }
5228
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005229 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005230 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005231 }
5232
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005233 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005234 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005235 }
5236
Vladimir Markoa1de9182016-02-25 11:37:38 +00005237 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5238 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5239 bool StaticTypeOfArrayIsObjectArray() const {
5240 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5241 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005242
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005243 HInstruction* GetArray() const { return InputAt(0); }
5244 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005245 HInstruction* GetValue() const { return InputAt(2); }
5246
5247 Primitive::Type GetComponentType() const {
5248 // The Dex format does not type floating point index operations. Since the
5249 // `expected_component_type_` is set during building and can therefore not
5250 // be correct, we also check what is the value type. If it is a floating
5251 // point type, we must use that type.
5252 Primitive::Type value_type = GetValue()->GetType();
5253 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5254 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005255 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005256 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005257
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005258 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005259 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005260 }
5261
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005262 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5263 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5264 }
5265
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005266 DECLARE_INSTRUCTION(ArraySet);
5267
5268 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005269 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5270 static constexpr size_t kFieldExpectedComponentTypeSize =
5271 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5272 static constexpr size_t kFlagNeedsTypeCheck =
5273 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5274 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005275 // Cached information for the reference_type_info_ so that codegen
5276 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005277 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5278 static constexpr size_t kNumberOfArraySetPackedBits =
5279 kFlagStaticTypeOfArrayIsObjectArray + 1;
5280 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5281 using ExpectedComponentTypeField =
5282 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005283
5284 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5285};
5286
5287class HArrayLength : public HExpression<1> {
5288 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005289 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005290 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005291 // Note that arrays do not change length, so the instruction does not
5292 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005293 SetRawInputAt(0, array);
5294 }
5295
Calin Juravle77520bc2015-01-12 18:45:46 +00005296 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005297 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005298 return true;
5299 }
Calin Juravle641547a2015-04-21 22:08:51 +01005300 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5301 return obj == InputAt(0);
5302 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005303
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005304 DECLARE_INSTRUCTION(ArrayLength);
5305
5306 private:
5307 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5308};
5309
5310class HBoundsCheck : public HExpression<2> {
5311 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005312 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5313 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005314 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005315 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005316 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005317 SetRawInputAt(0, index);
5318 SetRawInputAt(1, length);
5319 }
5320
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005321 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005322 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005323 return true;
5324 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005325
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005326 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005327
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005328 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005329
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005330 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005331
5332 DECLARE_INSTRUCTION(BoundsCheck);
5333
5334 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005335 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5336};
5337
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005338class HSuspendCheck : public HTemplateInstruction<0> {
5339 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005340 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005341 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005342
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005343 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005344 return true;
5345 }
5346
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005347 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5348 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005349
5350 DECLARE_INSTRUCTION(SuspendCheck);
5351
5352 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005353 // Only used for code generation, in order to share the same slow path between back edges
5354 // of a same loop.
5355 SlowPathCode* slow_path_;
5356
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005357 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5358};
5359
David Srbecky0cf44932015-12-09 14:09:59 +00005360// Pseudo-instruction which provides the native debugger with mapping information.
5361// It ensures that we can generate line number and local variables at this point.
5362class HNativeDebugInfo : public HTemplateInstruction<0> {
5363 public:
5364 explicit HNativeDebugInfo(uint32_t dex_pc)
5365 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5366
5367 bool NeedsEnvironment() const OVERRIDE {
5368 return true;
5369 }
5370
5371 DECLARE_INSTRUCTION(NativeDebugInfo);
5372
5373 private:
5374 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5375};
5376
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005377/**
5378 * Instruction to load a Class object.
5379 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005380class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005381 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005382 HLoadClass(HCurrentMethod* current_method,
5383 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005384 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005385 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005386 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005387 bool needs_access_check,
5388 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005389 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005390 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005391 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005392 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005393 // Referrers class should not need access check. We never inline unverified
5394 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005395 DCHECK(!is_referrers_class || !needs_access_check);
5396
5397 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5398 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5399 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5400 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005401 SetRawInputAt(0, current_method);
5402 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005403
5404 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005405
5406 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005407 // Note that we don't need to test for generate_clinit_check_.
5408 // Whether or not we need to generate the clinit check is processed in
5409 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005410 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005411 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005412 }
5413
5414 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5415
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005416 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005417 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005418
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005419 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005420 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005421 }
5422
Calin Juravle0ba218d2015-05-19 18:46:01 +01005423 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005424 // The entrypoint the code generator is going to call does not do
5425 // clinit of the class.
5426 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005427 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005428 }
5429
5430 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005431 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005432 (!IsReferrersClass() && !IsInDexCache()) ||
5433 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005434 }
5435
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005436
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005437 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005438 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005439 }
5440
Calin Juravleacf735c2015-02-12 15:25:22 +00005441 ReferenceTypeInfo GetLoadedClassRTI() {
5442 return loaded_class_rti_;
5443 }
5444
5445 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5446 // Make sure we only set exact types (the loaded class should never be merged).
5447 DCHECK(rti.IsExact());
5448 loaded_class_rti_ = rti;
5449 }
5450
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005451 const DexFile& GetDexFile() { return dex_file_; }
5452
Vladimir Markoa1de9182016-02-25 11:37:38 +00005453 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005454
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005455 static SideEffects SideEffectsForArchRuntimeCalls() {
5456 return SideEffects::CanTriggerGC();
5457 }
5458
Vladimir Markoa1de9182016-02-25 11:37:38 +00005459 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5460 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5461 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5462 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005463
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005464 DECLARE_INSTRUCTION(LoadClass);
5465
5466 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005467 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5468 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5469 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005470 // Whether this instruction must generate the initialization check.
5471 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005472 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5473 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5474 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5475
5476 const uint16_t type_index_;
5477 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005478
Calin Juravleacf735c2015-02-12 15:25:22 +00005479 ReferenceTypeInfo loaded_class_rti_;
5480
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005481 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5482};
5483
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005484class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005485 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005486 // Determines how to load the String.
5487 enum class LoadKind {
5488 // Use boot image String* address that will be known at link time.
5489 // Used for boot image strings referenced by boot image code in non-PIC mode.
5490 kBootImageLinkTimeAddress,
5491
5492 // Use PC-relative boot image String* address that will be known at link time.
5493 // Used for boot image strings referenced by boot image code in PIC mode.
5494 kBootImageLinkTimePcRelative,
5495
5496 // Use a known boot image String* address, embedded in the code by the codegen.
5497 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5498 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5499 // GetIncludePatchInformation().
5500 kBootImageAddress,
5501
5502 // Load from the resolved strings array at an absolute address.
5503 // Used for strings outside the boot image referenced by JIT-compiled code.
5504 kDexCacheAddress,
5505
5506 // Load from resolved strings array in the dex cache using a PC-relative load.
5507 // Used for strings outside boot image when we know that we can access
5508 // the dex cache arrays using a PC-relative load.
5509 kDexCachePcRelative,
5510
5511 // Load from resolved strings array accessed through the class loaded from
5512 // the compiled method's own ArtMethod*. This is the default access type when
5513 // all other types are unavailable.
5514 kDexCacheViaMethod,
5515
5516 kLast = kDexCacheViaMethod
5517 };
5518
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005519 HLoadString(HCurrentMethod* current_method,
5520 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005521 const DexFile& dex_file,
5522 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005523 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005524 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005525 SetPackedFlag<kFlagIsInDexCache>(false);
5526 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5527 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005528 SetRawInputAt(0, current_method);
5529 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005530
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005531 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5532 DCHECK(HasAddress(load_kind));
5533 load_data_.address = address;
5534 SetLoadKindInternal(load_kind);
5535 }
5536
5537 void SetLoadKindWithStringReference(LoadKind load_kind,
5538 const DexFile& dex_file,
5539 uint32_t string_index) {
5540 DCHECK(HasStringReference(load_kind));
5541 load_data_.ref.dex_file = &dex_file;
5542 string_index_ = string_index;
5543 SetLoadKindInternal(load_kind);
5544 }
5545
5546 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5547 const DexFile& dex_file,
5548 uint32_t element_index) {
5549 DCHECK(HasDexCacheReference(load_kind));
5550 load_data_.ref.dex_file = &dex_file;
5551 load_data_.ref.dex_cache_element_index = element_index;
5552 SetLoadKindInternal(load_kind);
5553 }
5554
5555 LoadKind GetLoadKind() const {
5556 return GetPackedField<LoadKindField>();
5557 }
5558
5559 const DexFile& GetDexFile() const;
5560
5561 uint32_t GetStringIndex() const {
5562 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5563 return string_index_;
5564 }
5565
5566 uint32_t GetDexCacheElementOffset() const;
5567
5568 uint64_t GetAddress() const {
5569 DCHECK(HasAddress(GetLoadKind()));
5570 return load_data_.address;
5571 }
5572
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005573 bool CanBeMoved() const OVERRIDE { return true; }
5574
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005575 bool InstructionDataEquals(HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005576
5577 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5578
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005579 // Will call the runtime if we need to load the string through
5580 // the dex cache and the string is not guaranteed to be there yet.
5581 bool NeedsEnvironment() const OVERRIDE {
5582 LoadKind load_kind = GetLoadKind();
5583 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5584 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5585 load_kind == LoadKind::kBootImageAddress) {
5586 return false;
5587 }
5588 return !IsInDexCache();
5589 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005590
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005591 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5592 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5593 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005594
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005595 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005596 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005597
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005598 static SideEffects SideEffectsForArchRuntimeCalls() {
5599 return SideEffects::CanTriggerGC();
5600 }
5601
Vladimir Markoa1de9182016-02-25 11:37:38 +00005602 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5603
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005604 void MarkInDexCache() {
5605 SetPackedFlag<kFlagIsInDexCache>(true);
5606 DCHECK(!NeedsEnvironment());
5607 RemoveEnvironment();
5608 }
5609
5610 size_t InputCount() const OVERRIDE {
5611 return (InputAt(0) != nullptr) ? 1u : 0u;
5612 }
5613
5614 void AddSpecialInput(HInstruction* special_input);
5615
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005616 DECLARE_INSTRUCTION(LoadString);
5617
5618 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005619 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005620 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5621 static constexpr size_t kFieldLoadKindSize =
5622 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5623 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005624 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005625 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005626
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005627 static bool HasStringReference(LoadKind load_kind) {
5628 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5629 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5630 load_kind == LoadKind::kDexCacheViaMethod;
5631 }
5632
5633 static bool HasAddress(LoadKind load_kind) {
5634 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5635 }
5636
5637 static bool HasDexCacheReference(LoadKind load_kind) {
5638 return load_kind == LoadKind::kDexCachePcRelative;
5639 }
5640
5641 void SetLoadKindInternal(LoadKind load_kind);
5642
5643 // String index serves also as the hash code and it's also needed for slow-paths,
5644 // so it must not be overwritten with other load data.
5645 uint32_t string_index_;
5646
5647 union {
5648 struct {
5649 const DexFile* dex_file; // For string reference and dex cache reference.
5650 uint32_t dex_cache_element_index; // Only for dex cache reference.
5651 } ref;
5652 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5653 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005654
5655 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5656};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005657std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5658
5659// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5660inline const DexFile& HLoadString::GetDexFile() const {
5661 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5662 << GetLoadKind();
5663 return *load_data_.ref.dex_file;
5664}
5665
5666// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5667inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5668 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5669 return load_data_.ref.dex_cache_element_index;
5670}
5671
5672// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5673inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5674 // The special input is used for PC-relative loads on some architectures.
5675 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5676 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
5677 DCHECK(InputAt(0) == nullptr);
5678 SetRawInputAt(0u, special_input);
5679 special_input->AddUseAt(this, 0);
5680}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005681
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005682/**
5683 * Performs an initialization check on its Class object input.
5684 */
5685class HClinitCheck : public HExpression<1> {
5686 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005687 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005688 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005689 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005690 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5691 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005692 SetRawInputAt(0, constant);
5693 }
5694
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005695 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005696 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005697 return true;
5698 }
5699
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005700 bool NeedsEnvironment() const OVERRIDE {
5701 // May call runtime to initialize the class.
5702 return true;
5703 }
5704
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005705 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005706
5707 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5708
5709 DECLARE_INSTRUCTION(ClinitCheck);
5710
5711 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005712 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5713};
5714
5715class HStaticFieldGet : public HExpression<1> {
5716 public:
5717 HStaticFieldGet(HInstruction* cls,
5718 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005719 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005720 bool is_volatile,
5721 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005722 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005723 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005724 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005725 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005726 : HExpression(field_type,
5727 SideEffects::FieldReadOfType(field_type, is_volatile),
5728 dex_pc),
5729 field_info_(field_offset,
5730 field_type,
5731 is_volatile,
5732 field_idx,
5733 declaring_class_def_index,
5734 dex_file,
5735 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005736 SetRawInputAt(0, cls);
5737 }
5738
Calin Juravle52c48962014-12-16 17:02:57 +00005739
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005740 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005741
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005742 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005743 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5744 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005745 }
5746
5747 size_t ComputeHashCode() const OVERRIDE {
5748 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5749 }
5750
Calin Juravle52c48962014-12-16 17:02:57 +00005751 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005752 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5753 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005754 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005755
5756 DECLARE_INSTRUCTION(StaticFieldGet);
5757
5758 private:
5759 const FieldInfo field_info_;
5760
5761 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5762};
5763
5764class HStaticFieldSet : public HTemplateInstruction<2> {
5765 public:
5766 HStaticFieldSet(HInstruction* cls,
5767 HInstruction* value,
5768 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005769 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005770 bool is_volatile,
5771 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005772 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005773 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005774 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005775 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005776 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5777 dex_pc),
5778 field_info_(field_offset,
5779 field_type,
5780 is_volatile,
5781 field_idx,
5782 declaring_class_def_index,
5783 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005784 dex_cache) {
5785 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005786 SetRawInputAt(0, cls);
5787 SetRawInputAt(1, value);
5788 }
5789
Calin Juravle52c48962014-12-16 17:02:57 +00005790 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005791 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5792 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005793 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005794
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005795 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005796 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5797 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005798
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005799 DECLARE_INSTRUCTION(StaticFieldSet);
5800
5801 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005802 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5803 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5804 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5805 "Too many packed fields.");
5806
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005807 const FieldInfo field_info_;
5808
5809 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5810};
5811
Calin Juravlee460d1d2015-09-29 04:52:17 +01005812class HUnresolvedInstanceFieldGet : public HExpression<1> {
5813 public:
5814 HUnresolvedInstanceFieldGet(HInstruction* obj,
5815 Primitive::Type field_type,
5816 uint32_t field_index,
5817 uint32_t dex_pc)
5818 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5819 field_index_(field_index) {
5820 SetRawInputAt(0, obj);
5821 }
5822
5823 bool NeedsEnvironment() const OVERRIDE { return true; }
5824 bool CanThrow() const OVERRIDE { return true; }
5825
5826 Primitive::Type GetFieldType() const { return GetType(); }
5827 uint32_t GetFieldIndex() const { return field_index_; }
5828
5829 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5830
5831 private:
5832 const uint32_t field_index_;
5833
5834 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5835};
5836
5837class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5838 public:
5839 HUnresolvedInstanceFieldSet(HInstruction* obj,
5840 HInstruction* value,
5841 Primitive::Type field_type,
5842 uint32_t field_index,
5843 uint32_t dex_pc)
5844 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005845 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005846 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005847 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005848 SetRawInputAt(0, obj);
5849 SetRawInputAt(1, value);
5850 }
5851
5852 bool NeedsEnvironment() const OVERRIDE { return true; }
5853 bool CanThrow() const OVERRIDE { return true; }
5854
Vladimir Markoa1de9182016-02-25 11:37:38 +00005855 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005856 uint32_t GetFieldIndex() const { return field_index_; }
5857
5858 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5859
5860 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005861 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5862 static constexpr size_t kFieldFieldTypeSize =
5863 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5864 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5865 kFieldFieldType + kFieldFieldTypeSize;
5866 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5867 "Too many packed fields.");
5868 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5869
Calin Juravlee460d1d2015-09-29 04:52:17 +01005870 const uint32_t field_index_;
5871
5872 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5873};
5874
5875class HUnresolvedStaticFieldGet : public HExpression<0> {
5876 public:
5877 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5878 uint32_t field_index,
5879 uint32_t dex_pc)
5880 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5881 field_index_(field_index) {
5882 }
5883
5884 bool NeedsEnvironment() const OVERRIDE { return true; }
5885 bool CanThrow() const OVERRIDE { return true; }
5886
5887 Primitive::Type GetFieldType() const { return GetType(); }
5888 uint32_t GetFieldIndex() const { return field_index_; }
5889
5890 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5891
5892 private:
5893 const uint32_t field_index_;
5894
5895 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5896};
5897
5898class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5899 public:
5900 HUnresolvedStaticFieldSet(HInstruction* value,
5901 Primitive::Type field_type,
5902 uint32_t field_index,
5903 uint32_t dex_pc)
5904 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005905 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005906 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005907 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005908 SetRawInputAt(0, value);
5909 }
5910
5911 bool NeedsEnvironment() const OVERRIDE { return true; }
5912 bool CanThrow() const OVERRIDE { return true; }
5913
Vladimir Markoa1de9182016-02-25 11:37:38 +00005914 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005915 uint32_t GetFieldIndex() const { return field_index_; }
5916
5917 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5918
5919 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005920 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5921 static constexpr size_t kFieldFieldTypeSize =
5922 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5923 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5924 kFieldFieldType + kFieldFieldTypeSize;
5925 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5926 "Too many packed fields.");
5927 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5928
Calin Juravlee460d1d2015-09-29 04:52:17 +01005929 const uint32_t field_index_;
5930
5931 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5932};
5933
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005934// Implement the move-exception DEX instruction.
5935class HLoadException : public HExpression<0> {
5936 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005937 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5938 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005939
David Brazdilbbd733e2015-08-18 17:48:17 +01005940 bool CanBeNull() const OVERRIDE { return false; }
5941
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005942 DECLARE_INSTRUCTION(LoadException);
5943
5944 private:
5945 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5946};
5947
David Brazdilcb1c0552015-08-04 16:22:25 +01005948// Implicit part of move-exception which clears thread-local exception storage.
5949// Must not be removed because the runtime expects the TLS to get cleared.
5950class HClearException : public HTemplateInstruction<0> {
5951 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005952 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5953 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005954
5955 DECLARE_INSTRUCTION(ClearException);
5956
5957 private:
5958 DISALLOW_COPY_AND_ASSIGN(HClearException);
5959};
5960
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005961class HThrow : public HTemplateInstruction<1> {
5962 public:
5963 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005964 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005965 SetRawInputAt(0, exception);
5966 }
5967
5968 bool IsControlFlow() const OVERRIDE { return true; }
5969
5970 bool NeedsEnvironment() const OVERRIDE { return true; }
5971
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005972 bool CanThrow() const OVERRIDE { return true; }
5973
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005974
5975 DECLARE_INSTRUCTION(Throw);
5976
5977 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005978 DISALLOW_COPY_AND_ASSIGN(HThrow);
5979};
5980
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005981/**
5982 * Implementation strategies for the code generator of a HInstanceOf
5983 * or `HCheckCast`.
5984 */
5985enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005986 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005987 kExactCheck, // Can do a single class compare.
5988 kClassHierarchyCheck, // Can just walk the super class chain.
5989 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5990 kInterfaceCheck, // No optimization yet when checking against an interface.
5991 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005992 kArrayCheck, // No optimization yet when checking against a generic array.
5993 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005994};
5995
Roland Levillain86503782016-02-11 19:07:30 +00005996std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5997
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005998class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005999 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006000 HInstanceOf(HInstruction* object,
6001 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006002 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006003 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006004 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006005 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006006 dex_pc) {
6007 SetPackedField<TypeCheckKindField>(check_kind);
6008 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006009 SetRawInputAt(0, object);
6010 SetRawInputAt(1, constant);
6011 }
6012
6013 bool CanBeMoved() const OVERRIDE { return true; }
6014
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006015 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006016 return true;
6017 }
6018
6019 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006020 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006021 }
6022
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006023 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006024 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6025 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6026 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6027 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006028
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006029 static bool CanCallRuntime(TypeCheckKind check_kind) {
6030 // Mips currently does runtime calls for any other checks.
6031 return check_kind != TypeCheckKind::kExactCheck;
6032 }
6033
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006034 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006035 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006036 }
6037
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006038 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006039
6040 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006041 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6042 static constexpr size_t kFieldTypeCheckKindSize =
6043 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6044 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6045 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6046 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6047 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006048
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006049 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6050};
6051
Calin Juravleb1498f62015-02-16 13:13:29 +00006052class HBoundType : public HExpression<1> {
6053 public:
David Brazdilf5552582015-12-27 13:36:12 +00006054 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006055 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006056 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6057 SetPackedFlag<kFlagUpperCanBeNull>(true);
6058 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006059 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006060 SetRawInputAt(0, input);
6061 }
6062
David Brazdilf5552582015-12-27 13:36:12 +00006063 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006064 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006065 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006066 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006067
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006068 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006069 DCHECK(GetUpperCanBeNull() || !can_be_null);
6070 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006071 }
6072
Vladimir Markoa1de9182016-02-25 11:37:38 +00006073 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006074
Calin Juravleb1498f62015-02-16 13:13:29 +00006075 DECLARE_INSTRUCTION(BoundType);
6076
6077 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006078 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6079 // is false then CanBeNull() cannot be true).
6080 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6081 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6082 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6083 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6084
Calin Juravleb1498f62015-02-16 13:13:29 +00006085 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006086 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6087 // It is used to bound the type in cases like:
6088 // if (x instanceof ClassX) {
6089 // // uper_bound_ will be ClassX
6090 // }
David Brazdilf5552582015-12-27 13:36:12 +00006091 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006092
6093 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6094};
6095
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006096class HCheckCast : public HTemplateInstruction<2> {
6097 public:
6098 HCheckCast(HInstruction* object,
6099 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006100 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006101 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006102 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6103 SetPackedField<TypeCheckKindField>(check_kind);
6104 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006105 SetRawInputAt(0, object);
6106 SetRawInputAt(1, constant);
6107 }
6108
6109 bool CanBeMoved() const OVERRIDE { return true; }
6110
6111 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6112 return true;
6113 }
6114
6115 bool NeedsEnvironment() const OVERRIDE {
6116 // Instruction may throw a CheckCastError.
6117 return true;
6118 }
6119
6120 bool CanThrow() const OVERRIDE { return true; }
6121
Vladimir Markoa1de9182016-02-25 11:37:38 +00006122 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6123 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6124 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6125 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006126
6127 DECLARE_INSTRUCTION(CheckCast);
6128
6129 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006130 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6131 static constexpr size_t kFieldTypeCheckKindSize =
6132 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6133 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6134 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6135 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6136 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006137
6138 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006139};
6140
Calin Juravle27df7582015-04-17 19:12:31 +01006141class HMemoryBarrier : public HTemplateInstruction<0> {
6142 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006143 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006144 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006145 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6146 SetPackedField<BarrierKindField>(barrier_kind);
6147 }
Calin Juravle27df7582015-04-17 19:12:31 +01006148
Vladimir Markoa1de9182016-02-25 11:37:38 +00006149 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006150
6151 DECLARE_INSTRUCTION(MemoryBarrier);
6152
6153 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006154 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6155 static constexpr size_t kFieldBarrierKindSize =
6156 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6157 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6158 kFieldBarrierKind + kFieldBarrierKindSize;
6159 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6160 "Too many packed fields.");
6161 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006162
6163 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6164};
6165
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006166class HMonitorOperation : public HTemplateInstruction<1> {
6167 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006168 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006169 kEnter,
6170 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006171 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006172 };
6173
6174 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006175 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006176 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6177 dex_pc) {
6178 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006179 SetRawInputAt(0, object);
6180 }
6181
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006182 // Instruction may go into runtime, so we need an environment.
6183 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006184
6185 bool CanThrow() const OVERRIDE {
6186 // Verifier guarantees that monitor-exit cannot throw.
6187 // This is important because it allows the HGraphBuilder to remove
6188 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6189 return IsEnter();
6190 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006191
Vladimir Markoa1de9182016-02-25 11:37:38 +00006192 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6193 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006194
6195 DECLARE_INSTRUCTION(MonitorOperation);
6196
Calin Juravle52c48962014-12-16 17:02:57 +00006197 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006198 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6199 static constexpr size_t kFieldOperationKindSize =
6200 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6201 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6202 kFieldOperationKind + kFieldOperationKindSize;
6203 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6204 "Too many packed fields.");
6205 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006206
6207 private:
6208 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6209};
6210
David Brazdil74eb1b22015-12-14 11:44:01 +00006211class HSelect : public HExpression<3> {
6212 public:
6213 HSelect(HInstruction* condition,
6214 HInstruction* true_value,
6215 HInstruction* false_value,
6216 uint32_t dex_pc)
6217 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6218 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6219
6220 // First input must be `true_value` or `false_value` to allow codegens to
6221 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6222 // that architectures which implement HSelect as a conditional move also
6223 // will not need to invert the condition.
6224 SetRawInputAt(0, false_value);
6225 SetRawInputAt(1, true_value);
6226 SetRawInputAt(2, condition);
6227 }
6228
6229 HInstruction* GetFalseValue() const { return InputAt(0); }
6230 HInstruction* GetTrueValue() const { return InputAt(1); }
6231 HInstruction* GetCondition() const { return InputAt(2); }
6232
6233 bool CanBeMoved() const OVERRIDE { return true; }
6234 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6235
6236 bool CanBeNull() const OVERRIDE {
6237 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6238 }
6239
6240 DECLARE_INSTRUCTION(Select);
6241
6242 private:
6243 DISALLOW_COPY_AND_ASSIGN(HSelect);
6244};
6245
Vladimir Markof9f64412015-09-02 14:05:49 +01006246class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006247 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006248 MoveOperands(Location source,
6249 Location destination,
6250 Primitive::Type type,
6251 HInstruction* instruction)
6252 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006253
6254 Location GetSource() const { return source_; }
6255 Location GetDestination() const { return destination_; }
6256
6257 void SetSource(Location value) { source_ = value; }
6258 void SetDestination(Location value) { destination_ = value; }
6259
6260 // The parallel move resolver marks moves as "in-progress" by clearing the
6261 // destination (but not the source).
6262 Location MarkPending() {
6263 DCHECK(!IsPending());
6264 Location dest = destination_;
6265 destination_ = Location::NoLocation();
6266 return dest;
6267 }
6268
6269 void ClearPending(Location dest) {
6270 DCHECK(IsPending());
6271 destination_ = dest;
6272 }
6273
6274 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006275 DCHECK(source_.IsValid() || destination_.IsInvalid());
6276 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006277 }
6278
6279 // True if this blocks a move from the given location.
6280 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006281 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006282 }
6283
6284 // A move is redundant if it's been eliminated, if its source and
6285 // destination are the same, or if its destination is unneeded.
6286 bool IsRedundant() const {
6287 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6288 }
6289
6290 // We clear both operands to indicate move that's been eliminated.
6291 void Eliminate() {
6292 source_ = destination_ = Location::NoLocation();
6293 }
6294
6295 bool IsEliminated() const {
6296 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6297 return source_.IsInvalid();
6298 }
6299
Alexey Frunze4dda3372015-06-01 18:31:49 -07006300 Primitive::Type GetType() const { return type_; }
6301
Nicolas Geoffray90218252015-04-15 11:56:51 +01006302 bool Is64BitMove() const {
6303 return Primitive::Is64BitType(type_);
6304 }
6305
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006306 HInstruction* GetInstruction() const { return instruction_; }
6307
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006308 private:
6309 Location source_;
6310 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006311 // The type this move is for.
6312 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006313 // The instruction this move is assocatied with. Null when this move is
6314 // for moving an input in the expected locations of user (including a phi user).
6315 // This is only used in debug mode, to ensure we do not connect interval siblings
6316 // in the same parallel move.
6317 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006318};
6319
Roland Levillainc9285912015-12-18 10:38:42 +00006320std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6321
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006322static constexpr size_t kDefaultNumberOfMoves = 4;
6323
6324class HParallelMove : public HTemplateInstruction<0> {
6325 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006326 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006327 : HTemplateInstruction(SideEffects::None(), dex_pc),
6328 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6329 moves_.reserve(kDefaultNumberOfMoves);
6330 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006331
Nicolas Geoffray90218252015-04-15 11:56:51 +01006332 void AddMove(Location source,
6333 Location destination,
6334 Primitive::Type type,
6335 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006336 DCHECK(source.IsValid());
6337 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006338 if (kIsDebugBuild) {
6339 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006340 for (const MoveOperands& move : moves_) {
6341 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006342 // Special case the situation where the move is for the spill slot
6343 // of the instruction.
6344 if ((GetPrevious() == instruction)
6345 || ((GetPrevious() == nullptr)
6346 && instruction->IsPhi()
6347 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006348 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006349 << "Doing parallel moves for the same instruction.";
6350 } else {
6351 DCHECK(false) << "Doing parallel moves for the same instruction.";
6352 }
6353 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006354 }
6355 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006356 for (const MoveOperands& move : moves_) {
6357 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006358 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006359 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006360 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006361 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006362 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006363 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006364 }
6365
Vladimir Marko225b6462015-09-28 12:17:40 +01006366 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006367 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006368 }
6369
Vladimir Marko225b6462015-09-28 12:17:40 +01006370 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006371
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006372 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006373
6374 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006375 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006376
6377 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6378};
6379
Mark Mendell0616ae02015-04-17 12:49:27 -04006380} // namespace art
6381
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006382#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6383#include "nodes_shared.h"
6384#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006385#ifdef ART_ENABLE_CODEGEN_arm
6386#include "nodes_arm.h"
6387#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006388#ifdef ART_ENABLE_CODEGEN_arm64
6389#include "nodes_arm64.h"
6390#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006391#ifdef ART_ENABLE_CODEGEN_x86
6392#include "nodes_x86.h"
6393#endif
6394
6395namespace art {
6396
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006397class HGraphVisitor : public ValueObject {
6398 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006399 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6400 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006401
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006402 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006403 virtual void VisitBasicBlock(HBasicBlock* block);
6404
Roland Levillain633021e2014-10-01 14:12:25 +01006405 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006406 void VisitInsertionOrder();
6407
Roland Levillain633021e2014-10-01 14:12:25 +01006408 // Visit the graph following dominator tree reverse post-order.
6409 void VisitReversePostOrder();
6410
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006411 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006412
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006413 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006414#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006415 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6416
6417 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6418
6419#undef DECLARE_VISIT_INSTRUCTION
6420
6421 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006422 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006423
6424 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6425};
6426
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006427class HGraphDelegateVisitor : public HGraphVisitor {
6428 public:
6429 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6430 virtual ~HGraphDelegateVisitor() {}
6431
6432 // Visit functions that delegate to to super class.
6433#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006434 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006435
6436 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6437
6438#undef DECLARE_VISIT_INSTRUCTION
6439
6440 private:
6441 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6442};
6443
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006444class HInsertionOrderIterator : public ValueObject {
6445 public:
6446 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6447
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006448 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006449 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006450 void Advance() { ++index_; }
6451
6452 private:
6453 const HGraph& graph_;
6454 size_t index_;
6455
6456 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6457};
6458
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006459class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006460 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006461 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6462 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006463 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006464 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006465
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006466 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6467 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006468 void Advance() { ++index_; }
6469
6470 private:
6471 const HGraph& graph_;
6472 size_t index_;
6473
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006474 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006475};
6476
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006477class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006478 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006479 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006480 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006481 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006482 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006483 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006484
6485 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006486 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006487 void Advance() { --index_; }
6488
6489 private:
6490 const HGraph& graph_;
6491 size_t index_;
6492
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006493 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006494};
6495
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006496class HLinearPostOrderIterator : public ValueObject {
6497 public:
6498 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006499 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006500
6501 bool Done() const { return index_ == 0; }
6502
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006503 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006504
6505 void Advance() {
6506 --index_;
6507 DCHECK_GE(index_, 0U);
6508 }
6509
6510 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006511 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006512 size_t index_;
6513
6514 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6515};
6516
6517class HLinearOrderIterator : public ValueObject {
6518 public:
6519 explicit HLinearOrderIterator(const HGraph& graph)
6520 : order_(graph.GetLinearOrder()), index_(0) {}
6521
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006522 bool Done() const { return index_ == order_.size(); }
6523 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006524 void Advance() { ++index_; }
6525
6526 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006527 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006528 size_t index_;
6529
6530 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6531};
6532
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006533// Iterator over the blocks that art part of the loop. Includes blocks part
6534// of an inner loop. The order in which the blocks are iterated is on their
6535// block id.
6536class HBlocksInLoopIterator : public ValueObject {
6537 public:
6538 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6539 : blocks_in_loop_(info.GetBlocks()),
6540 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6541 index_(0) {
6542 if (!blocks_in_loop_.IsBitSet(index_)) {
6543 Advance();
6544 }
6545 }
6546
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006547 bool Done() const { return index_ == blocks_.size(); }
6548 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006549 void Advance() {
6550 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006551 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006552 if (blocks_in_loop_.IsBitSet(index_)) {
6553 break;
6554 }
6555 }
6556 }
6557
6558 private:
6559 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006560 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006561 size_t index_;
6562
6563 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6564};
6565
Mingyao Yang3584bce2015-05-19 16:01:59 -07006566// Iterator over the blocks that art part of the loop. Includes blocks part
6567// of an inner loop. The order in which the blocks are iterated is reverse
6568// post order.
6569class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6570 public:
6571 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6572 : blocks_in_loop_(info.GetBlocks()),
6573 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6574 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006575 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006576 Advance();
6577 }
6578 }
6579
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006580 bool Done() const { return index_ == blocks_.size(); }
6581 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006582 void Advance() {
6583 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006584 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6585 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006586 break;
6587 }
6588 }
6589 }
6590
6591 private:
6592 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006593 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006594 size_t index_;
6595
6596 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6597};
6598
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006599inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006600 if (constant->IsIntConstant()) {
6601 return constant->AsIntConstant()->GetValue();
6602 } else if (constant->IsLongConstant()) {
6603 return constant->AsLongConstant()->GetValue();
6604 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006605 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006606 return 0;
6607 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006608}
6609
Vladimir Marko58155012015-08-19 12:49:41 +00006610inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6611 // For the purposes of the compiler, the dex files must actually be the same object
6612 // if we want to safely treat them as the same. This is especially important for JIT
6613 // as custom class loaders can open the same underlying file (or memory) multiple
6614 // times and provide different class resolution but no two class loaders should ever
6615 // use the same DexFile object - doing so is an unsupported hack that can lead to
6616 // all sorts of weird failures.
6617 return &lhs == &rhs;
6618}
6619
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006620#define INSTRUCTION_TYPE_CHECK(type, super) \
6621 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6622 inline const H##type* HInstruction::As##type() const { \
6623 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6624 } \
6625 inline H##type* HInstruction::As##type() { \
6626 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6627 }
6628
6629 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6630#undef INSTRUCTION_TYPE_CHECK
6631
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006632// Create space in `blocks` for adding `number_of_new_blocks` entries
6633// starting at location `at`. Blocks after `at` are moved accordingly.
6634inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6635 size_t number_of_new_blocks,
6636 size_t after) {
6637 DCHECK_LT(after, blocks->size());
6638 size_t old_size = blocks->size();
6639 size_t new_size = old_size + number_of_new_blocks;
6640 blocks->resize(new_size);
6641 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6642}
6643
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006644} // namespace art
6645
6646#endif // ART_COMPILER_OPTIMIZING_NODES_H_