blob: 8a2e83a9eff623603fe267ba70e25595a565f176 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010028#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000029#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "handle.h"
31#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "primitive.h"
David Brazdild26a4112015-11-10 11:07:31 +000038#include "utils/array_ref.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000039
40namespace art {
41
David Brazdil1abb4192015-02-17 18:33:36 +000042class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010046class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000048class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000051class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000052class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000054class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010056class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010057class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000059class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010060class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062
Mathieu Chartier736b5602015-09-02 14:54:11 -070063namespace mirror {
64class DexCache;
65} // namespace mirror
66
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067static const int kDefaultNumberOfBlocks = 8;
68static const int kDefaultNumberOfSuccessors = 2;
69static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010070static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010071static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000072static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Roland Levillain5b5b9312016-03-22 14:57:31 +000074// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
75static constexpr int32_t kMaxIntShiftDistance = 0x1f;
76// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
77static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000078
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010079static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070080static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010081
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010082static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
83
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060084static constexpr uint32_t kNoDexPc = -1;
85
Dave Allison20dfc792014-06-16 20:44:29 -070086enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070087 // All types.
88 kCondEQ, // ==
89 kCondNE, // !=
90 // Signed integers and floating-point numbers.
91 kCondLT, // <
92 kCondLE, // <=
93 kCondGT, // >
94 kCondGE, // >=
95 // Unsigned integers.
96 kCondB, // <
97 kCondBE, // <=
98 kCondA, // >
99 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -0700100};
101
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000102enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000103 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000104 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000105 kAnalysisFailThrowCatchLoop,
106 kAnalysisFailAmbiguousArrayOp,
107 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000108};
109
Vladimir Markof9f64412015-09-02 14:05:49 +0100110class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100111 public:
112 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
113
114 void AddInstruction(HInstruction* instruction);
115 void RemoveInstruction(HInstruction* instruction);
116
David Brazdilc3d743f2015-04-22 13:40:50 +0100117 // Insert `instruction` before/after an existing instruction `cursor`.
118 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
119 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
120
Roland Levillain6b469232014-09-25 10:10:38 +0100121 // Return true if this list contains `instruction`.
122 bool Contains(HInstruction* instruction) const;
123
Roland Levillainccc07a92014-09-16 14:48:16 +0100124 // Return true if `instruction1` is found before `instruction2` in
125 // this instruction list and false otherwise. Abort if none
126 // of these instructions is found.
127 bool FoundBefore(const HInstruction* instruction1,
128 const HInstruction* instruction2) const;
129
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000130 bool IsEmpty() const { return first_instruction_ == nullptr; }
131 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
132
133 // Update the block of all instructions to be `block`.
134 void SetBlockOfInstructions(HBasicBlock* block) const;
135
136 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000137 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000138 void Add(const HInstructionList& instruction_list);
139
David Brazdil2d7352b2015-04-20 14:52:42 +0100140 // Return the number of instructions in the list. This is an expensive operation.
141 size_t CountSize() const;
142
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100143 private:
144 HInstruction* first_instruction_;
145 HInstruction* last_instruction_;
146
147 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000148 friend class HGraph;
149 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100150 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100151 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100152
153 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
154};
155
David Brazdil4833f5a2015-12-16 10:37:39 +0000156class ReferenceTypeInfo : ValueObject {
157 public:
158 typedef Handle<mirror::Class> TypeHandle;
159
Vladimir Markoa1de9182016-02-25 11:37:38 +0000160 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
161
162 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000163 return ReferenceTypeInfo(type_handle, is_exact);
164 }
165
166 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
167
Vladimir Markof39745e2016-01-26 12:16:55 +0000168 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000169 return handle.GetReference() != nullptr;
170 }
171
172 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
173 return IsValidHandle(type_handle_);
174 }
175
176 bool IsExact() const { return is_exact_; }
177
178 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
179 DCHECK(IsValid());
180 return GetTypeHandle()->IsObjectClass();
181 }
182
183 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
184 DCHECK(IsValid());
185 return GetTypeHandle()->IsStringClass();
186 }
187
188 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
189 DCHECK(IsValid());
190 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
191 }
192
193 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
194 DCHECK(IsValid());
195 return GetTypeHandle()->IsInterface();
196 }
197
198 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
199 DCHECK(IsValid());
200 return GetTypeHandle()->IsArrayClass();
201 }
202
203 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
204 DCHECK(IsValid());
205 return GetTypeHandle()->IsPrimitiveArray();
206 }
207
208 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
209 DCHECK(IsValid());
210 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
211 }
212
213 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
214 DCHECK(IsValid());
215 if (!IsExact()) return false;
216 if (!IsArrayClass()) return false;
217 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
218 }
219
220 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
221 DCHECK(IsValid());
222 if (!IsExact()) return false;
223 if (!IsArrayClass()) return false;
224 if (!rti.IsArrayClass()) return false;
225 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
226 rti.GetTypeHandle()->GetComponentType());
227 }
228
229 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
230
231 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
232 DCHECK(IsValid());
233 DCHECK(rti.IsValid());
234 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
235 }
236
237 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
238 DCHECK(IsValid());
239 DCHECK(rti.IsValid());
240 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
241 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
242 }
243
244 // Returns true if the type information provide the same amount of details.
245 // Note that it does not mean that the instructions have the same actual type
246 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000247 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000248 if (!IsValid() && !rti.IsValid()) {
249 // Invalid types are equal.
250 return true;
251 }
252 if (!IsValid() || !rti.IsValid()) {
253 // One is valid, the other not.
254 return false;
255 }
256 return IsExact() == rti.IsExact()
257 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
258 }
259
260 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000261 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
262 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
263 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000264
265 // The class of the object.
266 TypeHandle type_handle_;
267 // Whether or not the type is exact or a superclass of the actual type.
268 // Whether or not we have any information about this type.
269 bool is_exact_;
270};
271
272std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
273
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000274// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100275class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000276 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100277 HGraph(ArenaAllocator* arena,
278 const DexFile& dex_file,
279 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100280 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700281 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100282 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100283 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000284 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100285 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000286 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100287 blocks_(arena->Adapter(kArenaAllocBlockList)),
288 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
289 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700290 entry_block_(nullptr),
291 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100292 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100293 number_of_vregs_(0),
294 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000295 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400296 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000297 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000298 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000299 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000300 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100301 dex_file_(dex_file),
302 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100303 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100304 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100305 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700306 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000307 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100308 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
309 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
310 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
311 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000312 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000313 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
314 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100315 blocks_.reserve(kDefaultNumberOfBlocks);
316 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000317
David Brazdilbadd8262016-02-02 16:28:56 +0000318 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
319 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
320
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000321 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100322 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
323
David Brazdil69ba7b72015-06-23 18:27:30 +0100324 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000325 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100326
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000327 HBasicBlock* GetEntryBlock() const { return entry_block_; }
328 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100329 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000330
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000331 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
332 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000333
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000334 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100335
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100336 void ComputeDominanceInformation();
337 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000338 void ClearLoopInformation();
339 void FindBackEdges(ArenaBitVector* visited);
340 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100341 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100342 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000343
David Brazdil4833f5a2015-12-16 10:37:39 +0000344 // Analyze all natural loops in this graph. Returns a code specifying that it
345 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000346 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000347 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100348
David Brazdilffee3d32015-07-06 11:48:53 +0100349 // Iterate over blocks to compute try block membership. Needs reverse post
350 // order and loop information.
351 void ComputeTryBlockInformation();
352
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000353 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000354 // Returns the instruction to replace the invoke expression or null if the
355 // invoke is for a void method. Note that the caller is responsible for replacing
356 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000357 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000358
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000359 // Update the loop and try membership of `block`, which was spawned from `reference`.
360 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
361 // should be the new back edge.
362 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
363 HBasicBlock* reference,
364 bool replace_if_back_edge);
365
Mingyao Yang3584bce2015-05-19 16:01:59 -0700366 // Need to add a couple of blocks to test if the loop body is entered and
367 // put deoptimization instructions, etc.
368 void TransformLoopHeaderForBCE(HBasicBlock* header);
369
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000370 // Removes `block` from the graph. Assumes `block` has been disconnected from
371 // other blocks and has no instructions or phis.
372 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000373
David Brazdilfc6a86a2015-06-26 10:33:45 +0000374 // Splits the edge between `block` and `successor` while preserving the
375 // indices in the predecessor/successor lists. If there are multiple edges
376 // between the blocks, the lowest indices are used.
377 // Returns the new block which is empty and has the same dex pc as `successor`.
378 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
379
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100380 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
381 void SimplifyLoop(HBasicBlock* header);
382
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000383 int32_t GetNextInstructionId() {
384 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000385 return current_instruction_id_++;
386 }
387
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000388 int32_t GetCurrentInstructionId() const {
389 return current_instruction_id_;
390 }
391
392 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000393 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000394 current_instruction_id_ = id;
395 }
396
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100397 uint16_t GetMaximumNumberOfOutVRegs() const {
398 return maximum_number_of_out_vregs_;
399 }
400
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000401 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
402 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100403 }
404
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100405 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
406 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
407 }
408
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000409 void UpdateTemporariesVRegSlots(size_t slots) {
410 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100411 }
412
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000413 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100414 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000415 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100416 }
417
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100418 void SetNumberOfVRegs(uint16_t number_of_vregs) {
419 number_of_vregs_ = number_of_vregs;
420 }
421
422 uint16_t GetNumberOfVRegs() const {
423 return number_of_vregs_;
424 }
425
426 void SetNumberOfInVRegs(uint16_t value) {
427 number_of_in_vregs_ = value;
428 }
429
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100430 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100431 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100432 return number_of_vregs_ - number_of_in_vregs_;
433 }
434
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100435 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100436 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100437 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100438
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100439 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100440 return linear_order_;
441 }
442
Mark Mendell1152c922015-04-24 17:06:35 -0400443 bool HasBoundsChecks() const {
444 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800445 }
446
Mark Mendell1152c922015-04-24 17:06:35 -0400447 void SetHasBoundsChecks(bool value) {
448 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800449 }
450
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100451 bool ShouldGenerateConstructorBarrier() const {
452 return should_generate_constructor_barrier_;
453 }
454
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000455 bool IsDebuggable() const { return debuggable_; }
456
David Brazdil8d5b8b22015-03-24 10:51:52 +0000457 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000458 // already, it is created and inserted into the graph. This method is only for
459 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600460 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000461
462 // TODO: This is problematic for the consistency of reference type propagation
463 // because it can be created anytime after the pass and thus it will be left
464 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600465 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000466
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600467 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
468 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000469 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600470 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
471 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000472 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600473 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
474 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000475 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600476 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
477 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000478 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000479
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100480 HCurrentMethod* GetCurrentMethod();
481
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100482 const DexFile& GetDexFile() const {
483 return dex_file_;
484 }
485
486 uint32_t GetMethodIdx() const {
487 return method_idx_;
488 }
489
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100490 InvokeType GetInvokeType() const {
491 return invoke_type_;
492 }
493
Mark Mendellc4701932015-04-10 13:18:51 -0400494 InstructionSet GetInstructionSet() const {
495 return instruction_set_;
496 }
497
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000498 bool IsCompilingOsr() const { return osr_; }
499
David Brazdil77a48ae2015-09-15 12:34:04 +0000500 bool HasTryCatch() const { return has_try_catch_; }
501 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100502
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000503 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
504 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
505
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100506 ArtMethod* GetArtMethod() const { return art_method_; }
507 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
508
Mark Mendellf6529172015-11-17 11:16:56 -0500509 // Returns an instruction with the opposite boolean value from 'cond'.
510 // The instruction has been inserted into the graph, either as a constant, or
511 // before cursor.
512 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
513
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000514 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
515
David Brazdil2d7352b2015-04-20 14:52:42 +0100516 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000517 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100518 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000519
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000520 template <class InstructionType, typename ValueType>
521 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600522 ArenaSafeMap<ValueType, InstructionType*>* cache,
523 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000524 // Try to find an existing constant of the given value.
525 InstructionType* constant = nullptr;
526 auto cached_constant = cache->find(value);
527 if (cached_constant != cache->end()) {
528 constant = cached_constant->second;
529 }
530
531 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100532 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000533 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600534 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000535 cache->Overwrite(value, constant);
536 InsertConstant(constant);
537 }
538 return constant;
539 }
540
David Brazdil8d5b8b22015-03-24 10:51:52 +0000541 void InsertConstant(HConstant* instruction);
542
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000543 // Cache a float constant into the graph. This method should only be
544 // called by the SsaBuilder when creating "equivalent" instructions.
545 void CacheFloatConstant(HFloatConstant* constant);
546
547 // See CacheFloatConstant comment.
548 void CacheDoubleConstant(HDoubleConstant* constant);
549
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000550 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000551
552 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100553 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000554
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100555 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100556 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000557
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100558 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100559 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100560
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000561 HBasicBlock* entry_block_;
562 HBasicBlock* exit_block_;
563
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100564 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100565 uint16_t maximum_number_of_out_vregs_;
566
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100567 // The number of virtual registers in this method. Contains the parameters.
568 uint16_t number_of_vregs_;
569
570 // The number of virtual registers used by parameters of this method.
571 uint16_t number_of_in_vregs_;
572
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000573 // Number of vreg size slots that the temporaries use (used in baseline compiler).
574 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100575
Mark Mendell1152c922015-04-24 17:06:35 -0400576 // Has bounds checks. We can totally skip BCE if it's false.
577 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800578
David Brazdil77a48ae2015-09-15 12:34:04 +0000579 // Flag whether there are any try/catch blocks in the graph. We will skip
580 // try/catch-related passes if false.
581 bool has_try_catch_;
582
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000583 // Flag whether there are any irreducible loops in the graph.
584 bool has_irreducible_loops_;
585
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000586 // Indicates whether the graph should be compiled in a way that
587 // ensures full debuggability. If false, we can apply more
588 // aggressive optimizations that may limit the level of debugging.
589 const bool debuggable_;
590
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000591 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000592 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000593
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100594 // The dex file from which the method is from.
595 const DexFile& dex_file_;
596
597 // The method index in the dex file.
598 const uint32_t method_idx_;
599
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100600 // If inlined, this encodes how the callee is being invoked.
601 const InvokeType invoke_type_;
602
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100603 // Whether the graph has been transformed to SSA form. Only used
604 // in debug mode to ensure we are not using properties only valid
605 // for non-SSA form (like the number of temporaries).
606 bool in_ssa_form_;
607
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100608 const bool should_generate_constructor_barrier_;
609
Mathieu Chartiere401d142015-04-22 13:56:20 -0700610 const InstructionSet instruction_set_;
611
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000612 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000613 HNullConstant* cached_null_constant_;
614 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000615 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000616 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000617 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000618
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100619 HCurrentMethod* cached_current_method_;
620
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100621 // The ArtMethod this graph is for. Note that for AOT, it may be null,
622 // for example for methods whose declaring class could not be resolved
623 // (such as when the superclass could not be found).
624 ArtMethod* art_method_;
625
David Brazdil4833f5a2015-12-16 10:37:39 +0000626 // Keep the RTI of inexact Object to avoid having to pass stack handle
627 // collection pointer to passes which may create NullConstant.
628 ReferenceTypeInfo inexact_object_rti_;
629
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000630 // Whether we are compiling this graph for on stack replacement: this will
631 // make all loops seen as irreducible and emit special stack maps to mark
632 // compiled code entries which the interpreter can directly jump to.
633 const bool osr_;
634
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000635 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100636 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000637 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000638 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000639 DISALLOW_COPY_AND_ASSIGN(HGraph);
640};
641
Vladimir Markof9f64412015-09-02 14:05:49 +0100642class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000643 public:
644 HLoopInformation(HBasicBlock* header, HGraph* graph)
645 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100646 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000647 irreducible_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100648 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100649 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000650 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100651 back_edges_.reserve(kDefaultNumberOfBackEdges);
652 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100653
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000654 bool IsIrreducible() const { return irreducible_; }
655
656 void Dump(std::ostream& os);
657
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100658 HBasicBlock* GetHeader() const {
659 return header_;
660 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000661
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000662 void SetHeader(HBasicBlock* block) {
663 header_ = block;
664 }
665
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100666 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
667 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
668 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
669
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000670 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100671 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000672 }
673
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100674 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100675 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100676 }
677
David Brazdil46e2a392015-03-16 17:31:52 +0000678 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100679 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100680 }
681
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000682 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100683 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000684 }
685
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100686 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100687
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100688 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100689 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100690 }
691
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100692 // Returns the lifetime position of the back edge that has the
693 // greatest lifetime position.
694 size_t GetLifetimeEnd() const;
695
696 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100697 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100698 }
699
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000700 // Finds blocks that are part of this loop.
701 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100702
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100703 // Returns whether this loop information contains `block`.
704 // Note that this loop information *must* be populated before entering this function.
705 bool Contains(const HBasicBlock& block) const;
706
707 // Returns whether this loop information is an inner loop of `other`.
708 // Note that `other` *must* be populated before entering this function.
709 bool IsIn(const HLoopInformation& other) const;
710
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800711 // Returns true if instruction is not defined within this loop.
712 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700713
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100714 const ArenaBitVector& GetBlocks() const { return blocks_; }
715
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000716 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000717 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000718
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000719 void ClearAllBlocks() {
720 blocks_.ClearAllBits();
721 }
722
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000723 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100724 // Internal recursive implementation of `Populate`.
725 void PopulateRecursive(HBasicBlock* block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000726 void PopulateIrreducibleRecursive(HBasicBlock* block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100727
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000728 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100729 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000730 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100731 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100732 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000733
734 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
735};
736
David Brazdilec16f792015-08-19 15:04:01 +0100737// Stores try/catch information for basic blocks.
738// Note that HGraph is constructed so that catch blocks cannot simultaneously
739// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100740class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100741 public:
742 // Try block information constructor.
743 explicit TryCatchInformation(const HTryBoundary& try_entry)
744 : try_entry_(&try_entry),
745 catch_dex_file_(nullptr),
746 catch_type_index_(DexFile::kDexNoIndex16) {
747 DCHECK(try_entry_ != nullptr);
748 }
749
750 // Catch block information constructor.
751 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
752 : try_entry_(nullptr),
753 catch_dex_file_(&dex_file),
754 catch_type_index_(catch_type_index) {}
755
756 bool IsTryBlock() const { return try_entry_ != nullptr; }
757
758 const HTryBoundary& GetTryEntry() const {
759 DCHECK(IsTryBlock());
760 return *try_entry_;
761 }
762
763 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
764
765 bool IsCatchAllTypeIndex() const {
766 DCHECK(IsCatchBlock());
767 return catch_type_index_ == DexFile::kDexNoIndex16;
768 }
769
770 uint16_t GetCatchTypeIndex() const {
771 DCHECK(IsCatchBlock());
772 return catch_type_index_;
773 }
774
775 const DexFile& GetCatchDexFile() const {
776 DCHECK(IsCatchBlock());
777 return *catch_dex_file_;
778 }
779
780 private:
781 // One of possibly several TryBoundary instructions entering the block's try.
782 // Only set for try blocks.
783 const HTryBoundary* try_entry_;
784
785 // Exception type information. Only set for catch blocks.
786 const DexFile* catch_dex_file_;
787 const uint16_t catch_type_index_;
788};
789
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100790static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100791static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100792
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000793// A block in a method. Contains the list of instructions represented
794// as a double linked list. Each block knows its predecessors and
795// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100796
Vladimir Markof9f64412015-09-02 14:05:49 +0100797class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000798 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600799 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000800 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000801 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
802 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000803 loop_information_(nullptr),
804 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000805 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100806 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100807 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100808 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000809 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000810 try_catch_information_(nullptr) {
811 predecessors_.reserve(kDefaultNumberOfPredecessors);
812 successors_.reserve(kDefaultNumberOfSuccessors);
813 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
814 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000815
Vladimir Marko60584552015-09-03 13:35:12 +0000816 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100817 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000818 }
819
Vladimir Marko60584552015-09-03 13:35:12 +0000820 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100821 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000822 }
823
David Brazdild26a4112015-11-10 11:07:31 +0000824 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
825 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
826
Vladimir Marko60584552015-09-03 13:35:12 +0000827 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
828 return ContainsElement(successors_, block, start_from);
829 }
830
831 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100832 return dominated_blocks_;
833 }
834
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100835 bool IsEntryBlock() const {
836 return graph_->GetEntryBlock() == this;
837 }
838
839 bool IsExitBlock() const {
840 return graph_->GetExitBlock() == this;
841 }
842
David Brazdil46e2a392015-03-16 17:31:52 +0000843 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000844 bool IsSingleTryBoundary() const;
845
846 // Returns true if this block emits nothing but a jump.
847 bool IsSingleJump() const {
848 HLoopInformation* loop_info = GetLoopInformation();
849 return (IsSingleGoto() || IsSingleTryBoundary())
850 // Back edges generate a suspend check.
851 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
852 }
David Brazdil46e2a392015-03-16 17:31:52 +0000853
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000854 void AddBackEdge(HBasicBlock* back_edge) {
855 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000856 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000857 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100858 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000859 loop_information_->AddBackEdge(back_edge);
860 }
861
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000862 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000863 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000864
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100865 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000866 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600867 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000868
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000869 HBasicBlock* GetDominator() const { return dominator_; }
870 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000871 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
872
873 void RemoveDominatedBlock(HBasicBlock* block) {
874 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100875 }
Vladimir Marko60584552015-09-03 13:35:12 +0000876
877 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
878 ReplaceElement(dominated_blocks_, existing, new_block);
879 }
880
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100881 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000882
883 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100884 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000885 }
886
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100887 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
888 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100889 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100890 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100891 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
892 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000893
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000894 HInstruction* GetFirstInstructionDisregardMoves() const;
895
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000896 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000897 successors_.push_back(block);
898 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000899 }
900
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100901 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
902 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100903 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000904 new_block->predecessors_.push_back(this);
905 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000906 }
907
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000908 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
909 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000910 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000911 new_block->successors_.push_back(this);
912 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000913 }
914
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100915 // Insert `this` between `predecessor` and `successor. This method
916 // preserves the indicies, and will update the first edge found between
917 // `predecessor` and `successor`.
918 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
919 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100920 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000921 successor->predecessors_[predecessor_index] = this;
922 predecessor->successors_[successor_index] = this;
923 successors_.push_back(successor);
924 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100925 }
926
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100927 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000928 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100929 }
930
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000931 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000932 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000933 }
934
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100935 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000936 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100937 }
938
939 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000940 predecessors_.push_back(block);
941 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100942 }
943
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100944 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000945 DCHECK_EQ(predecessors_.size(), 2u);
946 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100947 }
948
David Brazdil769c9e52015-04-27 13:54:09 +0100949 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000950 DCHECK_EQ(successors_.size(), 2u);
951 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100952 }
953
David Brazdilfc6a86a2015-06-26 10:33:45 +0000954 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000955 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100956 }
957
David Brazdilfc6a86a2015-06-26 10:33:45 +0000958 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000959 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100960 }
961
David Brazdilfc6a86a2015-06-26 10:33:45 +0000962 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000963 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100964 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000965 }
966
967 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000968 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100969 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000970 }
971
972 // Returns whether the first occurrence of `predecessor` in the list of
973 // predecessors is at index `idx`.
974 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100975 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000976 return GetPredecessorIndexOf(predecessor) == idx;
977 }
978
David Brazdild7558da2015-09-22 13:04:14 +0100979 // Create a new block between this block and its predecessors. The new block
980 // is added to the graph, all predecessor edges are relinked to it and an edge
981 // is created to `this`. Returns the new empty block. Reverse post order or
982 // loop and try/catch information are not updated.
983 HBasicBlock* CreateImmediateDominator();
984
David Brazdilfc6a86a2015-06-26 10:33:45 +0000985 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100986 // created, latter block. Note that this method will add the block to the
987 // graph, create a Goto at the end of the former block and will create an edge
988 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100989 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000990 HBasicBlock* SplitBefore(HInstruction* cursor);
991
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000992 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000993 // created block. Note that this method just updates raw block information,
994 // like predecessors, successors, dominators, and instruction list. It does not
995 // update the graph, reverse post order, loop information, nor make sure the
996 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000997 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
998
999 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1000 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001001
1002 // Merge `other` at the end of `this`. Successors and dominated blocks of
1003 // `other` are changed to be successors and dominated blocks of `this`. Note
1004 // that this method does not update the graph, reverse post order, loop
1005 // information, nor make sure the blocks are consistent (for example ending
1006 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001007 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001008
1009 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1010 // of `this` are moved to `other`.
1011 // Note that this method does not update the graph, reverse post order, loop
1012 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001013 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001014 void ReplaceWith(HBasicBlock* other);
1015
David Brazdil2d7352b2015-04-20 14:52:42 +01001016 // Merge `other` at the end of `this`. This method updates loops, reverse post
1017 // order, links to predecessors, successors, dominators and deletes the block
1018 // from the graph. The two blocks must be successive, i.e. `this` the only
1019 // predecessor of `other` and vice versa.
1020 void MergeWith(HBasicBlock* other);
1021
1022 // Disconnects `this` from all its predecessors, successors and dominator,
1023 // removes it from all loops it is included in and eventually from the graph.
1024 // The block must not dominate any other block. Predecessors and successors
1025 // are safely updated.
1026 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001027
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001028 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001029 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001030 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001031 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001032 // Replace instruction `initial` with `replacement` within this block.
1033 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1034 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001035 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001036 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001037 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001038 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1039 // instruction list. With 'ensure_safety' set to true, it verifies that the
1040 // instruction is not in use and removes it from the use lists of its inputs.
1041 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1042 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001043 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001044
1045 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001046 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001047 }
1048
Roland Levillain6b879dd2014-09-22 17:13:44 +01001049 bool IsLoopPreHeaderFirstPredecessor() const {
1050 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001051 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001052 }
1053
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001054 bool IsFirstPredecessorBackEdge() const {
1055 DCHECK(IsLoopHeader());
1056 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1057 }
1058
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001059 HLoopInformation* GetLoopInformation() const {
1060 return loop_information_;
1061 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001062
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001063 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001064 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001065 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001066 void SetInLoop(HLoopInformation* info) {
1067 if (IsLoopHeader()) {
1068 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001069 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001070 loop_information_ = info;
1071 } else if (loop_information_->Contains(*info->GetHeader())) {
1072 // Block is currently part of an outer loop. Make it part of this inner loop.
1073 // Note that a non loop header having a loop information means this loop information
1074 // has already been populated
1075 loop_information_ = info;
1076 } else {
1077 // Block is part of an inner loop. Do not update the loop information.
1078 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1079 // at this point, because this method is being called while populating `info`.
1080 }
1081 }
1082
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001083 // Raw update of the loop information.
1084 void SetLoopInformation(HLoopInformation* info) {
1085 loop_information_ = info;
1086 }
1087
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001088 bool IsInLoop() const { return loop_information_ != nullptr; }
1089
David Brazdilec16f792015-08-19 15:04:01 +01001090 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1091
1092 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1093 try_catch_information_ = try_catch_information;
1094 }
1095
1096 bool IsTryBlock() const {
1097 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1098 }
1099
1100 bool IsCatchBlock() const {
1101 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1102 }
David Brazdilffee3d32015-07-06 11:48:53 +01001103
1104 // Returns the try entry that this block's successors should have. They will
1105 // be in the same try, unless the block ends in a try boundary. In that case,
1106 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001107 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001108
David Brazdild7558da2015-09-22 13:04:14 +01001109 bool HasThrowingInstructions() const;
1110
David Brazdila4b8c212015-05-07 09:59:30 +01001111 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001112 bool Dominates(HBasicBlock* block) const;
1113
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001114 size_t GetLifetimeStart() const { return lifetime_start_; }
1115 size_t GetLifetimeEnd() const { return lifetime_end_; }
1116
1117 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1118 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1119
David Brazdil8d5b8b22015-03-24 10:51:52 +00001120 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001121 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001122 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001123 bool HasSinglePhi() const;
1124
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001125 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001126 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001127 ArenaVector<HBasicBlock*> predecessors_;
1128 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001129 HInstructionList instructions_;
1130 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001131 HLoopInformation* loop_information_;
1132 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001133 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001134 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001135 // The dex program counter of the first instruction of this block.
1136 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001137 size_t lifetime_start_;
1138 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001139 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001140
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001141 friend class HGraph;
1142 friend class HInstruction;
1143
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001144 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1145};
1146
David Brazdilb2bd1c52015-03-25 11:17:37 +00001147// Iterates over the LoopInformation of all loops which contain 'block'
1148// from the innermost to the outermost.
1149class HLoopInformationOutwardIterator : public ValueObject {
1150 public:
1151 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1152 : current_(block.GetLoopInformation()) {}
1153
1154 bool Done() const { return current_ == nullptr; }
1155
1156 void Advance() {
1157 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001158 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001159 }
1160
1161 HLoopInformation* Current() const {
1162 DCHECK(!Done());
1163 return current_;
1164 }
1165
1166 private:
1167 HLoopInformation* current_;
1168
1169 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1170};
1171
Alexandre Ramesef20f712015-06-09 10:29:30 +01001172#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001173 M(Above, Condition) \
1174 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001175 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001176 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001177 M(ArrayGet, Instruction) \
1178 M(ArrayLength, Instruction) \
1179 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001180 M(Below, Condition) \
1181 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001182 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001183 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001184 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001185 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001186 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001187 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001188 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001189 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001190 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001191 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001192 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001193 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001194 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001195 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001196 M(Exit, Instruction) \
1197 M(FloatConstant, Constant) \
1198 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001199 M(GreaterThan, Condition) \
1200 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001201 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001202 M(InstanceFieldGet, Instruction) \
1203 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001204 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001205 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001206 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001207 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001208 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001209 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001210 M(LessThan, Condition) \
1211 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001212 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001213 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001214 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001215 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001216 M(Local, Instruction) \
1217 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001218 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001219 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001220 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001221 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001222 M(Neg, UnaryOperation) \
1223 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001224 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001225 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001226 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001227 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001228 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001229 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001230 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001231 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001232 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001233 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001234 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001235 M(Return, Instruction) \
1236 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001237 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001238 M(Shl, BinaryOperation) \
1239 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001240 M(StaticFieldGet, Instruction) \
1241 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001242 M(UnresolvedInstanceFieldGet, Instruction) \
1243 M(UnresolvedInstanceFieldSet, Instruction) \
1244 M(UnresolvedStaticFieldGet, Instruction) \
1245 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001246 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001247 M(StoreLocal, Instruction) \
1248 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001249 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001250 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001251 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001252 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001253 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001254 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001255
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001256/*
1257 * Instructions, shared across several (not all) architectures.
1258 */
1259#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1260#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1261#else
1262#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001263 M(BitwiseNegatedRight, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001264 M(MultiplyAccumulate, Instruction)
1265#endif
1266
Vladimir Markob4536b72015-11-24 13:45:23 +00001267#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001268#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001269#else
1270#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1271 M(ArmDexCacheArraysBase, Instruction)
1272#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001273
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001274#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001275#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001276#else
1277#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001278 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001279 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001280#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001281
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001282#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1283
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001284#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1285
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001286#ifndef ART_ENABLE_CODEGEN_x86
1287#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1288#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001289#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1290 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001291 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001292 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001293 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001294#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001295
1296#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1297
1298#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1299 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001300 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001301 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1302 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001303 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001304 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001305 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1306 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1307
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001308#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1309 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001310 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001311 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001312 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001313 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001314
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001315#define FOR_EACH_INSTRUCTION(M) \
1316 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1317 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1318
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001319#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001320FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1321#undef FORWARD_DECLARATION
1322
Roland Levillainccc07a92014-09-16 14:48:16 +01001323#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001324 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001325 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001326 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001327 return other->Is##type(); \
1328 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001329 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001330
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001331#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1332 bool Is##type() const { return As##type() != nullptr; } \
1333 const H##type* As##type() const { return this; } \
1334 H##type* As##type() { return this; }
1335
David Brazdiled596192015-01-23 10:39:45 +00001336template <typename T> class HUseList;
1337
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001338template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001339class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001340 public:
David Brazdiled596192015-01-23 10:39:45 +00001341 HUseListNode* GetPrevious() const { return prev_; }
1342 HUseListNode* GetNext() const { return next_; }
1343 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001344 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001345 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001346
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001347 private:
David Brazdiled596192015-01-23 10:39:45 +00001348 HUseListNode(T user, size_t index)
1349 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1350
1351 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001352 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001353 HUseListNode<T>* prev_;
1354 HUseListNode<T>* next_;
1355
1356 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001357
1358 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1359};
1360
David Brazdiled596192015-01-23 10:39:45 +00001361template <typename T>
1362class HUseList : public ValueObject {
1363 public:
1364 HUseList() : first_(nullptr) {}
1365
1366 void Clear() {
1367 first_ = nullptr;
1368 }
1369
1370 // Adds a new entry at the beginning of the use list and returns
1371 // the newly created node.
1372 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001373 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001374 if (IsEmpty()) {
1375 first_ = new_node;
1376 } else {
1377 first_->prev_ = new_node;
1378 new_node->next_ = first_;
1379 first_ = new_node;
1380 }
1381 return new_node;
1382 }
1383
1384 HUseListNode<T>* GetFirst() const {
1385 return first_;
1386 }
1387
1388 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001389 DCHECK(node != nullptr);
1390 DCHECK(Contains(node));
1391
David Brazdiled596192015-01-23 10:39:45 +00001392 if (node->prev_ != nullptr) {
1393 node->prev_->next_ = node->next_;
1394 }
1395 if (node->next_ != nullptr) {
1396 node->next_->prev_ = node->prev_;
1397 }
1398 if (node == first_) {
1399 first_ = node->next_;
1400 }
1401 }
1402
David Brazdil1abb4192015-02-17 18:33:36 +00001403 bool Contains(const HUseListNode<T>* node) const {
1404 if (node == nullptr) {
1405 return false;
1406 }
1407 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1408 if (current == node) {
1409 return true;
1410 }
1411 }
1412 return false;
1413 }
1414
David Brazdiled596192015-01-23 10:39:45 +00001415 bool IsEmpty() const {
1416 return first_ == nullptr;
1417 }
1418
1419 bool HasOnlyOneUse() const {
1420 return first_ != nullptr && first_->next_ == nullptr;
1421 }
1422
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001423 size_t SizeSlow() const {
1424 size_t count = 0;
1425 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1426 ++count;
1427 }
1428 return count;
1429 }
1430
David Brazdiled596192015-01-23 10:39:45 +00001431 private:
1432 HUseListNode<T>* first_;
1433};
1434
1435template<typename T>
1436class HUseIterator : public ValueObject {
1437 public:
1438 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1439
1440 bool Done() const { return current_ == nullptr; }
1441
1442 void Advance() {
1443 DCHECK(!Done());
1444 current_ = current_->GetNext();
1445 }
1446
1447 HUseListNode<T>* Current() const {
1448 DCHECK(!Done());
1449 return current_;
1450 }
1451
1452 private:
1453 HUseListNode<T>* current_;
1454
1455 friend class HValue;
1456};
1457
David Brazdil1abb4192015-02-17 18:33:36 +00001458// This class is used by HEnvironment and HInstruction classes to record the
1459// instructions they use and pointers to the corresponding HUseListNodes kept
1460// by the used instructions.
1461template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001462class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001463 public:
1464 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1465 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1466
1467 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1468 : instruction_(old_record.instruction_), use_node_(use_node) {
1469 DCHECK(instruction_ != nullptr);
1470 DCHECK(use_node_ != nullptr);
1471 DCHECK(old_record.use_node_ == nullptr);
1472 }
1473
1474 HInstruction* GetInstruction() const { return instruction_; }
1475 HUseListNode<T>* GetUseNode() const { return use_node_; }
1476
1477 private:
1478 // Instruction used by the user.
1479 HInstruction* instruction_;
1480
1481 // Corresponding entry in the use list kept by 'instruction_'.
1482 HUseListNode<T>* use_node_;
1483};
1484
Aart Bik854a02b2015-07-14 16:07:00 -07001485/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001486 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001487 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001488 * For write/read dependences on fields/arrays, the dependence analysis uses
1489 * type disambiguation (e.g. a float field write cannot modify the value of an
1490 * integer field read) and the access type (e.g. a reference array write cannot
1491 * modify the value of a reference field read [although it may modify the
1492 * reference fetch prior to reading the field, which is represented by its own
1493 * write/read dependence]). The analysis makes conservative points-to
1494 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1495 * the same, and any reference read depends on any reference read without
1496 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001497 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001498 * The internal representation uses 38-bit and is described in the table below.
1499 * The first line indicates the side effect, and for field/array accesses the
1500 * second line indicates the type of the access (in the order of the
1501 * Primitive::Type enum).
1502 * The two numbered lines below indicate the bit position in the bitfield (read
1503 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001504 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001505 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1506 * +-------------+---------+---------+--------------+---------+---------+
1507 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1508 * | 3 |333333322|222222221| 1 |111111110|000000000|
1509 * | 7 |654321098|765432109| 8 |765432109|876543210|
1510 *
1511 * Note that, to ease the implementation, 'changes' bits are least significant
1512 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001513 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001514class SideEffects : public ValueObject {
1515 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001516 SideEffects() : flags_(0) {}
1517
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001518 static SideEffects None() {
1519 return SideEffects(0);
1520 }
1521
1522 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001523 return SideEffects(kAllChangeBits | kAllDependOnBits);
1524 }
1525
1526 static SideEffects AllChanges() {
1527 return SideEffects(kAllChangeBits);
1528 }
1529
1530 static SideEffects AllDependencies() {
1531 return SideEffects(kAllDependOnBits);
1532 }
1533
1534 static SideEffects AllExceptGCDependency() {
1535 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1536 }
1537
1538 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001539 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001540 }
1541
Aart Bik34c3ba92015-07-20 14:08:59 -07001542 static SideEffects AllWrites() {
1543 return SideEffects(kAllWrites);
1544 }
1545
1546 static SideEffects AllReads() {
1547 return SideEffects(kAllReads);
1548 }
1549
1550 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1551 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001552 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001553 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001554 }
1555
Aart Bik854a02b2015-07-14 16:07:00 -07001556 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1557 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001558 }
1559
Aart Bik34c3ba92015-07-20 14:08:59 -07001560 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1561 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001562 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001563 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001564 }
1565
1566 static SideEffects ArrayReadOfType(Primitive::Type type) {
1567 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1568 }
1569
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001570 static SideEffects CanTriggerGC() {
1571 return SideEffects(1ULL << kCanTriggerGCBit);
1572 }
1573
1574 static SideEffects DependsOnGC() {
1575 return SideEffects(1ULL << kDependsOnGCBit);
1576 }
1577
Aart Bik854a02b2015-07-14 16:07:00 -07001578 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001579 SideEffects Union(SideEffects other) const {
1580 return SideEffects(flags_ | other.flags_);
1581 }
1582
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001583 SideEffects Exclusion(SideEffects other) const {
1584 return SideEffects(flags_ & ~other.flags_);
1585 }
1586
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001587 void Add(SideEffects other) {
1588 flags_ |= other.flags_;
1589 }
1590
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001591 bool Includes(SideEffects other) const {
1592 return (other.flags_ & flags_) == other.flags_;
1593 }
1594
1595 bool HasSideEffects() const {
1596 return (flags_ & kAllChangeBits);
1597 }
1598
1599 bool HasDependencies() const {
1600 return (flags_ & kAllDependOnBits);
1601 }
1602
1603 // Returns true if there are no side effects or dependencies.
1604 bool DoesNothing() const {
1605 return flags_ == 0;
1606 }
1607
Aart Bik854a02b2015-07-14 16:07:00 -07001608 // Returns true if something is written.
1609 bool DoesAnyWrite() const {
1610 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001611 }
1612
Aart Bik854a02b2015-07-14 16:07:00 -07001613 // Returns true if something is read.
1614 bool DoesAnyRead() const {
1615 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001616 }
1617
Aart Bik854a02b2015-07-14 16:07:00 -07001618 // Returns true if potentially everything is written and read
1619 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001620 bool DoesAllReadWrite() const {
1621 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1622 }
1623
Aart Bik854a02b2015-07-14 16:07:00 -07001624 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001625 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001626 }
1627
Roland Levillain0d5a2812015-11-13 10:07:31 +00001628 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001629 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001630 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1631 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001632 }
1633
1634 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001635 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001636 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001637 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001638 for (int s = kLastBit; s >= 0; s--) {
1639 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1640 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1641 // This is a bit for the GC side effect.
1642 if (current_bit_is_set) {
1643 flags += "GC";
1644 }
Aart Bik854a02b2015-07-14 16:07:00 -07001645 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001646 } else {
1647 // This is a bit for the array/field analysis.
1648 // The underscore character stands for the 'can trigger GC' bit.
1649 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1650 if (current_bit_is_set) {
1651 flags += kDebug[s];
1652 }
1653 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1654 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1655 flags += "|";
1656 }
1657 }
Aart Bik854a02b2015-07-14 16:07:00 -07001658 }
1659 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001660 }
1661
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001662 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001663
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001664 private:
1665 static constexpr int kFieldArrayAnalysisBits = 9;
1666
1667 static constexpr int kFieldWriteOffset = 0;
1668 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1669 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1670 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1671
1672 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1673
1674 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1675 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1676 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1677 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1678
1679 static constexpr int kLastBit = kDependsOnGCBit;
1680 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1681
1682 // Aliases.
1683
1684 static_assert(kChangeBits == kDependOnBits,
1685 "the 'change' bits should match the 'depend on' bits.");
1686
1687 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1688 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1689 static constexpr uint64_t kAllWrites =
1690 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1691 static constexpr uint64_t kAllReads =
1692 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001693
Aart Bik854a02b2015-07-14 16:07:00 -07001694 // Work around the fact that HIR aliases I/F and J/D.
1695 // TODO: remove this interceptor once HIR types are clean
1696 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1697 switch (type) {
1698 case Primitive::kPrimInt:
1699 case Primitive::kPrimFloat:
1700 return TypeFlag(Primitive::kPrimInt, offset) |
1701 TypeFlag(Primitive::kPrimFloat, offset);
1702 case Primitive::kPrimLong:
1703 case Primitive::kPrimDouble:
1704 return TypeFlag(Primitive::kPrimLong, offset) |
1705 TypeFlag(Primitive::kPrimDouble, offset);
1706 default:
1707 return TypeFlag(type, offset);
1708 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001709 }
1710
Aart Bik854a02b2015-07-14 16:07:00 -07001711 // Translates type to bit flag.
1712 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1713 CHECK_NE(type, Primitive::kPrimVoid);
1714 const uint64_t one = 1;
1715 const int shift = type; // 0-based consecutive enum
1716 DCHECK_LE(kFieldWriteOffset, shift);
1717 DCHECK_LT(shift, kArrayWriteOffset);
1718 return one << (type + offset);
1719 }
1720
1721 // Private constructor on direct flags value.
1722 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1723
1724 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001725};
1726
David Brazdiled596192015-01-23 10:39:45 +00001727// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001728class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001729 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001730 HEnvironment(ArenaAllocator* arena,
1731 size_t number_of_vregs,
1732 const DexFile& dex_file,
1733 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001734 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001735 InvokeType invoke_type,
1736 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001737 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1738 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001739 parent_(nullptr),
1740 dex_file_(dex_file),
1741 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001742 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001743 invoke_type_(invoke_type),
1744 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001745 }
1746
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001747 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001748 : HEnvironment(arena,
1749 to_copy.Size(),
1750 to_copy.GetDexFile(),
1751 to_copy.GetMethodIdx(),
1752 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001753 to_copy.GetInvokeType(),
1754 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001755
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001756 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001757 if (parent_ != nullptr) {
1758 parent_->SetAndCopyParentChain(allocator, parent);
1759 } else {
1760 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1761 parent_->CopyFrom(parent);
1762 if (parent->GetParent() != nullptr) {
1763 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1764 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001765 }
David Brazdiled596192015-01-23 10:39:45 +00001766 }
1767
Vladimir Marko71bf8092015-09-15 15:33:14 +01001768 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001769 void CopyFrom(HEnvironment* environment);
1770
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001771 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1772 // input to the loop phi instead. This is for inserting instructions that
1773 // require an environment (like HDeoptimization) in the loop pre-header.
1774 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001775
1776 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001777 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001778 }
1779
1780 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001781 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001782 }
1783
David Brazdil1abb4192015-02-17 18:33:36 +00001784 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001785
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001786 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001787
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001788 HEnvironment* GetParent() const { return parent_; }
1789
1790 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001791 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001792 }
1793
1794 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001795 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001796 }
1797
1798 uint32_t GetDexPc() const {
1799 return dex_pc_;
1800 }
1801
1802 uint32_t GetMethodIdx() const {
1803 return method_idx_;
1804 }
1805
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001806 InvokeType GetInvokeType() const {
1807 return invoke_type_;
1808 }
1809
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001810 const DexFile& GetDexFile() const {
1811 return dex_file_;
1812 }
1813
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001814 HInstruction* GetHolder() const {
1815 return holder_;
1816 }
1817
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001818
1819 bool IsFromInlinedInvoke() const {
1820 return GetParent() != nullptr;
1821 }
1822
David Brazdiled596192015-01-23 10:39:45 +00001823 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001824 // Record instructions' use entries of this environment for constant-time removal.
1825 // It should only be called by HInstruction when a new environment use is added.
1826 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1827 DCHECK(env_use->GetUser() == this);
1828 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001829 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001830 }
David Brazdiled596192015-01-23 10:39:45 +00001831
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001832 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1833 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001834 HEnvironment* parent_;
1835 const DexFile& dex_file_;
1836 const uint32_t method_idx_;
1837 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001838 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001839
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001840 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001841 HInstruction* const holder_;
1842
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001843 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001844
1845 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1846};
1847
Vladimir Markof9f64412015-09-02 14:05:49 +01001848class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001849 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001850 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001851 : previous_(nullptr),
1852 next_(nullptr),
1853 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001854 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001855 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001856 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001857 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001858 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001859 locations_(nullptr),
1860 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001861 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001862 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001863 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1864 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1865 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001866
Dave Allison20dfc792014-06-16 20:44:29 -07001867 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001868
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001869#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001870 enum InstructionKind {
1871 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1872 };
1873#undef DECLARE_KIND
1874
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001875 HInstruction* GetNext() const { return next_; }
1876 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001877
Calin Juravle77520bc2015-01-12 18:45:46 +00001878 HInstruction* GetNextDisregardingMoves() const;
1879 HInstruction* GetPreviousDisregardingMoves() const;
1880
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001881 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001882 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001883 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001884 bool IsInBlock() const { return block_ != nullptr; }
1885 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001886 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1887 bool IsIrreducibleLoopHeaderPhi() const {
1888 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1889 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001890
Roland Levillain6b879dd2014-09-22 17:13:44 +01001891 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001892 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001893
1894 virtual void Accept(HGraphVisitor* visitor) = 0;
1895 virtual const char* DebugName() const = 0;
1896
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001897 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001898 void SetRawInputAt(size_t index, HInstruction* input) {
1899 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1900 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001901
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001902 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001903
1904 uint32_t GetDexPc() const { return dex_pc_; }
1905
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001906 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001907
Roland Levillaine161a2a2014-10-03 12:45:18 +01001908 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001909 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001910
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001911 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001912 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001913
Calin Juravle10e244f2015-01-26 18:54:32 +00001914 // Does not apply for all instructions, but having this at top level greatly
1915 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001916 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001917 virtual bool CanBeNull() const {
1918 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1919 return true;
1920 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001921
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001922 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001923 return false;
1924 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001925
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001926 virtual bool IsActualObject() const {
1927 return GetType() == Primitive::kPrimNot;
1928 }
1929
Calin Juravle2e768302015-07-28 14:41:11 +00001930 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001931
Calin Juravle61d544b2015-02-23 16:46:57 +00001932 ReferenceTypeInfo GetReferenceTypeInfo() const {
1933 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001934 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
1935 GetPackedFlag<kFlagReferenceTypeIsExact>());;
Calin Juravle61d544b2015-02-23 16:46:57 +00001936 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001937
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001938 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001939 DCHECK(user != nullptr);
1940 HUseListNode<HInstruction*>* use =
1941 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1942 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001943 }
1944
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001945 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001946 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001947 HUseListNode<HEnvironment*>* env_use =
1948 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1949 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001950 }
1951
David Brazdil1abb4192015-02-17 18:33:36 +00001952 void RemoveAsUserOfInput(size_t input) {
1953 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1954 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1955 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001956
David Brazdil1abb4192015-02-17 18:33:36 +00001957 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1958 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001959
David Brazdiled596192015-01-23 10:39:45 +00001960 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1961 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001962 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001963 bool HasOnlyOneNonEnvironmentUse() const {
1964 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1965 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001966
Roland Levillain6c82d402014-10-13 16:10:27 +01001967 // Does this instruction strictly dominate `other_instruction`?
1968 // Returns false if this instruction and `other_instruction` are the same.
1969 // Aborts if this instruction and `other_instruction` are both phis.
1970 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001971
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001972 int GetId() const { return id_; }
1973 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001974
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001975 int GetSsaIndex() const { return ssa_index_; }
1976 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1977 bool HasSsaIndex() const { return ssa_index_ != -1; }
1978
1979 bool HasEnvironment() const { return environment_ != nullptr; }
1980 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001981 // Set the `environment_` field. Raw because this method does not
1982 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001983 void SetRawEnvironment(HEnvironment* environment) {
1984 DCHECK(environment_ == nullptr);
1985 DCHECK_EQ(environment->GetHolder(), this);
1986 environment_ = environment;
1987 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001988
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001989 void RemoveEnvironment();
1990
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001991 // Set the environment of this instruction, copying it from `environment`. While
1992 // copying, the uses lists are being updated.
1993 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001994 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001995 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001996 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001997 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001998 if (environment->GetParent() != nullptr) {
1999 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2000 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002001 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002002
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002003 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2004 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002005 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002006 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002007 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002008 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002009 if (environment->GetParent() != nullptr) {
2010 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2011 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002012 }
2013
Nicolas Geoffray39468442014-09-02 15:17:15 +01002014 // Returns the number of entries in the environment. Typically, that is the
2015 // number of dex registers in a method. It could be more in case of inlining.
2016 size_t EnvironmentSize() const;
2017
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002018 LocationSummary* GetLocations() const { return locations_; }
2019 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002020
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002021 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002022 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002023
Alexandre Rames188d4312015-04-09 18:30:21 +01002024 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2025 // uses of this instruction by `other` are *not* updated.
2026 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2027 ReplaceWith(other);
2028 other->ReplaceInput(this, use_index);
2029 }
2030
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002031 // Move `this` instruction before `cursor`.
2032 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002033
Vladimir Markofb337ea2015-11-25 15:25:10 +00002034 // Move `this` before its first user and out of any loops. If there is no
2035 // out-of-loop user that dominates all other users, move the instruction
2036 // to the end of the out-of-loop common dominator of the user's blocks.
2037 //
2038 // This can be used only on non-throwing instructions with no side effects that
2039 // have at least one use but no environment uses.
2040 void MoveBeforeFirstUserAndOutOfLoops();
2041
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002042#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002043 bool Is##type() const; \
2044 const H##type* As##type() const; \
2045 H##type* As##type();
2046
2047 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2048#undef INSTRUCTION_TYPE_CHECK
2049
2050#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002051 bool Is##type() const { return (As##type() != nullptr); } \
2052 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002053 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002054 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002055#undef INSTRUCTION_TYPE_CHECK
2056
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002057 // Returns whether the instruction can be moved within the graph.
2058 virtual bool CanBeMoved() const { return false; }
2059
2060 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002061 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002062 return false;
2063 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002064
2065 // Returns whether any data encoded in the two instructions is equal.
2066 // This method does not look at the inputs. Both instructions must be
2067 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002068 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002069 return false;
2070 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002071
2072 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002073 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002074 // 2) Their inputs are identical.
2075 bool Equals(HInstruction* other) const;
2076
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002077 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2078 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2079 // the virtual function because the __attribute__((__pure__)) doesn't really
2080 // apply the strong requirement for virtual functions, preventing optimizations.
2081 InstructionKind GetKind() const PURE;
2082 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002083
2084 virtual size_t ComputeHashCode() const {
2085 size_t result = GetKind();
2086 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2087 result = (result * 31) + InputAt(i)->GetId();
2088 }
2089 return result;
2090 }
2091
2092 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002093 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002094 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002095
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002096 size_t GetLifetimePosition() const { return lifetime_position_; }
2097 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2098 LiveInterval* GetLiveInterval() const { return live_interval_; }
2099 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2100 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2101
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002102 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2103
2104 // Returns whether the code generation of the instruction will require to have access
2105 // to the current method. Such instructions are:
2106 // (1): Instructions that require an environment, as calling the runtime requires
2107 // to walk the stack and have the current method stored at a specific stack address.
2108 // (2): Object literals like classes and strings, that are loaded from the dex cache
2109 // fields of the current method.
2110 bool NeedsCurrentMethod() const {
2111 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2112 }
2113
Vladimir Markodc151b22015-10-15 18:02:30 +01002114 // Returns whether the code generation of the instruction will require to have access
2115 // to the dex cache of the current method's declaring class via the current method.
2116 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002117
Mark Mendellc4701932015-04-10 13:18:51 -04002118 // Does this instruction have any use in an environment before
2119 // control flow hits 'other'?
2120 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2121
2122 // Remove all references to environment uses of this instruction.
2123 // The caller must ensure that this is safe to do.
2124 void RemoveEnvironmentUsers();
2125
Vladimir Markoa1de9182016-02-25 11:37:38 +00002126 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2127 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002128
David Brazdil1abb4192015-02-17 18:33:36 +00002129 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002130 // If set, the machine code for this instruction is assumed to be generated by
2131 // its users. Used by liveness analysis to compute use positions accordingly.
2132 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2133 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2134 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2135 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2136
David Brazdil1abb4192015-02-17 18:33:36 +00002137 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2138 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2139
Vladimir Markoa1de9182016-02-25 11:37:38 +00002140 uint32_t GetPackedFields() const {
2141 return packed_fields_;
2142 }
2143
2144 template <size_t flag>
2145 bool GetPackedFlag() const {
2146 return (packed_fields_ & (1u << flag)) != 0u;
2147 }
2148
2149 template <size_t flag>
2150 void SetPackedFlag(bool value = true) {
2151 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2152 }
2153
2154 template <typename BitFieldType>
2155 typename BitFieldType::value_type GetPackedField() const {
2156 return BitFieldType::Decode(packed_fields_);
2157 }
2158
2159 template <typename BitFieldType>
2160 void SetPackedField(typename BitFieldType::value_type value) {
2161 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2162 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2163 }
2164
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002165 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002166 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2167
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002168 HInstruction* previous_;
2169 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002170 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002171 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002172
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002173 // An instruction gets an id when it is added to the graph.
2174 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002175 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002176 int id_;
2177
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002178 // When doing liveness analysis, instructions that have uses get an SSA index.
2179 int ssa_index_;
2180
Vladimir Markoa1de9182016-02-25 11:37:38 +00002181 // Packed fields.
2182 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002183
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002184 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002185 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002186
2187 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002188 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002189
Nicolas Geoffray39468442014-09-02 15:17:15 +01002190 // The environment associated with this instruction. Not null if the instruction
2191 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002192 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002193
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002194 // Set by the code generator.
2195 LocationSummary* locations_;
2196
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002197 // Set by the liveness analysis.
2198 LiveInterval* live_interval_;
2199
2200 // Set by the liveness analysis, this is the position in a linear
2201 // order of blocks where this instruction's live interval start.
2202 size_t lifetime_position_;
2203
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002204 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002205
Vladimir Markoa1de9182016-02-25 11:37:38 +00002206 // The reference handle part of the reference type info.
2207 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002208 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002209 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002210
David Brazdil1abb4192015-02-17 18:33:36 +00002211 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002212 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002213 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002214 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002215 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002216
2217 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2218};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002219std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002220
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002221class HInputIterator : public ValueObject {
2222 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002223 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002224
2225 bool Done() const { return index_ == instruction_->InputCount(); }
2226 HInstruction* Current() const { return instruction_->InputAt(index_); }
2227 void Advance() { index_++; }
2228
2229 private:
2230 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002231 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002232
2233 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2234};
2235
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002236class HInstructionIterator : public ValueObject {
2237 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002238 explicit HInstructionIterator(const HInstructionList& instructions)
2239 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002240 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002241 }
2242
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002243 bool Done() const { return instruction_ == nullptr; }
2244 HInstruction* Current() const { return instruction_; }
2245 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002246 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002247 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002248 }
2249
2250 private:
2251 HInstruction* instruction_;
2252 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002253
2254 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002255};
2256
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002257class HBackwardInstructionIterator : public ValueObject {
2258 public:
2259 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2260 : instruction_(instructions.last_instruction_) {
2261 next_ = Done() ? nullptr : instruction_->GetPrevious();
2262 }
2263
2264 bool Done() const { return instruction_ == nullptr; }
2265 HInstruction* Current() const { return instruction_; }
2266 void Advance() {
2267 instruction_ = next_;
2268 next_ = Done() ? nullptr : instruction_->GetPrevious();
2269 }
2270
2271 private:
2272 HInstruction* instruction_;
2273 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002274
2275 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002276};
2277
Vladimir Markof9f64412015-09-02 14:05:49 +01002278template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002279class HTemplateInstruction: public HInstruction {
2280 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002281 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002282 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002283 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002284
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002285 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002286
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002287 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002288 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2289 DCHECK_LT(i, N);
2290 return inputs_[i];
2291 }
David Brazdil1abb4192015-02-17 18:33:36 +00002292
2293 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002294 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002295 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002296 }
2297
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002298 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002299 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002300
2301 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002302};
2303
Vladimir Markof9f64412015-09-02 14:05:49 +01002304// HTemplateInstruction specialization for N=0.
2305template<>
2306class HTemplateInstruction<0>: public HInstruction {
2307 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002308 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002309 : HInstruction(side_effects, dex_pc) {}
2310
Vladimir Markof9f64412015-09-02 14:05:49 +01002311 virtual ~HTemplateInstruction() {}
2312
2313 size_t InputCount() const OVERRIDE { return 0; }
2314
2315 protected:
2316 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2317 LOG(FATAL) << "Unreachable";
2318 UNREACHABLE();
2319 }
2320
2321 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2322 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2323 LOG(FATAL) << "Unreachable";
2324 UNREACHABLE();
2325 }
2326
2327 private:
2328 friend class SsaBuilder;
2329};
2330
Dave Allison20dfc792014-06-16 20:44:29 -07002331template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002332class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002333 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002334 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002335 : HTemplateInstruction<N>(side_effects, dex_pc) {
2336 this->template SetPackedField<TypeField>(type);
2337 }
Dave Allison20dfc792014-06-16 20:44:29 -07002338 virtual ~HExpression() {}
2339
Vladimir Markoa1de9182016-02-25 11:37:38 +00002340 Primitive::Type GetType() const OVERRIDE {
2341 return TypeField::Decode(this->GetPackedFields());
2342 }
Dave Allison20dfc792014-06-16 20:44:29 -07002343
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002344 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002345 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2346 static constexpr size_t kFieldTypeSize =
2347 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2348 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2349 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2350 "Too many packed fields.");
2351 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002352};
2353
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002354// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2355// instruction that branches to the exit block.
2356class HReturnVoid : public HTemplateInstruction<0> {
2357 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002358 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2359 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002360
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002361 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002362
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002363 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002364
2365 private:
2366 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2367};
2368
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002369// Represents dex's RETURN opcodes. A HReturn is a control flow
2370// instruction that branches to the exit block.
2371class HReturn : public HTemplateInstruction<1> {
2372 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002373 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2374 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002375 SetRawInputAt(0, value);
2376 }
2377
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002378 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002379
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002380 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002381
2382 private:
2383 DISALLOW_COPY_AND_ASSIGN(HReturn);
2384};
2385
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002386// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002387// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002388// exit block.
2389class HExit : public HTemplateInstruction<0> {
2390 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002391 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002392
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002393 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002394
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002395 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002396
2397 private:
2398 DISALLOW_COPY_AND_ASSIGN(HExit);
2399};
2400
2401// Jumps from one block to another.
2402class HGoto : public HTemplateInstruction<0> {
2403 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002404 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002405
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002406 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002407
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002408 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002409 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002410 }
2411
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002412 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002413
2414 private:
2415 DISALLOW_COPY_AND_ASSIGN(HGoto);
2416};
2417
Roland Levillain9867bc72015-08-05 10:21:34 +01002418class HConstant : public HExpression<0> {
2419 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002420 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2421 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002422
2423 bool CanBeMoved() const OVERRIDE { return true; }
2424
Roland Levillain1a653882016-03-18 18:05:57 +00002425 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002426 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002427 // Is this constant 0 in the arithmetic sense?
2428 virtual bool IsArithmeticZero() const { return false; }
2429 // Is this constant a 0-bit pattern?
2430 virtual bool IsZeroBitPattern() const { return false; }
2431 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002432 virtual bool IsOne() const { return false; }
2433
David Brazdil77a48ae2015-09-15 12:34:04 +00002434 virtual uint64_t GetValueAsUint64() const = 0;
2435
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002436 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002437
2438 private:
2439 DISALLOW_COPY_AND_ASSIGN(HConstant);
2440};
2441
2442class HNullConstant : public HConstant {
2443 public:
2444 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2445 return true;
2446 }
2447
David Brazdil77a48ae2015-09-15 12:34:04 +00002448 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2449
Roland Levillain9867bc72015-08-05 10:21:34 +01002450 size_t ComputeHashCode() const OVERRIDE { return 0; }
2451
Roland Levillain1a653882016-03-18 18:05:57 +00002452 // The null constant representation is a 0-bit pattern.
2453 virtual bool IsZeroBitPattern() const { return true; }
2454
Roland Levillain9867bc72015-08-05 10:21:34 +01002455 DECLARE_INSTRUCTION(NullConstant);
2456
2457 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002458 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002459
2460 friend class HGraph;
2461 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2462};
2463
2464// Constants of the type int. Those can be from Dex instructions, or
2465// synthesized (for example with the if-eqz instruction).
2466class HIntConstant : public HConstant {
2467 public:
2468 int32_t GetValue() const { return value_; }
2469
David Brazdil9f389d42015-10-01 14:32:56 +01002470 uint64_t GetValueAsUint64() const OVERRIDE {
2471 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2472 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002473
Roland Levillain9867bc72015-08-05 10:21:34 +01002474 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002475 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002476 return other->AsIntConstant()->value_ == value_;
2477 }
2478
2479 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2480
2481 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002482 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2483 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002484 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2485
Roland Levillain1a653882016-03-18 18:05:57 +00002486 // Integer constants are used to encode Boolean values as well,
2487 // where 1 means true and 0 means false.
2488 bool IsTrue() const { return GetValue() == 1; }
2489 bool IsFalse() const { return GetValue() == 0; }
2490
Roland Levillain9867bc72015-08-05 10:21:34 +01002491 DECLARE_INSTRUCTION(IntConstant);
2492
2493 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002494 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2495 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2496 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2497 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002498
2499 const int32_t value_;
2500
2501 friend class HGraph;
2502 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2503 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2504 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2505};
2506
2507class HLongConstant : public HConstant {
2508 public:
2509 int64_t GetValue() const { return value_; }
2510
David Brazdil77a48ae2015-09-15 12:34:04 +00002511 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2512
Roland Levillain9867bc72015-08-05 10:21:34 +01002513 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002514 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002515 return other->AsLongConstant()->value_ == value_;
2516 }
2517
2518 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2519
2520 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002521 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2522 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002523 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2524
2525 DECLARE_INSTRUCTION(LongConstant);
2526
2527 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002528 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2529 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002530
2531 const int64_t value_;
2532
2533 friend class HGraph;
2534 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2535};
Dave Allison20dfc792014-06-16 20:44:29 -07002536
Roland Levillain31dd3d62016-02-16 12:21:02 +00002537class HFloatConstant : public HConstant {
2538 public:
2539 float GetValue() const { return value_; }
2540
2541 uint64_t GetValueAsUint64() const OVERRIDE {
2542 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2543 }
2544
2545 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2546 DCHECK(other->IsFloatConstant()) << other->DebugName();
2547 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2548 }
2549
2550 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2551
2552 bool IsMinusOne() const OVERRIDE {
2553 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2554 }
Roland Levillain1a653882016-03-18 18:05:57 +00002555 bool IsArithmeticZero() const OVERRIDE {
2556 return std::fpclassify(value_) == FP_ZERO;
2557 }
2558 bool IsArithmeticPositiveZero() const {
2559 return IsArithmeticZero() && !std::signbit(value_);
2560 }
2561 bool IsArithmeticNegativeZero() const {
2562 return IsArithmeticZero() && std::signbit(value_);
2563 }
2564 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002565 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002566 }
2567 bool IsOne() const OVERRIDE {
2568 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2569 }
2570 bool IsNaN() const {
2571 return std::isnan(value_);
2572 }
2573
2574 DECLARE_INSTRUCTION(FloatConstant);
2575
2576 private:
2577 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2578 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2579 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2580 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2581
2582 const float value_;
2583
2584 // Only the SsaBuilder and HGraph can create floating-point constants.
2585 friend class SsaBuilder;
2586 friend class HGraph;
2587 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2588};
2589
2590class HDoubleConstant : public HConstant {
2591 public:
2592 double GetValue() const { return value_; }
2593
2594 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2595
2596 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2597 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2598 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2599 }
2600
2601 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2602
2603 bool IsMinusOne() const OVERRIDE {
2604 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2605 }
Roland Levillain1a653882016-03-18 18:05:57 +00002606 bool IsArithmeticZero() const OVERRIDE {
2607 return std::fpclassify(value_) == FP_ZERO;
2608 }
2609 bool IsArithmeticPositiveZero() const {
2610 return IsArithmeticZero() && !std::signbit(value_);
2611 }
2612 bool IsArithmeticNegativeZero() const {
2613 return IsArithmeticZero() && std::signbit(value_);
2614 }
2615 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002616 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002617 }
2618 bool IsOne() const OVERRIDE {
2619 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2620 }
2621 bool IsNaN() const {
2622 return std::isnan(value_);
2623 }
2624
2625 DECLARE_INSTRUCTION(DoubleConstant);
2626
2627 private:
2628 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2629 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2630 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2631 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2632
2633 const double value_;
2634
2635 // Only the SsaBuilder and HGraph can create floating-point constants.
2636 friend class SsaBuilder;
2637 friend class HGraph;
2638 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2639};
2640
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002641// Conditional branch. A block ending with an HIf instruction must have
2642// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002643class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002644 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002645 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2646 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002647 SetRawInputAt(0, input);
2648 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002649
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002650 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002651
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002652 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002653 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002654 }
2655
2656 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002657 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002658 }
2659
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002660 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002661
2662 private:
2663 DISALLOW_COPY_AND_ASSIGN(HIf);
2664};
2665
David Brazdilfc6a86a2015-06-26 10:33:45 +00002666
2667// Abstract instruction which marks the beginning and/or end of a try block and
2668// links it to the respective exception handlers. Behaves the same as a Goto in
2669// non-exceptional control flow.
2670// Normal-flow successor is stored at index zero, exception handlers under
2671// higher indices in no particular order.
2672class HTryBoundary : public HTemplateInstruction<0> {
2673 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002674 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002675 kEntry,
2676 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002677 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002678 };
2679
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002680 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002681 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2682 SetPackedField<BoundaryKindField>(kind);
2683 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002684
2685 bool IsControlFlow() const OVERRIDE { return true; }
2686
2687 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002688 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002689
David Brazdild26a4112015-11-10 11:07:31 +00002690 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2691 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2692 }
2693
David Brazdilfc6a86a2015-06-26 10:33:45 +00002694 // Returns whether `handler` is among its exception handlers (non-zero index
2695 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002696 bool HasExceptionHandler(const HBasicBlock& handler) const {
2697 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002698 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002699 }
2700
2701 // If not present already, adds `handler` to its block's list of exception
2702 // handlers.
2703 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002704 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002705 GetBlock()->AddSuccessor(handler);
2706 }
2707 }
2708
Vladimir Markoa1de9182016-02-25 11:37:38 +00002709 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2710 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002711
David Brazdilffee3d32015-07-06 11:48:53 +01002712 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2713
David Brazdilfc6a86a2015-06-26 10:33:45 +00002714 DECLARE_INSTRUCTION(TryBoundary);
2715
2716 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002717 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2718 static constexpr size_t kFieldBoundaryKindSize =
2719 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2720 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2721 kFieldBoundaryKind + kFieldBoundaryKindSize;
2722 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2723 "Too many packed fields.");
2724 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002725
2726 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2727};
2728
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002729// Deoptimize to interpreter, upon checking a condition.
2730class HDeoptimize : public HTemplateInstruction<1> {
2731 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002732 // We set CanTriggerGC to prevent any intermediate address to be live
2733 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002734 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002735 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002736 SetRawInputAt(0, cond);
2737 }
2738
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002739 bool CanBeMoved() const OVERRIDE { return true; }
2740 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2741 return true;
2742 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002743 bool NeedsEnvironment() const OVERRIDE { return true; }
2744 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002745
2746 DECLARE_INSTRUCTION(Deoptimize);
2747
2748 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002749 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2750};
2751
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002752// Represents the ArtMethod that was passed as a first argument to
2753// the method. It is used by instructions that depend on it, like
2754// instructions that work with the dex cache.
2755class HCurrentMethod : public HExpression<0> {
2756 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002757 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2758 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002759
2760 DECLARE_INSTRUCTION(CurrentMethod);
2761
2762 private:
2763 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2764};
2765
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002766// Fetches an ArtMethod from the virtual table or the interface method table
2767// of a class.
2768class HClassTableGet : public HExpression<1> {
2769 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002770 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002771 kVTable,
2772 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002773 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002774 };
2775 HClassTableGet(HInstruction* cls,
2776 Primitive::Type type,
2777 TableKind kind,
2778 size_t index,
2779 uint32_t dex_pc)
2780 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002781 index_(index) {
2782 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002783 SetRawInputAt(0, cls);
2784 }
2785
2786 bool CanBeMoved() const OVERRIDE { return true; }
2787 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2788 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002789 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002790 }
2791
Vladimir Markoa1de9182016-02-25 11:37:38 +00002792 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002793 size_t GetIndex() const { return index_; }
2794
2795 DECLARE_INSTRUCTION(ClassTableGet);
2796
2797 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002798 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2799 static constexpr size_t kFieldTableKindSize =
2800 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2801 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2802 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2803 "Too many packed fields.");
2804 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2805
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002806 // The index of the ArtMethod in the table.
2807 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002808
2809 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2810};
2811
Mark Mendellfe57faa2015-09-18 09:26:15 -04002812// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2813// have one successor for each entry in the switch table, and the final successor
2814// will be the block containing the next Dex opcode.
2815class HPackedSwitch : public HTemplateInstruction<1> {
2816 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002817 HPackedSwitch(int32_t start_value,
2818 uint32_t num_entries,
2819 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002820 uint32_t dex_pc = kNoDexPc)
2821 : HTemplateInstruction(SideEffects::None(), dex_pc),
2822 start_value_(start_value),
2823 num_entries_(num_entries) {
2824 SetRawInputAt(0, input);
2825 }
2826
2827 bool IsControlFlow() const OVERRIDE { return true; }
2828
2829 int32_t GetStartValue() const { return start_value_; }
2830
Vladimir Marko211c2112015-09-24 16:52:33 +01002831 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002832
2833 HBasicBlock* GetDefaultBlock() const {
2834 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002835 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002836 }
2837 DECLARE_INSTRUCTION(PackedSwitch);
2838
2839 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002840 const int32_t start_value_;
2841 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002842
2843 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2844};
2845
Roland Levillain88cb1752014-10-20 16:36:47 +01002846class HUnaryOperation : public HExpression<1> {
2847 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002848 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2849 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002850 SetRawInputAt(0, input);
2851 }
2852
2853 HInstruction* GetInput() const { return InputAt(0); }
2854 Primitive::Type GetResultType() const { return GetType(); }
2855
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002856 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002857 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002858 return true;
2859 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002860
Roland Levillain31dd3d62016-02-16 12:21:02 +00002861 // Try to statically evaluate `this` and return a HConstant
2862 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002863 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002864 HConstant* TryStaticEvaluation() const;
2865
2866 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002867 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2868 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002869 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2870 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002871
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002872 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002873
2874 private:
2875 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2876};
2877
Dave Allison20dfc792014-06-16 20:44:29 -07002878class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002879 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002880 HBinaryOperation(Primitive::Type result_type,
2881 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002882 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002883 SideEffects side_effects = SideEffects::None(),
2884 uint32_t dex_pc = kNoDexPc)
2885 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002886 SetRawInputAt(0, left);
2887 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002888 }
2889
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002890 HInstruction* GetLeft() const { return InputAt(0); }
2891 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002892 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002893
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002894 virtual bool IsCommutative() const { return false; }
2895
2896 // Put constant on the right.
2897 // Returns whether order is changed.
2898 bool OrderInputsWithConstantOnTheRight() {
2899 HInstruction* left = InputAt(0);
2900 HInstruction* right = InputAt(1);
2901 if (left->IsConstant() && !right->IsConstant()) {
2902 ReplaceInput(right, 0);
2903 ReplaceInput(left, 1);
2904 return true;
2905 }
2906 return false;
2907 }
2908
2909 // Order inputs by instruction id, but favor constant on the right side.
2910 // This helps GVN for commutative ops.
2911 void OrderInputs() {
2912 DCHECK(IsCommutative());
2913 HInstruction* left = InputAt(0);
2914 HInstruction* right = InputAt(1);
2915 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2916 return;
2917 }
2918 if (OrderInputsWithConstantOnTheRight()) {
2919 return;
2920 }
2921 // Order according to instruction id.
2922 if (left->GetId() > right->GetId()) {
2923 ReplaceInput(right, 0);
2924 ReplaceInput(left, 1);
2925 }
2926 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002927
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002928 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002929 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002930 return true;
2931 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002932
Roland Levillain31dd3d62016-02-16 12:21:02 +00002933 // Try to statically evaluate `this` and return a HConstant
2934 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002935 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002936 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002937
2938 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002939 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2940 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002941 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2942 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002943 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002944 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2945 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002946 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2947 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002948 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2949 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01002950 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002951 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2952 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002953
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002954 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002955 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002956 HConstant* GetConstantRight() const;
2957
2958 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002959 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002960 HInstruction* GetLeastConstantLeft() const;
2961
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002962 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002963
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002964 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002965 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2966};
2967
Mark Mendellc4701932015-04-10 13:18:51 -04002968// The comparison bias applies for floating point operations and indicates how NaN
2969// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002970enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002971 kNoBias, // bias is not applicable (i.e. for long operation)
2972 kGtBias, // return 1 for NaN comparisons
2973 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00002974 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04002975};
2976
Roland Levillain31dd3d62016-02-16 12:21:02 +00002977std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
2978
Dave Allison20dfc792014-06-16 20:44:29 -07002979class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002980 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002981 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002982 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
2983 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
2984 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002985
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002986 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002987 // `instruction`, and disregard moves in between.
2988 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002989
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002990 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002991
2992 virtual IfCondition GetCondition() const = 0;
2993
Mark Mendellc4701932015-04-10 13:18:51 -04002994 virtual IfCondition GetOppositeCondition() const = 0;
2995
Vladimir Markoa1de9182016-02-25 11:37:38 +00002996 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06002997 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
2998
Vladimir Markoa1de9182016-02-25 11:37:38 +00002999 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3000 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003001
3002 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003003 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003004 }
3005
Roland Levillain4fa13f62015-07-06 18:11:54 +01003006 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003007 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003008 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003009 if (if_cond == kCondNE) {
3010 return true;
3011 } else if (if_cond == kCondEQ) {
3012 return false;
3013 }
3014 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003015 }
3016
3017 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003018 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003019 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003020 if (if_cond == kCondEQ) {
3021 return true;
3022 } else if (if_cond == kCondNE) {
3023 return false;
3024 }
3025 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003026 }
3027
Roland Levillain31dd3d62016-02-16 12:21:02 +00003028 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003029 // Needed if we merge a HCompare into a HCondition.
3030 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3031 static constexpr size_t kFieldComparisonBiasSize =
3032 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3033 static constexpr size_t kNumberOfConditionPackedBits =
3034 kFieldComparisonBias + kFieldComparisonBiasSize;
3035 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3036 using ComparisonBiasField =
3037 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3038
Roland Levillain31dd3d62016-02-16 12:21:02 +00003039 template <typename T>
3040 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3041
3042 template <typename T>
3043 int32_t CompareFP(T x, T y) const {
3044 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3045 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3046 // Handle the bias.
3047 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3048 }
3049
3050 // Return an integer constant containing the result of a condition evaluated at compile time.
3051 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3052 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3053 }
3054
Dave Allison20dfc792014-06-16 20:44:29 -07003055 private:
3056 DISALLOW_COPY_AND_ASSIGN(HCondition);
3057};
3058
3059// Instruction to check if two inputs are equal to each other.
3060class HEqual : public HCondition {
3061 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003062 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3063 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003064
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003065 bool IsCommutative() const OVERRIDE { return true; }
3066
Vladimir Marko9e23df52015-11-10 17:14:35 +00003067 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3068 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003069 return MakeConstantCondition(true, GetDexPc());
3070 }
3071 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3072 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3073 }
3074 // In the following Evaluate methods, a HCompare instruction has
3075 // been merged into this HEqual instruction; evaluate it as
3076 // `Compare(x, y) == 0`.
3077 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3078 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3079 GetDexPc());
3080 }
3081 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3082 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3083 }
3084 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3085 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003086 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003087
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003088 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003089
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003090 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003091 return kCondEQ;
3092 }
3093
Mark Mendellc4701932015-04-10 13:18:51 -04003094 IfCondition GetOppositeCondition() const OVERRIDE {
3095 return kCondNE;
3096 }
3097
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003098 private:
Aart Bike9f37602015-10-09 11:15:55 -07003099 template <typename T> bool Compute(T x, T y) const { return x == y; }
3100
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003101 DISALLOW_COPY_AND_ASSIGN(HEqual);
3102};
3103
Dave Allison20dfc792014-06-16 20:44:29 -07003104class HNotEqual : public HCondition {
3105 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003106 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3107 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003108
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003109 bool IsCommutative() const OVERRIDE { return true; }
3110
Vladimir Marko9e23df52015-11-10 17:14:35 +00003111 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3112 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003113 return MakeConstantCondition(false, GetDexPc());
3114 }
3115 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3116 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3117 }
3118 // In the following Evaluate methods, a HCompare instruction has
3119 // been merged into this HNotEqual instruction; evaluate it as
3120 // `Compare(x, y) != 0`.
3121 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3122 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3123 }
3124 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3125 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3126 }
3127 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3128 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003129 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003130
Dave Allison20dfc792014-06-16 20:44:29 -07003131 DECLARE_INSTRUCTION(NotEqual);
3132
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003133 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003134 return kCondNE;
3135 }
3136
Mark Mendellc4701932015-04-10 13:18:51 -04003137 IfCondition GetOppositeCondition() const OVERRIDE {
3138 return kCondEQ;
3139 }
3140
Dave Allison20dfc792014-06-16 20:44:29 -07003141 private:
Aart Bike9f37602015-10-09 11:15:55 -07003142 template <typename T> bool Compute(T x, T y) const { return x != y; }
3143
Dave Allison20dfc792014-06-16 20:44:29 -07003144 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3145};
3146
3147class HLessThan : public HCondition {
3148 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003149 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3150 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003151
Roland Levillain9867bc72015-08-05 10:21:34 +01003152 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003153 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003154 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003155 // In the following Evaluate methods, a HCompare instruction has
3156 // been merged into this HLessThan instruction; evaluate it as
3157 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003158 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003159 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3160 }
3161 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3162 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3163 }
3164 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3165 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003166 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003167
Dave Allison20dfc792014-06-16 20:44:29 -07003168 DECLARE_INSTRUCTION(LessThan);
3169
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003170 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003171 return kCondLT;
3172 }
3173
Mark Mendellc4701932015-04-10 13:18:51 -04003174 IfCondition GetOppositeCondition() const OVERRIDE {
3175 return kCondGE;
3176 }
3177
Dave Allison20dfc792014-06-16 20:44:29 -07003178 private:
Aart Bike9f37602015-10-09 11:15:55 -07003179 template <typename T> bool Compute(T x, T y) const { return x < y; }
3180
Dave Allison20dfc792014-06-16 20:44:29 -07003181 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3182};
3183
3184class HLessThanOrEqual : public HCondition {
3185 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003186 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3187 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003188
Roland Levillain9867bc72015-08-05 10:21:34 +01003189 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003190 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003191 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003192 // In the following Evaluate methods, a HCompare instruction has
3193 // been merged into this HLessThanOrEqual instruction; evaluate it as
3194 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003195 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003196 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3197 }
3198 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3199 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3200 }
3201 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3202 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003203 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003204
Dave Allison20dfc792014-06-16 20:44:29 -07003205 DECLARE_INSTRUCTION(LessThanOrEqual);
3206
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003207 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003208 return kCondLE;
3209 }
3210
Mark Mendellc4701932015-04-10 13:18:51 -04003211 IfCondition GetOppositeCondition() const OVERRIDE {
3212 return kCondGT;
3213 }
3214
Dave Allison20dfc792014-06-16 20:44:29 -07003215 private:
Aart Bike9f37602015-10-09 11:15:55 -07003216 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3217
Dave Allison20dfc792014-06-16 20:44:29 -07003218 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3219};
3220
3221class HGreaterThan : public HCondition {
3222 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003223 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3224 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003225
Roland Levillain9867bc72015-08-05 10:21:34 +01003226 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003227 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003228 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003229 // In the following Evaluate methods, a HCompare instruction has
3230 // been merged into this HGreaterThan instruction; evaluate it as
3231 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003232 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003233 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3234 }
3235 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3236 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3237 }
3238 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3239 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003240 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003241
Dave Allison20dfc792014-06-16 20:44:29 -07003242 DECLARE_INSTRUCTION(GreaterThan);
3243
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003244 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003245 return kCondGT;
3246 }
3247
Mark Mendellc4701932015-04-10 13:18:51 -04003248 IfCondition GetOppositeCondition() const OVERRIDE {
3249 return kCondLE;
3250 }
3251
Dave Allison20dfc792014-06-16 20:44:29 -07003252 private:
Aart Bike9f37602015-10-09 11:15:55 -07003253 template <typename T> bool Compute(T x, T y) const { return x > y; }
3254
Dave Allison20dfc792014-06-16 20:44:29 -07003255 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3256};
3257
3258class HGreaterThanOrEqual : public HCondition {
3259 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003260 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3261 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003262
Roland Levillain9867bc72015-08-05 10:21:34 +01003263 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003264 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003265 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003266 // In the following Evaluate methods, a HCompare instruction has
3267 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3268 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003269 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003270 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3271 }
3272 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3273 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3274 }
3275 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3276 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003277 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003278
Dave Allison20dfc792014-06-16 20:44:29 -07003279 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3280
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003281 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003282 return kCondGE;
3283 }
3284
Mark Mendellc4701932015-04-10 13:18:51 -04003285 IfCondition GetOppositeCondition() const OVERRIDE {
3286 return kCondLT;
3287 }
3288
Dave Allison20dfc792014-06-16 20:44:29 -07003289 private:
Aart Bike9f37602015-10-09 11:15:55 -07003290 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3291
Dave Allison20dfc792014-06-16 20:44:29 -07003292 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3293};
3294
Aart Bike9f37602015-10-09 11:15:55 -07003295class HBelow : public HCondition {
3296 public:
3297 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3298 : HCondition(first, second, dex_pc) {}
3299
3300 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003301 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003302 }
3303 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003304 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3305 }
3306 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3307 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3308 LOG(FATAL) << DebugName() << " is not defined for float values";
3309 UNREACHABLE();
3310 }
3311 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3312 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3313 LOG(FATAL) << DebugName() << " is not defined for double values";
3314 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003315 }
3316
3317 DECLARE_INSTRUCTION(Below);
3318
3319 IfCondition GetCondition() const OVERRIDE {
3320 return kCondB;
3321 }
3322
3323 IfCondition GetOppositeCondition() const OVERRIDE {
3324 return kCondAE;
3325 }
3326
3327 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003328 template <typename T> bool Compute(T x, T y) const {
3329 return MakeUnsigned(x) < MakeUnsigned(y);
3330 }
Aart Bike9f37602015-10-09 11:15:55 -07003331
3332 DISALLOW_COPY_AND_ASSIGN(HBelow);
3333};
3334
3335class HBelowOrEqual : public HCondition {
3336 public:
3337 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3338 : HCondition(first, second, dex_pc) {}
3339
3340 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003341 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003342 }
3343 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003344 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3345 }
3346 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3347 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3348 LOG(FATAL) << DebugName() << " is not defined for float values";
3349 UNREACHABLE();
3350 }
3351 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3352 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3353 LOG(FATAL) << DebugName() << " is not defined for double values";
3354 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003355 }
3356
3357 DECLARE_INSTRUCTION(BelowOrEqual);
3358
3359 IfCondition GetCondition() const OVERRIDE {
3360 return kCondBE;
3361 }
3362
3363 IfCondition GetOppositeCondition() const OVERRIDE {
3364 return kCondA;
3365 }
3366
3367 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003368 template <typename T> bool Compute(T x, T y) const {
3369 return MakeUnsigned(x) <= MakeUnsigned(y);
3370 }
Aart Bike9f37602015-10-09 11:15:55 -07003371
3372 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3373};
3374
3375class HAbove : public HCondition {
3376 public:
3377 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3378 : HCondition(first, second, dex_pc) {}
3379
3380 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003381 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003382 }
3383 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003384 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3385 }
3386 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3387 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3388 LOG(FATAL) << DebugName() << " is not defined for float values";
3389 UNREACHABLE();
3390 }
3391 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3392 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3393 LOG(FATAL) << DebugName() << " is not defined for double values";
3394 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003395 }
3396
3397 DECLARE_INSTRUCTION(Above);
3398
3399 IfCondition GetCondition() const OVERRIDE {
3400 return kCondA;
3401 }
3402
3403 IfCondition GetOppositeCondition() const OVERRIDE {
3404 return kCondBE;
3405 }
3406
3407 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003408 template <typename T> bool Compute(T x, T y) const {
3409 return MakeUnsigned(x) > MakeUnsigned(y);
3410 }
Aart Bike9f37602015-10-09 11:15:55 -07003411
3412 DISALLOW_COPY_AND_ASSIGN(HAbove);
3413};
3414
3415class HAboveOrEqual : public HCondition {
3416 public:
3417 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3418 : HCondition(first, second, dex_pc) {}
3419
3420 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003421 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003422 }
3423 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003424 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3425 }
3426 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3427 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3428 LOG(FATAL) << DebugName() << " is not defined for float values";
3429 UNREACHABLE();
3430 }
3431 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3432 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3433 LOG(FATAL) << DebugName() << " is not defined for double values";
3434 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003435 }
3436
3437 DECLARE_INSTRUCTION(AboveOrEqual);
3438
3439 IfCondition GetCondition() const OVERRIDE {
3440 return kCondAE;
3441 }
3442
3443 IfCondition GetOppositeCondition() const OVERRIDE {
3444 return kCondB;
3445 }
3446
3447 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003448 template <typename T> bool Compute(T x, T y) const {
3449 return MakeUnsigned(x) >= MakeUnsigned(y);
3450 }
Aart Bike9f37602015-10-09 11:15:55 -07003451
3452 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3453};
Dave Allison20dfc792014-06-16 20:44:29 -07003454
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003455// Instruction to check how two inputs compare to each other.
3456// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3457class HCompare : public HBinaryOperation {
3458 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003459 // Note that `comparison_type` is the type of comparison performed
3460 // between the comparison's inputs, not the type of the instantiated
3461 // HCompare instruction (which is always Primitive::kPrimInt).
3462 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003463 HInstruction* first,
3464 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003465 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003466 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003467 : HBinaryOperation(Primitive::kPrimInt,
3468 first,
3469 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003470 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003471 dex_pc) {
3472 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003473 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3474 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003475 }
3476
Roland Levillain9867bc72015-08-05 10:21:34 +01003477 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003478 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3479
3480 template <typename T>
3481 int32_t ComputeFP(T x, T y) const {
3482 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3483 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3484 // Handle the bias.
3485 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3486 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003487
Roland Levillain9867bc72015-08-05 10:21:34 +01003488 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003489 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3490 // reach this code path when processing a freshly built HIR
3491 // graph. However HCompare integer instructions can be synthesized
3492 // by the instruction simplifier to implement IntegerCompare and
3493 // IntegerSignum intrinsics, so we have to handle this case.
3494 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003495 }
3496 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003497 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3498 }
3499 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3500 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3501 }
3502 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3503 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003504 }
3505
Calin Juravleddb7df22014-11-25 20:56:51 +00003506 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003507 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003508 }
3509
Vladimir Markoa1de9182016-02-25 11:37:38 +00003510 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003511
Roland Levillain31dd3d62016-02-16 12:21:02 +00003512 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003513 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003514 bool IsGtBias() const {
3515 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003516 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003517 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003518
Roland Levillain1693a1f2016-03-15 14:57:31 +00003519 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3520 // Comparisons do not require a runtime call in any back end.
3521 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003522 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003523
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003524 DECLARE_INSTRUCTION(Compare);
3525
Roland Levillain31dd3d62016-02-16 12:21:02 +00003526 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003527 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3528 static constexpr size_t kFieldComparisonBiasSize =
3529 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3530 static constexpr size_t kNumberOfComparePackedBits =
3531 kFieldComparisonBias + kFieldComparisonBiasSize;
3532 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3533 using ComparisonBiasField =
3534 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3535
Roland Levillain31dd3d62016-02-16 12:21:02 +00003536 // Return an integer constant containing the result of a comparison evaluated at compile time.
3537 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3538 DCHECK(value == -1 || value == 0 || value == 1) << value;
3539 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3540 }
3541
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003542 private:
3543 DISALLOW_COPY_AND_ASSIGN(HCompare);
3544};
3545
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003546// A local in the graph. Corresponds to a Dex register.
3547class HLocal : public HTemplateInstruction<0> {
3548 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003549 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003550 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003551
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003552 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003553
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003554 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003555
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003556 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003557 // The Dex register number.
3558 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003559
3560 DISALLOW_COPY_AND_ASSIGN(HLocal);
3561};
3562
3563// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003564class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003565 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003566 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3567 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003568 SetRawInputAt(0, local);
3569 }
3570
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003571 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3572
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003573 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003574
3575 private:
3576 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3577};
3578
3579// Store a value in a given local. This instruction has two inputs: the value
3580// and the local.
3581class HStoreLocal : public HTemplateInstruction<2> {
3582 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003583 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3584 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003585 SetRawInputAt(0, local);
3586 SetRawInputAt(1, value);
3587 }
3588
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003589 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3590
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003591 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003592
3593 private:
3594 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3595};
3596
David Brazdil6de19382016-01-08 17:37:10 +00003597class HNewInstance : public HExpression<2> {
3598 public:
3599 HNewInstance(HInstruction* cls,
3600 HCurrentMethod* current_method,
3601 uint32_t dex_pc,
3602 uint16_t type_index,
3603 const DexFile& dex_file,
3604 bool can_throw,
3605 bool finalizable,
3606 QuickEntrypointEnum entrypoint)
3607 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3608 type_index_(type_index),
3609 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003610 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003611 SetPackedFlag<kFlagCanThrow>(can_throw);
3612 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003613 SetRawInputAt(0, cls);
3614 SetRawInputAt(1, current_method);
3615 }
3616
3617 uint16_t GetTypeIndex() const { return type_index_; }
3618 const DexFile& GetDexFile() const { return dex_file_; }
3619
3620 // Calls runtime so needs an environment.
3621 bool NeedsEnvironment() const OVERRIDE { return true; }
3622
3623 // It may throw when called on type that's not instantiable/accessible.
3624 // It can throw OOME.
3625 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003626 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>() || true; }
David Brazdil6de19382016-01-08 17:37:10 +00003627
Vladimir Markoa1de9182016-02-25 11:37:38 +00003628 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003629
3630 bool CanBeNull() const OVERRIDE { return false; }
3631
3632 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3633
3634 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3635 entrypoint_ = entrypoint;
3636 }
3637
3638 bool IsStringAlloc() const;
3639
3640 DECLARE_INSTRUCTION(NewInstance);
3641
3642 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003643 static constexpr size_t kFlagCanThrow = kNumberOfExpressionPackedBits;
3644 static constexpr size_t kFlagFinalizable = kFlagCanThrow + 1;
3645 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3646 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3647 "Too many packed fields.");
3648
David Brazdil6de19382016-01-08 17:37:10 +00003649 const uint16_t type_index_;
3650 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003651 QuickEntrypointEnum entrypoint_;
3652
3653 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3654};
3655
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003656enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003657#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3658 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003659#include "intrinsics_list.h"
3660 kNone,
3661 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3662#undef INTRINSICS_LIST
3663#undef OPTIMIZING_INTRINSICS
3664};
3665std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3666
Agi Csaki05f20562015-08-19 14:58:14 -07003667enum IntrinsicNeedsEnvironmentOrCache {
3668 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3669 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003670};
3671
Aart Bik5d75afe2015-12-14 11:57:01 -08003672enum IntrinsicSideEffects {
3673 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3674 kReadSideEffects, // Intrinsic may read heap memory.
3675 kWriteSideEffects, // Intrinsic may write heap memory.
3676 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3677};
3678
3679enum IntrinsicExceptions {
3680 kNoThrow, // Intrinsic does not throw any exceptions.
3681 kCanThrow // Intrinsic may throw exceptions.
3682};
3683
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003684class HInvoke : public HInstruction {
3685 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003686 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003687
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003688 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003689
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003690 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003691 SetRawInputAt(index, argument);
3692 }
3693
Roland Levillain3e3d7332015-04-28 11:00:54 +01003694 // Return the number of arguments. This number can be lower than
3695 // the number of inputs returned by InputCount(), as some invoke
3696 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3697 // inputs at the end of their list of inputs.
3698 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3699
Vladimir Markoa1de9182016-02-25 11:37:38 +00003700 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003701
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003702 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003703 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003704
Vladimir Markoa1de9182016-02-25 11:37:38 +00003705 InvokeType GetOriginalInvokeType() const {
3706 return GetPackedField<OriginalInvokeTypeField>();
3707 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003708
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003709 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003710 return intrinsic_;
3711 }
3712
Aart Bik5d75afe2015-12-14 11:57:01 -08003713 void SetIntrinsic(Intrinsics intrinsic,
3714 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3715 IntrinsicSideEffects side_effects,
3716 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003717
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003718 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003719 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003720 }
3721
Vladimir Markoa1de9182016-02-25 11:37:38 +00003722 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003723
3724 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3725
3726 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3727 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3728 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003729
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003730 uint32_t* GetIntrinsicOptimizations() {
3731 return &intrinsic_optimizations_;
3732 }
3733
3734 const uint32_t* GetIntrinsicOptimizations() const {
3735 return &intrinsic_optimizations_;
3736 }
3737
3738 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3739
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003740 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003741
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003742 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003743 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3744 static constexpr size_t kFieldOriginalInvokeTypeSize =
3745 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3746 static constexpr size_t kFieldReturnType =
3747 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3748 static constexpr size_t kFieldReturnTypeSize =
3749 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3750 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3751 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3752 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3753 using OriginalInvokeTypeField =
3754 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3755 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3756
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003757 HInvoke(ArenaAllocator* arena,
3758 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003759 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003760 Primitive::Type return_type,
3761 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003762 uint32_t dex_method_index,
3763 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003764 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003765 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003766 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003767 inputs_(number_of_arguments + number_of_other_inputs,
3768 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003769 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003770 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003771 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003772 SetPackedField<ReturnTypeField>(return_type);
3773 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3774 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003775 }
3776
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003777 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003778 return inputs_[index];
3779 }
3780
David Brazdil1abb4192015-02-17 18:33:36 +00003781 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003782 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003783 }
3784
Vladimir Markoa1de9182016-02-25 11:37:38 +00003785 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003786
Roland Levillain3e3d7332015-04-28 11:00:54 +01003787 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003788 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003789 const uint32_t dex_method_index_;
3790 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003791
3792 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3793 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003794
3795 private:
3796 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3797};
3798
Calin Juravle175dc732015-08-25 15:42:32 +01003799class HInvokeUnresolved : public HInvoke {
3800 public:
3801 HInvokeUnresolved(ArenaAllocator* arena,
3802 uint32_t number_of_arguments,
3803 Primitive::Type return_type,
3804 uint32_t dex_pc,
3805 uint32_t dex_method_index,
3806 InvokeType invoke_type)
3807 : HInvoke(arena,
3808 number_of_arguments,
3809 0u /* number_of_other_inputs */,
3810 return_type,
3811 dex_pc,
3812 dex_method_index,
3813 invoke_type) {
3814 }
3815
3816 DECLARE_INSTRUCTION(InvokeUnresolved);
3817
3818 private:
3819 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3820};
3821
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003822class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003823 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003824 // Requirements of this method call regarding the class
3825 // initialization (clinit) check of its declaring class.
3826 enum class ClinitCheckRequirement {
3827 kNone, // Class already initialized.
3828 kExplicit, // Static call having explicit clinit check as last input.
3829 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003830 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003831 };
3832
Vladimir Marko58155012015-08-19 12:49:41 +00003833 // Determines how to load the target ArtMethod*.
3834 enum class MethodLoadKind {
3835 // Use a String init ArtMethod* loaded from Thread entrypoints.
3836 kStringInit,
3837
3838 // Use the method's own ArtMethod* loaded by the register allocator.
3839 kRecursive,
3840
3841 // Use ArtMethod* at a known address, embed the direct address in the code.
3842 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3843 kDirectAddress,
3844
3845 // Use ArtMethod* at an address that will be known at link time, embed the direct
3846 // address in the code. If the image is relocatable, emit .patch_oat entry.
3847 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3848 // the image relocatable or not.
3849 kDirectAddressWithFixup,
3850
Vladimir Markoa1de9182016-02-25 11:37:38 +00003851 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003852 // Used when we need to use the dex cache, for example for invoke-static that
3853 // may cause class initialization (the entry may point to a resolution method),
3854 // and we know that we can access the dex cache arrays using a PC-relative load.
3855 kDexCachePcRelative,
3856
3857 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3858 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3859 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3860 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3861 kDexCacheViaMethod,
3862 };
3863
3864 // Determines the location of the code pointer.
3865 enum class CodePtrLocation {
3866 // Recursive call, use local PC-relative call instruction.
3867 kCallSelf,
3868
3869 // Use PC-relative call instruction patched at link time.
3870 // Used for calls within an oat file, boot->boot or app->app.
3871 kCallPCRelative,
3872
3873 // Call to a known target address, embed the direct address in code.
3874 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3875 kCallDirect,
3876
3877 // Call to a target address that will be known at link time, embed the direct
3878 // address in code. If the image is relocatable, emit .patch_oat entry.
3879 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3880 // the image relocatable or not.
3881 kCallDirectWithFixup,
3882
3883 // Use code pointer from the ArtMethod*.
3884 // Used when we don't know the target code. This is also the last-resort-kind used when
3885 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3886 kCallArtMethod,
3887 };
3888
3889 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003890 MethodLoadKind method_load_kind;
3891 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003892 // The method load data holds
3893 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3894 // Note that there are multiple string init methods, each having its own offset.
3895 // - the method address for kDirectAddress
3896 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003897 uint64_t method_load_data;
3898 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003899 };
3900
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003901 HInvokeStaticOrDirect(ArenaAllocator* arena,
3902 uint32_t number_of_arguments,
3903 Primitive::Type return_type,
3904 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003905 uint32_t method_index,
3906 MethodReference target_method,
3907 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003908 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003909 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003910 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003911 : HInvoke(arena,
3912 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003913 // There is potentially one extra argument for the HCurrentMethod node, and
3914 // potentially one other if the clinit check is explicit, and potentially
3915 // one other if the method is a string factory.
3916 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3917 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3918 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003919 return_type,
3920 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003921 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003922 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003923 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003924 dispatch_info_(dispatch_info) {
3925 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3926 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3927 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003928
Vladimir Markodc151b22015-10-15 18:02:30 +01003929 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003930 bool had_current_method_input = HasCurrentMethodInput();
3931 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3932
3933 // Using the current method is the default and once we find a better
3934 // method load kind, we should not go back to using the current method.
3935 DCHECK(had_current_method_input || !needs_current_method_input);
3936
3937 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003938 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3939 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003940 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003941 dispatch_info_ = dispatch_info;
3942 }
3943
Vladimir Markoc53c0792015-11-19 15:48:33 +00003944 void AddSpecialInput(HInstruction* input) {
3945 // We allow only one special input.
3946 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3947 DCHECK(InputCount() == GetSpecialInputIndex() ||
3948 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3949 InsertInputAt(GetSpecialInputIndex(), input);
3950 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003951
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003952 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003953 // We access the method via the dex cache so we can't do an implicit null check.
3954 // TODO: for intrinsics we can generate implicit null checks.
3955 return false;
3956 }
3957
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003958 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003959 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003960 }
3961
Vladimir Markoc53c0792015-11-19 15:48:33 +00003962 // Get the index of the special input, if any.
3963 //
David Brazdil6de19382016-01-08 17:37:10 +00003964 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3965 // method pointer; otherwise there may be one platform-specific special input,
3966 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003967 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003968 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003969
Vladimir Markoa1de9182016-02-25 11:37:38 +00003970 InvokeType GetOptimizedInvokeType() const {
3971 return GetPackedField<OptimizedInvokeTypeField>();
3972 }
3973
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003974 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003975 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003976 }
3977
Vladimir Marko58155012015-08-19 12:49:41 +00003978 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3979 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3980 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003981 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003982 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003983 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003984 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003985 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3986 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003987 bool HasCurrentMethodInput() const {
3988 // This function can be called only after the invoke has been fully initialized by the builder.
3989 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003990 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003991 return true;
3992 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003993 DCHECK(InputCount() == GetSpecialInputIndex() ||
3994 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003995 return false;
3996 }
3997 }
Vladimir Marko58155012015-08-19 12:49:41 +00003998 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3999 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004000 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00004001
4002 int32_t GetStringInitOffset() const {
4003 DCHECK(IsStringInit());
4004 return dispatch_info_.method_load_data;
4005 }
4006
4007 uint64_t GetMethodAddress() const {
4008 DCHECK(HasMethodAddress());
4009 return dispatch_info_.method_load_data;
4010 }
4011
4012 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004013 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004014 return dispatch_info_.method_load_data;
4015 }
4016
4017 uint64_t GetDirectCodePtr() const {
4018 DCHECK(HasDirectCodePtr());
4019 return dispatch_info_.direct_code_ptr;
4020 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004021
Vladimir Markoa1de9182016-02-25 11:37:38 +00004022 ClinitCheckRequirement GetClinitCheckRequirement() const {
4023 return GetPackedField<ClinitCheckRequirementField>();
4024 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004025
Roland Levillain4c0eb422015-04-24 16:43:49 +01004026 // Is this instruction a call to a static method?
4027 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004028 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004029 }
4030
Vladimir Markofbb184a2015-11-13 14:47:00 +00004031 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4032 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4033 // instruction; only relevant for static calls with explicit clinit check.
4034 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004035 DCHECK(IsStaticWithExplicitClinitCheck());
4036 size_t last_input_index = InputCount() - 1;
4037 HInstruction* last_input = InputAt(last_input_index);
4038 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004039 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004040 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004041 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004042 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004043 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004044 }
4045
David Brazdilbc9ab162016-01-20 14:50:19 +00004046 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00004047 DCHECK(IsStringInit());
4048 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00004049 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00004050 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004051 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00004052 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004053 }
4054
Roland Levillain4c0eb422015-04-24 16:43:49 +01004055 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004056 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004057 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004058 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004059 }
4060
4061 // Is this a call to a static method whose declaring class has an
4062 // implicit intialization check requirement?
4063 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004064 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004065 }
4066
Vladimir Markob554b5a2015-11-06 12:57:55 +00004067 // Does this method load kind need the current method as an input?
4068 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4069 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4070 }
4071
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004072 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004073
Roland Levillain4c0eb422015-04-24 16:43:49 +01004074 protected:
4075 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
4076 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
4077 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
4078 HInstruction* input = input_record.GetInstruction();
4079 // `input` is the last input of a static invoke marked as having
4080 // an explicit clinit check. It must either be:
4081 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4082 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4083 DCHECK(input != nullptr);
4084 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
4085 }
4086 return input_record;
4087 }
4088
Vladimir Markoc53c0792015-11-19 15:48:33 +00004089 void InsertInputAt(size_t index, HInstruction* input);
4090 void RemoveInputAt(size_t index);
4091
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004092 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004093 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4094 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4095 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4096 static constexpr size_t kFieldClinitCheckRequirement =
4097 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4098 static constexpr size_t kFieldClinitCheckRequirementSize =
4099 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4100 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4101 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4102 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4103 "Too many packed fields.");
4104 using OptimizedInvokeTypeField =
4105 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4106 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4107 kFieldClinitCheckRequirement,
4108 kFieldClinitCheckRequirementSize>;
4109
Vladimir Marko58155012015-08-19 12:49:41 +00004110 // The target method may refer to different dex file or method index than the original
4111 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4112 // in derived class to increase visibility.
4113 MethodReference target_method_;
4114 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004115
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004116 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004117};
Vladimir Markof64242a2015-12-01 14:58:23 +00004118std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004119std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004120
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004121class HInvokeVirtual : public HInvoke {
4122 public:
4123 HInvokeVirtual(ArenaAllocator* arena,
4124 uint32_t number_of_arguments,
4125 Primitive::Type return_type,
4126 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004127 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004128 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004129 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004130 vtable_index_(vtable_index) {}
4131
Calin Juravle641547a2015-04-21 22:08:51 +01004132 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004133 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004134 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004135 }
4136
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004137 uint32_t GetVTableIndex() const { return vtable_index_; }
4138
4139 DECLARE_INSTRUCTION(InvokeVirtual);
4140
4141 private:
4142 const uint32_t vtable_index_;
4143
4144 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4145};
4146
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004147class HInvokeInterface : public HInvoke {
4148 public:
4149 HInvokeInterface(ArenaAllocator* arena,
4150 uint32_t number_of_arguments,
4151 Primitive::Type return_type,
4152 uint32_t dex_pc,
4153 uint32_t dex_method_index,
4154 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004155 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004156 imt_index_(imt_index) {}
4157
Calin Juravle641547a2015-04-21 22:08:51 +01004158 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004159 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004160 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004161 }
4162
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004163 uint32_t GetImtIndex() const { return imt_index_; }
4164 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4165
4166 DECLARE_INSTRUCTION(InvokeInterface);
4167
4168 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004169 const uint32_t imt_index_;
4170
4171 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4172};
4173
Roland Levillain88cb1752014-10-20 16:36:47 +01004174class HNeg : public HUnaryOperation {
4175 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004176 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004177 : HUnaryOperation(result_type, input, dex_pc) {
4178 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4179 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004180
Roland Levillain9867bc72015-08-05 10:21:34 +01004181 template <typename T> T Compute(T x) const { return -x; }
4182
4183 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004184 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004185 }
4186 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004187 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004188 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004189 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4190 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4191 }
4192 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4193 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4194 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004195
Roland Levillain88cb1752014-10-20 16:36:47 +01004196 DECLARE_INSTRUCTION(Neg);
4197
4198 private:
4199 DISALLOW_COPY_AND_ASSIGN(HNeg);
4200};
4201
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004202class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004203 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004204 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004205 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004206 uint32_t dex_pc,
4207 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004208 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004209 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004210 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004211 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004212 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004213 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004214 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004215 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004216 }
4217
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004218 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004219 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004220
4221 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004222 bool NeedsEnvironment() const OVERRIDE { return true; }
4223
Mingyao Yang0c365e62015-03-31 15:09:29 -07004224 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4225 bool CanThrow() const OVERRIDE { return true; }
4226
Calin Juravle10e244f2015-01-26 18:54:32 +00004227 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004228
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004229 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4230
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004231 DECLARE_INSTRUCTION(NewArray);
4232
4233 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004234 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004235 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004236 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004237
4238 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4239};
4240
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004241class HAdd : public HBinaryOperation {
4242 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004243 HAdd(Primitive::Type result_type,
4244 HInstruction* left,
4245 HInstruction* right,
4246 uint32_t dex_pc = kNoDexPc)
4247 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004248
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004249 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004250
Roland Levillain9867bc72015-08-05 10:21:34 +01004251 template <typename T> T Compute(T x, T y) const { return x + y; }
4252
4253 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004254 return GetBlock()->GetGraph()->GetIntConstant(
4255 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004256 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004257 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004258 return GetBlock()->GetGraph()->GetLongConstant(
4259 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004260 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004261 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4262 return GetBlock()->GetGraph()->GetFloatConstant(
4263 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4264 }
4265 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4266 return GetBlock()->GetGraph()->GetDoubleConstant(
4267 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4268 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004269
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004270 DECLARE_INSTRUCTION(Add);
4271
4272 private:
4273 DISALLOW_COPY_AND_ASSIGN(HAdd);
4274};
4275
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004276class HSub : public HBinaryOperation {
4277 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004278 HSub(Primitive::Type result_type,
4279 HInstruction* left,
4280 HInstruction* right,
4281 uint32_t dex_pc = kNoDexPc)
4282 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004283
Roland Levillain9867bc72015-08-05 10:21:34 +01004284 template <typename T> T Compute(T x, T y) const { return x - y; }
4285
4286 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004287 return GetBlock()->GetGraph()->GetIntConstant(
4288 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004289 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004290 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004291 return GetBlock()->GetGraph()->GetLongConstant(
4292 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004293 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004294 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4295 return GetBlock()->GetGraph()->GetFloatConstant(
4296 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4297 }
4298 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4299 return GetBlock()->GetGraph()->GetDoubleConstant(
4300 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4301 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004302
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004303 DECLARE_INSTRUCTION(Sub);
4304
4305 private:
4306 DISALLOW_COPY_AND_ASSIGN(HSub);
4307};
4308
Calin Juravle34bacdf2014-10-07 20:23:36 +01004309class HMul : public HBinaryOperation {
4310 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004311 HMul(Primitive::Type result_type,
4312 HInstruction* left,
4313 HInstruction* right,
4314 uint32_t dex_pc = kNoDexPc)
4315 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004316
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004317 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004318
Roland Levillain9867bc72015-08-05 10:21:34 +01004319 template <typename T> T Compute(T x, T y) const { return x * y; }
4320
4321 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004322 return GetBlock()->GetGraph()->GetIntConstant(
4323 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004324 }
4325 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004326 return GetBlock()->GetGraph()->GetLongConstant(
4327 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004328 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004329 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4330 return GetBlock()->GetGraph()->GetFloatConstant(
4331 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4332 }
4333 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4334 return GetBlock()->GetGraph()->GetDoubleConstant(
4335 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4336 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004337
4338 DECLARE_INSTRUCTION(Mul);
4339
4340 private:
4341 DISALLOW_COPY_AND_ASSIGN(HMul);
4342};
4343
Calin Juravle7c4954d2014-10-28 16:57:40 +00004344class HDiv : public HBinaryOperation {
4345 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004346 HDiv(Primitive::Type result_type,
4347 HInstruction* left,
4348 HInstruction* right,
4349 uint32_t dex_pc)
4350 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004351
Roland Levillain9867bc72015-08-05 10:21:34 +01004352 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004353 T ComputeIntegral(T x, T y) const {
4354 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004355 // Our graph structure ensures we never have 0 for `y` during
4356 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004357 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004358 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004359 return (y == -1) ? -x : x / y;
4360 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004361
Roland Levillain31dd3d62016-02-16 12:21:02 +00004362 template <typename T>
4363 T ComputeFP(T x, T y) const {
4364 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4365 return x / y;
4366 }
4367
Roland Levillain9867bc72015-08-05 10:21:34 +01004368 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004369 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004370 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004371 }
4372 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004373 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004374 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4375 }
4376 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4377 return GetBlock()->GetGraph()->GetFloatConstant(
4378 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4379 }
4380 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4381 return GetBlock()->GetGraph()->GetDoubleConstant(
4382 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004383 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004384
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004385 static SideEffects SideEffectsForArchRuntimeCalls() {
4386 // The generated code can use a runtime call.
4387 return SideEffects::CanTriggerGC();
4388 }
4389
Calin Juravle7c4954d2014-10-28 16:57:40 +00004390 DECLARE_INSTRUCTION(Div);
4391
4392 private:
4393 DISALLOW_COPY_AND_ASSIGN(HDiv);
4394};
4395
Calin Juravlebacfec32014-11-14 15:54:36 +00004396class HRem : public HBinaryOperation {
4397 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004398 HRem(Primitive::Type result_type,
4399 HInstruction* left,
4400 HInstruction* right,
4401 uint32_t dex_pc)
4402 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004403
Roland Levillain9867bc72015-08-05 10:21:34 +01004404 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004405 T ComputeIntegral(T x, T y) const {
4406 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004407 // Our graph structure ensures we never have 0 for `y` during
4408 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004409 DCHECK_NE(y, 0);
4410 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4411 return (y == -1) ? 0 : x % y;
4412 }
4413
Roland Levillain31dd3d62016-02-16 12:21:02 +00004414 template <typename T>
4415 T ComputeFP(T x, T y) const {
4416 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4417 return std::fmod(x, y);
4418 }
4419
Roland Levillain9867bc72015-08-05 10:21:34 +01004420 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004421 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004422 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004423 }
4424 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004425 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004426 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004427 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004428 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4429 return GetBlock()->GetGraph()->GetFloatConstant(
4430 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4431 }
4432 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4433 return GetBlock()->GetGraph()->GetDoubleConstant(
4434 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4435 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004436
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004437 static SideEffects SideEffectsForArchRuntimeCalls() {
4438 return SideEffects::CanTriggerGC();
4439 }
4440
Calin Juravlebacfec32014-11-14 15:54:36 +00004441 DECLARE_INSTRUCTION(Rem);
4442
4443 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004444 DISALLOW_COPY_AND_ASSIGN(HRem);
4445};
4446
Calin Juravled0d48522014-11-04 16:40:20 +00004447class HDivZeroCheck : public HExpression<1> {
4448 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004449 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4450 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004451 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004452 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004453 SetRawInputAt(0, value);
4454 }
4455
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004456 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4457
Calin Juravled0d48522014-11-04 16:40:20 +00004458 bool CanBeMoved() const OVERRIDE { return true; }
4459
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004460 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004461 return true;
4462 }
4463
4464 bool NeedsEnvironment() const OVERRIDE { return true; }
4465 bool CanThrow() const OVERRIDE { return true; }
4466
Calin Juravled0d48522014-11-04 16:40:20 +00004467 DECLARE_INSTRUCTION(DivZeroCheck);
4468
4469 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004470 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4471};
4472
Calin Juravle9aec02f2014-11-18 23:06:35 +00004473class HShl : public HBinaryOperation {
4474 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004475 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004476 HInstruction* value,
4477 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004478 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004479 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4480 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4481 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004482 }
4483
Roland Levillain5b5b9312016-03-22 14:57:31 +00004484 template <typename T>
4485 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4486 return value << (distance & max_shift_distance);
4487 }
4488
4489 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004490 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004491 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004492 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004493 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004494 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004495 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004496 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004497 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4498 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4499 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4500 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004501 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004502 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4503 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004504 LOG(FATAL) << DebugName() << " is not defined for float values";
4505 UNREACHABLE();
4506 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004507 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4508 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004509 LOG(FATAL) << DebugName() << " is not defined for double values";
4510 UNREACHABLE();
4511 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004512
4513 DECLARE_INSTRUCTION(Shl);
4514
4515 private:
4516 DISALLOW_COPY_AND_ASSIGN(HShl);
4517};
4518
4519class HShr : public HBinaryOperation {
4520 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004521 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004522 HInstruction* value,
4523 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004524 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004525 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4526 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4527 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004528 }
4529
Roland Levillain5b5b9312016-03-22 14:57:31 +00004530 template <typename T>
4531 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4532 return value >> (distance & max_shift_distance);
4533 }
4534
4535 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004536 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004537 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004538 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004539 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004540 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004541 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004542 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004543 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4544 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4545 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4546 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004547 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004548 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4549 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004550 LOG(FATAL) << DebugName() << " is not defined for float values";
4551 UNREACHABLE();
4552 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004553 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4554 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004555 LOG(FATAL) << DebugName() << " is not defined for double values";
4556 UNREACHABLE();
4557 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004558
4559 DECLARE_INSTRUCTION(Shr);
4560
4561 private:
4562 DISALLOW_COPY_AND_ASSIGN(HShr);
4563};
4564
4565class HUShr : public HBinaryOperation {
4566 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004567 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004568 HInstruction* value,
4569 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004570 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004571 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4572 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4573 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004574 }
4575
Roland Levillain5b5b9312016-03-22 14:57:31 +00004576 template <typename T>
4577 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4578 typedef typename std::make_unsigned<T>::type V;
4579 V ux = static_cast<V>(value);
4580 return static_cast<T>(ux >> (distance & max_shift_distance));
4581 }
4582
4583 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004584 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004585 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004586 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004587 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004588 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004589 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004590 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004591 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4592 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4593 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4594 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004595 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004596 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4597 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004598 LOG(FATAL) << DebugName() << " is not defined for float values";
4599 UNREACHABLE();
4600 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004601 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4602 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004603 LOG(FATAL) << DebugName() << " is not defined for double values";
4604 UNREACHABLE();
4605 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004606
4607 DECLARE_INSTRUCTION(UShr);
4608
4609 private:
4610 DISALLOW_COPY_AND_ASSIGN(HUShr);
4611};
4612
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004613class HAnd : public HBinaryOperation {
4614 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004615 HAnd(Primitive::Type result_type,
4616 HInstruction* left,
4617 HInstruction* right,
4618 uint32_t dex_pc = kNoDexPc)
4619 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004620
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004621 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004622
Roland Levillaine53bd812016-02-24 14:54:18 +00004623 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004624
4625 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004626 return GetBlock()->GetGraph()->GetIntConstant(
4627 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004628 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004629 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004630 return GetBlock()->GetGraph()->GetLongConstant(
4631 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004632 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004633 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4634 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4635 LOG(FATAL) << DebugName() << " is not defined for float values";
4636 UNREACHABLE();
4637 }
4638 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4639 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4640 LOG(FATAL) << DebugName() << " is not defined for double values";
4641 UNREACHABLE();
4642 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004643
4644 DECLARE_INSTRUCTION(And);
4645
4646 private:
4647 DISALLOW_COPY_AND_ASSIGN(HAnd);
4648};
4649
4650class HOr : public HBinaryOperation {
4651 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004652 HOr(Primitive::Type result_type,
4653 HInstruction* left,
4654 HInstruction* right,
4655 uint32_t dex_pc = kNoDexPc)
4656 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004657
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004658 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004659
Roland Levillaine53bd812016-02-24 14:54:18 +00004660 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004661
4662 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004663 return GetBlock()->GetGraph()->GetIntConstant(
4664 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004665 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004666 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004667 return GetBlock()->GetGraph()->GetLongConstant(
4668 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004669 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004670 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4671 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4672 LOG(FATAL) << DebugName() << " is not defined for float values";
4673 UNREACHABLE();
4674 }
4675 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4676 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4677 LOG(FATAL) << DebugName() << " is not defined for double values";
4678 UNREACHABLE();
4679 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004680
4681 DECLARE_INSTRUCTION(Or);
4682
4683 private:
4684 DISALLOW_COPY_AND_ASSIGN(HOr);
4685};
4686
4687class HXor : public HBinaryOperation {
4688 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004689 HXor(Primitive::Type result_type,
4690 HInstruction* left,
4691 HInstruction* right,
4692 uint32_t dex_pc = kNoDexPc)
4693 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004694
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004695 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004696
Roland Levillaine53bd812016-02-24 14:54:18 +00004697 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004698
4699 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004700 return GetBlock()->GetGraph()->GetIntConstant(
4701 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004702 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004703 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004704 return GetBlock()->GetGraph()->GetLongConstant(
4705 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004706 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004707 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4708 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4709 LOG(FATAL) << DebugName() << " is not defined for float values";
4710 UNREACHABLE();
4711 }
4712 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4713 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4714 LOG(FATAL) << DebugName() << " is not defined for double values";
4715 UNREACHABLE();
4716 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004717
4718 DECLARE_INSTRUCTION(Xor);
4719
4720 private:
4721 DISALLOW_COPY_AND_ASSIGN(HXor);
4722};
4723
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004724class HRor : public HBinaryOperation {
4725 public:
4726 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004727 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004728 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4729 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004730 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004731
Roland Levillain5b5b9312016-03-22 14:57:31 +00004732 template <typename T>
4733 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4734 typedef typename std::make_unsigned<T>::type V;
4735 V ux = static_cast<V>(value);
4736 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004737 return static_cast<T>(ux);
4738 } else {
4739 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004740 return static_cast<T>(ux >> (distance & max_shift_value)) |
4741 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004742 }
4743 }
4744
Roland Levillain5b5b9312016-03-22 14:57:31 +00004745 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004746 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004747 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004748 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004749 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004750 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004751 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004752 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004753 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4754 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4755 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4756 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004757 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004758 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4759 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004760 LOG(FATAL) << DebugName() << " is not defined for float values";
4761 UNREACHABLE();
4762 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004763 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4764 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004765 LOG(FATAL) << DebugName() << " is not defined for double values";
4766 UNREACHABLE();
4767 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004768
4769 DECLARE_INSTRUCTION(Ror);
4770
4771 private:
4772 DISALLOW_COPY_AND_ASSIGN(HRor);
4773};
4774
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004775// The value of a parameter in this method. Its location depends on
4776// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004777class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004778 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004779 HParameterValue(const DexFile& dex_file,
4780 uint16_t type_index,
4781 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004782 Primitive::Type parameter_type,
4783 bool is_this = false)
4784 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004785 dex_file_(dex_file),
4786 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004787 index_(index) {
4788 SetPackedFlag<kFlagIsThis>(is_this);
4789 SetPackedFlag<kFlagCanBeNull>(!is_this);
4790 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004791
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004792 const DexFile& GetDexFile() const { return dex_file_; }
4793 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004794 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004795 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004796
Vladimir Markoa1de9182016-02-25 11:37:38 +00004797 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4798 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004799
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004800 DECLARE_INSTRUCTION(ParameterValue);
4801
4802 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004803 // Whether or not the parameter value corresponds to 'this' argument.
4804 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4805 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4806 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4807 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4808 "Too many packed fields.");
4809
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004810 const DexFile& dex_file_;
4811 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004812 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004813 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004814 const uint8_t index_;
4815
4816 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4817};
4818
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004819class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004820 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004821 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4822 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004823
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004824 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004825 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004826 return true;
4827 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004828
Roland Levillain9867bc72015-08-05 10:21:34 +01004829 template <typename T> T Compute(T x) const { return ~x; }
4830
4831 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004832 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004833 }
4834 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004835 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004836 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004837 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4838 LOG(FATAL) << DebugName() << " is not defined for float values";
4839 UNREACHABLE();
4840 }
4841 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4842 LOG(FATAL) << DebugName() << " is not defined for double values";
4843 UNREACHABLE();
4844 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004845
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004846 DECLARE_INSTRUCTION(Not);
4847
4848 private:
4849 DISALLOW_COPY_AND_ASSIGN(HNot);
4850};
4851
David Brazdil66d126e2015-04-03 16:02:44 +01004852class HBooleanNot : public HUnaryOperation {
4853 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004854 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4855 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004856
4857 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004858 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004859 return true;
4860 }
4861
Roland Levillain9867bc72015-08-05 10:21:34 +01004862 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004863 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004864 return !x;
4865 }
4866
Roland Levillain9867bc72015-08-05 10:21:34 +01004867 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004868 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004869 }
4870 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4871 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004872 UNREACHABLE();
4873 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004874 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4875 LOG(FATAL) << DebugName() << " is not defined for float values";
4876 UNREACHABLE();
4877 }
4878 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4879 LOG(FATAL) << DebugName() << " is not defined for double values";
4880 UNREACHABLE();
4881 }
David Brazdil66d126e2015-04-03 16:02:44 +01004882
4883 DECLARE_INSTRUCTION(BooleanNot);
4884
4885 private:
4886 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4887};
4888
Roland Levillaindff1f282014-11-05 14:15:05 +00004889class HTypeConversion : public HExpression<1> {
4890 public:
4891 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004892 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004893 : HExpression(result_type,
4894 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4895 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004896 SetRawInputAt(0, input);
4897 DCHECK_NE(input->GetType(), result_type);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004898 // Invariant: We should never generate a conversion to a Boolean value.
4899 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004900 }
4901
4902 HInstruction* GetInput() const { return InputAt(0); }
4903 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4904 Primitive::Type GetResultType() const { return GetType(); }
4905
4906 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004907 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004908
Mark Mendelle82549b2015-05-06 10:55:34 -04004909 // Try to statically evaluate the conversion and return a HConstant
4910 // containing the result. If the input cannot be converted, return nullptr.
4911 HConstant* TryStaticEvaluation() const;
4912
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004913 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4914 Primitive::Type result_type) {
4915 // Some architectures may not require the 'GC' side effects, but at this point
4916 // in the compilation process we do not know what architecture we will
4917 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004918 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4919 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004920 return SideEffects::CanTriggerGC();
4921 }
4922 return SideEffects::None();
4923 }
4924
Roland Levillaindff1f282014-11-05 14:15:05 +00004925 DECLARE_INSTRUCTION(TypeConversion);
4926
4927 private:
4928 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4929};
4930
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004931static constexpr uint32_t kNoRegNumber = -1;
4932
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004933class HPhi : public HInstruction {
4934 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004935 HPhi(ArenaAllocator* arena,
4936 uint32_t reg_number,
4937 size_t number_of_inputs,
4938 Primitive::Type type,
4939 uint32_t dex_pc = kNoDexPc)
4940 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004941 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004942 reg_number_(reg_number) {
4943 SetPackedField<TypeField>(ToPhiType(type));
4944 DCHECK_NE(GetType(), Primitive::kPrimVoid);
4945 // Phis are constructed live and marked dead if conflicting or unused.
4946 // Individual steps of SsaBuilder should assume that if a phi has been
4947 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4948 SetPackedFlag<kFlagIsLive>(true);
4949 SetPackedFlag<kFlagCanBeNull>(true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004950 }
4951
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004952 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4953 static Primitive::Type ToPhiType(Primitive::Type type) {
4954 switch (type) {
4955 case Primitive::kPrimBoolean:
4956 case Primitive::kPrimByte:
4957 case Primitive::kPrimShort:
4958 case Primitive::kPrimChar:
4959 return Primitive::kPrimInt;
4960 default:
4961 return type;
4962 }
4963 }
4964
David Brazdilffee3d32015-07-06 11:48:53 +01004965 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4966
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004967 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004968
4969 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004970 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004971
Vladimir Markoa1de9182016-02-25 11:37:38 +00004972 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
David Brazdil4833f5a2015-12-16 10:37:39 +00004973 void SetType(Primitive::Type new_type) {
4974 // Make sure that only valid type changes occur. The following are allowed:
4975 // (1) int -> float/ref (primitive type propagation),
4976 // (2) long -> double (primitive type propagation).
Vladimir Markoa1de9182016-02-25 11:37:38 +00004977 DCHECK(GetType() == new_type ||
4978 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4979 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4980 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4981 SetPackedField<TypeField>(new_type);
David Brazdil4833f5a2015-12-16 10:37:39 +00004982 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004983
Vladimir Markoa1de9182016-02-25 11:37:38 +00004984 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4985 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004986
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004987 uint32_t GetRegNumber() const { return reg_number_; }
4988
Vladimir Markoa1de9182016-02-25 11:37:38 +00004989 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
4990 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
4991 bool IsDead() const { return !IsLive(); }
4992 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004993
David Brazdil77a48ae2015-09-15 12:34:04 +00004994 bool IsVRegEquivalentOf(HInstruction* other) const {
4995 return other != nullptr
4996 && other->IsPhi()
4997 && other->AsPhi()->GetBlock() == GetBlock()
4998 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4999 }
5000
Calin Juravlea4f88312015-04-16 12:57:19 +01005001 // Returns the next equivalent phi (starting from the current one) or null if there is none.
5002 // An equivalent phi is a phi having the same dex register and type.
5003 // It assumes that phis with the same dex register are adjacent.
5004 HPhi* GetNextEquivalentPhiWithSameType() {
5005 HInstruction* next = GetNext();
5006 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
5007 if (next->GetType() == GetType()) {
5008 return next->AsPhi();
5009 }
5010 next = next->GetNext();
5011 }
5012 return nullptr;
5013 }
5014
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005015 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005016
David Brazdil1abb4192015-02-17 18:33:36 +00005017 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005018 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005019 return inputs_[index];
5020 }
David Brazdil1abb4192015-02-17 18:33:36 +00005021
5022 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005023 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00005024 }
5025
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005026 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005027 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
5028 static constexpr size_t kFieldTypeSize =
5029 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5030 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
5031 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
5032 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
5033 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5034 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
5035
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005036 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005037 const uint32_t reg_number_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005038
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005039 DISALLOW_COPY_AND_ASSIGN(HPhi);
5040};
5041
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005042class HNullCheck : public HExpression<1> {
5043 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005044 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5045 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005046 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005047 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005048 SetRawInputAt(0, value);
5049 }
5050
Calin Juravle10e244f2015-01-26 18:54:32 +00005051 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005052 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005053 return true;
5054 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005055
Calin Juravle10e244f2015-01-26 18:54:32 +00005056 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005057
Calin Juravle10e244f2015-01-26 18:54:32 +00005058 bool CanThrow() const OVERRIDE { return true; }
5059
5060 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005061
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005062
5063 DECLARE_INSTRUCTION(NullCheck);
5064
5065 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005066 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5067};
5068
5069class FieldInfo : public ValueObject {
5070 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005071 FieldInfo(MemberOffset field_offset,
5072 Primitive::Type field_type,
5073 bool is_volatile,
5074 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005075 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005076 const DexFile& dex_file,
5077 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005078 : field_offset_(field_offset),
5079 field_type_(field_type),
5080 is_volatile_(is_volatile),
5081 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005082 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005083 dex_file_(dex_file),
5084 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005085
5086 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005087 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005088 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005089 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005090 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005091 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005092 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005093
5094 private:
5095 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005096 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005097 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005098 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005099 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005100 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005101 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005102};
5103
5104class HInstanceFieldGet : public HExpression<1> {
5105 public:
5106 HInstanceFieldGet(HInstruction* value,
5107 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005108 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005109 bool is_volatile,
5110 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005111 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005112 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005113 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005114 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005115 : HExpression(field_type,
5116 SideEffects::FieldReadOfType(field_type, is_volatile),
5117 dex_pc),
5118 field_info_(field_offset,
5119 field_type,
5120 is_volatile,
5121 field_idx,
5122 declaring_class_def_index,
5123 dex_file,
5124 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005125 SetRawInputAt(0, value);
5126 }
5127
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005128 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005129
5130 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5131 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
5132 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005133 }
5134
Calin Juravle641547a2015-04-21 22:08:51 +01005135 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5136 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005137 }
5138
5139 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005140 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5141 }
5142
Calin Juravle52c48962014-12-16 17:02:57 +00005143 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005144 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005145 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005146 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005147
5148 DECLARE_INSTRUCTION(InstanceFieldGet);
5149
5150 private:
5151 const FieldInfo field_info_;
5152
5153 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5154};
5155
5156class HInstanceFieldSet : public HTemplateInstruction<2> {
5157 public:
5158 HInstanceFieldSet(HInstruction* object,
5159 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005160 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005161 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005162 bool is_volatile,
5163 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005164 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005165 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005166 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005167 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005168 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5169 dex_pc),
5170 field_info_(field_offset,
5171 field_type,
5172 is_volatile,
5173 field_idx,
5174 declaring_class_def_index,
5175 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005176 dex_cache) {
5177 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005178 SetRawInputAt(0, object);
5179 SetRawInputAt(1, value);
5180 }
5181
Calin Juravle641547a2015-04-21 22:08:51 +01005182 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5183 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005184 }
5185
Calin Juravle52c48962014-12-16 17:02:57 +00005186 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005187 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005188 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005189 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005190 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005191 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5192 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005193
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005194 DECLARE_INSTRUCTION(InstanceFieldSet);
5195
5196 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005197 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5198 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5199 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5200 "Too many packed fields.");
5201
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005202 const FieldInfo field_info_;
5203
5204 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5205};
5206
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005207class HArrayGet : public HExpression<2> {
5208 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005209 HArrayGet(HInstruction* array,
5210 HInstruction* index,
5211 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005212 uint32_t dex_pc,
5213 SideEffects additional_side_effects = SideEffects::None())
5214 : HExpression(type,
5215 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00005216 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005217 SetRawInputAt(0, array);
5218 SetRawInputAt(1, index);
5219 }
5220
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005221 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005222 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005223 return true;
5224 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005225 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005226 // TODO: We can be smarter here.
5227 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5228 // which generates the implicit null check. There are cases when these can be removed
5229 // to produce better code. If we ever add optimizations to do so we should allow an
5230 // implicit check here (as long as the address falls in the first page).
5231 return false;
5232 }
5233
David Brazdil4833f5a2015-12-16 10:37:39 +00005234 bool IsEquivalentOf(HArrayGet* other) const {
5235 bool result = (GetDexPc() == other->GetDexPc());
5236 if (kIsDebugBuild && result) {
5237 DCHECK_EQ(GetBlock(), other->GetBlock());
5238 DCHECK_EQ(GetArray(), other->GetArray());
5239 DCHECK_EQ(GetIndex(), other->GetIndex());
5240 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005241 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005242 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005243 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5244 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005245 }
5246 }
5247 return result;
5248 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005249
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005250 HInstruction* GetArray() const { return InputAt(0); }
5251 HInstruction* GetIndex() const { return InputAt(1); }
5252
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005253 DECLARE_INSTRUCTION(ArrayGet);
5254
5255 private:
5256 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5257};
5258
5259class HArraySet : public HTemplateInstruction<3> {
5260 public:
5261 HArraySet(HInstruction* array,
5262 HInstruction* index,
5263 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005264 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005265 uint32_t dex_pc,
5266 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005267 : HTemplateInstruction(
5268 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005269 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
5270 additional_side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005271 dex_pc) {
5272 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5273 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5274 SetPackedFlag<kFlagValueCanBeNull>(true);
5275 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005276 SetRawInputAt(0, array);
5277 SetRawInputAt(1, index);
5278 SetRawInputAt(2, value);
5279 }
5280
Calin Juravle77520bc2015-01-12 18:45:46 +00005281 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005282 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005283 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005284 }
5285
Mingyao Yang81014cb2015-06-02 03:16:27 -07005286 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005287 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005288
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005289 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005290 // TODO: Same as for ArrayGet.
5291 return false;
5292 }
5293
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005294 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005295 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005296 }
5297
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005298 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005299 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005300 }
5301
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005302 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005303 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005304 }
5305
Vladimir Markoa1de9182016-02-25 11:37:38 +00005306 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5307 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5308 bool StaticTypeOfArrayIsObjectArray() const {
5309 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5310 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005311
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005312 HInstruction* GetArray() const { return InputAt(0); }
5313 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005314 HInstruction* GetValue() const { return InputAt(2); }
5315
5316 Primitive::Type GetComponentType() const {
5317 // The Dex format does not type floating point index operations. Since the
5318 // `expected_component_type_` is set during building and can therefore not
5319 // be correct, we also check what is the value type. If it is a floating
5320 // point type, we must use that type.
5321 Primitive::Type value_type = GetValue()->GetType();
5322 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5323 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005324 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005325 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005326
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005327 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005328 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005329 }
5330
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005331 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5332 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5333 }
5334
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005335 DECLARE_INSTRUCTION(ArraySet);
5336
5337 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005338 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5339 static constexpr size_t kFieldExpectedComponentTypeSize =
5340 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5341 static constexpr size_t kFlagNeedsTypeCheck =
5342 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5343 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005344 // Cached information for the reference_type_info_ so that codegen
5345 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005346 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5347 static constexpr size_t kNumberOfArraySetPackedBits =
5348 kFlagStaticTypeOfArrayIsObjectArray + 1;
5349 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5350 using ExpectedComponentTypeField =
5351 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005352
5353 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5354};
5355
5356class HArrayLength : public HExpression<1> {
5357 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005358 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005359 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005360 // Note that arrays do not change length, so the instruction does not
5361 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005362 SetRawInputAt(0, array);
5363 }
5364
Calin Juravle77520bc2015-01-12 18:45:46 +00005365 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005366 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005367 return true;
5368 }
Calin Juravle641547a2015-04-21 22:08:51 +01005369 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5370 return obj == InputAt(0);
5371 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005372
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005373 DECLARE_INSTRUCTION(ArrayLength);
5374
5375 private:
5376 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5377};
5378
5379class HBoundsCheck : public HExpression<2> {
5380 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005381 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5382 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005383 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005384 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005385 DCHECK(index->GetType() == Primitive::kPrimInt);
5386 SetRawInputAt(0, index);
5387 SetRawInputAt(1, length);
5388 }
5389
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005390 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005391 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005392 return true;
5393 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005394
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005395 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005396
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005397 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005398
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005399 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005400
5401 DECLARE_INSTRUCTION(BoundsCheck);
5402
5403 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005404 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5405};
5406
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005407class HSuspendCheck : public HTemplateInstruction<0> {
5408 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005409 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005410 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005411
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005412 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005413 return true;
5414 }
5415
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005416 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5417 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005418
5419 DECLARE_INSTRUCTION(SuspendCheck);
5420
5421 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005422 // Only used for code generation, in order to share the same slow path between back edges
5423 // of a same loop.
5424 SlowPathCode* slow_path_;
5425
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005426 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5427};
5428
David Srbecky0cf44932015-12-09 14:09:59 +00005429// Pseudo-instruction which provides the native debugger with mapping information.
5430// It ensures that we can generate line number and local variables at this point.
5431class HNativeDebugInfo : public HTemplateInstruction<0> {
5432 public:
5433 explicit HNativeDebugInfo(uint32_t dex_pc)
5434 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5435
5436 bool NeedsEnvironment() const OVERRIDE {
5437 return true;
5438 }
5439
5440 DECLARE_INSTRUCTION(NativeDebugInfo);
5441
5442 private:
5443 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5444};
5445
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005446/**
5447 * Instruction to load a Class object.
5448 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005449class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005450 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005451 HLoadClass(HCurrentMethod* current_method,
5452 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005453 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005454 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005455 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005456 bool needs_access_check,
5457 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005458 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005459 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005460 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005461 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005462 // Referrers class should not need access check. We never inline unverified
5463 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005464 DCHECK(!is_referrers_class || !needs_access_check);
5465
5466 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5467 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5468 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5469 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005470 SetRawInputAt(0, current_method);
5471 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005472
5473 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005474
5475 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005476 // Note that we don't need to test for generate_clinit_check_.
5477 // Whether or not we need to generate the clinit check is processed in
5478 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005479 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005480 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005481 }
5482
5483 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5484
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005485 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005486 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005487
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005488 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005489 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005490 }
5491
Calin Juravle0ba218d2015-05-19 18:46:01 +01005492 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005493 // The entrypoint the code generator is going to call does not do
5494 // clinit of the class.
5495 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005496 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005497 }
5498
5499 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005500 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005501 (!IsReferrersClass() && !IsInDexCache()) ||
5502 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005503 }
5504
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005505
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005506 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005507 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005508 }
5509
Calin Juravleacf735c2015-02-12 15:25:22 +00005510 ReferenceTypeInfo GetLoadedClassRTI() {
5511 return loaded_class_rti_;
5512 }
5513
5514 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5515 // Make sure we only set exact types (the loaded class should never be merged).
5516 DCHECK(rti.IsExact());
5517 loaded_class_rti_ = rti;
5518 }
5519
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005520 const DexFile& GetDexFile() { return dex_file_; }
5521
Vladimir Markoa1de9182016-02-25 11:37:38 +00005522 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005523
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005524 static SideEffects SideEffectsForArchRuntimeCalls() {
5525 return SideEffects::CanTriggerGC();
5526 }
5527
Vladimir Markoa1de9182016-02-25 11:37:38 +00005528 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5529 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5530 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5531 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005532
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005533 DECLARE_INSTRUCTION(LoadClass);
5534
5535 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005536 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5537 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5538 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005539 // Whether this instruction must generate the initialization check.
5540 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005541 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5542 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5543 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5544
5545 const uint16_t type_index_;
5546 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005547
Calin Juravleacf735c2015-02-12 15:25:22 +00005548 ReferenceTypeInfo loaded_class_rti_;
5549
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005550 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5551};
5552
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005553class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005554 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005555 // Determines how to load the String.
5556 enum class LoadKind {
5557 // Use boot image String* address that will be known at link time.
5558 // Used for boot image strings referenced by boot image code in non-PIC mode.
5559 kBootImageLinkTimeAddress,
5560
5561 // Use PC-relative boot image String* address that will be known at link time.
5562 // Used for boot image strings referenced by boot image code in PIC mode.
5563 kBootImageLinkTimePcRelative,
5564
5565 // Use a known boot image String* address, embedded in the code by the codegen.
5566 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5567 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5568 // GetIncludePatchInformation().
5569 kBootImageAddress,
5570
5571 // Load from the resolved strings array at an absolute address.
5572 // Used for strings outside the boot image referenced by JIT-compiled code.
5573 kDexCacheAddress,
5574
5575 // Load from resolved strings array in the dex cache using a PC-relative load.
5576 // Used for strings outside boot image when we know that we can access
5577 // the dex cache arrays using a PC-relative load.
5578 kDexCachePcRelative,
5579
5580 // Load from resolved strings array accessed through the class loaded from
5581 // the compiled method's own ArtMethod*. This is the default access type when
5582 // all other types are unavailable.
5583 kDexCacheViaMethod,
5584
5585 kLast = kDexCacheViaMethod
5586 };
5587
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005588 HLoadString(HCurrentMethod* current_method,
5589 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005590 const DexFile& dex_file,
5591 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005592 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005593 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005594 SetPackedFlag<kFlagIsInDexCache>(false);
5595 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5596 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005597 SetRawInputAt(0, current_method);
5598 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005599
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005600 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5601 DCHECK(HasAddress(load_kind));
5602 load_data_.address = address;
5603 SetLoadKindInternal(load_kind);
5604 }
5605
5606 void SetLoadKindWithStringReference(LoadKind load_kind,
5607 const DexFile& dex_file,
5608 uint32_t string_index) {
5609 DCHECK(HasStringReference(load_kind));
5610 load_data_.ref.dex_file = &dex_file;
5611 string_index_ = string_index;
5612 SetLoadKindInternal(load_kind);
5613 }
5614
5615 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5616 const DexFile& dex_file,
5617 uint32_t element_index) {
5618 DCHECK(HasDexCacheReference(load_kind));
5619 load_data_.ref.dex_file = &dex_file;
5620 load_data_.ref.dex_cache_element_index = element_index;
5621 SetLoadKindInternal(load_kind);
5622 }
5623
5624 LoadKind GetLoadKind() const {
5625 return GetPackedField<LoadKindField>();
5626 }
5627
5628 const DexFile& GetDexFile() const;
5629
5630 uint32_t GetStringIndex() const {
5631 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5632 return string_index_;
5633 }
5634
5635 uint32_t GetDexCacheElementOffset() const;
5636
5637 uint64_t GetAddress() const {
5638 DCHECK(HasAddress(GetLoadKind()));
5639 return load_data_.address;
5640 }
5641
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005642 bool CanBeMoved() const OVERRIDE { return true; }
5643
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005644 bool InstructionDataEquals(HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005645
5646 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5647
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005648 // Will call the runtime if we need to load the string through
5649 // the dex cache and the string is not guaranteed to be there yet.
5650 bool NeedsEnvironment() const OVERRIDE {
5651 LoadKind load_kind = GetLoadKind();
5652 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5653 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5654 load_kind == LoadKind::kBootImageAddress) {
5655 return false;
5656 }
5657 return !IsInDexCache();
5658 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005659
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005660 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5661 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5662 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005663
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005664 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005665 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005666
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005667 static SideEffects SideEffectsForArchRuntimeCalls() {
5668 return SideEffects::CanTriggerGC();
5669 }
5670
Vladimir Markoa1de9182016-02-25 11:37:38 +00005671 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5672
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005673 void MarkInDexCache() {
5674 SetPackedFlag<kFlagIsInDexCache>(true);
5675 DCHECK(!NeedsEnvironment());
5676 RemoveEnvironment();
5677 }
5678
5679 size_t InputCount() const OVERRIDE {
5680 return (InputAt(0) != nullptr) ? 1u : 0u;
5681 }
5682
5683 void AddSpecialInput(HInstruction* special_input);
5684
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005685 DECLARE_INSTRUCTION(LoadString);
5686
5687 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005688 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005689 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5690 static constexpr size_t kFieldLoadKindSize =
5691 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5692 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005693 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005694 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005695
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005696 static bool HasStringReference(LoadKind load_kind) {
5697 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5698 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5699 load_kind == LoadKind::kDexCacheViaMethod;
5700 }
5701
5702 static bool HasAddress(LoadKind load_kind) {
5703 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5704 }
5705
5706 static bool HasDexCacheReference(LoadKind load_kind) {
5707 return load_kind == LoadKind::kDexCachePcRelative;
5708 }
5709
5710 void SetLoadKindInternal(LoadKind load_kind);
5711
5712 // String index serves also as the hash code and it's also needed for slow-paths,
5713 // so it must not be overwritten with other load data.
5714 uint32_t string_index_;
5715
5716 union {
5717 struct {
5718 const DexFile* dex_file; // For string reference and dex cache reference.
5719 uint32_t dex_cache_element_index; // Only for dex cache reference.
5720 } ref;
5721 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5722 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005723
5724 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5725};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005726std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5727
5728// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5729inline const DexFile& HLoadString::GetDexFile() const {
5730 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5731 << GetLoadKind();
5732 return *load_data_.ref.dex_file;
5733}
5734
5735// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5736inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5737 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5738 return load_data_.ref.dex_cache_element_index;
5739}
5740
5741// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5742inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5743 // The special input is used for PC-relative loads on some architectures.
5744 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5745 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
5746 DCHECK(InputAt(0) == nullptr);
5747 SetRawInputAt(0u, special_input);
5748 special_input->AddUseAt(this, 0);
5749}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005750
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005751/**
5752 * Performs an initialization check on its Class object input.
5753 */
5754class HClinitCheck : public HExpression<1> {
5755 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005756 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005757 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005758 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005759 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5760 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005761 SetRawInputAt(0, constant);
5762 }
5763
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005764 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005765 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005766 return true;
5767 }
5768
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005769 bool NeedsEnvironment() const OVERRIDE {
5770 // May call runtime to initialize the class.
5771 return true;
5772 }
5773
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005774 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005775
5776 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5777
5778 DECLARE_INSTRUCTION(ClinitCheck);
5779
5780 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005781 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5782};
5783
5784class HStaticFieldGet : public HExpression<1> {
5785 public:
5786 HStaticFieldGet(HInstruction* cls,
5787 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005788 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005789 bool is_volatile,
5790 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005791 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005792 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005793 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005794 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005795 : HExpression(field_type,
5796 SideEffects::FieldReadOfType(field_type, is_volatile),
5797 dex_pc),
5798 field_info_(field_offset,
5799 field_type,
5800 is_volatile,
5801 field_idx,
5802 declaring_class_def_index,
5803 dex_file,
5804 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005805 SetRawInputAt(0, cls);
5806 }
5807
Calin Juravle52c48962014-12-16 17:02:57 +00005808
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005809 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005810
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005811 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005812 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5813 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005814 }
5815
5816 size_t ComputeHashCode() const OVERRIDE {
5817 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5818 }
5819
Calin Juravle52c48962014-12-16 17:02:57 +00005820 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005821 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5822 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005823 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005824
5825 DECLARE_INSTRUCTION(StaticFieldGet);
5826
5827 private:
5828 const FieldInfo field_info_;
5829
5830 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5831};
5832
5833class HStaticFieldSet : public HTemplateInstruction<2> {
5834 public:
5835 HStaticFieldSet(HInstruction* cls,
5836 HInstruction* value,
5837 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005838 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005839 bool is_volatile,
5840 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005841 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005842 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005843 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005844 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005845 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5846 dex_pc),
5847 field_info_(field_offset,
5848 field_type,
5849 is_volatile,
5850 field_idx,
5851 declaring_class_def_index,
5852 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005853 dex_cache) {
5854 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005855 SetRawInputAt(0, cls);
5856 SetRawInputAt(1, value);
5857 }
5858
Calin Juravle52c48962014-12-16 17:02:57 +00005859 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005860 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5861 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005862 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005863
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005864 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005865 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5866 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005867
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005868 DECLARE_INSTRUCTION(StaticFieldSet);
5869
5870 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005871 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5872 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5873 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5874 "Too many packed fields.");
5875
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005876 const FieldInfo field_info_;
5877
5878 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5879};
5880
Calin Juravlee460d1d2015-09-29 04:52:17 +01005881class HUnresolvedInstanceFieldGet : public HExpression<1> {
5882 public:
5883 HUnresolvedInstanceFieldGet(HInstruction* obj,
5884 Primitive::Type field_type,
5885 uint32_t field_index,
5886 uint32_t dex_pc)
5887 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5888 field_index_(field_index) {
5889 SetRawInputAt(0, obj);
5890 }
5891
5892 bool NeedsEnvironment() const OVERRIDE { return true; }
5893 bool CanThrow() const OVERRIDE { return true; }
5894
5895 Primitive::Type GetFieldType() const { return GetType(); }
5896 uint32_t GetFieldIndex() const { return field_index_; }
5897
5898 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5899
5900 private:
5901 const uint32_t field_index_;
5902
5903 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5904};
5905
5906class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5907 public:
5908 HUnresolvedInstanceFieldSet(HInstruction* obj,
5909 HInstruction* value,
5910 Primitive::Type field_type,
5911 uint32_t field_index,
5912 uint32_t dex_pc)
5913 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005914 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005915 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005916 DCHECK_EQ(field_type, value->GetType());
5917 SetRawInputAt(0, obj);
5918 SetRawInputAt(1, value);
5919 }
5920
5921 bool NeedsEnvironment() const OVERRIDE { return true; }
5922 bool CanThrow() const OVERRIDE { return true; }
5923
Vladimir Markoa1de9182016-02-25 11:37:38 +00005924 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005925 uint32_t GetFieldIndex() const { return field_index_; }
5926
5927 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5928
5929 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005930 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5931 static constexpr size_t kFieldFieldTypeSize =
5932 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5933 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5934 kFieldFieldType + kFieldFieldTypeSize;
5935 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5936 "Too many packed fields.");
5937 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5938
Calin Juravlee460d1d2015-09-29 04:52:17 +01005939 const uint32_t field_index_;
5940
5941 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5942};
5943
5944class HUnresolvedStaticFieldGet : public HExpression<0> {
5945 public:
5946 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5947 uint32_t field_index,
5948 uint32_t dex_pc)
5949 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5950 field_index_(field_index) {
5951 }
5952
5953 bool NeedsEnvironment() const OVERRIDE { return true; }
5954 bool CanThrow() const OVERRIDE { return true; }
5955
5956 Primitive::Type GetFieldType() const { return GetType(); }
5957 uint32_t GetFieldIndex() const { return field_index_; }
5958
5959 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5960
5961 private:
5962 const uint32_t field_index_;
5963
5964 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5965};
5966
5967class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5968 public:
5969 HUnresolvedStaticFieldSet(HInstruction* value,
5970 Primitive::Type field_type,
5971 uint32_t field_index,
5972 uint32_t dex_pc)
5973 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005974 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005975 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005976 DCHECK_EQ(field_type, value->GetType());
5977 SetRawInputAt(0, value);
5978 }
5979
5980 bool NeedsEnvironment() const OVERRIDE { return true; }
5981 bool CanThrow() const OVERRIDE { return true; }
5982
Vladimir Markoa1de9182016-02-25 11:37:38 +00005983 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005984 uint32_t GetFieldIndex() const { return field_index_; }
5985
5986 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5987
5988 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005989 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5990 static constexpr size_t kFieldFieldTypeSize =
5991 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5992 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5993 kFieldFieldType + kFieldFieldTypeSize;
5994 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5995 "Too many packed fields.");
5996 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5997
Calin Juravlee460d1d2015-09-29 04:52:17 +01005998 const uint32_t field_index_;
5999
6000 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6001};
6002
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006003// Implement the move-exception DEX instruction.
6004class HLoadException : public HExpression<0> {
6005 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006006 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6007 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006008
David Brazdilbbd733e2015-08-18 17:48:17 +01006009 bool CanBeNull() const OVERRIDE { return false; }
6010
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006011 DECLARE_INSTRUCTION(LoadException);
6012
6013 private:
6014 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6015};
6016
David Brazdilcb1c0552015-08-04 16:22:25 +01006017// Implicit part of move-exception which clears thread-local exception storage.
6018// Must not be removed because the runtime expects the TLS to get cleared.
6019class HClearException : public HTemplateInstruction<0> {
6020 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006021 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6022 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006023
6024 DECLARE_INSTRUCTION(ClearException);
6025
6026 private:
6027 DISALLOW_COPY_AND_ASSIGN(HClearException);
6028};
6029
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006030class HThrow : public HTemplateInstruction<1> {
6031 public:
6032 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006033 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006034 SetRawInputAt(0, exception);
6035 }
6036
6037 bool IsControlFlow() const OVERRIDE { return true; }
6038
6039 bool NeedsEnvironment() const OVERRIDE { return true; }
6040
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006041 bool CanThrow() const OVERRIDE { return true; }
6042
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006043
6044 DECLARE_INSTRUCTION(Throw);
6045
6046 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006047 DISALLOW_COPY_AND_ASSIGN(HThrow);
6048};
6049
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006050/**
6051 * Implementation strategies for the code generator of a HInstanceOf
6052 * or `HCheckCast`.
6053 */
6054enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006055 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006056 kExactCheck, // Can do a single class compare.
6057 kClassHierarchyCheck, // Can just walk the super class chain.
6058 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6059 kInterfaceCheck, // No optimization yet when checking against an interface.
6060 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006061 kArrayCheck, // No optimization yet when checking against a generic array.
6062 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006063};
6064
Roland Levillain86503782016-02-11 19:07:30 +00006065std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6066
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006067class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006068 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006069 HInstanceOf(HInstruction* object,
6070 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006071 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006072 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006073 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006074 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006075 dex_pc) {
6076 SetPackedField<TypeCheckKindField>(check_kind);
6077 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006078 SetRawInputAt(0, object);
6079 SetRawInputAt(1, constant);
6080 }
6081
6082 bool CanBeMoved() const OVERRIDE { return true; }
6083
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006084 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006085 return true;
6086 }
6087
6088 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006089 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006090 }
6091
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006092 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006093 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6094 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6095 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6096 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006097
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006098 static bool CanCallRuntime(TypeCheckKind check_kind) {
6099 // Mips currently does runtime calls for any other checks.
6100 return check_kind != TypeCheckKind::kExactCheck;
6101 }
6102
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006103 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006104 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006105 }
6106
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006107 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006108
6109 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006110 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6111 static constexpr size_t kFieldTypeCheckKindSize =
6112 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6113 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6114 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6115 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6116 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006117
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006118 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6119};
6120
Calin Juravleb1498f62015-02-16 13:13:29 +00006121class HBoundType : public HExpression<1> {
6122 public:
David Brazdilf5552582015-12-27 13:36:12 +00006123 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006124 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006125 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6126 SetPackedFlag<kFlagUpperCanBeNull>(true);
6127 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006128 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006129 SetRawInputAt(0, input);
6130 }
6131
David Brazdilf5552582015-12-27 13:36:12 +00006132 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006133 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006134 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006135 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006136
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006137 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006138 DCHECK(GetUpperCanBeNull() || !can_be_null);
6139 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006140 }
6141
Vladimir Markoa1de9182016-02-25 11:37:38 +00006142 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006143
Calin Juravleb1498f62015-02-16 13:13:29 +00006144 DECLARE_INSTRUCTION(BoundType);
6145
6146 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006147 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6148 // is false then CanBeNull() cannot be true).
6149 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6150 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6151 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6152 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6153
Calin Juravleb1498f62015-02-16 13:13:29 +00006154 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006155 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6156 // It is used to bound the type in cases like:
6157 // if (x instanceof ClassX) {
6158 // // uper_bound_ will be ClassX
6159 // }
David Brazdilf5552582015-12-27 13:36:12 +00006160 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006161
6162 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6163};
6164
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006165class HCheckCast : public HTemplateInstruction<2> {
6166 public:
6167 HCheckCast(HInstruction* object,
6168 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006169 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006170 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006171 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6172 SetPackedField<TypeCheckKindField>(check_kind);
6173 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006174 SetRawInputAt(0, object);
6175 SetRawInputAt(1, constant);
6176 }
6177
6178 bool CanBeMoved() const OVERRIDE { return true; }
6179
6180 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6181 return true;
6182 }
6183
6184 bool NeedsEnvironment() const OVERRIDE {
6185 // Instruction may throw a CheckCastError.
6186 return true;
6187 }
6188
6189 bool CanThrow() const OVERRIDE { return true; }
6190
Vladimir Markoa1de9182016-02-25 11:37:38 +00006191 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6192 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6193 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6194 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006195
6196 DECLARE_INSTRUCTION(CheckCast);
6197
6198 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006199 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6200 static constexpr size_t kFieldTypeCheckKindSize =
6201 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6202 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6203 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6204 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6205 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006206
6207 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006208};
6209
Calin Juravle27df7582015-04-17 19:12:31 +01006210class HMemoryBarrier : public HTemplateInstruction<0> {
6211 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006212 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006213 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006214 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6215 SetPackedField<BarrierKindField>(barrier_kind);
6216 }
Calin Juravle27df7582015-04-17 19:12:31 +01006217
Vladimir Markoa1de9182016-02-25 11:37:38 +00006218 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006219
6220 DECLARE_INSTRUCTION(MemoryBarrier);
6221
6222 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006223 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6224 static constexpr size_t kFieldBarrierKindSize =
6225 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6226 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6227 kFieldBarrierKind + kFieldBarrierKindSize;
6228 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6229 "Too many packed fields.");
6230 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006231
6232 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6233};
6234
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006235class HMonitorOperation : public HTemplateInstruction<1> {
6236 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006237 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006238 kEnter,
6239 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006240 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006241 };
6242
6243 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006244 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006245 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6246 dex_pc) {
6247 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006248 SetRawInputAt(0, object);
6249 }
6250
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006251 // Instruction may go into runtime, so we need an environment.
6252 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006253
6254 bool CanThrow() const OVERRIDE {
6255 // Verifier guarantees that monitor-exit cannot throw.
6256 // This is important because it allows the HGraphBuilder to remove
6257 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6258 return IsEnter();
6259 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006260
Vladimir Markoa1de9182016-02-25 11:37:38 +00006261 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6262 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006263
6264 DECLARE_INSTRUCTION(MonitorOperation);
6265
Calin Juravle52c48962014-12-16 17:02:57 +00006266 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006267 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6268 static constexpr size_t kFieldOperationKindSize =
6269 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6270 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6271 kFieldOperationKind + kFieldOperationKindSize;
6272 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6273 "Too many packed fields.");
6274 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006275
6276 private:
6277 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6278};
6279
David Brazdil74eb1b22015-12-14 11:44:01 +00006280class HSelect : public HExpression<3> {
6281 public:
6282 HSelect(HInstruction* condition,
6283 HInstruction* true_value,
6284 HInstruction* false_value,
6285 uint32_t dex_pc)
6286 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6287 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6288
6289 // First input must be `true_value` or `false_value` to allow codegens to
6290 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6291 // that architectures which implement HSelect as a conditional move also
6292 // will not need to invert the condition.
6293 SetRawInputAt(0, false_value);
6294 SetRawInputAt(1, true_value);
6295 SetRawInputAt(2, condition);
6296 }
6297
6298 HInstruction* GetFalseValue() const { return InputAt(0); }
6299 HInstruction* GetTrueValue() const { return InputAt(1); }
6300 HInstruction* GetCondition() const { return InputAt(2); }
6301
6302 bool CanBeMoved() const OVERRIDE { return true; }
6303 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6304
6305 bool CanBeNull() const OVERRIDE {
6306 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6307 }
6308
6309 DECLARE_INSTRUCTION(Select);
6310
6311 private:
6312 DISALLOW_COPY_AND_ASSIGN(HSelect);
6313};
6314
Vladimir Markof9f64412015-09-02 14:05:49 +01006315class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006316 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006317 MoveOperands(Location source,
6318 Location destination,
6319 Primitive::Type type,
6320 HInstruction* instruction)
6321 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006322
6323 Location GetSource() const { return source_; }
6324 Location GetDestination() const { return destination_; }
6325
6326 void SetSource(Location value) { source_ = value; }
6327 void SetDestination(Location value) { destination_ = value; }
6328
6329 // The parallel move resolver marks moves as "in-progress" by clearing the
6330 // destination (but not the source).
6331 Location MarkPending() {
6332 DCHECK(!IsPending());
6333 Location dest = destination_;
6334 destination_ = Location::NoLocation();
6335 return dest;
6336 }
6337
6338 void ClearPending(Location dest) {
6339 DCHECK(IsPending());
6340 destination_ = dest;
6341 }
6342
6343 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006344 DCHECK(source_.IsValid() || destination_.IsInvalid());
6345 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006346 }
6347
6348 // True if this blocks a move from the given location.
6349 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006350 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006351 }
6352
6353 // A move is redundant if it's been eliminated, if its source and
6354 // destination are the same, or if its destination is unneeded.
6355 bool IsRedundant() const {
6356 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6357 }
6358
6359 // We clear both operands to indicate move that's been eliminated.
6360 void Eliminate() {
6361 source_ = destination_ = Location::NoLocation();
6362 }
6363
6364 bool IsEliminated() const {
6365 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6366 return source_.IsInvalid();
6367 }
6368
Alexey Frunze4dda3372015-06-01 18:31:49 -07006369 Primitive::Type GetType() const { return type_; }
6370
Nicolas Geoffray90218252015-04-15 11:56:51 +01006371 bool Is64BitMove() const {
6372 return Primitive::Is64BitType(type_);
6373 }
6374
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006375 HInstruction* GetInstruction() const { return instruction_; }
6376
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006377 private:
6378 Location source_;
6379 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006380 // The type this move is for.
6381 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006382 // The instruction this move is assocatied with. Null when this move is
6383 // for moving an input in the expected locations of user (including a phi user).
6384 // This is only used in debug mode, to ensure we do not connect interval siblings
6385 // in the same parallel move.
6386 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006387};
6388
Roland Levillainc9285912015-12-18 10:38:42 +00006389std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6390
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006391static constexpr size_t kDefaultNumberOfMoves = 4;
6392
6393class HParallelMove : public HTemplateInstruction<0> {
6394 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006395 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006396 : HTemplateInstruction(SideEffects::None(), dex_pc),
6397 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6398 moves_.reserve(kDefaultNumberOfMoves);
6399 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006400
Nicolas Geoffray90218252015-04-15 11:56:51 +01006401 void AddMove(Location source,
6402 Location destination,
6403 Primitive::Type type,
6404 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006405 DCHECK(source.IsValid());
6406 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006407 if (kIsDebugBuild) {
6408 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006409 for (const MoveOperands& move : moves_) {
6410 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006411 // Special case the situation where the move is for the spill slot
6412 // of the instruction.
6413 if ((GetPrevious() == instruction)
6414 || ((GetPrevious() == nullptr)
6415 && instruction->IsPhi()
6416 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006417 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006418 << "Doing parallel moves for the same instruction.";
6419 } else {
6420 DCHECK(false) << "Doing parallel moves for the same instruction.";
6421 }
6422 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006423 }
6424 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006425 for (const MoveOperands& move : moves_) {
6426 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006427 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006428 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006429 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006430 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006431 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006432 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006433 }
6434
Vladimir Marko225b6462015-09-28 12:17:40 +01006435 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006436 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006437 }
6438
Vladimir Marko225b6462015-09-28 12:17:40 +01006439 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006440
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006441 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006442
6443 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006444 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006445
6446 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6447};
6448
Mark Mendell0616ae02015-04-17 12:49:27 -04006449} // namespace art
6450
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006451#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6452#include "nodes_shared.h"
6453#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006454#ifdef ART_ENABLE_CODEGEN_arm
6455#include "nodes_arm.h"
6456#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006457#ifdef ART_ENABLE_CODEGEN_arm64
6458#include "nodes_arm64.h"
6459#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006460#ifdef ART_ENABLE_CODEGEN_x86
6461#include "nodes_x86.h"
6462#endif
6463
6464namespace art {
6465
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006466class HGraphVisitor : public ValueObject {
6467 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006468 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6469 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006470
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006471 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006472 virtual void VisitBasicBlock(HBasicBlock* block);
6473
Roland Levillain633021e2014-10-01 14:12:25 +01006474 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006475 void VisitInsertionOrder();
6476
Roland Levillain633021e2014-10-01 14:12:25 +01006477 // Visit the graph following dominator tree reverse post-order.
6478 void VisitReversePostOrder();
6479
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006480 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006481
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006482 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006483#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006484 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6485
6486 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6487
6488#undef DECLARE_VISIT_INSTRUCTION
6489
6490 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006491 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006492
6493 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6494};
6495
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006496class HGraphDelegateVisitor : public HGraphVisitor {
6497 public:
6498 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6499 virtual ~HGraphDelegateVisitor() {}
6500
6501 // Visit functions that delegate to to super class.
6502#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006503 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006504
6505 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6506
6507#undef DECLARE_VISIT_INSTRUCTION
6508
6509 private:
6510 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6511};
6512
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006513class HInsertionOrderIterator : public ValueObject {
6514 public:
6515 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6516
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006517 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006518 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006519 void Advance() { ++index_; }
6520
6521 private:
6522 const HGraph& graph_;
6523 size_t index_;
6524
6525 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6526};
6527
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006528class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006529 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006530 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6531 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006532 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006533 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006534
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006535 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6536 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006537 void Advance() { ++index_; }
6538
6539 private:
6540 const HGraph& graph_;
6541 size_t index_;
6542
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006543 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006544};
6545
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006546class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006547 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006548 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006549 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006550 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006551 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006552 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006553
6554 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006555 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006556 void Advance() { --index_; }
6557
6558 private:
6559 const HGraph& graph_;
6560 size_t index_;
6561
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006562 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006563};
6564
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006565class HLinearPostOrderIterator : public ValueObject {
6566 public:
6567 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006568 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006569
6570 bool Done() const { return index_ == 0; }
6571
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006572 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006573
6574 void Advance() {
6575 --index_;
6576 DCHECK_GE(index_, 0U);
6577 }
6578
6579 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006580 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006581 size_t index_;
6582
6583 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6584};
6585
6586class HLinearOrderIterator : public ValueObject {
6587 public:
6588 explicit HLinearOrderIterator(const HGraph& graph)
6589 : order_(graph.GetLinearOrder()), index_(0) {}
6590
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006591 bool Done() const { return index_ == order_.size(); }
6592 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006593 void Advance() { ++index_; }
6594
6595 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006596 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006597 size_t index_;
6598
6599 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6600};
6601
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006602// Iterator over the blocks that art part of the loop. Includes blocks part
6603// of an inner loop. The order in which the blocks are iterated is on their
6604// block id.
6605class HBlocksInLoopIterator : public ValueObject {
6606 public:
6607 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6608 : blocks_in_loop_(info.GetBlocks()),
6609 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6610 index_(0) {
6611 if (!blocks_in_loop_.IsBitSet(index_)) {
6612 Advance();
6613 }
6614 }
6615
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006616 bool Done() const { return index_ == blocks_.size(); }
6617 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006618 void Advance() {
6619 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006620 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006621 if (blocks_in_loop_.IsBitSet(index_)) {
6622 break;
6623 }
6624 }
6625 }
6626
6627 private:
6628 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006629 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006630 size_t index_;
6631
6632 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6633};
6634
Mingyao Yang3584bce2015-05-19 16:01:59 -07006635// Iterator over the blocks that art part of the loop. Includes blocks part
6636// of an inner loop. The order in which the blocks are iterated is reverse
6637// post order.
6638class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6639 public:
6640 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6641 : blocks_in_loop_(info.GetBlocks()),
6642 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6643 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006644 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006645 Advance();
6646 }
6647 }
6648
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006649 bool Done() const { return index_ == blocks_.size(); }
6650 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006651 void Advance() {
6652 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006653 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6654 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006655 break;
6656 }
6657 }
6658 }
6659
6660 private:
6661 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006662 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006663 size_t index_;
6664
6665 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6666};
6667
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006668inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006669 if (constant->IsIntConstant()) {
6670 return constant->AsIntConstant()->GetValue();
6671 } else if (constant->IsLongConstant()) {
6672 return constant->AsLongConstant()->GetValue();
6673 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006674 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006675 return 0;
6676 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006677}
6678
Vladimir Marko58155012015-08-19 12:49:41 +00006679inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6680 // For the purposes of the compiler, the dex files must actually be the same object
6681 // if we want to safely treat them as the same. This is especially important for JIT
6682 // as custom class loaders can open the same underlying file (or memory) multiple
6683 // times and provide different class resolution but no two class loaders should ever
6684 // use the same DexFile object - doing so is an unsupported hack that can lead to
6685 // all sorts of weird failures.
6686 return &lhs == &rhs;
6687}
6688
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006689#define INSTRUCTION_TYPE_CHECK(type, super) \
6690 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6691 inline const H##type* HInstruction::As##type() const { \
6692 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6693 } \
6694 inline H##type* HInstruction::As##type() { \
6695 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6696 }
6697
6698 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6699#undef INSTRUCTION_TYPE_CHECK
6700
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006701// Create space in `blocks` for adding `number_of_new_blocks` entries
6702// starting at location `at`. Blocks after `at` are moved accordingly.
6703inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6704 size_t number_of_new_blocks,
6705 size_t after) {
6706 DCHECK_LT(after, blocks->size());
6707 size_t old_size = blocks->size();
6708 size_t new_size = old_size + number_of_new_blocks;
6709 blocks->resize(new_size);
6710 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6711}
6712
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006713} // namespace art
6714
6715#endif // ART_COMPILER_OPTIMIZING_NODES_H_