blob: 808ab0985ba8224e4ff35f854778cc71fe1535d6 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010028#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000029#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "handle.h"
31#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "primitive.h"
David Brazdild26a4112015-11-10 11:07:31 +000038#include "utils/array_ref.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000039
40namespace art {
41
David Brazdil1abb4192015-02-17 18:33:36 +000042class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010046class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000048class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000051class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000052class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000054class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010056class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010057class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000059class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010060class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062
Mathieu Chartier736b5602015-09-02 14:54:11 -070063namespace mirror {
64class DexCache;
65} // namespace mirror
66
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067static const int kDefaultNumberOfBlocks = 8;
68static const int kDefaultNumberOfSuccessors = 2;
69static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010070static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010071static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000072static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Roland Levillain5b5b9312016-03-22 14:57:31 +000074// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
75static constexpr int32_t kMaxIntShiftDistance = 0x1f;
76// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
77static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000078
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010079static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070080static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010081
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010082static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
83
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060084static constexpr uint32_t kNoDexPc = -1;
85
Dave Allison20dfc792014-06-16 20:44:29 -070086enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070087 // All types.
88 kCondEQ, // ==
89 kCondNE, // !=
90 // Signed integers and floating-point numbers.
91 kCondLT, // <
92 kCondLE, // <=
93 kCondGT, // >
94 kCondGE, // >=
95 // Unsigned integers.
96 kCondB, // <
97 kCondBE, // <=
98 kCondA, // >
99 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -0700100};
101
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000102enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000103 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000104 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000105 kAnalysisFailThrowCatchLoop,
106 kAnalysisFailAmbiguousArrayOp,
107 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000108};
109
Vladimir Markof9f64412015-09-02 14:05:49 +0100110class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100111 public:
112 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
113
114 void AddInstruction(HInstruction* instruction);
115 void RemoveInstruction(HInstruction* instruction);
116
David Brazdilc3d743f2015-04-22 13:40:50 +0100117 // Insert `instruction` before/after an existing instruction `cursor`.
118 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
119 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
120
Roland Levillain6b469232014-09-25 10:10:38 +0100121 // Return true if this list contains `instruction`.
122 bool Contains(HInstruction* instruction) const;
123
Roland Levillainccc07a92014-09-16 14:48:16 +0100124 // Return true if `instruction1` is found before `instruction2` in
125 // this instruction list and false otherwise. Abort if none
126 // of these instructions is found.
127 bool FoundBefore(const HInstruction* instruction1,
128 const HInstruction* instruction2) const;
129
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000130 bool IsEmpty() const { return first_instruction_ == nullptr; }
131 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
132
133 // Update the block of all instructions to be `block`.
134 void SetBlockOfInstructions(HBasicBlock* block) const;
135
136 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000137 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000138 void Add(const HInstructionList& instruction_list);
139
David Brazdil2d7352b2015-04-20 14:52:42 +0100140 // Return the number of instructions in the list. This is an expensive operation.
141 size_t CountSize() const;
142
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100143 private:
144 HInstruction* first_instruction_;
145 HInstruction* last_instruction_;
146
147 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000148 friend class HGraph;
149 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100150 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100151 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100152
153 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
154};
155
David Brazdil4833f5a2015-12-16 10:37:39 +0000156class ReferenceTypeInfo : ValueObject {
157 public:
158 typedef Handle<mirror::Class> TypeHandle;
159
Vladimir Markoa1de9182016-02-25 11:37:38 +0000160 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
161
162 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000163 return ReferenceTypeInfo(type_handle, is_exact);
164 }
165
166 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
167
Vladimir Markof39745e2016-01-26 12:16:55 +0000168 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000169 return handle.GetReference() != nullptr;
170 }
171
Vladimir Marko456307a2016-04-19 14:12:13 +0000172 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000173 return IsValidHandle(type_handle_);
174 }
175
176 bool IsExact() const { return is_exact_; }
177
178 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
179 DCHECK(IsValid());
180 return GetTypeHandle()->IsObjectClass();
181 }
182
183 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
184 DCHECK(IsValid());
185 return GetTypeHandle()->IsStringClass();
186 }
187
188 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
189 DCHECK(IsValid());
190 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
191 }
192
193 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
194 DCHECK(IsValid());
195 return GetTypeHandle()->IsInterface();
196 }
197
198 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
199 DCHECK(IsValid());
200 return GetTypeHandle()->IsArrayClass();
201 }
202
203 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
204 DCHECK(IsValid());
205 return GetTypeHandle()->IsPrimitiveArray();
206 }
207
208 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
209 DCHECK(IsValid());
210 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
211 }
212
213 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
214 DCHECK(IsValid());
215 if (!IsExact()) return false;
216 if (!IsArrayClass()) return false;
217 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
218 }
219
220 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
221 DCHECK(IsValid());
222 if (!IsExact()) return false;
223 if (!IsArrayClass()) return false;
224 if (!rti.IsArrayClass()) return false;
225 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
226 rti.GetTypeHandle()->GetComponentType());
227 }
228
229 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
230
231 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
232 DCHECK(IsValid());
233 DCHECK(rti.IsValid());
234 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
235 }
236
237 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
238 DCHECK(IsValid());
239 DCHECK(rti.IsValid());
240 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
241 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
242 }
243
244 // Returns true if the type information provide the same amount of details.
245 // Note that it does not mean that the instructions have the same actual type
246 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000247 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000248 if (!IsValid() && !rti.IsValid()) {
249 // Invalid types are equal.
250 return true;
251 }
252 if (!IsValid() || !rti.IsValid()) {
253 // One is valid, the other not.
254 return false;
255 }
256 return IsExact() == rti.IsExact()
257 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
258 }
259
260 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000261 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
262 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
263 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000264
265 // The class of the object.
266 TypeHandle type_handle_;
267 // Whether or not the type is exact or a superclass of the actual type.
268 // Whether or not we have any information about this type.
269 bool is_exact_;
270};
271
272std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
273
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000274// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100275class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000276 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100277 HGraph(ArenaAllocator* arena,
278 const DexFile& dex_file,
279 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100280 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700281 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100282 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100283 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000284 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100285 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000286 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100287 blocks_(arena->Adapter(kArenaAllocBlockList)),
288 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
289 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700290 entry_block_(nullptr),
291 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100292 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100293 number_of_vregs_(0),
294 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000295 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400296 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000297 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000298 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000299 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000300 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100301 dex_file_(dex_file),
302 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100303 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100304 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100305 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700306 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000307 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100308 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
309 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
310 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
311 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000312 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000313 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
314 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100315 blocks_.reserve(kDefaultNumberOfBlocks);
316 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000317
David Brazdilbadd8262016-02-02 16:28:56 +0000318 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
319 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
320
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000321 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100322 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
323
David Brazdil69ba7b72015-06-23 18:27:30 +0100324 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000325 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100326
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000327 HBasicBlock* GetEntryBlock() const { return entry_block_; }
328 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100329 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000330
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000331 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
332 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000333
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000334 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100335
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100336 void ComputeDominanceInformation();
337 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000338 void ClearLoopInformation();
339 void FindBackEdges(ArenaBitVector* visited);
340 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100341 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100342 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000343
David Brazdil4833f5a2015-12-16 10:37:39 +0000344 // Analyze all natural loops in this graph. Returns a code specifying that it
345 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000346 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000347 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100348
David Brazdilffee3d32015-07-06 11:48:53 +0100349 // Iterate over blocks to compute try block membership. Needs reverse post
350 // order and loop information.
351 void ComputeTryBlockInformation();
352
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000353 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000354 // Returns the instruction to replace the invoke expression or null if the
355 // invoke is for a void method. Note that the caller is responsible for replacing
356 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000357 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000358
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000359 // Update the loop and try membership of `block`, which was spawned from `reference`.
360 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
361 // should be the new back edge.
362 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
363 HBasicBlock* reference,
364 bool replace_if_back_edge);
365
Mingyao Yang3584bce2015-05-19 16:01:59 -0700366 // Need to add a couple of blocks to test if the loop body is entered and
367 // put deoptimization instructions, etc.
368 void TransformLoopHeaderForBCE(HBasicBlock* header);
369
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000370 // Removes `block` from the graph. Assumes `block` has been disconnected from
371 // other blocks and has no instructions or phis.
372 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000373
David Brazdilfc6a86a2015-06-26 10:33:45 +0000374 // Splits the edge between `block` and `successor` while preserving the
375 // indices in the predecessor/successor lists. If there are multiple edges
376 // between the blocks, the lowest indices are used.
377 // Returns the new block which is empty and has the same dex pc as `successor`.
378 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
379
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100380 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
381 void SimplifyLoop(HBasicBlock* header);
382
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000383 int32_t GetNextInstructionId() {
384 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000385 return current_instruction_id_++;
386 }
387
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000388 int32_t GetCurrentInstructionId() const {
389 return current_instruction_id_;
390 }
391
392 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000393 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000394 current_instruction_id_ = id;
395 }
396
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100397 uint16_t GetMaximumNumberOfOutVRegs() const {
398 return maximum_number_of_out_vregs_;
399 }
400
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000401 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
402 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100403 }
404
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100405 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
406 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
407 }
408
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000409 void UpdateTemporariesVRegSlots(size_t slots) {
410 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100411 }
412
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000413 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100414 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000415 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100416 }
417
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100418 void SetNumberOfVRegs(uint16_t number_of_vregs) {
419 number_of_vregs_ = number_of_vregs;
420 }
421
422 uint16_t GetNumberOfVRegs() const {
423 return number_of_vregs_;
424 }
425
426 void SetNumberOfInVRegs(uint16_t value) {
427 number_of_in_vregs_ = value;
428 }
429
David Brazdildee58d62016-04-07 09:54:26 +0000430 uint16_t GetNumberOfInVRegs() const {
431 return number_of_in_vregs_;
432 }
433
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100434 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100435 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100436 return number_of_vregs_ - number_of_in_vregs_;
437 }
438
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100439 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100440 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100441 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100442
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100443 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100444 return linear_order_;
445 }
446
Mark Mendell1152c922015-04-24 17:06:35 -0400447 bool HasBoundsChecks() const {
448 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800449 }
450
Mark Mendell1152c922015-04-24 17:06:35 -0400451 void SetHasBoundsChecks(bool value) {
452 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800453 }
454
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100455 bool ShouldGenerateConstructorBarrier() const {
456 return should_generate_constructor_barrier_;
457 }
458
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000459 bool IsDebuggable() const { return debuggable_; }
460
David Brazdil8d5b8b22015-03-24 10:51:52 +0000461 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000462 // already, it is created and inserted into the graph. This method is only for
463 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600464 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000465
466 // TODO: This is problematic for the consistency of reference type propagation
467 // because it can be created anytime after the pass and thus it will be left
468 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600469 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000470
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600471 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
472 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000473 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600474 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
475 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000476 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600477 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
478 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000479 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600480 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
481 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000482 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000483
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100484 HCurrentMethod* GetCurrentMethod();
485
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100486 const DexFile& GetDexFile() const {
487 return dex_file_;
488 }
489
490 uint32_t GetMethodIdx() const {
491 return method_idx_;
492 }
493
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100494 InvokeType GetInvokeType() const {
495 return invoke_type_;
496 }
497
Mark Mendellc4701932015-04-10 13:18:51 -0400498 InstructionSet GetInstructionSet() const {
499 return instruction_set_;
500 }
501
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000502 bool IsCompilingOsr() const { return osr_; }
503
David Brazdil77a48ae2015-09-15 12:34:04 +0000504 bool HasTryCatch() const { return has_try_catch_; }
505 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100506
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000507 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
508 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
509
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100510 ArtMethod* GetArtMethod() const { return art_method_; }
511 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
512
Mark Mendellf6529172015-11-17 11:16:56 -0500513 // Returns an instruction with the opposite boolean value from 'cond'.
514 // The instruction has been inserted into the graph, either as a constant, or
515 // before cursor.
516 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
517
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000518 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
519
David Brazdil2d7352b2015-04-20 14:52:42 +0100520 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000521 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100522 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000523
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000524 template <class InstructionType, typename ValueType>
525 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600526 ArenaSafeMap<ValueType, InstructionType*>* cache,
527 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000528 // Try to find an existing constant of the given value.
529 InstructionType* constant = nullptr;
530 auto cached_constant = cache->find(value);
531 if (cached_constant != cache->end()) {
532 constant = cached_constant->second;
533 }
534
535 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100536 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000537 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600538 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000539 cache->Overwrite(value, constant);
540 InsertConstant(constant);
541 }
542 return constant;
543 }
544
David Brazdil8d5b8b22015-03-24 10:51:52 +0000545 void InsertConstant(HConstant* instruction);
546
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000547 // Cache a float constant into the graph. This method should only be
548 // called by the SsaBuilder when creating "equivalent" instructions.
549 void CacheFloatConstant(HFloatConstant* constant);
550
551 // See CacheFloatConstant comment.
552 void CacheDoubleConstant(HDoubleConstant* constant);
553
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000554 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000555
556 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100557 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000558
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100559 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100560 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000561
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100562 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100563 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100564
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000565 HBasicBlock* entry_block_;
566 HBasicBlock* exit_block_;
567
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100568 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100569 uint16_t maximum_number_of_out_vregs_;
570
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100571 // The number of virtual registers in this method. Contains the parameters.
572 uint16_t number_of_vregs_;
573
574 // The number of virtual registers used by parameters of this method.
575 uint16_t number_of_in_vregs_;
576
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000577 // Number of vreg size slots that the temporaries use (used in baseline compiler).
578 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100579
Mark Mendell1152c922015-04-24 17:06:35 -0400580 // Has bounds checks. We can totally skip BCE if it's false.
581 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800582
David Brazdil77a48ae2015-09-15 12:34:04 +0000583 // Flag whether there are any try/catch blocks in the graph. We will skip
584 // try/catch-related passes if false.
585 bool has_try_catch_;
586
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000587 // Flag whether there are any irreducible loops in the graph.
588 bool has_irreducible_loops_;
589
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000590 // Indicates whether the graph should be compiled in a way that
591 // ensures full debuggability. If false, we can apply more
592 // aggressive optimizations that may limit the level of debugging.
593 const bool debuggable_;
594
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000595 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000596 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000597
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100598 // The dex file from which the method is from.
599 const DexFile& dex_file_;
600
601 // The method index in the dex file.
602 const uint32_t method_idx_;
603
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100604 // If inlined, this encodes how the callee is being invoked.
605 const InvokeType invoke_type_;
606
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100607 // Whether the graph has been transformed to SSA form. Only used
608 // in debug mode to ensure we are not using properties only valid
609 // for non-SSA form (like the number of temporaries).
610 bool in_ssa_form_;
611
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100612 const bool should_generate_constructor_barrier_;
613
Mathieu Chartiere401d142015-04-22 13:56:20 -0700614 const InstructionSet instruction_set_;
615
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000616 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000617 HNullConstant* cached_null_constant_;
618 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000619 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000620 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000621 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000622
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100623 HCurrentMethod* cached_current_method_;
624
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100625 // The ArtMethod this graph is for. Note that for AOT, it may be null,
626 // for example for methods whose declaring class could not be resolved
627 // (such as when the superclass could not be found).
628 ArtMethod* art_method_;
629
David Brazdil4833f5a2015-12-16 10:37:39 +0000630 // Keep the RTI of inexact Object to avoid having to pass stack handle
631 // collection pointer to passes which may create NullConstant.
632 ReferenceTypeInfo inexact_object_rti_;
633
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000634 // Whether we are compiling this graph for on stack replacement: this will
635 // make all loops seen as irreducible and emit special stack maps to mark
636 // compiled code entries which the interpreter can directly jump to.
637 const bool osr_;
638
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000639 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100640 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000641 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000642 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000643 DISALLOW_COPY_AND_ASSIGN(HGraph);
644};
645
Vladimir Markof9f64412015-09-02 14:05:49 +0100646class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000647 public:
648 HLoopInformation(HBasicBlock* header, HGraph* graph)
649 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100650 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000651 irreducible_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100652 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100653 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000654 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100655 back_edges_.reserve(kDefaultNumberOfBackEdges);
656 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100657
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000658 bool IsIrreducible() const { return irreducible_; }
659
660 void Dump(std::ostream& os);
661
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100662 HBasicBlock* GetHeader() const {
663 return header_;
664 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000665
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000666 void SetHeader(HBasicBlock* block) {
667 header_ = block;
668 }
669
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100670 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
671 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
672 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
673
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000674 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100675 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000676 }
677
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100678 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100679 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100680 }
681
David Brazdil46e2a392015-03-16 17:31:52 +0000682 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100683 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100684 }
685
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000686 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100687 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000688 }
689
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100690 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100691
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100692 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100693 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100694 }
695
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100696 // Returns the lifetime position of the back edge that has the
697 // greatest lifetime position.
698 size_t GetLifetimeEnd() const;
699
700 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100701 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100702 }
703
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000704 // Finds blocks that are part of this loop.
705 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100706
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100707 // Returns whether this loop information contains `block`.
708 // Note that this loop information *must* be populated before entering this function.
709 bool Contains(const HBasicBlock& block) const;
710
711 // Returns whether this loop information is an inner loop of `other`.
712 // Note that `other` *must* be populated before entering this function.
713 bool IsIn(const HLoopInformation& other) const;
714
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800715 // Returns true if instruction is not defined within this loop.
716 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700717
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100718 const ArenaBitVector& GetBlocks() const { return blocks_; }
719
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000720 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000721 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000722
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000723 void ClearAllBlocks() {
724 blocks_.ClearAllBits();
725 }
726
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000727 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100728 // Internal recursive implementation of `Populate`.
729 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100730 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100731
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000732 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100733 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000734 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100735 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100736 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000737
738 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
739};
740
David Brazdilec16f792015-08-19 15:04:01 +0100741// Stores try/catch information for basic blocks.
742// Note that HGraph is constructed so that catch blocks cannot simultaneously
743// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100744class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100745 public:
746 // Try block information constructor.
747 explicit TryCatchInformation(const HTryBoundary& try_entry)
748 : try_entry_(&try_entry),
749 catch_dex_file_(nullptr),
750 catch_type_index_(DexFile::kDexNoIndex16) {
751 DCHECK(try_entry_ != nullptr);
752 }
753
754 // Catch block information constructor.
755 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
756 : try_entry_(nullptr),
757 catch_dex_file_(&dex_file),
758 catch_type_index_(catch_type_index) {}
759
760 bool IsTryBlock() const { return try_entry_ != nullptr; }
761
762 const HTryBoundary& GetTryEntry() const {
763 DCHECK(IsTryBlock());
764 return *try_entry_;
765 }
766
767 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
768
769 bool IsCatchAllTypeIndex() const {
770 DCHECK(IsCatchBlock());
771 return catch_type_index_ == DexFile::kDexNoIndex16;
772 }
773
774 uint16_t GetCatchTypeIndex() const {
775 DCHECK(IsCatchBlock());
776 return catch_type_index_;
777 }
778
779 const DexFile& GetCatchDexFile() const {
780 DCHECK(IsCatchBlock());
781 return *catch_dex_file_;
782 }
783
784 private:
785 // One of possibly several TryBoundary instructions entering the block's try.
786 // Only set for try blocks.
787 const HTryBoundary* try_entry_;
788
789 // Exception type information. Only set for catch blocks.
790 const DexFile* catch_dex_file_;
791 const uint16_t catch_type_index_;
792};
793
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100794static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100795static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100796
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000797// A block in a method. Contains the list of instructions represented
798// as a double linked list. Each block knows its predecessors and
799// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100800
Vladimir Markof9f64412015-09-02 14:05:49 +0100801class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000802 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600803 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000804 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000805 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
806 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000807 loop_information_(nullptr),
808 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000809 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100810 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100811 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100812 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000813 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000814 try_catch_information_(nullptr) {
815 predecessors_.reserve(kDefaultNumberOfPredecessors);
816 successors_.reserve(kDefaultNumberOfSuccessors);
817 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
818 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000819
Vladimir Marko60584552015-09-03 13:35:12 +0000820 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100821 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000822 }
823
Vladimir Marko60584552015-09-03 13:35:12 +0000824 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100825 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000826 }
827
David Brazdild26a4112015-11-10 11:07:31 +0000828 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
829 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
830
Vladimir Marko60584552015-09-03 13:35:12 +0000831 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
832 return ContainsElement(successors_, block, start_from);
833 }
834
835 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100836 return dominated_blocks_;
837 }
838
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100839 bool IsEntryBlock() const {
840 return graph_->GetEntryBlock() == this;
841 }
842
843 bool IsExitBlock() const {
844 return graph_->GetExitBlock() == this;
845 }
846
David Brazdil46e2a392015-03-16 17:31:52 +0000847 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000848 bool IsSingleTryBoundary() const;
849
850 // Returns true if this block emits nothing but a jump.
851 bool IsSingleJump() const {
852 HLoopInformation* loop_info = GetLoopInformation();
853 return (IsSingleGoto() || IsSingleTryBoundary())
854 // Back edges generate a suspend check.
855 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
856 }
David Brazdil46e2a392015-03-16 17:31:52 +0000857
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000858 void AddBackEdge(HBasicBlock* back_edge) {
859 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000860 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000861 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100862 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000863 loop_information_->AddBackEdge(back_edge);
864 }
865
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000866 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000867 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000868
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100869 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000870 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600871 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000872
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000873 HBasicBlock* GetDominator() const { return dominator_; }
874 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000875 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
876
877 void RemoveDominatedBlock(HBasicBlock* block) {
878 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100879 }
Vladimir Marko60584552015-09-03 13:35:12 +0000880
881 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
882 ReplaceElement(dominated_blocks_, existing, new_block);
883 }
884
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100885 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000886
887 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100888 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000889 }
890
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100891 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
892 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100893 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100894 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100895 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
896 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000897
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000898 HInstruction* GetFirstInstructionDisregardMoves() const;
899
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000900 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000901 successors_.push_back(block);
902 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000903 }
904
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100905 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
906 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100907 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000908 new_block->predecessors_.push_back(this);
909 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000910 }
911
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000912 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
913 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000914 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000915 new_block->successors_.push_back(this);
916 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000917 }
918
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100919 // Insert `this` between `predecessor` and `successor. This method
920 // preserves the indicies, and will update the first edge found between
921 // `predecessor` and `successor`.
922 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
923 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100924 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000925 successor->predecessors_[predecessor_index] = this;
926 predecessor->successors_[successor_index] = this;
927 successors_.push_back(successor);
928 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100929 }
930
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100931 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000932 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100933 }
934
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000935 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000936 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000937 }
938
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100939 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000940 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100941 }
942
943 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000944 predecessors_.push_back(block);
945 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100946 }
947
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100948 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000949 DCHECK_EQ(predecessors_.size(), 2u);
950 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100951 }
952
David Brazdil769c9e52015-04-27 13:54:09 +0100953 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000954 DCHECK_EQ(successors_.size(), 2u);
955 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100956 }
957
David Brazdilfc6a86a2015-06-26 10:33:45 +0000958 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000959 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100960 }
961
David Brazdilfc6a86a2015-06-26 10:33:45 +0000962 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000963 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100964 }
965
David Brazdilfc6a86a2015-06-26 10:33:45 +0000966 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000967 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100968 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000969 }
970
971 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000972 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100973 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000974 }
975
976 // Returns whether the first occurrence of `predecessor` in the list of
977 // predecessors is at index `idx`.
978 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100979 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000980 return GetPredecessorIndexOf(predecessor) == idx;
981 }
982
David Brazdild7558da2015-09-22 13:04:14 +0100983 // Create a new block between this block and its predecessors. The new block
984 // is added to the graph, all predecessor edges are relinked to it and an edge
985 // is created to `this`. Returns the new empty block. Reverse post order or
986 // loop and try/catch information are not updated.
987 HBasicBlock* CreateImmediateDominator();
988
David Brazdilfc6a86a2015-06-26 10:33:45 +0000989 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100990 // created, latter block. Note that this method will add the block to the
991 // graph, create a Goto at the end of the former block and will create an edge
992 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100993 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000994 HBasicBlock* SplitBefore(HInstruction* cursor);
995
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000996 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000997 // created block. Note that this method just updates raw block information,
998 // like predecessors, successors, dominators, and instruction list. It does not
999 // update the graph, reverse post order, loop information, nor make sure the
1000 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001001 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1002
1003 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1004 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001005
1006 // Merge `other` at the end of `this`. Successors and dominated blocks of
1007 // `other` are changed to be successors and dominated blocks of `this`. Note
1008 // that this method does not update the graph, reverse post order, loop
1009 // information, nor make sure the blocks are consistent (for example ending
1010 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001011 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001012
1013 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1014 // of `this` are moved to `other`.
1015 // Note that this method does not update the graph, reverse post order, loop
1016 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001017 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001018 void ReplaceWith(HBasicBlock* other);
1019
David Brazdil2d7352b2015-04-20 14:52:42 +01001020 // Merge `other` at the end of `this`. This method updates loops, reverse post
1021 // order, links to predecessors, successors, dominators and deletes the block
1022 // from the graph. The two blocks must be successive, i.e. `this` the only
1023 // predecessor of `other` and vice versa.
1024 void MergeWith(HBasicBlock* other);
1025
1026 // Disconnects `this` from all its predecessors, successors and dominator,
1027 // removes it from all loops it is included in and eventually from the graph.
1028 // The block must not dominate any other block. Predecessors and successors
1029 // are safely updated.
1030 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001031
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001032 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001033 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001034 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001035 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001036 // Replace instruction `initial` with `replacement` within this block.
1037 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1038 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001039 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001040 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001041 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001042 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1043 // instruction list. With 'ensure_safety' set to true, it verifies that the
1044 // instruction is not in use and removes it from the use lists of its inputs.
1045 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1046 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001047 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001048
1049 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001050 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001051 }
1052
Roland Levillain6b879dd2014-09-22 17:13:44 +01001053 bool IsLoopPreHeaderFirstPredecessor() const {
1054 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001055 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001056 }
1057
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001058 bool IsFirstPredecessorBackEdge() const {
1059 DCHECK(IsLoopHeader());
1060 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1061 }
1062
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001063 HLoopInformation* GetLoopInformation() const {
1064 return loop_information_;
1065 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001066
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001067 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001068 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001069 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001070 void SetInLoop(HLoopInformation* info) {
1071 if (IsLoopHeader()) {
1072 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001073 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001074 loop_information_ = info;
1075 } else if (loop_information_->Contains(*info->GetHeader())) {
1076 // Block is currently part of an outer loop. Make it part of this inner loop.
1077 // Note that a non loop header having a loop information means this loop information
1078 // has already been populated
1079 loop_information_ = info;
1080 } else {
1081 // Block is part of an inner loop. Do not update the loop information.
1082 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1083 // at this point, because this method is being called while populating `info`.
1084 }
1085 }
1086
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001087 // Raw update of the loop information.
1088 void SetLoopInformation(HLoopInformation* info) {
1089 loop_information_ = info;
1090 }
1091
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001092 bool IsInLoop() const { return loop_information_ != nullptr; }
1093
David Brazdilec16f792015-08-19 15:04:01 +01001094 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1095
1096 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1097 try_catch_information_ = try_catch_information;
1098 }
1099
1100 bool IsTryBlock() const {
1101 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1102 }
1103
1104 bool IsCatchBlock() const {
1105 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1106 }
David Brazdilffee3d32015-07-06 11:48:53 +01001107
1108 // Returns the try entry that this block's successors should have. They will
1109 // be in the same try, unless the block ends in a try boundary. In that case,
1110 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001111 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001112
David Brazdild7558da2015-09-22 13:04:14 +01001113 bool HasThrowingInstructions() const;
1114
David Brazdila4b8c212015-05-07 09:59:30 +01001115 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001116 bool Dominates(HBasicBlock* block) const;
1117
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001118 size_t GetLifetimeStart() const { return lifetime_start_; }
1119 size_t GetLifetimeEnd() const { return lifetime_end_; }
1120
1121 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1122 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1123
David Brazdil8d5b8b22015-03-24 10:51:52 +00001124 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001125 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001126 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001127 bool HasSinglePhi() const;
1128
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001129 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001130 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001131 ArenaVector<HBasicBlock*> predecessors_;
1132 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001133 HInstructionList instructions_;
1134 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001135 HLoopInformation* loop_information_;
1136 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001137 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001138 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001139 // The dex program counter of the first instruction of this block.
1140 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001141 size_t lifetime_start_;
1142 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001143 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001144
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001145 friend class HGraph;
1146 friend class HInstruction;
1147
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001148 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1149};
1150
David Brazdilb2bd1c52015-03-25 11:17:37 +00001151// Iterates over the LoopInformation of all loops which contain 'block'
1152// from the innermost to the outermost.
1153class HLoopInformationOutwardIterator : public ValueObject {
1154 public:
1155 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1156 : current_(block.GetLoopInformation()) {}
1157
1158 bool Done() const { return current_ == nullptr; }
1159
1160 void Advance() {
1161 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001162 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001163 }
1164
1165 HLoopInformation* Current() const {
1166 DCHECK(!Done());
1167 return current_;
1168 }
1169
1170 private:
1171 HLoopInformation* current_;
1172
1173 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1174};
1175
Alexandre Ramesef20f712015-06-09 10:29:30 +01001176#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001177 M(Above, Condition) \
1178 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001179 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001180 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001181 M(ArrayGet, Instruction) \
1182 M(ArrayLength, Instruction) \
1183 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001184 M(Below, Condition) \
1185 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001186 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001187 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001188 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001189 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001190 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001191 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001192 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001193 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001194 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001195 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001196 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001197 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001198 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001199 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001200 M(Exit, Instruction) \
1201 M(FloatConstant, Constant) \
1202 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001203 M(GreaterThan, Condition) \
1204 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001205 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001206 M(InstanceFieldGet, Instruction) \
1207 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001208 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001209 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001210 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001211 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001212 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001213 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001214 M(LessThan, Condition) \
1215 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001216 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001217 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001218 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001219 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001220 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001221 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001222 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001223 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001224 M(Neg, UnaryOperation) \
1225 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001226 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001227 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001228 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001229 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001230 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001231 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001232 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001233 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001234 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001235 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001236 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001237 M(Return, Instruction) \
1238 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001239 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001240 M(Shl, BinaryOperation) \
1241 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001242 M(StaticFieldGet, Instruction) \
1243 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001244 M(UnresolvedInstanceFieldGet, Instruction) \
1245 M(UnresolvedInstanceFieldSet, Instruction) \
1246 M(UnresolvedStaticFieldGet, Instruction) \
1247 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001248 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001249 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001250 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001251 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001252 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001253 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001254 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001255 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001256
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001257/*
1258 * Instructions, shared across several (not all) architectures.
1259 */
1260#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1261#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1262#else
1263#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001264 M(BitwiseNegatedRight, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001265 M(MultiplyAccumulate, Instruction)
1266#endif
1267
Vladimir Markob4536b72015-11-24 13:45:23 +00001268#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001269#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001270#else
1271#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1272 M(ArmDexCacheArraysBase, Instruction)
1273#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001274
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001275#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001276#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001277#else
1278#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001279 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001280 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001281#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001282
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001283#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1284
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001285#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1286
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001287#ifndef ART_ENABLE_CODEGEN_x86
1288#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1289#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001290#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1291 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001292 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001293 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001294 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001295#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001296
1297#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1298
1299#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1300 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001301 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001302 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1303 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001304 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001305 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001306 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1307 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1308
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001309#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1310 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001311 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001312 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001313 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001314 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001315
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001316#define FOR_EACH_INSTRUCTION(M) \
1317 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1318 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1319
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001320#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001321FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1322#undef FORWARD_DECLARATION
1323
Roland Levillainccc07a92014-09-16 14:48:16 +01001324#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001325 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001326 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001327 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001328 return other->Is##type(); \
1329 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001330 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001331
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001332#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1333 bool Is##type() const { return As##type() != nullptr; } \
1334 const H##type* As##type() const { return this; } \
1335 H##type* As##type() { return this; }
1336
David Brazdiled596192015-01-23 10:39:45 +00001337template <typename T> class HUseList;
1338
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001339template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001340class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001341 public:
David Brazdiled596192015-01-23 10:39:45 +00001342 HUseListNode* GetPrevious() const { return prev_; }
1343 HUseListNode* GetNext() const { return next_; }
1344 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001345 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001346 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001347
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001348 private:
David Brazdiled596192015-01-23 10:39:45 +00001349 HUseListNode(T user, size_t index)
1350 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1351
1352 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001353 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001354 HUseListNode<T>* prev_;
1355 HUseListNode<T>* next_;
1356
1357 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001358
1359 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1360};
1361
David Brazdiled596192015-01-23 10:39:45 +00001362template <typename T>
1363class HUseList : public ValueObject {
1364 public:
1365 HUseList() : first_(nullptr) {}
1366
1367 void Clear() {
1368 first_ = nullptr;
1369 }
1370
1371 // Adds a new entry at the beginning of the use list and returns
1372 // the newly created node.
1373 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001374 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001375 if (IsEmpty()) {
1376 first_ = new_node;
1377 } else {
1378 first_->prev_ = new_node;
1379 new_node->next_ = first_;
1380 first_ = new_node;
1381 }
1382 return new_node;
1383 }
1384
1385 HUseListNode<T>* GetFirst() const {
1386 return first_;
1387 }
1388
1389 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001390 DCHECK(node != nullptr);
1391 DCHECK(Contains(node));
1392
David Brazdiled596192015-01-23 10:39:45 +00001393 if (node->prev_ != nullptr) {
1394 node->prev_->next_ = node->next_;
1395 }
1396 if (node->next_ != nullptr) {
1397 node->next_->prev_ = node->prev_;
1398 }
1399 if (node == first_) {
1400 first_ = node->next_;
1401 }
1402 }
1403
David Brazdil1abb4192015-02-17 18:33:36 +00001404 bool Contains(const HUseListNode<T>* node) const {
1405 if (node == nullptr) {
1406 return false;
1407 }
1408 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1409 if (current == node) {
1410 return true;
1411 }
1412 }
1413 return false;
1414 }
1415
David Brazdiled596192015-01-23 10:39:45 +00001416 bool IsEmpty() const {
1417 return first_ == nullptr;
1418 }
1419
1420 bool HasOnlyOneUse() const {
1421 return first_ != nullptr && first_->next_ == nullptr;
1422 }
1423
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001424 size_t SizeSlow() const {
1425 size_t count = 0;
1426 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1427 ++count;
1428 }
1429 return count;
1430 }
1431
David Brazdiled596192015-01-23 10:39:45 +00001432 private:
1433 HUseListNode<T>* first_;
1434};
1435
1436template<typename T>
1437class HUseIterator : public ValueObject {
1438 public:
1439 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1440
1441 bool Done() const { return current_ == nullptr; }
1442
1443 void Advance() {
1444 DCHECK(!Done());
1445 current_ = current_->GetNext();
1446 }
1447
1448 HUseListNode<T>* Current() const {
1449 DCHECK(!Done());
1450 return current_;
1451 }
1452
1453 private:
1454 HUseListNode<T>* current_;
1455
1456 friend class HValue;
1457};
1458
David Brazdil1abb4192015-02-17 18:33:36 +00001459// This class is used by HEnvironment and HInstruction classes to record the
1460// instructions they use and pointers to the corresponding HUseListNodes kept
1461// by the used instructions.
1462template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001463class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001464 public:
1465 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1466 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1467
1468 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1469 : instruction_(old_record.instruction_), use_node_(use_node) {
1470 DCHECK(instruction_ != nullptr);
1471 DCHECK(use_node_ != nullptr);
1472 DCHECK(old_record.use_node_ == nullptr);
1473 }
1474
1475 HInstruction* GetInstruction() const { return instruction_; }
1476 HUseListNode<T>* GetUseNode() const { return use_node_; }
1477
1478 private:
1479 // Instruction used by the user.
1480 HInstruction* instruction_;
1481
1482 // Corresponding entry in the use list kept by 'instruction_'.
1483 HUseListNode<T>* use_node_;
1484};
1485
Aart Bik854a02b2015-07-14 16:07:00 -07001486/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001487 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001488 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001489 * For write/read dependences on fields/arrays, the dependence analysis uses
1490 * type disambiguation (e.g. a float field write cannot modify the value of an
1491 * integer field read) and the access type (e.g. a reference array write cannot
1492 * modify the value of a reference field read [although it may modify the
1493 * reference fetch prior to reading the field, which is represented by its own
1494 * write/read dependence]). The analysis makes conservative points-to
1495 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1496 * the same, and any reference read depends on any reference read without
1497 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001498 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001499 * The internal representation uses 38-bit and is described in the table below.
1500 * The first line indicates the side effect, and for field/array accesses the
1501 * second line indicates the type of the access (in the order of the
1502 * Primitive::Type enum).
1503 * The two numbered lines below indicate the bit position in the bitfield (read
1504 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001505 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001506 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1507 * +-------------+---------+---------+--------------+---------+---------+
1508 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1509 * | 3 |333333322|222222221| 1 |111111110|000000000|
1510 * | 7 |654321098|765432109| 8 |765432109|876543210|
1511 *
1512 * Note that, to ease the implementation, 'changes' bits are least significant
1513 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001514 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001515class SideEffects : public ValueObject {
1516 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001517 SideEffects() : flags_(0) {}
1518
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001519 static SideEffects None() {
1520 return SideEffects(0);
1521 }
1522
1523 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001524 return SideEffects(kAllChangeBits | kAllDependOnBits);
1525 }
1526
1527 static SideEffects AllChanges() {
1528 return SideEffects(kAllChangeBits);
1529 }
1530
1531 static SideEffects AllDependencies() {
1532 return SideEffects(kAllDependOnBits);
1533 }
1534
1535 static SideEffects AllExceptGCDependency() {
1536 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1537 }
1538
1539 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001540 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001541 }
1542
Aart Bik34c3ba92015-07-20 14:08:59 -07001543 static SideEffects AllWrites() {
1544 return SideEffects(kAllWrites);
1545 }
1546
1547 static SideEffects AllReads() {
1548 return SideEffects(kAllReads);
1549 }
1550
1551 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1552 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001553 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001554 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001555 }
1556
Aart Bik854a02b2015-07-14 16:07:00 -07001557 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001558 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001559 }
1560
Aart Bik34c3ba92015-07-20 14:08:59 -07001561 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1562 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001563 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001564 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001565 }
1566
1567 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001568 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001569 }
1570
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001571 static SideEffects CanTriggerGC() {
1572 return SideEffects(1ULL << kCanTriggerGCBit);
1573 }
1574
1575 static SideEffects DependsOnGC() {
1576 return SideEffects(1ULL << kDependsOnGCBit);
1577 }
1578
Aart Bik854a02b2015-07-14 16:07:00 -07001579 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001580 SideEffects Union(SideEffects other) const {
1581 return SideEffects(flags_ | other.flags_);
1582 }
1583
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001584 SideEffects Exclusion(SideEffects other) const {
1585 return SideEffects(flags_ & ~other.flags_);
1586 }
1587
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001588 void Add(SideEffects other) {
1589 flags_ |= other.flags_;
1590 }
1591
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001592 bool Includes(SideEffects other) const {
1593 return (other.flags_ & flags_) == other.flags_;
1594 }
1595
1596 bool HasSideEffects() const {
1597 return (flags_ & kAllChangeBits);
1598 }
1599
1600 bool HasDependencies() const {
1601 return (flags_ & kAllDependOnBits);
1602 }
1603
1604 // Returns true if there are no side effects or dependencies.
1605 bool DoesNothing() const {
1606 return flags_ == 0;
1607 }
1608
Aart Bik854a02b2015-07-14 16:07:00 -07001609 // Returns true if something is written.
1610 bool DoesAnyWrite() const {
1611 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001612 }
1613
Aart Bik854a02b2015-07-14 16:07:00 -07001614 // Returns true if something is read.
1615 bool DoesAnyRead() const {
1616 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001617 }
1618
Aart Bik854a02b2015-07-14 16:07:00 -07001619 // Returns true if potentially everything is written and read
1620 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001621 bool DoesAllReadWrite() const {
1622 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1623 }
1624
Aart Bik854a02b2015-07-14 16:07:00 -07001625 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001626 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001627 }
1628
Roland Levillain0d5a2812015-11-13 10:07:31 +00001629 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001630 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001631 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1632 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001633 }
1634
1635 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001636 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001637 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001638 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001639 for (int s = kLastBit; s >= 0; s--) {
1640 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1641 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1642 // This is a bit for the GC side effect.
1643 if (current_bit_is_set) {
1644 flags += "GC";
1645 }
Aart Bik854a02b2015-07-14 16:07:00 -07001646 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001647 } else {
1648 // This is a bit for the array/field analysis.
1649 // The underscore character stands for the 'can trigger GC' bit.
1650 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1651 if (current_bit_is_set) {
1652 flags += kDebug[s];
1653 }
1654 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1655 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1656 flags += "|";
1657 }
1658 }
Aart Bik854a02b2015-07-14 16:07:00 -07001659 }
1660 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001661 }
1662
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001663 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001664
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001665 private:
1666 static constexpr int kFieldArrayAnalysisBits = 9;
1667
1668 static constexpr int kFieldWriteOffset = 0;
1669 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1670 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1671 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1672
1673 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1674
1675 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1676 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1677 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1678 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1679
1680 static constexpr int kLastBit = kDependsOnGCBit;
1681 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1682
1683 // Aliases.
1684
1685 static_assert(kChangeBits == kDependOnBits,
1686 "the 'change' bits should match the 'depend on' bits.");
1687
1688 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1689 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1690 static constexpr uint64_t kAllWrites =
1691 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1692 static constexpr uint64_t kAllReads =
1693 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001694
Aart Bik854a02b2015-07-14 16:07:00 -07001695 // Translates type to bit flag.
1696 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1697 CHECK_NE(type, Primitive::kPrimVoid);
1698 const uint64_t one = 1;
1699 const int shift = type; // 0-based consecutive enum
1700 DCHECK_LE(kFieldWriteOffset, shift);
1701 DCHECK_LT(shift, kArrayWriteOffset);
1702 return one << (type + offset);
1703 }
1704
1705 // Private constructor on direct flags value.
1706 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1707
1708 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001709};
1710
David Brazdiled596192015-01-23 10:39:45 +00001711// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001712class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001713 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001714 HEnvironment(ArenaAllocator* arena,
1715 size_t number_of_vregs,
1716 const DexFile& dex_file,
1717 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001718 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001719 InvokeType invoke_type,
1720 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001721 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1722 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001723 parent_(nullptr),
1724 dex_file_(dex_file),
1725 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001726 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001727 invoke_type_(invoke_type),
1728 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001729 }
1730
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001731 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001732 : HEnvironment(arena,
1733 to_copy.Size(),
1734 to_copy.GetDexFile(),
1735 to_copy.GetMethodIdx(),
1736 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001737 to_copy.GetInvokeType(),
1738 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001739
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001740 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001741 if (parent_ != nullptr) {
1742 parent_->SetAndCopyParentChain(allocator, parent);
1743 } else {
1744 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1745 parent_->CopyFrom(parent);
1746 if (parent->GetParent() != nullptr) {
1747 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1748 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001749 }
David Brazdiled596192015-01-23 10:39:45 +00001750 }
1751
Vladimir Marko71bf8092015-09-15 15:33:14 +01001752 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001753 void CopyFrom(HEnvironment* environment);
1754
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001755 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1756 // input to the loop phi instead. This is for inserting instructions that
1757 // require an environment (like HDeoptimization) in the loop pre-header.
1758 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001759
1760 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001761 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001762 }
1763
1764 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001765 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001766 }
1767
David Brazdil1abb4192015-02-17 18:33:36 +00001768 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001769
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001770 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001771
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001772 HEnvironment* GetParent() const { return parent_; }
1773
1774 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001775 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001776 }
1777
1778 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001779 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001780 }
1781
1782 uint32_t GetDexPc() const {
1783 return dex_pc_;
1784 }
1785
1786 uint32_t GetMethodIdx() const {
1787 return method_idx_;
1788 }
1789
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001790 InvokeType GetInvokeType() const {
1791 return invoke_type_;
1792 }
1793
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001794 const DexFile& GetDexFile() const {
1795 return dex_file_;
1796 }
1797
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001798 HInstruction* GetHolder() const {
1799 return holder_;
1800 }
1801
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001802
1803 bool IsFromInlinedInvoke() const {
1804 return GetParent() != nullptr;
1805 }
1806
David Brazdiled596192015-01-23 10:39:45 +00001807 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001808 // Record instructions' use entries of this environment for constant-time removal.
1809 // It should only be called by HInstruction when a new environment use is added.
1810 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1811 DCHECK(env_use->GetUser() == this);
1812 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001813 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001814 }
David Brazdiled596192015-01-23 10:39:45 +00001815
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001816 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1817 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001818 HEnvironment* parent_;
1819 const DexFile& dex_file_;
1820 const uint32_t method_idx_;
1821 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001822 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001823
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001824 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001825 HInstruction* const holder_;
1826
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001827 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001828
1829 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1830};
1831
Vladimir Markof9f64412015-09-02 14:05:49 +01001832class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001833 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001834 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001835 : previous_(nullptr),
1836 next_(nullptr),
1837 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001838 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001839 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001840 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001841 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001842 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001843 locations_(nullptr),
1844 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001845 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001846 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001847 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1848 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1849 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001850
Dave Allison20dfc792014-06-16 20:44:29 -07001851 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001852
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001853#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001854 enum InstructionKind {
1855 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1856 };
1857#undef DECLARE_KIND
1858
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001859 HInstruction* GetNext() const { return next_; }
1860 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001861
Calin Juravle77520bc2015-01-12 18:45:46 +00001862 HInstruction* GetNextDisregardingMoves() const;
1863 HInstruction* GetPreviousDisregardingMoves() const;
1864
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001865 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001866 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001867 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001868 bool IsInBlock() const { return block_ != nullptr; }
1869 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001870 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1871 bool IsIrreducibleLoopHeaderPhi() const {
1872 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1873 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001874
Roland Levillain6b879dd2014-09-22 17:13:44 +01001875 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001876 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001877
1878 virtual void Accept(HGraphVisitor* visitor) = 0;
1879 virtual const char* DebugName() const = 0;
1880
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001881 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001882 void SetRawInputAt(size_t index, HInstruction* input) {
1883 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1884 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001885
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001886 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001887
1888 uint32_t GetDexPc() const { return dex_pc_; }
1889
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001890 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001891
Roland Levillaine161a2a2014-10-03 12:45:18 +01001892 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001893 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001894
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001895 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001896 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001897
Calin Juravle10e244f2015-01-26 18:54:32 +00001898 // Does not apply for all instructions, but having this at top level greatly
1899 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001900 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001901 virtual bool CanBeNull() const {
1902 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1903 return true;
1904 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001905
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001906 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001907 return false;
1908 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001909
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001910 virtual bool IsActualObject() const {
1911 return GetType() == Primitive::kPrimNot;
1912 }
1913
Calin Juravle2e768302015-07-28 14:41:11 +00001914 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001915
Calin Juravle61d544b2015-02-23 16:46:57 +00001916 ReferenceTypeInfo GetReferenceTypeInfo() const {
1917 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001918 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001919 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001920 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001921
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001922 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001923 DCHECK(user != nullptr);
1924 HUseListNode<HInstruction*>* use =
1925 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1926 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001927 }
1928
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001929 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001930 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001931 HUseListNode<HEnvironment*>* env_use =
1932 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1933 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001934 }
1935
David Brazdil1abb4192015-02-17 18:33:36 +00001936 void RemoveAsUserOfInput(size_t input) {
1937 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1938 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1939 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001940
David Brazdil1abb4192015-02-17 18:33:36 +00001941 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1942 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001943
David Brazdiled596192015-01-23 10:39:45 +00001944 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1945 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001946 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001947 bool HasOnlyOneNonEnvironmentUse() const {
1948 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1949 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001950
Roland Levillain6c82d402014-10-13 16:10:27 +01001951 // Does this instruction strictly dominate `other_instruction`?
1952 // Returns false if this instruction and `other_instruction` are the same.
1953 // Aborts if this instruction and `other_instruction` are both phis.
1954 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001955
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001956 int GetId() const { return id_; }
1957 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001958
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001959 int GetSsaIndex() const { return ssa_index_; }
1960 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1961 bool HasSsaIndex() const { return ssa_index_ != -1; }
1962
1963 bool HasEnvironment() const { return environment_ != nullptr; }
1964 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001965 // Set the `environment_` field. Raw because this method does not
1966 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001967 void SetRawEnvironment(HEnvironment* environment) {
1968 DCHECK(environment_ == nullptr);
1969 DCHECK_EQ(environment->GetHolder(), this);
1970 environment_ = environment;
1971 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001972
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001973 void RemoveEnvironment();
1974
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001975 // Set the environment of this instruction, copying it from `environment`. While
1976 // copying, the uses lists are being updated.
1977 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001978 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001979 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001980 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001981 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001982 if (environment->GetParent() != nullptr) {
1983 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1984 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001985 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001986
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001987 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1988 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001989 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001990 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001991 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001992 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001993 if (environment->GetParent() != nullptr) {
1994 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1995 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001996 }
1997
Nicolas Geoffray39468442014-09-02 15:17:15 +01001998 // Returns the number of entries in the environment. Typically, that is the
1999 // number of dex registers in a method. It could be more in case of inlining.
2000 size_t EnvironmentSize() const;
2001
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002002 LocationSummary* GetLocations() const { return locations_; }
2003 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002004
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002005 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002006 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002007
Alexandre Rames188d4312015-04-09 18:30:21 +01002008 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2009 // uses of this instruction by `other` are *not* updated.
2010 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2011 ReplaceWith(other);
2012 other->ReplaceInput(this, use_index);
2013 }
2014
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002015 // Move `this` instruction before `cursor`.
2016 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002017
Vladimir Markofb337ea2015-11-25 15:25:10 +00002018 // Move `this` before its first user and out of any loops. If there is no
2019 // out-of-loop user that dominates all other users, move the instruction
2020 // to the end of the out-of-loop common dominator of the user's blocks.
2021 //
2022 // This can be used only on non-throwing instructions with no side effects that
2023 // have at least one use but no environment uses.
2024 void MoveBeforeFirstUserAndOutOfLoops();
2025
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002026#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002027 bool Is##type() const; \
2028 const H##type* As##type() const; \
2029 H##type* As##type();
2030
2031 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2032#undef INSTRUCTION_TYPE_CHECK
2033
2034#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002035 bool Is##type() const { return (As##type() != nullptr); } \
2036 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002037 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002038 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002039#undef INSTRUCTION_TYPE_CHECK
2040
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002041 // Returns whether the instruction can be moved within the graph.
2042 virtual bool CanBeMoved() const { return false; }
2043
2044 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002045 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002046 return false;
2047 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002048
2049 // Returns whether any data encoded in the two instructions is equal.
2050 // This method does not look at the inputs. Both instructions must be
2051 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002052 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002053 return false;
2054 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002055
2056 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002057 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002058 // 2) Their inputs are identical.
2059 bool Equals(HInstruction* other) const;
2060
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002061 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2062 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2063 // the virtual function because the __attribute__((__pure__)) doesn't really
2064 // apply the strong requirement for virtual functions, preventing optimizations.
2065 InstructionKind GetKind() const PURE;
2066 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002067
2068 virtual size_t ComputeHashCode() const {
2069 size_t result = GetKind();
2070 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2071 result = (result * 31) + InputAt(i)->GetId();
2072 }
2073 return result;
2074 }
2075
2076 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002077 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002078 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002079
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002080 size_t GetLifetimePosition() const { return lifetime_position_; }
2081 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2082 LiveInterval* GetLiveInterval() const { return live_interval_; }
2083 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2084 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2085
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002086 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2087
2088 // Returns whether the code generation of the instruction will require to have access
2089 // to the current method. Such instructions are:
2090 // (1): Instructions that require an environment, as calling the runtime requires
2091 // to walk the stack and have the current method stored at a specific stack address.
2092 // (2): Object literals like classes and strings, that are loaded from the dex cache
2093 // fields of the current method.
2094 bool NeedsCurrentMethod() const {
2095 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2096 }
2097
Vladimir Markodc151b22015-10-15 18:02:30 +01002098 // Returns whether the code generation of the instruction will require to have access
2099 // to the dex cache of the current method's declaring class via the current method.
2100 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002101
Mark Mendellc4701932015-04-10 13:18:51 -04002102 // Does this instruction have any use in an environment before
2103 // control flow hits 'other'?
2104 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2105
2106 // Remove all references to environment uses of this instruction.
2107 // The caller must ensure that this is safe to do.
2108 void RemoveEnvironmentUsers();
2109
Vladimir Markoa1de9182016-02-25 11:37:38 +00002110 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2111 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002112
David Brazdil1abb4192015-02-17 18:33:36 +00002113 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002114 // If set, the machine code for this instruction is assumed to be generated by
2115 // its users. Used by liveness analysis to compute use positions accordingly.
2116 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2117 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2118 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2119 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2120
David Brazdil1abb4192015-02-17 18:33:36 +00002121 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2122 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2123
Vladimir Markoa1de9182016-02-25 11:37:38 +00002124 uint32_t GetPackedFields() const {
2125 return packed_fields_;
2126 }
2127
2128 template <size_t flag>
2129 bool GetPackedFlag() const {
2130 return (packed_fields_ & (1u << flag)) != 0u;
2131 }
2132
2133 template <size_t flag>
2134 void SetPackedFlag(bool value = true) {
2135 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2136 }
2137
2138 template <typename BitFieldType>
2139 typename BitFieldType::value_type GetPackedField() const {
2140 return BitFieldType::Decode(packed_fields_);
2141 }
2142
2143 template <typename BitFieldType>
2144 void SetPackedField(typename BitFieldType::value_type value) {
2145 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2146 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2147 }
2148
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002149 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002150 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2151
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002152 HInstruction* previous_;
2153 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002154 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002155 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002156
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002157 // An instruction gets an id when it is added to the graph.
2158 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002159 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002160 int id_;
2161
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002162 // When doing liveness analysis, instructions that have uses get an SSA index.
2163 int ssa_index_;
2164
Vladimir Markoa1de9182016-02-25 11:37:38 +00002165 // Packed fields.
2166 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002167
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002168 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002169 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002170
2171 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002172 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002173
Nicolas Geoffray39468442014-09-02 15:17:15 +01002174 // The environment associated with this instruction. Not null if the instruction
2175 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002176 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002177
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002178 // Set by the code generator.
2179 LocationSummary* locations_;
2180
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002181 // Set by the liveness analysis.
2182 LiveInterval* live_interval_;
2183
2184 // Set by the liveness analysis, this is the position in a linear
2185 // order of blocks where this instruction's live interval start.
2186 size_t lifetime_position_;
2187
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002188 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002189
Vladimir Markoa1de9182016-02-25 11:37:38 +00002190 // The reference handle part of the reference type info.
2191 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002192 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002193 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002194
David Brazdil1abb4192015-02-17 18:33:36 +00002195 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002196 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002197 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002198 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002199 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002200
2201 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2202};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002203std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002204
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002205class HInputIterator : public ValueObject {
2206 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002207 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002208
2209 bool Done() const { return index_ == instruction_->InputCount(); }
2210 HInstruction* Current() const { return instruction_->InputAt(index_); }
2211 void Advance() { index_++; }
2212
2213 private:
2214 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002215 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002216
2217 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2218};
2219
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002220class HInstructionIterator : public ValueObject {
2221 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002222 explicit HInstructionIterator(const HInstructionList& instructions)
2223 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002224 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002225 }
2226
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002227 bool Done() const { return instruction_ == nullptr; }
2228 HInstruction* Current() const { return instruction_; }
2229 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002230 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002231 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002232 }
2233
2234 private:
2235 HInstruction* instruction_;
2236 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002237
2238 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002239};
2240
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002241class HBackwardInstructionIterator : public ValueObject {
2242 public:
2243 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2244 : instruction_(instructions.last_instruction_) {
2245 next_ = Done() ? nullptr : instruction_->GetPrevious();
2246 }
2247
2248 bool Done() const { return instruction_ == nullptr; }
2249 HInstruction* Current() const { return instruction_; }
2250 void Advance() {
2251 instruction_ = next_;
2252 next_ = Done() ? nullptr : instruction_->GetPrevious();
2253 }
2254
2255 private:
2256 HInstruction* instruction_;
2257 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002258
2259 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002260};
2261
Vladimir Markof9f64412015-09-02 14:05:49 +01002262template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002263class HTemplateInstruction: public HInstruction {
2264 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002265 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002266 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002267 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002268
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002269 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002270
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002271 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002272 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2273 DCHECK_LT(i, N);
2274 return inputs_[i];
2275 }
David Brazdil1abb4192015-02-17 18:33:36 +00002276
2277 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002278 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002279 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002280 }
2281
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002282 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002283 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002284
2285 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002286};
2287
Vladimir Markof9f64412015-09-02 14:05:49 +01002288// HTemplateInstruction specialization for N=0.
2289template<>
2290class HTemplateInstruction<0>: public HInstruction {
2291 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002292 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002293 : HInstruction(side_effects, dex_pc) {}
2294
Vladimir Markof9f64412015-09-02 14:05:49 +01002295 virtual ~HTemplateInstruction() {}
2296
2297 size_t InputCount() const OVERRIDE { return 0; }
2298
2299 protected:
2300 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2301 LOG(FATAL) << "Unreachable";
2302 UNREACHABLE();
2303 }
2304
2305 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2306 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2307 LOG(FATAL) << "Unreachable";
2308 UNREACHABLE();
2309 }
2310
2311 private:
2312 friend class SsaBuilder;
2313};
2314
Dave Allison20dfc792014-06-16 20:44:29 -07002315template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002316class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002317 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002318 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002319 : HTemplateInstruction<N>(side_effects, dex_pc) {
2320 this->template SetPackedField<TypeField>(type);
2321 }
Dave Allison20dfc792014-06-16 20:44:29 -07002322 virtual ~HExpression() {}
2323
Vladimir Markoa1de9182016-02-25 11:37:38 +00002324 Primitive::Type GetType() const OVERRIDE {
2325 return TypeField::Decode(this->GetPackedFields());
2326 }
Dave Allison20dfc792014-06-16 20:44:29 -07002327
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002328 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002329 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2330 static constexpr size_t kFieldTypeSize =
2331 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2332 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2333 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2334 "Too many packed fields.");
2335 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002336};
2337
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002338// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2339// instruction that branches to the exit block.
2340class HReturnVoid : public HTemplateInstruction<0> {
2341 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002342 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2343 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002344
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002345 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002346
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002347 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002348
2349 private:
2350 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2351};
2352
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002353// Represents dex's RETURN opcodes. A HReturn is a control flow
2354// instruction that branches to the exit block.
2355class HReturn : public HTemplateInstruction<1> {
2356 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002357 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2358 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002359 SetRawInputAt(0, value);
2360 }
2361
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002362 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002363
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002364 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002365
2366 private:
2367 DISALLOW_COPY_AND_ASSIGN(HReturn);
2368};
2369
David Brazdildee58d62016-04-07 09:54:26 +00002370class HPhi : public HInstruction {
2371 public:
2372 HPhi(ArenaAllocator* arena,
2373 uint32_t reg_number,
2374 size_t number_of_inputs,
2375 Primitive::Type type,
2376 uint32_t dex_pc = kNoDexPc)
2377 : HInstruction(SideEffects::None(), dex_pc),
2378 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2379 reg_number_(reg_number) {
2380 SetPackedField<TypeField>(ToPhiType(type));
2381 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2382 // Phis are constructed live and marked dead if conflicting or unused.
2383 // Individual steps of SsaBuilder should assume that if a phi has been
2384 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2385 SetPackedFlag<kFlagIsLive>(true);
2386 SetPackedFlag<kFlagCanBeNull>(true);
2387 }
2388
2389 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2390 static Primitive::Type ToPhiType(Primitive::Type type) {
2391 return Primitive::PrimitiveKind(type);
2392 }
2393
2394 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2395
2396 size_t InputCount() const OVERRIDE { return inputs_.size(); }
2397
2398 void AddInput(HInstruction* input);
2399 void RemoveInputAt(size_t index);
2400
2401 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2402 void SetType(Primitive::Type new_type) {
2403 // Make sure that only valid type changes occur. The following are allowed:
2404 // (1) int -> float/ref (primitive type propagation),
2405 // (2) long -> double (primitive type propagation).
2406 DCHECK(GetType() == new_type ||
2407 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2408 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2409 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2410 SetPackedField<TypeField>(new_type);
2411 }
2412
2413 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2414 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2415
2416 uint32_t GetRegNumber() const { return reg_number_; }
2417
2418 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2419 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2420 bool IsDead() const { return !IsLive(); }
2421 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2422
2423 bool IsVRegEquivalentOf(HInstruction* other) const {
2424 return other != nullptr
2425 && other->IsPhi()
2426 && other->AsPhi()->GetBlock() == GetBlock()
2427 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2428 }
2429
2430 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2431 // An equivalent phi is a phi having the same dex register and type.
2432 // It assumes that phis with the same dex register are adjacent.
2433 HPhi* GetNextEquivalentPhiWithSameType() {
2434 HInstruction* next = GetNext();
2435 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2436 if (next->GetType() == GetType()) {
2437 return next->AsPhi();
2438 }
2439 next = next->GetNext();
2440 }
2441 return nullptr;
2442 }
2443
2444 DECLARE_INSTRUCTION(Phi);
2445
2446 protected:
2447 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
2448 return inputs_[index];
2449 }
2450
2451 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2452 inputs_[index] = input;
2453 }
2454
2455 private:
2456 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2457 static constexpr size_t kFieldTypeSize =
2458 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2459 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2460 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2461 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2462 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2463 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2464
2465 ArenaVector<HUserRecord<HInstruction*> > inputs_;
2466 const uint32_t reg_number_;
2467
2468 DISALLOW_COPY_AND_ASSIGN(HPhi);
2469};
2470
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002471// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002472// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002473// exit block.
2474class HExit : public HTemplateInstruction<0> {
2475 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002476 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002477
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002478 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002479
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002480 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002481
2482 private:
2483 DISALLOW_COPY_AND_ASSIGN(HExit);
2484};
2485
2486// Jumps from one block to another.
2487class HGoto : public HTemplateInstruction<0> {
2488 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002489 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002490
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002491 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002492
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002493 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002494 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002495 }
2496
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002497 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002498
2499 private:
2500 DISALLOW_COPY_AND_ASSIGN(HGoto);
2501};
2502
Roland Levillain9867bc72015-08-05 10:21:34 +01002503class HConstant : public HExpression<0> {
2504 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002505 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2506 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002507
2508 bool CanBeMoved() const OVERRIDE { return true; }
2509
Roland Levillain1a653882016-03-18 18:05:57 +00002510 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002511 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002512 // Is this constant 0 in the arithmetic sense?
2513 virtual bool IsArithmeticZero() const { return false; }
2514 // Is this constant a 0-bit pattern?
2515 virtual bool IsZeroBitPattern() const { return false; }
2516 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002517 virtual bool IsOne() const { return false; }
2518
David Brazdil77a48ae2015-09-15 12:34:04 +00002519 virtual uint64_t GetValueAsUint64() const = 0;
2520
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002521 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002522
2523 private:
2524 DISALLOW_COPY_AND_ASSIGN(HConstant);
2525};
2526
2527class HNullConstant : public HConstant {
2528 public:
2529 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2530 return true;
2531 }
2532
David Brazdil77a48ae2015-09-15 12:34:04 +00002533 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2534
Roland Levillain9867bc72015-08-05 10:21:34 +01002535 size_t ComputeHashCode() const OVERRIDE { return 0; }
2536
Roland Levillain1a653882016-03-18 18:05:57 +00002537 // The null constant representation is a 0-bit pattern.
2538 virtual bool IsZeroBitPattern() const { return true; }
2539
Roland Levillain9867bc72015-08-05 10:21:34 +01002540 DECLARE_INSTRUCTION(NullConstant);
2541
2542 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002543 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002544
2545 friend class HGraph;
2546 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2547};
2548
2549// Constants of the type int. Those can be from Dex instructions, or
2550// synthesized (for example with the if-eqz instruction).
2551class HIntConstant : public HConstant {
2552 public:
2553 int32_t GetValue() const { return value_; }
2554
David Brazdil9f389d42015-10-01 14:32:56 +01002555 uint64_t GetValueAsUint64() const OVERRIDE {
2556 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2557 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002558
Roland Levillain9867bc72015-08-05 10:21:34 +01002559 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002560 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002561 return other->AsIntConstant()->value_ == value_;
2562 }
2563
2564 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2565
2566 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002567 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2568 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002569 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2570
Roland Levillain1a653882016-03-18 18:05:57 +00002571 // Integer constants are used to encode Boolean values as well,
2572 // where 1 means true and 0 means false.
2573 bool IsTrue() const { return GetValue() == 1; }
2574 bool IsFalse() const { return GetValue() == 0; }
2575
Roland Levillain9867bc72015-08-05 10:21:34 +01002576 DECLARE_INSTRUCTION(IntConstant);
2577
2578 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002579 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2580 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2581 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2582 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002583
2584 const int32_t value_;
2585
2586 friend class HGraph;
2587 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2588 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2589 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2590};
2591
2592class HLongConstant : public HConstant {
2593 public:
2594 int64_t GetValue() const { return value_; }
2595
David Brazdil77a48ae2015-09-15 12:34:04 +00002596 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2597
Roland Levillain9867bc72015-08-05 10:21:34 +01002598 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002599 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002600 return other->AsLongConstant()->value_ == value_;
2601 }
2602
2603 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2604
2605 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002606 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2607 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002608 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2609
2610 DECLARE_INSTRUCTION(LongConstant);
2611
2612 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002613 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2614 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002615
2616 const int64_t value_;
2617
2618 friend class HGraph;
2619 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2620};
Dave Allison20dfc792014-06-16 20:44:29 -07002621
Roland Levillain31dd3d62016-02-16 12:21:02 +00002622class HFloatConstant : public HConstant {
2623 public:
2624 float GetValue() const { return value_; }
2625
2626 uint64_t GetValueAsUint64() const OVERRIDE {
2627 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2628 }
2629
2630 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2631 DCHECK(other->IsFloatConstant()) << other->DebugName();
2632 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2633 }
2634
2635 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2636
2637 bool IsMinusOne() const OVERRIDE {
2638 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2639 }
Roland Levillain1a653882016-03-18 18:05:57 +00002640 bool IsArithmeticZero() const OVERRIDE {
2641 return std::fpclassify(value_) == FP_ZERO;
2642 }
2643 bool IsArithmeticPositiveZero() const {
2644 return IsArithmeticZero() && !std::signbit(value_);
2645 }
2646 bool IsArithmeticNegativeZero() const {
2647 return IsArithmeticZero() && std::signbit(value_);
2648 }
2649 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002650 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002651 }
2652 bool IsOne() const OVERRIDE {
2653 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2654 }
2655 bool IsNaN() const {
2656 return std::isnan(value_);
2657 }
2658
2659 DECLARE_INSTRUCTION(FloatConstant);
2660
2661 private:
2662 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2663 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2664 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2665 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2666
2667 const float value_;
2668
2669 // Only the SsaBuilder and HGraph can create floating-point constants.
2670 friend class SsaBuilder;
2671 friend class HGraph;
2672 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2673};
2674
2675class HDoubleConstant : public HConstant {
2676 public:
2677 double GetValue() const { return value_; }
2678
2679 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2680
2681 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2682 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2683 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2684 }
2685
2686 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2687
2688 bool IsMinusOne() const OVERRIDE {
2689 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2690 }
Roland Levillain1a653882016-03-18 18:05:57 +00002691 bool IsArithmeticZero() const OVERRIDE {
2692 return std::fpclassify(value_) == FP_ZERO;
2693 }
2694 bool IsArithmeticPositiveZero() const {
2695 return IsArithmeticZero() && !std::signbit(value_);
2696 }
2697 bool IsArithmeticNegativeZero() const {
2698 return IsArithmeticZero() && std::signbit(value_);
2699 }
2700 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002701 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002702 }
2703 bool IsOne() const OVERRIDE {
2704 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2705 }
2706 bool IsNaN() const {
2707 return std::isnan(value_);
2708 }
2709
2710 DECLARE_INSTRUCTION(DoubleConstant);
2711
2712 private:
2713 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2714 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2715 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2716 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2717
2718 const double value_;
2719
2720 // Only the SsaBuilder and HGraph can create floating-point constants.
2721 friend class SsaBuilder;
2722 friend class HGraph;
2723 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2724};
2725
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002726// Conditional branch. A block ending with an HIf instruction must have
2727// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002728class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002729 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002730 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2731 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002732 SetRawInputAt(0, input);
2733 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002734
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002735 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002736
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002737 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002738 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002739 }
2740
2741 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002742 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002743 }
2744
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002745 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002746
2747 private:
2748 DISALLOW_COPY_AND_ASSIGN(HIf);
2749};
2750
David Brazdilfc6a86a2015-06-26 10:33:45 +00002751
2752// Abstract instruction which marks the beginning and/or end of a try block and
2753// links it to the respective exception handlers. Behaves the same as a Goto in
2754// non-exceptional control flow.
2755// Normal-flow successor is stored at index zero, exception handlers under
2756// higher indices in no particular order.
2757class HTryBoundary : public HTemplateInstruction<0> {
2758 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002759 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002760 kEntry,
2761 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002762 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002763 };
2764
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002765 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002766 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2767 SetPackedField<BoundaryKindField>(kind);
2768 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002769
2770 bool IsControlFlow() const OVERRIDE { return true; }
2771
2772 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002773 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002774
David Brazdild26a4112015-11-10 11:07:31 +00002775 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2776 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2777 }
2778
David Brazdilfc6a86a2015-06-26 10:33:45 +00002779 // Returns whether `handler` is among its exception handlers (non-zero index
2780 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002781 bool HasExceptionHandler(const HBasicBlock& handler) const {
2782 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002783 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002784 }
2785
2786 // If not present already, adds `handler` to its block's list of exception
2787 // handlers.
2788 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002789 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002790 GetBlock()->AddSuccessor(handler);
2791 }
2792 }
2793
Vladimir Markoa1de9182016-02-25 11:37:38 +00002794 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2795 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002796
David Brazdilffee3d32015-07-06 11:48:53 +01002797 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2798
David Brazdilfc6a86a2015-06-26 10:33:45 +00002799 DECLARE_INSTRUCTION(TryBoundary);
2800
2801 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002802 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2803 static constexpr size_t kFieldBoundaryKindSize =
2804 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2805 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2806 kFieldBoundaryKind + kFieldBoundaryKindSize;
2807 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2808 "Too many packed fields.");
2809 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002810
2811 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2812};
2813
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002814// Deoptimize to interpreter, upon checking a condition.
2815class HDeoptimize : public HTemplateInstruction<1> {
2816 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002817 // We set CanTriggerGC to prevent any intermediate address to be live
2818 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002819 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002820 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002821 SetRawInputAt(0, cond);
2822 }
2823
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002824 bool CanBeMoved() const OVERRIDE { return true; }
2825 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2826 return true;
2827 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002828 bool NeedsEnvironment() const OVERRIDE { return true; }
2829 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002830
2831 DECLARE_INSTRUCTION(Deoptimize);
2832
2833 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002834 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2835};
2836
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002837// Represents the ArtMethod that was passed as a first argument to
2838// the method. It is used by instructions that depend on it, like
2839// instructions that work with the dex cache.
2840class HCurrentMethod : public HExpression<0> {
2841 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002842 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2843 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002844
2845 DECLARE_INSTRUCTION(CurrentMethod);
2846
2847 private:
2848 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2849};
2850
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002851// Fetches an ArtMethod from the virtual table or the interface method table
2852// of a class.
2853class HClassTableGet : public HExpression<1> {
2854 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002855 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002856 kVTable,
2857 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002858 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002859 };
2860 HClassTableGet(HInstruction* cls,
2861 Primitive::Type type,
2862 TableKind kind,
2863 size_t index,
2864 uint32_t dex_pc)
2865 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002866 index_(index) {
2867 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002868 SetRawInputAt(0, cls);
2869 }
2870
2871 bool CanBeMoved() const OVERRIDE { return true; }
2872 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2873 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002874 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002875 }
2876
Vladimir Markoa1de9182016-02-25 11:37:38 +00002877 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002878 size_t GetIndex() const { return index_; }
2879
2880 DECLARE_INSTRUCTION(ClassTableGet);
2881
2882 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002883 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2884 static constexpr size_t kFieldTableKindSize =
2885 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2886 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2887 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2888 "Too many packed fields.");
2889 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2890
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002891 // The index of the ArtMethod in the table.
2892 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002893
2894 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2895};
2896
Mark Mendellfe57faa2015-09-18 09:26:15 -04002897// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2898// have one successor for each entry in the switch table, and the final successor
2899// will be the block containing the next Dex opcode.
2900class HPackedSwitch : public HTemplateInstruction<1> {
2901 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002902 HPackedSwitch(int32_t start_value,
2903 uint32_t num_entries,
2904 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002905 uint32_t dex_pc = kNoDexPc)
2906 : HTemplateInstruction(SideEffects::None(), dex_pc),
2907 start_value_(start_value),
2908 num_entries_(num_entries) {
2909 SetRawInputAt(0, input);
2910 }
2911
2912 bool IsControlFlow() const OVERRIDE { return true; }
2913
2914 int32_t GetStartValue() const { return start_value_; }
2915
Vladimir Marko211c2112015-09-24 16:52:33 +01002916 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002917
2918 HBasicBlock* GetDefaultBlock() const {
2919 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002920 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002921 }
2922 DECLARE_INSTRUCTION(PackedSwitch);
2923
2924 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002925 const int32_t start_value_;
2926 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002927
2928 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2929};
2930
Roland Levillain88cb1752014-10-20 16:36:47 +01002931class HUnaryOperation : public HExpression<1> {
2932 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002933 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2934 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002935 SetRawInputAt(0, input);
2936 }
2937
2938 HInstruction* GetInput() const { return InputAt(0); }
2939 Primitive::Type GetResultType() const { return GetType(); }
2940
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002941 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002942 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002943 return true;
2944 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002945
Roland Levillain31dd3d62016-02-16 12:21:02 +00002946 // Try to statically evaluate `this` and return a HConstant
2947 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002948 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002949 HConstant* TryStaticEvaluation() const;
2950
2951 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002952 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2953 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002954 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2955 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002956
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002957 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002958
2959 private:
2960 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2961};
2962
Dave Allison20dfc792014-06-16 20:44:29 -07002963class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002964 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002965 HBinaryOperation(Primitive::Type result_type,
2966 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002967 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002968 SideEffects side_effects = SideEffects::None(),
2969 uint32_t dex_pc = kNoDexPc)
2970 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002971 SetRawInputAt(0, left);
2972 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002973 }
2974
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002975 HInstruction* GetLeft() const { return InputAt(0); }
2976 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002977 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002978
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002979 virtual bool IsCommutative() const { return false; }
2980
2981 // Put constant on the right.
2982 // Returns whether order is changed.
2983 bool OrderInputsWithConstantOnTheRight() {
2984 HInstruction* left = InputAt(0);
2985 HInstruction* right = InputAt(1);
2986 if (left->IsConstant() && !right->IsConstant()) {
2987 ReplaceInput(right, 0);
2988 ReplaceInput(left, 1);
2989 return true;
2990 }
2991 return false;
2992 }
2993
2994 // Order inputs by instruction id, but favor constant on the right side.
2995 // This helps GVN for commutative ops.
2996 void OrderInputs() {
2997 DCHECK(IsCommutative());
2998 HInstruction* left = InputAt(0);
2999 HInstruction* right = InputAt(1);
3000 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3001 return;
3002 }
3003 if (OrderInputsWithConstantOnTheRight()) {
3004 return;
3005 }
3006 // Order according to instruction id.
3007 if (left->GetId() > right->GetId()) {
3008 ReplaceInput(right, 0);
3009 ReplaceInput(left, 1);
3010 }
3011 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003012
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003013 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003014 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003015 return true;
3016 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003017
Roland Levillain31dd3d62016-02-16 12:21:02 +00003018 // Try to statically evaluate `this` and return a HConstant
3019 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003020 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003021 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003022
3023 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003024 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3025 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003026 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3027 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003028 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003029 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3030 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003031 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3032 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003033 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3034 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003035 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003036 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3037 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003038
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003039 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003040 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003041 HConstant* GetConstantRight() const;
3042
3043 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003044 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003045 HInstruction* GetLeastConstantLeft() const;
3046
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003047 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003048
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003049 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003050 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3051};
3052
Mark Mendellc4701932015-04-10 13:18:51 -04003053// The comparison bias applies for floating point operations and indicates how NaN
3054// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003055enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003056 kNoBias, // bias is not applicable (i.e. for long operation)
3057 kGtBias, // return 1 for NaN comparisons
3058 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003059 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003060};
3061
Roland Levillain31dd3d62016-02-16 12:21:02 +00003062std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3063
Dave Allison20dfc792014-06-16 20:44:29 -07003064class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003065 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003066 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003067 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3068 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3069 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003070
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003071 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003072 // `instruction`, and disregard moves in between.
3073 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003074
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003075 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003076
3077 virtual IfCondition GetCondition() const = 0;
3078
Mark Mendellc4701932015-04-10 13:18:51 -04003079 virtual IfCondition GetOppositeCondition() const = 0;
3080
Vladimir Markoa1de9182016-02-25 11:37:38 +00003081 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003082 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3083
Vladimir Markoa1de9182016-02-25 11:37:38 +00003084 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3085 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003086
3087 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003088 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003089 }
3090
Roland Levillain4fa13f62015-07-06 18:11:54 +01003091 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003092 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003093 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003094 if (if_cond == kCondNE) {
3095 return true;
3096 } else if (if_cond == kCondEQ) {
3097 return false;
3098 }
3099 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003100 }
3101
3102 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003103 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003104 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003105 if (if_cond == kCondEQ) {
3106 return true;
3107 } else if (if_cond == kCondNE) {
3108 return false;
3109 }
3110 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003111 }
3112
Roland Levillain31dd3d62016-02-16 12:21:02 +00003113 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003114 // Needed if we merge a HCompare into a HCondition.
3115 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3116 static constexpr size_t kFieldComparisonBiasSize =
3117 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3118 static constexpr size_t kNumberOfConditionPackedBits =
3119 kFieldComparisonBias + kFieldComparisonBiasSize;
3120 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3121 using ComparisonBiasField =
3122 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3123
Roland Levillain31dd3d62016-02-16 12:21:02 +00003124 template <typename T>
3125 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3126
3127 template <typename T>
3128 int32_t CompareFP(T x, T y) const {
3129 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3130 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3131 // Handle the bias.
3132 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3133 }
3134
3135 // Return an integer constant containing the result of a condition evaluated at compile time.
3136 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3137 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3138 }
3139
Dave Allison20dfc792014-06-16 20:44:29 -07003140 private:
3141 DISALLOW_COPY_AND_ASSIGN(HCondition);
3142};
3143
3144// Instruction to check if two inputs are equal to each other.
3145class HEqual : public HCondition {
3146 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003147 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3148 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003149
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003150 bool IsCommutative() const OVERRIDE { return true; }
3151
Vladimir Marko9e23df52015-11-10 17:14:35 +00003152 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3153 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003154 return MakeConstantCondition(true, GetDexPc());
3155 }
3156 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3157 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3158 }
3159 // In the following Evaluate methods, a HCompare instruction has
3160 // been merged into this HEqual instruction; evaluate it as
3161 // `Compare(x, y) == 0`.
3162 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3163 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3164 GetDexPc());
3165 }
3166 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3167 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3168 }
3169 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3170 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003171 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003172
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003173 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003174
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003175 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003176 return kCondEQ;
3177 }
3178
Mark Mendellc4701932015-04-10 13:18:51 -04003179 IfCondition GetOppositeCondition() const OVERRIDE {
3180 return kCondNE;
3181 }
3182
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003183 private:
Aart Bike9f37602015-10-09 11:15:55 -07003184 template <typename T> bool Compute(T x, T y) const { return x == y; }
3185
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003186 DISALLOW_COPY_AND_ASSIGN(HEqual);
3187};
3188
Dave Allison20dfc792014-06-16 20:44:29 -07003189class HNotEqual : public HCondition {
3190 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003191 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3192 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003193
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003194 bool IsCommutative() const OVERRIDE { return true; }
3195
Vladimir Marko9e23df52015-11-10 17:14:35 +00003196 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3197 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003198 return MakeConstantCondition(false, GetDexPc());
3199 }
3200 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3201 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3202 }
3203 // In the following Evaluate methods, a HCompare instruction has
3204 // been merged into this HNotEqual instruction; evaluate it as
3205 // `Compare(x, y) != 0`.
3206 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3207 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3208 }
3209 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3210 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3211 }
3212 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3213 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003214 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003215
Dave Allison20dfc792014-06-16 20:44:29 -07003216 DECLARE_INSTRUCTION(NotEqual);
3217
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003218 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003219 return kCondNE;
3220 }
3221
Mark Mendellc4701932015-04-10 13:18:51 -04003222 IfCondition GetOppositeCondition() const OVERRIDE {
3223 return kCondEQ;
3224 }
3225
Dave Allison20dfc792014-06-16 20:44:29 -07003226 private:
Aart Bike9f37602015-10-09 11:15:55 -07003227 template <typename T> bool Compute(T x, T y) const { return x != y; }
3228
Dave Allison20dfc792014-06-16 20:44:29 -07003229 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3230};
3231
3232class HLessThan : public HCondition {
3233 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003234 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3235 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003236
Roland Levillain9867bc72015-08-05 10:21:34 +01003237 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003238 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003239 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003240 // In the following Evaluate methods, a HCompare instruction has
3241 // been merged into this HLessThan instruction; evaluate it as
3242 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003243 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003244 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3245 }
3246 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3247 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3248 }
3249 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3250 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003251 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003252
Dave Allison20dfc792014-06-16 20:44:29 -07003253 DECLARE_INSTRUCTION(LessThan);
3254
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003255 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003256 return kCondLT;
3257 }
3258
Mark Mendellc4701932015-04-10 13:18:51 -04003259 IfCondition GetOppositeCondition() const OVERRIDE {
3260 return kCondGE;
3261 }
3262
Dave Allison20dfc792014-06-16 20:44:29 -07003263 private:
Aart Bike9f37602015-10-09 11:15:55 -07003264 template <typename T> bool Compute(T x, T y) const { return x < y; }
3265
Dave Allison20dfc792014-06-16 20:44:29 -07003266 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3267};
3268
3269class HLessThanOrEqual : public HCondition {
3270 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003271 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3272 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003273
Roland Levillain9867bc72015-08-05 10:21:34 +01003274 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003275 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003276 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003277 // In the following Evaluate methods, a HCompare instruction has
3278 // been merged into this HLessThanOrEqual instruction; evaluate it as
3279 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003280 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003281 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3282 }
3283 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3284 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3285 }
3286 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3287 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003288 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003289
Dave Allison20dfc792014-06-16 20:44:29 -07003290 DECLARE_INSTRUCTION(LessThanOrEqual);
3291
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003292 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003293 return kCondLE;
3294 }
3295
Mark Mendellc4701932015-04-10 13:18:51 -04003296 IfCondition GetOppositeCondition() const OVERRIDE {
3297 return kCondGT;
3298 }
3299
Dave Allison20dfc792014-06-16 20:44:29 -07003300 private:
Aart Bike9f37602015-10-09 11:15:55 -07003301 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3302
Dave Allison20dfc792014-06-16 20:44:29 -07003303 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3304};
3305
3306class HGreaterThan : public HCondition {
3307 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003308 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3309 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003310
Roland Levillain9867bc72015-08-05 10:21:34 +01003311 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003312 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003313 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003314 // In the following Evaluate methods, a HCompare instruction has
3315 // been merged into this HGreaterThan instruction; evaluate it as
3316 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003317 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003318 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3319 }
3320 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3321 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3322 }
3323 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3324 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003325 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003326
Dave Allison20dfc792014-06-16 20:44:29 -07003327 DECLARE_INSTRUCTION(GreaterThan);
3328
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003329 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003330 return kCondGT;
3331 }
3332
Mark Mendellc4701932015-04-10 13:18:51 -04003333 IfCondition GetOppositeCondition() const OVERRIDE {
3334 return kCondLE;
3335 }
3336
Dave Allison20dfc792014-06-16 20:44:29 -07003337 private:
Aart Bike9f37602015-10-09 11:15:55 -07003338 template <typename T> bool Compute(T x, T y) const { return x > y; }
3339
Dave Allison20dfc792014-06-16 20:44:29 -07003340 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3341};
3342
3343class HGreaterThanOrEqual : public HCondition {
3344 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003345 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3346 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003347
Roland Levillain9867bc72015-08-05 10:21:34 +01003348 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003349 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003350 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003351 // In the following Evaluate methods, a HCompare instruction has
3352 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3353 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003354 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003355 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3356 }
3357 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3358 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3359 }
3360 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3361 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003362 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003363
Dave Allison20dfc792014-06-16 20:44:29 -07003364 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3365
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003366 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003367 return kCondGE;
3368 }
3369
Mark Mendellc4701932015-04-10 13:18:51 -04003370 IfCondition GetOppositeCondition() const OVERRIDE {
3371 return kCondLT;
3372 }
3373
Dave Allison20dfc792014-06-16 20:44:29 -07003374 private:
Aart Bike9f37602015-10-09 11:15:55 -07003375 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3376
Dave Allison20dfc792014-06-16 20:44:29 -07003377 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3378};
3379
Aart Bike9f37602015-10-09 11:15:55 -07003380class HBelow : public HCondition {
3381 public:
3382 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3383 : HCondition(first, second, dex_pc) {}
3384
3385 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003386 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003387 }
3388 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003389 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3390 }
3391 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3392 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3393 LOG(FATAL) << DebugName() << " is not defined for float values";
3394 UNREACHABLE();
3395 }
3396 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3397 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3398 LOG(FATAL) << DebugName() << " is not defined for double values";
3399 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003400 }
3401
3402 DECLARE_INSTRUCTION(Below);
3403
3404 IfCondition GetCondition() const OVERRIDE {
3405 return kCondB;
3406 }
3407
3408 IfCondition GetOppositeCondition() const OVERRIDE {
3409 return kCondAE;
3410 }
3411
3412 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003413 template <typename T> bool Compute(T x, T y) const {
3414 return MakeUnsigned(x) < MakeUnsigned(y);
3415 }
Aart Bike9f37602015-10-09 11:15:55 -07003416
3417 DISALLOW_COPY_AND_ASSIGN(HBelow);
3418};
3419
3420class HBelowOrEqual : public HCondition {
3421 public:
3422 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3423 : HCondition(first, second, dex_pc) {}
3424
3425 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003426 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003427 }
3428 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003429 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3430 }
3431 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3432 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3433 LOG(FATAL) << DebugName() << " is not defined for float values";
3434 UNREACHABLE();
3435 }
3436 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3437 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3438 LOG(FATAL) << DebugName() << " is not defined for double values";
3439 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003440 }
3441
3442 DECLARE_INSTRUCTION(BelowOrEqual);
3443
3444 IfCondition GetCondition() const OVERRIDE {
3445 return kCondBE;
3446 }
3447
3448 IfCondition GetOppositeCondition() const OVERRIDE {
3449 return kCondA;
3450 }
3451
3452 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003453 template <typename T> bool Compute(T x, T y) const {
3454 return MakeUnsigned(x) <= MakeUnsigned(y);
3455 }
Aart Bike9f37602015-10-09 11:15:55 -07003456
3457 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3458};
3459
3460class HAbove : public HCondition {
3461 public:
3462 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3463 : HCondition(first, second, dex_pc) {}
3464
3465 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003466 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003467 }
3468 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003469 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3470 }
3471 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3472 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3473 LOG(FATAL) << DebugName() << " is not defined for float values";
3474 UNREACHABLE();
3475 }
3476 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3477 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3478 LOG(FATAL) << DebugName() << " is not defined for double values";
3479 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003480 }
3481
3482 DECLARE_INSTRUCTION(Above);
3483
3484 IfCondition GetCondition() const OVERRIDE {
3485 return kCondA;
3486 }
3487
3488 IfCondition GetOppositeCondition() const OVERRIDE {
3489 return kCondBE;
3490 }
3491
3492 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003493 template <typename T> bool Compute(T x, T y) const {
3494 return MakeUnsigned(x) > MakeUnsigned(y);
3495 }
Aart Bike9f37602015-10-09 11:15:55 -07003496
3497 DISALLOW_COPY_AND_ASSIGN(HAbove);
3498};
3499
3500class HAboveOrEqual : public HCondition {
3501 public:
3502 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3503 : HCondition(first, second, dex_pc) {}
3504
3505 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003506 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003507 }
3508 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003509 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3510 }
3511 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3512 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3513 LOG(FATAL) << DebugName() << " is not defined for float values";
3514 UNREACHABLE();
3515 }
3516 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3517 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3518 LOG(FATAL) << DebugName() << " is not defined for double values";
3519 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003520 }
3521
3522 DECLARE_INSTRUCTION(AboveOrEqual);
3523
3524 IfCondition GetCondition() const OVERRIDE {
3525 return kCondAE;
3526 }
3527
3528 IfCondition GetOppositeCondition() const OVERRIDE {
3529 return kCondB;
3530 }
3531
3532 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003533 template <typename T> bool Compute(T x, T y) const {
3534 return MakeUnsigned(x) >= MakeUnsigned(y);
3535 }
Aart Bike9f37602015-10-09 11:15:55 -07003536
3537 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3538};
Dave Allison20dfc792014-06-16 20:44:29 -07003539
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003540// Instruction to check how two inputs compare to each other.
3541// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3542class HCompare : public HBinaryOperation {
3543 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003544 // Note that `comparison_type` is the type of comparison performed
3545 // between the comparison's inputs, not the type of the instantiated
3546 // HCompare instruction (which is always Primitive::kPrimInt).
3547 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003548 HInstruction* first,
3549 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003550 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003551 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003552 : HBinaryOperation(Primitive::kPrimInt,
3553 first,
3554 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003555 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003556 dex_pc) {
3557 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003558 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3559 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003560 }
3561
Roland Levillain9867bc72015-08-05 10:21:34 +01003562 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003563 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3564
3565 template <typename T>
3566 int32_t ComputeFP(T x, T y) const {
3567 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3568 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3569 // Handle the bias.
3570 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3571 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003572
Roland Levillain9867bc72015-08-05 10:21:34 +01003573 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003574 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3575 // reach this code path when processing a freshly built HIR
3576 // graph. However HCompare integer instructions can be synthesized
3577 // by the instruction simplifier to implement IntegerCompare and
3578 // IntegerSignum intrinsics, so we have to handle this case.
3579 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003580 }
3581 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003582 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3583 }
3584 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3585 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3586 }
3587 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3588 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003589 }
3590
Calin Juravleddb7df22014-11-25 20:56:51 +00003591 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003592 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003593 }
3594
Vladimir Markoa1de9182016-02-25 11:37:38 +00003595 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003596
Roland Levillain31dd3d62016-02-16 12:21:02 +00003597 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003598 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003599 bool IsGtBias() const {
3600 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003601 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003602 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003603
Roland Levillain1693a1f2016-03-15 14:57:31 +00003604 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3605 // Comparisons do not require a runtime call in any back end.
3606 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003607 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003608
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003609 DECLARE_INSTRUCTION(Compare);
3610
Roland Levillain31dd3d62016-02-16 12:21:02 +00003611 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003612 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3613 static constexpr size_t kFieldComparisonBiasSize =
3614 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3615 static constexpr size_t kNumberOfComparePackedBits =
3616 kFieldComparisonBias + kFieldComparisonBiasSize;
3617 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3618 using ComparisonBiasField =
3619 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3620
Roland Levillain31dd3d62016-02-16 12:21:02 +00003621 // Return an integer constant containing the result of a comparison evaluated at compile time.
3622 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3623 DCHECK(value == -1 || value == 0 || value == 1) << value;
3624 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3625 }
3626
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003627 private:
3628 DISALLOW_COPY_AND_ASSIGN(HCompare);
3629};
3630
David Brazdil6de19382016-01-08 17:37:10 +00003631class HNewInstance : public HExpression<2> {
3632 public:
3633 HNewInstance(HInstruction* cls,
3634 HCurrentMethod* current_method,
3635 uint32_t dex_pc,
3636 uint16_t type_index,
3637 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003638 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003639 bool finalizable,
3640 QuickEntrypointEnum entrypoint)
3641 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3642 type_index_(type_index),
3643 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003644 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003645 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003646 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003647 SetRawInputAt(0, cls);
3648 SetRawInputAt(1, current_method);
3649 }
3650
3651 uint16_t GetTypeIndex() const { return type_index_; }
3652 const DexFile& GetDexFile() const { return dex_file_; }
3653
3654 // Calls runtime so needs an environment.
3655 bool NeedsEnvironment() const OVERRIDE { return true; }
3656
Mingyao Yang062157f2016-03-02 10:15:36 -08003657 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3658 bool CanThrow() const OVERRIDE { return true; }
3659
3660 // Needs to call into runtime to make sure it's instantiable/accessible.
3661 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003662
Vladimir Markoa1de9182016-02-25 11:37:38 +00003663 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003664
3665 bool CanBeNull() const OVERRIDE { return false; }
3666
3667 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3668
3669 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3670 entrypoint_ = entrypoint;
3671 }
3672
3673 bool IsStringAlloc() const;
3674
3675 DECLARE_INSTRUCTION(NewInstance);
3676
3677 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003678 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3679 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003680 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3681 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3682 "Too many packed fields.");
3683
David Brazdil6de19382016-01-08 17:37:10 +00003684 const uint16_t type_index_;
3685 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003686 QuickEntrypointEnum entrypoint_;
3687
3688 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3689};
3690
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003691enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003692#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3693 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003694#include "intrinsics_list.h"
3695 kNone,
3696 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3697#undef INTRINSICS_LIST
3698#undef OPTIMIZING_INTRINSICS
3699};
3700std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3701
Agi Csaki05f20562015-08-19 14:58:14 -07003702enum IntrinsicNeedsEnvironmentOrCache {
3703 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3704 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003705};
3706
Aart Bik5d75afe2015-12-14 11:57:01 -08003707enum IntrinsicSideEffects {
3708 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3709 kReadSideEffects, // Intrinsic may read heap memory.
3710 kWriteSideEffects, // Intrinsic may write heap memory.
3711 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3712};
3713
3714enum IntrinsicExceptions {
3715 kNoThrow, // Intrinsic does not throw any exceptions.
3716 kCanThrow // Intrinsic may throw exceptions.
3717};
3718
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003719class HInvoke : public HInstruction {
3720 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003721 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003722
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003723 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003724
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003725 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003726 SetRawInputAt(index, argument);
3727 }
3728
Roland Levillain3e3d7332015-04-28 11:00:54 +01003729 // Return the number of arguments. This number can be lower than
3730 // the number of inputs returned by InputCount(), as some invoke
3731 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3732 // inputs at the end of their list of inputs.
3733 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3734
Vladimir Markoa1de9182016-02-25 11:37:38 +00003735 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003736
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003737 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003738 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003739
Vladimir Markoa1de9182016-02-25 11:37:38 +00003740 InvokeType GetOriginalInvokeType() const {
3741 return GetPackedField<OriginalInvokeTypeField>();
3742 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003743
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003744 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003745 return intrinsic_;
3746 }
3747
Aart Bik5d75afe2015-12-14 11:57:01 -08003748 void SetIntrinsic(Intrinsics intrinsic,
3749 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3750 IntrinsicSideEffects side_effects,
3751 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003752
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003753 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003754 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003755 }
3756
Vladimir Markoa1de9182016-02-25 11:37:38 +00003757 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003758
3759 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3760
3761 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3762 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3763 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003764
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003765 uint32_t* GetIntrinsicOptimizations() {
3766 return &intrinsic_optimizations_;
3767 }
3768
3769 const uint32_t* GetIntrinsicOptimizations() const {
3770 return &intrinsic_optimizations_;
3771 }
3772
3773 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3774
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003775 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003776
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003777 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003778 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3779 static constexpr size_t kFieldOriginalInvokeTypeSize =
3780 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3781 static constexpr size_t kFieldReturnType =
3782 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3783 static constexpr size_t kFieldReturnTypeSize =
3784 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3785 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3786 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3787 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3788 using OriginalInvokeTypeField =
3789 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3790 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3791
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003792 HInvoke(ArenaAllocator* arena,
3793 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003794 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003795 Primitive::Type return_type,
3796 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003797 uint32_t dex_method_index,
3798 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003799 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003800 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003801 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003802 inputs_(number_of_arguments + number_of_other_inputs,
3803 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003804 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003805 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003806 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003807 SetPackedField<ReturnTypeField>(return_type);
3808 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3809 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003810 }
3811
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003812 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003813 return inputs_[index];
3814 }
3815
David Brazdil1abb4192015-02-17 18:33:36 +00003816 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003817 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003818 }
3819
Vladimir Markoa1de9182016-02-25 11:37:38 +00003820 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003821
Roland Levillain3e3d7332015-04-28 11:00:54 +01003822 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003823 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003824 const uint32_t dex_method_index_;
3825 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003826
3827 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3828 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003829
3830 private:
3831 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3832};
3833
Calin Juravle175dc732015-08-25 15:42:32 +01003834class HInvokeUnresolved : public HInvoke {
3835 public:
3836 HInvokeUnresolved(ArenaAllocator* arena,
3837 uint32_t number_of_arguments,
3838 Primitive::Type return_type,
3839 uint32_t dex_pc,
3840 uint32_t dex_method_index,
3841 InvokeType invoke_type)
3842 : HInvoke(arena,
3843 number_of_arguments,
3844 0u /* number_of_other_inputs */,
3845 return_type,
3846 dex_pc,
3847 dex_method_index,
3848 invoke_type) {
3849 }
3850
3851 DECLARE_INSTRUCTION(InvokeUnresolved);
3852
3853 private:
3854 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3855};
3856
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003857class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003858 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003859 // Requirements of this method call regarding the class
3860 // initialization (clinit) check of its declaring class.
3861 enum class ClinitCheckRequirement {
3862 kNone, // Class already initialized.
3863 kExplicit, // Static call having explicit clinit check as last input.
3864 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003865 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003866 };
3867
Vladimir Marko58155012015-08-19 12:49:41 +00003868 // Determines how to load the target ArtMethod*.
3869 enum class MethodLoadKind {
3870 // Use a String init ArtMethod* loaded from Thread entrypoints.
3871 kStringInit,
3872
3873 // Use the method's own ArtMethod* loaded by the register allocator.
3874 kRecursive,
3875
3876 // Use ArtMethod* at a known address, embed the direct address in the code.
3877 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3878 kDirectAddress,
3879
3880 // Use ArtMethod* at an address that will be known at link time, embed the direct
3881 // address in the code. If the image is relocatable, emit .patch_oat entry.
3882 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3883 // the image relocatable or not.
3884 kDirectAddressWithFixup,
3885
Vladimir Markoa1de9182016-02-25 11:37:38 +00003886 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003887 // Used when we need to use the dex cache, for example for invoke-static that
3888 // may cause class initialization (the entry may point to a resolution method),
3889 // and we know that we can access the dex cache arrays using a PC-relative load.
3890 kDexCachePcRelative,
3891
3892 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3893 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3894 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3895 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3896 kDexCacheViaMethod,
3897 };
3898
3899 // Determines the location of the code pointer.
3900 enum class CodePtrLocation {
3901 // Recursive call, use local PC-relative call instruction.
3902 kCallSelf,
3903
3904 // Use PC-relative call instruction patched at link time.
3905 // Used for calls within an oat file, boot->boot or app->app.
3906 kCallPCRelative,
3907
3908 // Call to a known target address, embed the direct address in code.
3909 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3910 kCallDirect,
3911
3912 // Call to a target address that will be known at link time, embed the direct
3913 // address in code. If the image is relocatable, emit .patch_oat entry.
3914 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3915 // the image relocatable or not.
3916 kCallDirectWithFixup,
3917
3918 // Use code pointer from the ArtMethod*.
3919 // Used when we don't know the target code. This is also the last-resort-kind used when
3920 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3921 kCallArtMethod,
3922 };
3923
3924 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003925 MethodLoadKind method_load_kind;
3926 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003927 // The method load data holds
3928 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3929 // Note that there are multiple string init methods, each having its own offset.
3930 // - the method address for kDirectAddress
3931 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003932 uint64_t method_load_data;
3933 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003934 };
3935
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003936 HInvokeStaticOrDirect(ArenaAllocator* arena,
3937 uint32_t number_of_arguments,
3938 Primitive::Type return_type,
3939 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003940 uint32_t method_index,
3941 MethodReference target_method,
3942 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003943 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003944 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003945 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003946 : HInvoke(arena,
3947 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003948 // There is potentially one extra argument for the HCurrentMethod node, and
3949 // potentially one other if the clinit check is explicit, and potentially
3950 // one other if the method is a string factory.
3951 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003952 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003953 return_type,
3954 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003955 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003956 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003957 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003958 dispatch_info_(dispatch_info) {
3959 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3960 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3961 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003962
Vladimir Markodc151b22015-10-15 18:02:30 +01003963 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003964 bool had_current_method_input = HasCurrentMethodInput();
3965 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3966
3967 // Using the current method is the default and once we find a better
3968 // method load kind, we should not go back to using the current method.
3969 DCHECK(had_current_method_input || !needs_current_method_input);
3970
3971 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003972 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3973 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003974 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003975 dispatch_info_ = dispatch_info;
3976 }
3977
Vladimir Markoc53c0792015-11-19 15:48:33 +00003978 void AddSpecialInput(HInstruction* input) {
3979 // We allow only one special input.
3980 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3981 DCHECK(InputCount() == GetSpecialInputIndex() ||
3982 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3983 InsertInputAt(GetSpecialInputIndex(), input);
3984 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003985
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003986 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003987 // We access the method via the dex cache so we can't do an implicit null check.
3988 // TODO: for intrinsics we can generate implicit null checks.
3989 return false;
3990 }
3991
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003992 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003993 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003994 }
3995
Vladimir Markoc53c0792015-11-19 15:48:33 +00003996 // Get the index of the special input, if any.
3997 //
David Brazdil6de19382016-01-08 17:37:10 +00003998 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3999 // method pointer; otherwise there may be one platform-specific special input,
4000 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004001 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004002 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004003
Vladimir Markoa1de9182016-02-25 11:37:38 +00004004 InvokeType GetOptimizedInvokeType() const {
4005 return GetPackedField<OptimizedInvokeTypeField>();
4006 }
4007
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004008 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004009 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004010 }
4011
Vladimir Marko58155012015-08-19 12:49:41 +00004012 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4013 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4014 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004015 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004016 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004017 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004018 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004019 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4020 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004021 bool HasCurrentMethodInput() const {
4022 // This function can be called only after the invoke has been fully initialized by the builder.
4023 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004024 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004025 return true;
4026 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004027 DCHECK(InputCount() == GetSpecialInputIndex() ||
4028 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004029 return false;
4030 }
4031 }
Vladimir Marko58155012015-08-19 12:49:41 +00004032 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
4033 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004034 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00004035
4036 int32_t GetStringInitOffset() const {
4037 DCHECK(IsStringInit());
4038 return dispatch_info_.method_load_data;
4039 }
4040
4041 uint64_t GetMethodAddress() const {
4042 DCHECK(HasMethodAddress());
4043 return dispatch_info_.method_load_data;
4044 }
4045
4046 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004047 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004048 return dispatch_info_.method_load_data;
4049 }
4050
4051 uint64_t GetDirectCodePtr() const {
4052 DCHECK(HasDirectCodePtr());
4053 return dispatch_info_.direct_code_ptr;
4054 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004055
Vladimir Markoa1de9182016-02-25 11:37:38 +00004056 ClinitCheckRequirement GetClinitCheckRequirement() const {
4057 return GetPackedField<ClinitCheckRequirementField>();
4058 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004059
Roland Levillain4c0eb422015-04-24 16:43:49 +01004060 // Is this instruction a call to a static method?
4061 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004062 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004063 }
4064
Vladimir Markofbb184a2015-11-13 14:47:00 +00004065 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4066 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4067 // instruction; only relevant for static calls with explicit clinit check.
4068 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004069 DCHECK(IsStaticWithExplicitClinitCheck());
4070 size_t last_input_index = InputCount() - 1;
4071 HInstruction* last_input = InputAt(last_input_index);
4072 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004073 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004074 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004075 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004076 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004077 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004078 }
4079
4080 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004081 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004082 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004083 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004084 }
4085
4086 // Is this a call to a static method whose declaring class has an
4087 // implicit intialization check requirement?
4088 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004089 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004090 }
4091
Vladimir Markob554b5a2015-11-06 12:57:55 +00004092 // Does this method load kind need the current method as an input?
4093 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4094 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4095 }
4096
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004097 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004098
Roland Levillain4c0eb422015-04-24 16:43:49 +01004099 protected:
4100 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
4101 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
4102 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
4103 HInstruction* input = input_record.GetInstruction();
4104 // `input` is the last input of a static invoke marked as having
4105 // an explicit clinit check. It must either be:
4106 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4107 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4108 DCHECK(input != nullptr);
4109 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
4110 }
4111 return input_record;
4112 }
4113
Vladimir Markoc53c0792015-11-19 15:48:33 +00004114 void InsertInputAt(size_t index, HInstruction* input);
4115 void RemoveInputAt(size_t index);
4116
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004117 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004118 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4119 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4120 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4121 static constexpr size_t kFieldClinitCheckRequirement =
4122 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4123 static constexpr size_t kFieldClinitCheckRequirementSize =
4124 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4125 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4126 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4127 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4128 "Too many packed fields.");
4129 using OptimizedInvokeTypeField =
4130 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4131 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4132 kFieldClinitCheckRequirement,
4133 kFieldClinitCheckRequirementSize>;
4134
Vladimir Marko58155012015-08-19 12:49:41 +00004135 // The target method may refer to different dex file or method index than the original
4136 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4137 // in derived class to increase visibility.
4138 MethodReference target_method_;
4139 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004140
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004141 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004142};
Vladimir Markof64242a2015-12-01 14:58:23 +00004143std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004144std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004145
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004146class HInvokeVirtual : public HInvoke {
4147 public:
4148 HInvokeVirtual(ArenaAllocator* arena,
4149 uint32_t number_of_arguments,
4150 Primitive::Type return_type,
4151 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004152 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004153 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004154 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004155 vtable_index_(vtable_index) {}
4156
Calin Juravle641547a2015-04-21 22:08:51 +01004157 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004158 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004159 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004160 }
4161
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004162 uint32_t GetVTableIndex() const { return vtable_index_; }
4163
4164 DECLARE_INSTRUCTION(InvokeVirtual);
4165
4166 private:
4167 const uint32_t vtable_index_;
4168
4169 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4170};
4171
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004172class HInvokeInterface : public HInvoke {
4173 public:
4174 HInvokeInterface(ArenaAllocator* arena,
4175 uint32_t number_of_arguments,
4176 Primitive::Type return_type,
4177 uint32_t dex_pc,
4178 uint32_t dex_method_index,
4179 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004180 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004181 imt_index_(imt_index) {}
4182
Calin Juravle641547a2015-04-21 22:08:51 +01004183 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004184 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004185 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004186 }
4187
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004188 uint32_t GetImtIndex() const { return imt_index_; }
4189 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4190
4191 DECLARE_INSTRUCTION(InvokeInterface);
4192
4193 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004194 const uint32_t imt_index_;
4195
4196 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4197};
4198
Roland Levillain88cb1752014-10-20 16:36:47 +01004199class HNeg : public HUnaryOperation {
4200 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004201 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004202 : HUnaryOperation(result_type, input, dex_pc) {
4203 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4204 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004205
Roland Levillain9867bc72015-08-05 10:21:34 +01004206 template <typename T> T Compute(T x) const { return -x; }
4207
4208 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004209 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004210 }
4211 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004212 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004213 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004214 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4215 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4216 }
4217 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4218 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4219 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004220
Roland Levillain88cb1752014-10-20 16:36:47 +01004221 DECLARE_INSTRUCTION(Neg);
4222
4223 private:
4224 DISALLOW_COPY_AND_ASSIGN(HNeg);
4225};
4226
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004227class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004228 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004229 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004230 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004231 uint32_t dex_pc,
4232 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004233 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004234 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004235 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004236 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004237 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004238 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004239 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004240 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004241 }
4242
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004243 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004244 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004245
4246 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004247 bool NeedsEnvironment() const OVERRIDE { return true; }
4248
Mingyao Yang0c365e62015-03-31 15:09:29 -07004249 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4250 bool CanThrow() const OVERRIDE { return true; }
4251
Calin Juravle10e244f2015-01-26 18:54:32 +00004252 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004253
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004254 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4255
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004256 DECLARE_INSTRUCTION(NewArray);
4257
4258 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004259 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004260 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004261 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004262
4263 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4264};
4265
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004266class HAdd : public HBinaryOperation {
4267 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004268 HAdd(Primitive::Type result_type,
4269 HInstruction* left,
4270 HInstruction* right,
4271 uint32_t dex_pc = kNoDexPc)
4272 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004273
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004274 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004275
Roland Levillain9867bc72015-08-05 10:21:34 +01004276 template <typename T> T Compute(T x, T y) const { return x + y; }
4277
4278 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004279 return GetBlock()->GetGraph()->GetIntConstant(
4280 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004281 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004282 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004283 return GetBlock()->GetGraph()->GetLongConstant(
4284 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004285 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004286 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4287 return GetBlock()->GetGraph()->GetFloatConstant(
4288 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4289 }
4290 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4291 return GetBlock()->GetGraph()->GetDoubleConstant(
4292 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4293 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004294
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004295 DECLARE_INSTRUCTION(Add);
4296
4297 private:
4298 DISALLOW_COPY_AND_ASSIGN(HAdd);
4299};
4300
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004301class HSub : public HBinaryOperation {
4302 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004303 HSub(Primitive::Type result_type,
4304 HInstruction* left,
4305 HInstruction* right,
4306 uint32_t dex_pc = kNoDexPc)
4307 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004308
Roland Levillain9867bc72015-08-05 10:21:34 +01004309 template <typename T> T Compute(T x, T y) const { return x - y; }
4310
4311 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004312 return GetBlock()->GetGraph()->GetIntConstant(
4313 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004314 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004315 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004316 return GetBlock()->GetGraph()->GetLongConstant(
4317 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004318 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004319 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4320 return GetBlock()->GetGraph()->GetFloatConstant(
4321 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4322 }
4323 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4324 return GetBlock()->GetGraph()->GetDoubleConstant(
4325 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4326 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004327
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004328 DECLARE_INSTRUCTION(Sub);
4329
4330 private:
4331 DISALLOW_COPY_AND_ASSIGN(HSub);
4332};
4333
Calin Juravle34bacdf2014-10-07 20:23:36 +01004334class HMul : public HBinaryOperation {
4335 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004336 HMul(Primitive::Type result_type,
4337 HInstruction* left,
4338 HInstruction* right,
4339 uint32_t dex_pc = kNoDexPc)
4340 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004341
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004342 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004343
Roland Levillain9867bc72015-08-05 10:21:34 +01004344 template <typename T> T Compute(T x, T y) const { return x * y; }
4345
4346 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004347 return GetBlock()->GetGraph()->GetIntConstant(
4348 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004349 }
4350 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004351 return GetBlock()->GetGraph()->GetLongConstant(
4352 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004353 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004354 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4355 return GetBlock()->GetGraph()->GetFloatConstant(
4356 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4357 }
4358 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4359 return GetBlock()->GetGraph()->GetDoubleConstant(
4360 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4361 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004362
4363 DECLARE_INSTRUCTION(Mul);
4364
4365 private:
4366 DISALLOW_COPY_AND_ASSIGN(HMul);
4367};
4368
Calin Juravle7c4954d2014-10-28 16:57:40 +00004369class HDiv : public HBinaryOperation {
4370 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004371 HDiv(Primitive::Type result_type,
4372 HInstruction* left,
4373 HInstruction* right,
4374 uint32_t dex_pc)
4375 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004376
Roland Levillain9867bc72015-08-05 10:21:34 +01004377 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004378 T ComputeIntegral(T x, T y) const {
4379 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004380 // Our graph structure ensures we never have 0 for `y` during
4381 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004382 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004383 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004384 return (y == -1) ? -x : x / y;
4385 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004386
Roland Levillain31dd3d62016-02-16 12:21:02 +00004387 template <typename T>
4388 T ComputeFP(T x, T y) const {
4389 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4390 return x / y;
4391 }
4392
Roland Levillain9867bc72015-08-05 10:21:34 +01004393 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004394 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004395 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004396 }
4397 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004398 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004399 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4400 }
4401 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4402 return GetBlock()->GetGraph()->GetFloatConstant(
4403 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4404 }
4405 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4406 return GetBlock()->GetGraph()->GetDoubleConstant(
4407 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004408 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004409
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004410 static SideEffects SideEffectsForArchRuntimeCalls() {
4411 // The generated code can use a runtime call.
4412 return SideEffects::CanTriggerGC();
4413 }
4414
Calin Juravle7c4954d2014-10-28 16:57:40 +00004415 DECLARE_INSTRUCTION(Div);
4416
4417 private:
4418 DISALLOW_COPY_AND_ASSIGN(HDiv);
4419};
4420
Calin Juravlebacfec32014-11-14 15:54:36 +00004421class HRem : public HBinaryOperation {
4422 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004423 HRem(Primitive::Type result_type,
4424 HInstruction* left,
4425 HInstruction* right,
4426 uint32_t dex_pc)
4427 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004428
Roland Levillain9867bc72015-08-05 10:21:34 +01004429 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004430 T ComputeIntegral(T x, T y) const {
4431 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004432 // Our graph structure ensures we never have 0 for `y` during
4433 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004434 DCHECK_NE(y, 0);
4435 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4436 return (y == -1) ? 0 : x % y;
4437 }
4438
Roland Levillain31dd3d62016-02-16 12:21:02 +00004439 template <typename T>
4440 T ComputeFP(T x, T y) const {
4441 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4442 return std::fmod(x, y);
4443 }
4444
Roland Levillain9867bc72015-08-05 10:21:34 +01004445 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004446 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004447 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004448 }
4449 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004450 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004451 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004452 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004453 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4454 return GetBlock()->GetGraph()->GetFloatConstant(
4455 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4456 }
4457 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4458 return GetBlock()->GetGraph()->GetDoubleConstant(
4459 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4460 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004461
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004462 static SideEffects SideEffectsForArchRuntimeCalls() {
4463 return SideEffects::CanTriggerGC();
4464 }
4465
Calin Juravlebacfec32014-11-14 15:54:36 +00004466 DECLARE_INSTRUCTION(Rem);
4467
4468 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004469 DISALLOW_COPY_AND_ASSIGN(HRem);
4470};
4471
Calin Juravled0d48522014-11-04 16:40:20 +00004472class HDivZeroCheck : public HExpression<1> {
4473 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004474 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4475 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004476 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004477 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004478 SetRawInputAt(0, value);
4479 }
4480
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004481 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4482
Calin Juravled0d48522014-11-04 16:40:20 +00004483 bool CanBeMoved() const OVERRIDE { return true; }
4484
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004485 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004486 return true;
4487 }
4488
4489 bool NeedsEnvironment() const OVERRIDE { return true; }
4490 bool CanThrow() const OVERRIDE { return true; }
4491
Calin Juravled0d48522014-11-04 16:40:20 +00004492 DECLARE_INSTRUCTION(DivZeroCheck);
4493
4494 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004495 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4496};
4497
Calin Juravle9aec02f2014-11-18 23:06:35 +00004498class HShl : public HBinaryOperation {
4499 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004500 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004501 HInstruction* value,
4502 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004503 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004504 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4505 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4506 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004507 }
4508
Roland Levillain5b5b9312016-03-22 14:57:31 +00004509 template <typename T>
4510 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4511 return value << (distance & max_shift_distance);
4512 }
4513
4514 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004515 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004516 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004517 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004518 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004519 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004520 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004521 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004522 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4523 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4524 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4525 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004526 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004527 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4528 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004529 LOG(FATAL) << DebugName() << " is not defined for float values";
4530 UNREACHABLE();
4531 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004532 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4533 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004534 LOG(FATAL) << DebugName() << " is not defined for double values";
4535 UNREACHABLE();
4536 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004537
4538 DECLARE_INSTRUCTION(Shl);
4539
4540 private:
4541 DISALLOW_COPY_AND_ASSIGN(HShl);
4542};
4543
4544class HShr : public HBinaryOperation {
4545 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004546 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004547 HInstruction* value,
4548 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004549 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004550 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4551 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4552 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004553 }
4554
Roland Levillain5b5b9312016-03-22 14:57:31 +00004555 template <typename T>
4556 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4557 return value >> (distance & max_shift_distance);
4558 }
4559
4560 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004561 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004562 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004563 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004564 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004565 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004566 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004567 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004568 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4569 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4570 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4571 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004572 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004573 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4574 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004575 LOG(FATAL) << DebugName() << " is not defined for float values";
4576 UNREACHABLE();
4577 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004578 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4579 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004580 LOG(FATAL) << DebugName() << " is not defined for double values";
4581 UNREACHABLE();
4582 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004583
4584 DECLARE_INSTRUCTION(Shr);
4585
4586 private:
4587 DISALLOW_COPY_AND_ASSIGN(HShr);
4588};
4589
4590class HUShr : public HBinaryOperation {
4591 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004592 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004593 HInstruction* value,
4594 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004595 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004596 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4597 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4598 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004599 }
4600
Roland Levillain5b5b9312016-03-22 14:57:31 +00004601 template <typename T>
4602 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4603 typedef typename std::make_unsigned<T>::type V;
4604 V ux = static_cast<V>(value);
4605 return static_cast<T>(ux >> (distance & max_shift_distance));
4606 }
4607
4608 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004609 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004610 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004611 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004612 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004613 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004614 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004615 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004616 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4617 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4618 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4619 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004620 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004621 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4622 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004623 LOG(FATAL) << DebugName() << " is not defined for float values";
4624 UNREACHABLE();
4625 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004626 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4627 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004628 LOG(FATAL) << DebugName() << " is not defined for double values";
4629 UNREACHABLE();
4630 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004631
4632 DECLARE_INSTRUCTION(UShr);
4633
4634 private:
4635 DISALLOW_COPY_AND_ASSIGN(HUShr);
4636};
4637
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004638class HAnd : public HBinaryOperation {
4639 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004640 HAnd(Primitive::Type result_type,
4641 HInstruction* left,
4642 HInstruction* right,
4643 uint32_t dex_pc = kNoDexPc)
4644 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004645
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004646 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004647
Roland Levillaine53bd812016-02-24 14:54:18 +00004648 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004649
4650 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004651 return GetBlock()->GetGraph()->GetIntConstant(
4652 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004653 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004654 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004655 return GetBlock()->GetGraph()->GetLongConstant(
4656 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004657 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004658 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4659 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4660 LOG(FATAL) << DebugName() << " is not defined for float values";
4661 UNREACHABLE();
4662 }
4663 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4664 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4665 LOG(FATAL) << DebugName() << " is not defined for double values";
4666 UNREACHABLE();
4667 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004668
4669 DECLARE_INSTRUCTION(And);
4670
4671 private:
4672 DISALLOW_COPY_AND_ASSIGN(HAnd);
4673};
4674
4675class HOr : public HBinaryOperation {
4676 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004677 HOr(Primitive::Type result_type,
4678 HInstruction* left,
4679 HInstruction* right,
4680 uint32_t dex_pc = kNoDexPc)
4681 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004682
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004683 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004684
Roland Levillaine53bd812016-02-24 14:54:18 +00004685 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004686
4687 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004688 return GetBlock()->GetGraph()->GetIntConstant(
4689 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004690 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004691 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004692 return GetBlock()->GetGraph()->GetLongConstant(
4693 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004694 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004695 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4696 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4697 LOG(FATAL) << DebugName() << " is not defined for float values";
4698 UNREACHABLE();
4699 }
4700 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4701 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4702 LOG(FATAL) << DebugName() << " is not defined for double values";
4703 UNREACHABLE();
4704 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004705
4706 DECLARE_INSTRUCTION(Or);
4707
4708 private:
4709 DISALLOW_COPY_AND_ASSIGN(HOr);
4710};
4711
4712class HXor : public HBinaryOperation {
4713 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004714 HXor(Primitive::Type result_type,
4715 HInstruction* left,
4716 HInstruction* right,
4717 uint32_t dex_pc = kNoDexPc)
4718 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004719
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004720 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004721
Roland Levillaine53bd812016-02-24 14:54:18 +00004722 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004723
4724 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004725 return GetBlock()->GetGraph()->GetIntConstant(
4726 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004727 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004728 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004729 return GetBlock()->GetGraph()->GetLongConstant(
4730 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004731 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004732 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4733 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4734 LOG(FATAL) << DebugName() << " is not defined for float values";
4735 UNREACHABLE();
4736 }
4737 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4738 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4739 LOG(FATAL) << DebugName() << " is not defined for double values";
4740 UNREACHABLE();
4741 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004742
4743 DECLARE_INSTRUCTION(Xor);
4744
4745 private:
4746 DISALLOW_COPY_AND_ASSIGN(HXor);
4747};
4748
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004749class HRor : public HBinaryOperation {
4750 public:
4751 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004752 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004753 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4754 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004755 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004756
Roland Levillain5b5b9312016-03-22 14:57:31 +00004757 template <typename T>
4758 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4759 typedef typename std::make_unsigned<T>::type V;
4760 V ux = static_cast<V>(value);
4761 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004762 return static_cast<T>(ux);
4763 } else {
4764 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004765 return static_cast<T>(ux >> (distance & max_shift_value)) |
4766 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004767 }
4768 }
4769
Roland Levillain5b5b9312016-03-22 14:57:31 +00004770 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004771 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004772 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004773 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004774 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004775 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004776 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004777 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004778 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4779 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4780 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4781 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004782 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004783 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4784 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004785 LOG(FATAL) << DebugName() << " is not defined for float values";
4786 UNREACHABLE();
4787 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004788 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4789 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004790 LOG(FATAL) << DebugName() << " is not defined for double values";
4791 UNREACHABLE();
4792 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004793
4794 DECLARE_INSTRUCTION(Ror);
4795
4796 private:
4797 DISALLOW_COPY_AND_ASSIGN(HRor);
4798};
4799
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004800// The value of a parameter in this method. Its location depends on
4801// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004802class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004803 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004804 HParameterValue(const DexFile& dex_file,
4805 uint16_t type_index,
4806 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004807 Primitive::Type parameter_type,
4808 bool is_this = false)
4809 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004810 dex_file_(dex_file),
4811 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004812 index_(index) {
4813 SetPackedFlag<kFlagIsThis>(is_this);
4814 SetPackedFlag<kFlagCanBeNull>(!is_this);
4815 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004816
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004817 const DexFile& GetDexFile() const { return dex_file_; }
4818 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004819 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004820 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004821
Vladimir Markoa1de9182016-02-25 11:37:38 +00004822 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4823 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004824
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004825 DECLARE_INSTRUCTION(ParameterValue);
4826
4827 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004828 // Whether or not the parameter value corresponds to 'this' argument.
4829 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4830 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4831 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4832 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4833 "Too many packed fields.");
4834
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004835 const DexFile& dex_file_;
4836 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004837 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004838 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004839 const uint8_t index_;
4840
4841 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4842};
4843
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004844class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004845 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004846 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4847 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004848
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004849 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004850 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004851 return true;
4852 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004853
Roland Levillain9867bc72015-08-05 10:21:34 +01004854 template <typename T> T Compute(T x) const { return ~x; }
4855
4856 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004857 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004858 }
4859 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004860 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004861 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004862 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4863 LOG(FATAL) << DebugName() << " is not defined for float values";
4864 UNREACHABLE();
4865 }
4866 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4867 LOG(FATAL) << DebugName() << " is not defined for double values";
4868 UNREACHABLE();
4869 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004870
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004871 DECLARE_INSTRUCTION(Not);
4872
4873 private:
4874 DISALLOW_COPY_AND_ASSIGN(HNot);
4875};
4876
David Brazdil66d126e2015-04-03 16:02:44 +01004877class HBooleanNot : public HUnaryOperation {
4878 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004879 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4880 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004881
4882 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004883 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004884 return true;
4885 }
4886
Roland Levillain9867bc72015-08-05 10:21:34 +01004887 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004888 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004889 return !x;
4890 }
4891
Roland Levillain9867bc72015-08-05 10:21:34 +01004892 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004893 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004894 }
4895 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4896 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004897 UNREACHABLE();
4898 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004899 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4900 LOG(FATAL) << DebugName() << " is not defined for float values";
4901 UNREACHABLE();
4902 }
4903 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4904 LOG(FATAL) << DebugName() << " is not defined for double values";
4905 UNREACHABLE();
4906 }
David Brazdil66d126e2015-04-03 16:02:44 +01004907
4908 DECLARE_INSTRUCTION(BooleanNot);
4909
4910 private:
4911 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4912};
4913
Roland Levillaindff1f282014-11-05 14:15:05 +00004914class HTypeConversion : public HExpression<1> {
4915 public:
4916 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004917 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004918 : HExpression(result_type,
4919 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4920 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004921 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004922 // Invariant: We should never generate a conversion to a Boolean value.
4923 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004924 }
4925
4926 HInstruction* GetInput() const { return InputAt(0); }
4927 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4928 Primitive::Type GetResultType() const { return GetType(); }
4929
4930 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004931 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004932
Mark Mendelle82549b2015-05-06 10:55:34 -04004933 // Try to statically evaluate the conversion and return a HConstant
4934 // containing the result. If the input cannot be converted, return nullptr.
4935 HConstant* TryStaticEvaluation() const;
4936
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004937 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4938 Primitive::Type result_type) {
4939 // Some architectures may not require the 'GC' side effects, but at this point
4940 // in the compilation process we do not know what architecture we will
4941 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004942 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4943 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004944 return SideEffects::CanTriggerGC();
4945 }
4946 return SideEffects::None();
4947 }
4948
Roland Levillaindff1f282014-11-05 14:15:05 +00004949 DECLARE_INSTRUCTION(TypeConversion);
4950
4951 private:
4952 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4953};
4954
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004955static constexpr uint32_t kNoRegNumber = -1;
4956
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004957class HNullCheck : public HExpression<1> {
4958 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004959 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4960 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004961 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004962 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004963 SetRawInputAt(0, value);
4964 }
4965
Calin Juravle10e244f2015-01-26 18:54:32 +00004966 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004967 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004968 return true;
4969 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004970
Calin Juravle10e244f2015-01-26 18:54:32 +00004971 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004972
Calin Juravle10e244f2015-01-26 18:54:32 +00004973 bool CanThrow() const OVERRIDE { return true; }
4974
4975 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004976
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004977
4978 DECLARE_INSTRUCTION(NullCheck);
4979
4980 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004981 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4982};
4983
4984class FieldInfo : public ValueObject {
4985 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004986 FieldInfo(MemberOffset field_offset,
4987 Primitive::Type field_type,
4988 bool is_volatile,
4989 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004990 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004991 const DexFile& dex_file,
4992 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004993 : field_offset_(field_offset),
4994 field_type_(field_type),
4995 is_volatile_(is_volatile),
4996 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004997 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004998 dex_file_(dex_file),
4999 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005000
5001 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005002 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005003 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005004 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005005 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005006 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005007 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005008
5009 private:
5010 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005011 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005012 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005013 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005014 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005015 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005016 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005017};
5018
5019class HInstanceFieldGet : public HExpression<1> {
5020 public:
5021 HInstanceFieldGet(HInstruction* value,
5022 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005023 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005024 bool is_volatile,
5025 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005026 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005027 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005028 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005029 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005030 : HExpression(field_type,
5031 SideEffects::FieldReadOfType(field_type, is_volatile),
5032 dex_pc),
5033 field_info_(field_offset,
5034 field_type,
5035 is_volatile,
5036 field_idx,
5037 declaring_class_def_index,
5038 dex_file,
5039 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005040 SetRawInputAt(0, value);
5041 }
5042
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005043 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005044
5045 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5046 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
5047 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005048 }
5049
Calin Juravle641547a2015-04-21 22:08:51 +01005050 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5051 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005052 }
5053
5054 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005055 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5056 }
5057
Calin Juravle52c48962014-12-16 17:02:57 +00005058 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005059 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005060 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005061 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005062
5063 DECLARE_INSTRUCTION(InstanceFieldGet);
5064
5065 private:
5066 const FieldInfo field_info_;
5067
5068 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5069};
5070
5071class HInstanceFieldSet : public HTemplateInstruction<2> {
5072 public:
5073 HInstanceFieldSet(HInstruction* object,
5074 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005075 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005076 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005077 bool is_volatile,
5078 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005079 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005080 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005081 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005082 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005083 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5084 dex_pc),
5085 field_info_(field_offset,
5086 field_type,
5087 is_volatile,
5088 field_idx,
5089 declaring_class_def_index,
5090 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005091 dex_cache) {
5092 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005093 SetRawInputAt(0, object);
5094 SetRawInputAt(1, value);
5095 }
5096
Calin Juravle641547a2015-04-21 22:08:51 +01005097 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5098 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005099 }
5100
Calin Juravle52c48962014-12-16 17:02:57 +00005101 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005102 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005103 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005104 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005105 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005106 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5107 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005108
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005109 DECLARE_INSTRUCTION(InstanceFieldSet);
5110
5111 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005112 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5113 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5114 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5115 "Too many packed fields.");
5116
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005117 const FieldInfo field_info_;
5118
5119 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5120};
5121
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005122class HArrayGet : public HExpression<2> {
5123 public:
Aart Bik18b36ab2016-04-13 16:41:35 -07005124 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type, uint32_t dex_pc)
5125 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005126 SetRawInputAt(0, array);
5127 SetRawInputAt(1, index);
5128 }
5129
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005130 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005131 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005132 return true;
5133 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005134 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005135 // TODO: We can be smarter here.
5136 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5137 // which generates the implicit null check. There are cases when these can be removed
5138 // to produce better code. If we ever add optimizations to do so we should allow an
5139 // implicit check here (as long as the address falls in the first page).
5140 return false;
5141 }
5142
David Brazdil4833f5a2015-12-16 10:37:39 +00005143 bool IsEquivalentOf(HArrayGet* other) const {
5144 bool result = (GetDexPc() == other->GetDexPc());
5145 if (kIsDebugBuild && result) {
5146 DCHECK_EQ(GetBlock(), other->GetBlock());
5147 DCHECK_EQ(GetArray(), other->GetArray());
5148 DCHECK_EQ(GetIndex(), other->GetIndex());
5149 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005150 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005151 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005152 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5153 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005154 }
5155 }
5156 return result;
5157 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005158
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005159 HInstruction* GetArray() const { return InputAt(0); }
5160 HInstruction* GetIndex() const { return InputAt(1); }
5161
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005162 DECLARE_INSTRUCTION(ArrayGet);
5163
5164 private:
5165 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5166};
5167
5168class HArraySet : public HTemplateInstruction<3> {
5169 public:
5170 HArraySet(HInstruction* array,
5171 HInstruction* index,
5172 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005173 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005174 uint32_t dex_pc)
5175 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005176 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5177 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5178 SetPackedFlag<kFlagValueCanBeNull>(true);
5179 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005180 SetRawInputAt(0, array);
5181 SetRawInputAt(1, index);
5182 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005183 // Make a best guess now, may be refined during SSA building.
5184 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005185 }
5186
Calin Juravle77520bc2015-01-12 18:45:46 +00005187 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005188 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005189 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005190 }
5191
Mingyao Yang81014cb2015-06-02 03:16:27 -07005192 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005193 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005194
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005195 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005196 // TODO: Same as for ArrayGet.
5197 return false;
5198 }
5199
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005200 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005201 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005202 }
5203
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005204 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005205 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005206 }
5207
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005208 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005209 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005210 }
5211
Vladimir Markoa1de9182016-02-25 11:37:38 +00005212 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5213 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5214 bool StaticTypeOfArrayIsObjectArray() const {
5215 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5216 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005217
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005218 HInstruction* GetArray() const { return InputAt(0); }
5219 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005220 HInstruction* GetValue() const { return InputAt(2); }
5221
5222 Primitive::Type GetComponentType() const {
5223 // The Dex format does not type floating point index operations. Since the
5224 // `expected_component_type_` is set during building and can therefore not
5225 // be correct, we also check what is the value type. If it is a floating
5226 // point type, we must use that type.
5227 Primitive::Type value_type = GetValue()->GetType();
5228 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5229 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005230 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005231 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005232
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005233 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005234 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005235 }
5236
Aart Bik18b36ab2016-04-13 16:41:35 -07005237 void ComputeSideEffects() {
5238 Primitive::Type type = GetComponentType();
5239 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5240 SideEffectsForArchRuntimeCalls(type)));
5241 }
5242
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005243 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5244 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5245 }
5246
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005247 DECLARE_INSTRUCTION(ArraySet);
5248
5249 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005250 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5251 static constexpr size_t kFieldExpectedComponentTypeSize =
5252 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5253 static constexpr size_t kFlagNeedsTypeCheck =
5254 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5255 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005256 // Cached information for the reference_type_info_ so that codegen
5257 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005258 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5259 static constexpr size_t kNumberOfArraySetPackedBits =
5260 kFlagStaticTypeOfArrayIsObjectArray + 1;
5261 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5262 using ExpectedComponentTypeField =
5263 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005264
5265 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5266};
5267
5268class HArrayLength : public HExpression<1> {
5269 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005270 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005271 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005272 // Note that arrays do not change length, so the instruction does not
5273 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005274 SetRawInputAt(0, array);
5275 }
5276
Calin Juravle77520bc2015-01-12 18:45:46 +00005277 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005278 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005279 return true;
5280 }
Calin Juravle641547a2015-04-21 22:08:51 +01005281 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5282 return obj == InputAt(0);
5283 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005284
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005285 DECLARE_INSTRUCTION(ArrayLength);
5286
5287 private:
5288 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5289};
5290
5291class HBoundsCheck : public HExpression<2> {
5292 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005293 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5294 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005295 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005296 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005297 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005298 SetRawInputAt(0, index);
5299 SetRawInputAt(1, length);
5300 }
5301
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005302 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005303 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005304 return true;
5305 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005306
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005307 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005308
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005309 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005310
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005311 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005312
5313 DECLARE_INSTRUCTION(BoundsCheck);
5314
5315 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005316 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5317};
5318
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005319class HSuspendCheck : public HTemplateInstruction<0> {
5320 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005321 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005322 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005323
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005324 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005325 return true;
5326 }
5327
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005328 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5329 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005330
5331 DECLARE_INSTRUCTION(SuspendCheck);
5332
5333 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005334 // Only used for code generation, in order to share the same slow path between back edges
5335 // of a same loop.
5336 SlowPathCode* slow_path_;
5337
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005338 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5339};
5340
David Srbecky0cf44932015-12-09 14:09:59 +00005341// Pseudo-instruction which provides the native debugger with mapping information.
5342// It ensures that we can generate line number and local variables at this point.
5343class HNativeDebugInfo : public HTemplateInstruction<0> {
5344 public:
5345 explicit HNativeDebugInfo(uint32_t dex_pc)
5346 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5347
5348 bool NeedsEnvironment() const OVERRIDE {
5349 return true;
5350 }
5351
5352 DECLARE_INSTRUCTION(NativeDebugInfo);
5353
5354 private:
5355 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5356};
5357
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005358/**
5359 * Instruction to load a Class object.
5360 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005361class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005362 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005363 HLoadClass(HCurrentMethod* current_method,
5364 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005365 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005366 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005367 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005368 bool needs_access_check,
5369 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005370 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005371 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005372 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005373 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005374 // Referrers class should not need access check. We never inline unverified
5375 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005376 DCHECK(!is_referrers_class || !needs_access_check);
5377
5378 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5379 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5380 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5381 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005382 SetRawInputAt(0, current_method);
5383 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005384
5385 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005386
5387 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005388 // Note that we don't need to test for generate_clinit_check_.
5389 // Whether or not we need to generate the clinit check is processed in
5390 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005391 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005392 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005393 }
5394
5395 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5396
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005397 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005398 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005399
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005400 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005401 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005402 }
5403
Calin Juravle0ba218d2015-05-19 18:46:01 +01005404 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005405 // The entrypoint the code generator is going to call does not do
5406 // clinit of the class.
5407 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005408 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005409 }
5410
5411 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005412 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005413 (!IsReferrersClass() && !IsInDexCache()) ||
5414 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005415 }
5416
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005417
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005418 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005419 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005420 }
5421
Calin Juravleacf735c2015-02-12 15:25:22 +00005422 ReferenceTypeInfo GetLoadedClassRTI() {
5423 return loaded_class_rti_;
5424 }
5425
5426 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5427 // Make sure we only set exact types (the loaded class should never be merged).
5428 DCHECK(rti.IsExact());
5429 loaded_class_rti_ = rti;
5430 }
5431
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005432 const DexFile& GetDexFile() { return dex_file_; }
5433
Vladimir Markoa1de9182016-02-25 11:37:38 +00005434 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005435
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005436 static SideEffects SideEffectsForArchRuntimeCalls() {
5437 return SideEffects::CanTriggerGC();
5438 }
5439
Vladimir Markoa1de9182016-02-25 11:37:38 +00005440 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5441 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5442 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5443 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005444
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005445 DECLARE_INSTRUCTION(LoadClass);
5446
5447 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005448 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5449 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5450 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005451 // Whether this instruction must generate the initialization check.
5452 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005453 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5454 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5455 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5456
5457 const uint16_t type_index_;
5458 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005459
Calin Juravleacf735c2015-02-12 15:25:22 +00005460 ReferenceTypeInfo loaded_class_rti_;
5461
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005462 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5463};
5464
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005465class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005466 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005467 // Determines how to load the String.
5468 enum class LoadKind {
5469 // Use boot image String* address that will be known at link time.
5470 // Used for boot image strings referenced by boot image code in non-PIC mode.
5471 kBootImageLinkTimeAddress,
5472
5473 // Use PC-relative boot image String* address that will be known at link time.
5474 // Used for boot image strings referenced by boot image code in PIC mode.
5475 kBootImageLinkTimePcRelative,
5476
5477 // Use a known boot image String* address, embedded in the code by the codegen.
5478 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5479 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5480 // GetIncludePatchInformation().
5481 kBootImageAddress,
5482
5483 // Load from the resolved strings array at an absolute address.
5484 // Used for strings outside the boot image referenced by JIT-compiled code.
5485 kDexCacheAddress,
5486
5487 // Load from resolved strings array in the dex cache using a PC-relative load.
5488 // Used for strings outside boot image when we know that we can access
5489 // the dex cache arrays using a PC-relative load.
5490 kDexCachePcRelative,
5491
5492 // Load from resolved strings array accessed through the class loaded from
5493 // the compiled method's own ArtMethod*. This is the default access type when
5494 // all other types are unavailable.
5495 kDexCacheViaMethod,
5496
5497 kLast = kDexCacheViaMethod
5498 };
5499
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005500 HLoadString(HCurrentMethod* current_method,
5501 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005502 const DexFile& dex_file,
5503 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005504 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005505 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005506 SetPackedFlag<kFlagIsInDexCache>(false);
5507 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5508 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005509 SetRawInputAt(0, current_method);
5510 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005511
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005512 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5513 DCHECK(HasAddress(load_kind));
5514 load_data_.address = address;
5515 SetLoadKindInternal(load_kind);
5516 }
5517
5518 void SetLoadKindWithStringReference(LoadKind load_kind,
5519 const DexFile& dex_file,
5520 uint32_t string_index) {
5521 DCHECK(HasStringReference(load_kind));
5522 load_data_.ref.dex_file = &dex_file;
5523 string_index_ = string_index;
5524 SetLoadKindInternal(load_kind);
5525 }
5526
5527 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5528 const DexFile& dex_file,
5529 uint32_t element_index) {
5530 DCHECK(HasDexCacheReference(load_kind));
5531 load_data_.ref.dex_file = &dex_file;
5532 load_data_.ref.dex_cache_element_index = element_index;
5533 SetLoadKindInternal(load_kind);
5534 }
5535
5536 LoadKind GetLoadKind() const {
5537 return GetPackedField<LoadKindField>();
5538 }
5539
5540 const DexFile& GetDexFile() const;
5541
5542 uint32_t GetStringIndex() const {
5543 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5544 return string_index_;
5545 }
5546
5547 uint32_t GetDexCacheElementOffset() const;
5548
5549 uint64_t GetAddress() const {
5550 DCHECK(HasAddress(GetLoadKind()));
5551 return load_data_.address;
5552 }
5553
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005554 bool CanBeMoved() const OVERRIDE { return true; }
5555
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005556 bool InstructionDataEquals(HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005557
5558 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5559
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005560 // Will call the runtime if we need to load the string through
5561 // the dex cache and the string is not guaranteed to be there yet.
5562 bool NeedsEnvironment() const OVERRIDE {
5563 LoadKind load_kind = GetLoadKind();
5564 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5565 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5566 load_kind == LoadKind::kBootImageAddress) {
5567 return false;
5568 }
5569 return !IsInDexCache();
5570 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005571
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005572 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5573 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5574 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005575
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005576 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005577 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005578
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005579 static SideEffects SideEffectsForArchRuntimeCalls() {
5580 return SideEffects::CanTriggerGC();
5581 }
5582
Vladimir Markoa1de9182016-02-25 11:37:38 +00005583 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5584
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005585 void MarkInDexCache() {
5586 SetPackedFlag<kFlagIsInDexCache>(true);
5587 DCHECK(!NeedsEnvironment());
5588 RemoveEnvironment();
5589 }
5590
5591 size_t InputCount() const OVERRIDE {
5592 return (InputAt(0) != nullptr) ? 1u : 0u;
5593 }
5594
5595 void AddSpecialInput(HInstruction* special_input);
5596
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005597 DECLARE_INSTRUCTION(LoadString);
5598
5599 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005600 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005601 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5602 static constexpr size_t kFieldLoadKindSize =
5603 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5604 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005605 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005606 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005607
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005608 static bool HasStringReference(LoadKind load_kind) {
5609 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5610 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5611 load_kind == LoadKind::kDexCacheViaMethod;
5612 }
5613
5614 static bool HasAddress(LoadKind load_kind) {
5615 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5616 }
5617
5618 static bool HasDexCacheReference(LoadKind load_kind) {
5619 return load_kind == LoadKind::kDexCachePcRelative;
5620 }
5621
5622 void SetLoadKindInternal(LoadKind load_kind);
5623
5624 // String index serves also as the hash code and it's also needed for slow-paths,
5625 // so it must not be overwritten with other load data.
5626 uint32_t string_index_;
5627
5628 union {
5629 struct {
5630 const DexFile* dex_file; // For string reference and dex cache reference.
5631 uint32_t dex_cache_element_index; // Only for dex cache reference.
5632 } ref;
5633 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5634 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005635
5636 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5637};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005638std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5639
5640// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5641inline const DexFile& HLoadString::GetDexFile() const {
5642 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5643 << GetLoadKind();
5644 return *load_data_.ref.dex_file;
5645}
5646
5647// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5648inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5649 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5650 return load_data_.ref.dex_cache_element_index;
5651}
5652
5653// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5654inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5655 // The special input is used for PC-relative loads on some architectures.
5656 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5657 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
5658 DCHECK(InputAt(0) == nullptr);
5659 SetRawInputAt(0u, special_input);
5660 special_input->AddUseAt(this, 0);
5661}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005662
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005663/**
5664 * Performs an initialization check on its Class object input.
5665 */
5666class HClinitCheck : public HExpression<1> {
5667 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005668 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005669 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005670 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005671 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5672 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005673 SetRawInputAt(0, constant);
5674 }
5675
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005676 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005677 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005678 return true;
5679 }
5680
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005681 bool NeedsEnvironment() const OVERRIDE {
5682 // May call runtime to initialize the class.
5683 return true;
5684 }
5685
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005686 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005687
5688 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5689
5690 DECLARE_INSTRUCTION(ClinitCheck);
5691
5692 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005693 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5694};
5695
5696class HStaticFieldGet : public HExpression<1> {
5697 public:
5698 HStaticFieldGet(HInstruction* cls,
5699 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005700 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005701 bool is_volatile,
5702 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005703 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005704 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005705 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005706 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005707 : HExpression(field_type,
5708 SideEffects::FieldReadOfType(field_type, is_volatile),
5709 dex_pc),
5710 field_info_(field_offset,
5711 field_type,
5712 is_volatile,
5713 field_idx,
5714 declaring_class_def_index,
5715 dex_file,
5716 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005717 SetRawInputAt(0, cls);
5718 }
5719
Calin Juravle52c48962014-12-16 17:02:57 +00005720
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005721 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005722
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005723 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005724 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5725 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005726 }
5727
5728 size_t ComputeHashCode() const OVERRIDE {
5729 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5730 }
5731
Calin Juravle52c48962014-12-16 17:02:57 +00005732 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005733 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5734 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005735 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005736
5737 DECLARE_INSTRUCTION(StaticFieldGet);
5738
5739 private:
5740 const FieldInfo field_info_;
5741
5742 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5743};
5744
5745class HStaticFieldSet : public HTemplateInstruction<2> {
5746 public:
5747 HStaticFieldSet(HInstruction* cls,
5748 HInstruction* value,
5749 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005750 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005751 bool is_volatile,
5752 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005753 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005754 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005755 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005756 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005757 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5758 dex_pc),
5759 field_info_(field_offset,
5760 field_type,
5761 is_volatile,
5762 field_idx,
5763 declaring_class_def_index,
5764 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005765 dex_cache) {
5766 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005767 SetRawInputAt(0, cls);
5768 SetRawInputAt(1, value);
5769 }
5770
Calin Juravle52c48962014-12-16 17:02:57 +00005771 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005772 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5773 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005774 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005775
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005776 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005777 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5778 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005779
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005780 DECLARE_INSTRUCTION(StaticFieldSet);
5781
5782 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005783 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5784 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5785 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5786 "Too many packed fields.");
5787
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005788 const FieldInfo field_info_;
5789
5790 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5791};
5792
Calin Juravlee460d1d2015-09-29 04:52:17 +01005793class HUnresolvedInstanceFieldGet : public HExpression<1> {
5794 public:
5795 HUnresolvedInstanceFieldGet(HInstruction* obj,
5796 Primitive::Type field_type,
5797 uint32_t field_index,
5798 uint32_t dex_pc)
5799 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5800 field_index_(field_index) {
5801 SetRawInputAt(0, obj);
5802 }
5803
5804 bool NeedsEnvironment() const OVERRIDE { return true; }
5805 bool CanThrow() const OVERRIDE { return true; }
5806
5807 Primitive::Type GetFieldType() const { return GetType(); }
5808 uint32_t GetFieldIndex() const { return field_index_; }
5809
5810 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5811
5812 private:
5813 const uint32_t field_index_;
5814
5815 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5816};
5817
5818class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5819 public:
5820 HUnresolvedInstanceFieldSet(HInstruction* obj,
5821 HInstruction* value,
5822 Primitive::Type field_type,
5823 uint32_t field_index,
5824 uint32_t dex_pc)
5825 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005826 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005827 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005828 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005829 SetRawInputAt(0, obj);
5830 SetRawInputAt(1, value);
5831 }
5832
5833 bool NeedsEnvironment() const OVERRIDE { return true; }
5834 bool CanThrow() const OVERRIDE { return true; }
5835
Vladimir Markoa1de9182016-02-25 11:37:38 +00005836 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005837 uint32_t GetFieldIndex() const { return field_index_; }
5838
5839 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5840
5841 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005842 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5843 static constexpr size_t kFieldFieldTypeSize =
5844 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5845 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5846 kFieldFieldType + kFieldFieldTypeSize;
5847 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5848 "Too many packed fields.");
5849 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5850
Calin Juravlee460d1d2015-09-29 04:52:17 +01005851 const uint32_t field_index_;
5852
5853 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5854};
5855
5856class HUnresolvedStaticFieldGet : public HExpression<0> {
5857 public:
5858 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5859 uint32_t field_index,
5860 uint32_t dex_pc)
5861 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5862 field_index_(field_index) {
5863 }
5864
5865 bool NeedsEnvironment() const OVERRIDE { return true; }
5866 bool CanThrow() const OVERRIDE { return true; }
5867
5868 Primitive::Type GetFieldType() const { return GetType(); }
5869 uint32_t GetFieldIndex() const { return field_index_; }
5870
5871 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5872
5873 private:
5874 const uint32_t field_index_;
5875
5876 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5877};
5878
5879class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5880 public:
5881 HUnresolvedStaticFieldSet(HInstruction* value,
5882 Primitive::Type field_type,
5883 uint32_t field_index,
5884 uint32_t dex_pc)
5885 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005886 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005887 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005888 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005889 SetRawInputAt(0, value);
5890 }
5891
5892 bool NeedsEnvironment() const OVERRIDE { return true; }
5893 bool CanThrow() const OVERRIDE { return true; }
5894
Vladimir Markoa1de9182016-02-25 11:37:38 +00005895 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005896 uint32_t GetFieldIndex() const { return field_index_; }
5897
5898 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5899
5900 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005901 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5902 static constexpr size_t kFieldFieldTypeSize =
5903 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5904 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5905 kFieldFieldType + kFieldFieldTypeSize;
5906 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5907 "Too many packed fields.");
5908 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5909
Calin Juravlee460d1d2015-09-29 04:52:17 +01005910 const uint32_t field_index_;
5911
5912 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5913};
5914
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005915// Implement the move-exception DEX instruction.
5916class HLoadException : public HExpression<0> {
5917 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005918 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5919 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005920
David Brazdilbbd733e2015-08-18 17:48:17 +01005921 bool CanBeNull() const OVERRIDE { return false; }
5922
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005923 DECLARE_INSTRUCTION(LoadException);
5924
5925 private:
5926 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5927};
5928
David Brazdilcb1c0552015-08-04 16:22:25 +01005929// Implicit part of move-exception which clears thread-local exception storage.
5930// Must not be removed because the runtime expects the TLS to get cleared.
5931class HClearException : public HTemplateInstruction<0> {
5932 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005933 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5934 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005935
5936 DECLARE_INSTRUCTION(ClearException);
5937
5938 private:
5939 DISALLOW_COPY_AND_ASSIGN(HClearException);
5940};
5941
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005942class HThrow : public HTemplateInstruction<1> {
5943 public:
5944 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005945 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005946 SetRawInputAt(0, exception);
5947 }
5948
5949 bool IsControlFlow() const OVERRIDE { return true; }
5950
5951 bool NeedsEnvironment() const OVERRIDE { return true; }
5952
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005953 bool CanThrow() const OVERRIDE { return true; }
5954
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005955
5956 DECLARE_INSTRUCTION(Throw);
5957
5958 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005959 DISALLOW_COPY_AND_ASSIGN(HThrow);
5960};
5961
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005962/**
5963 * Implementation strategies for the code generator of a HInstanceOf
5964 * or `HCheckCast`.
5965 */
5966enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005967 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005968 kExactCheck, // Can do a single class compare.
5969 kClassHierarchyCheck, // Can just walk the super class chain.
5970 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5971 kInterfaceCheck, // No optimization yet when checking against an interface.
5972 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005973 kArrayCheck, // No optimization yet when checking against a generic array.
5974 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005975};
5976
Roland Levillain86503782016-02-11 19:07:30 +00005977std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5978
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005979class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005980 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005981 HInstanceOf(HInstruction* object,
5982 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005983 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005984 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005985 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005986 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005987 dex_pc) {
5988 SetPackedField<TypeCheckKindField>(check_kind);
5989 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005990 SetRawInputAt(0, object);
5991 SetRawInputAt(1, constant);
5992 }
5993
5994 bool CanBeMoved() const OVERRIDE { return true; }
5995
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005996 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005997 return true;
5998 }
5999
6000 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006001 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006002 }
6003
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006004 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006005 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6006 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6007 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6008 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006009
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006010 static bool CanCallRuntime(TypeCheckKind check_kind) {
6011 // Mips currently does runtime calls for any other checks.
6012 return check_kind != TypeCheckKind::kExactCheck;
6013 }
6014
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006015 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006016 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006017 }
6018
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006019 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006020
6021 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006022 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6023 static constexpr size_t kFieldTypeCheckKindSize =
6024 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6025 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6026 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6027 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6028 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006029
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006030 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6031};
6032
Calin Juravleb1498f62015-02-16 13:13:29 +00006033class HBoundType : public HExpression<1> {
6034 public:
David Brazdilf5552582015-12-27 13:36:12 +00006035 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006036 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006037 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6038 SetPackedFlag<kFlagUpperCanBeNull>(true);
6039 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006040 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006041 SetRawInputAt(0, input);
6042 }
6043
David Brazdilf5552582015-12-27 13:36:12 +00006044 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006045 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006046 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006047 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006048
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006049 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006050 DCHECK(GetUpperCanBeNull() || !can_be_null);
6051 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006052 }
6053
Vladimir Markoa1de9182016-02-25 11:37:38 +00006054 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006055
Calin Juravleb1498f62015-02-16 13:13:29 +00006056 DECLARE_INSTRUCTION(BoundType);
6057
6058 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006059 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6060 // is false then CanBeNull() cannot be true).
6061 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6062 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6063 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6064 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6065
Calin Juravleb1498f62015-02-16 13:13:29 +00006066 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006067 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6068 // It is used to bound the type in cases like:
6069 // if (x instanceof ClassX) {
6070 // // uper_bound_ will be ClassX
6071 // }
David Brazdilf5552582015-12-27 13:36:12 +00006072 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006073
6074 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6075};
6076
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006077class HCheckCast : public HTemplateInstruction<2> {
6078 public:
6079 HCheckCast(HInstruction* object,
6080 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006081 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006082 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006083 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6084 SetPackedField<TypeCheckKindField>(check_kind);
6085 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006086 SetRawInputAt(0, object);
6087 SetRawInputAt(1, constant);
6088 }
6089
6090 bool CanBeMoved() const OVERRIDE { return true; }
6091
6092 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6093 return true;
6094 }
6095
6096 bool NeedsEnvironment() const OVERRIDE {
6097 // Instruction may throw a CheckCastError.
6098 return true;
6099 }
6100
6101 bool CanThrow() const OVERRIDE { return true; }
6102
Vladimir Markoa1de9182016-02-25 11:37:38 +00006103 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6104 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6105 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6106 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006107
6108 DECLARE_INSTRUCTION(CheckCast);
6109
6110 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006111 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6112 static constexpr size_t kFieldTypeCheckKindSize =
6113 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6114 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6115 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6116 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6117 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006118
6119 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006120};
6121
Calin Juravle27df7582015-04-17 19:12:31 +01006122class HMemoryBarrier : public HTemplateInstruction<0> {
6123 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006124 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006125 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006126 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6127 SetPackedField<BarrierKindField>(barrier_kind);
6128 }
Calin Juravle27df7582015-04-17 19:12:31 +01006129
Vladimir Markoa1de9182016-02-25 11:37:38 +00006130 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006131
6132 DECLARE_INSTRUCTION(MemoryBarrier);
6133
6134 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006135 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6136 static constexpr size_t kFieldBarrierKindSize =
6137 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6138 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6139 kFieldBarrierKind + kFieldBarrierKindSize;
6140 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6141 "Too many packed fields.");
6142 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006143
6144 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6145};
6146
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006147class HMonitorOperation : public HTemplateInstruction<1> {
6148 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006149 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006150 kEnter,
6151 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006152 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006153 };
6154
6155 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006156 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006157 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6158 dex_pc) {
6159 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006160 SetRawInputAt(0, object);
6161 }
6162
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006163 // Instruction may go into runtime, so we need an environment.
6164 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006165
6166 bool CanThrow() const OVERRIDE {
6167 // Verifier guarantees that monitor-exit cannot throw.
6168 // This is important because it allows the HGraphBuilder to remove
6169 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6170 return IsEnter();
6171 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006172
Vladimir Markoa1de9182016-02-25 11:37:38 +00006173 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6174 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006175
6176 DECLARE_INSTRUCTION(MonitorOperation);
6177
Calin Juravle52c48962014-12-16 17:02:57 +00006178 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006179 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6180 static constexpr size_t kFieldOperationKindSize =
6181 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6182 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6183 kFieldOperationKind + kFieldOperationKindSize;
6184 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6185 "Too many packed fields.");
6186 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006187
6188 private:
6189 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6190};
6191
David Brazdil74eb1b22015-12-14 11:44:01 +00006192class HSelect : public HExpression<3> {
6193 public:
6194 HSelect(HInstruction* condition,
6195 HInstruction* true_value,
6196 HInstruction* false_value,
6197 uint32_t dex_pc)
6198 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6199 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6200
6201 // First input must be `true_value` or `false_value` to allow codegens to
6202 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6203 // that architectures which implement HSelect as a conditional move also
6204 // will not need to invert the condition.
6205 SetRawInputAt(0, false_value);
6206 SetRawInputAt(1, true_value);
6207 SetRawInputAt(2, condition);
6208 }
6209
6210 HInstruction* GetFalseValue() const { return InputAt(0); }
6211 HInstruction* GetTrueValue() const { return InputAt(1); }
6212 HInstruction* GetCondition() const { return InputAt(2); }
6213
6214 bool CanBeMoved() const OVERRIDE { return true; }
6215 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6216
6217 bool CanBeNull() const OVERRIDE {
6218 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6219 }
6220
6221 DECLARE_INSTRUCTION(Select);
6222
6223 private:
6224 DISALLOW_COPY_AND_ASSIGN(HSelect);
6225};
6226
Vladimir Markof9f64412015-09-02 14:05:49 +01006227class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006228 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006229 MoveOperands(Location source,
6230 Location destination,
6231 Primitive::Type type,
6232 HInstruction* instruction)
6233 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006234
6235 Location GetSource() const { return source_; }
6236 Location GetDestination() const { return destination_; }
6237
6238 void SetSource(Location value) { source_ = value; }
6239 void SetDestination(Location value) { destination_ = value; }
6240
6241 // The parallel move resolver marks moves as "in-progress" by clearing the
6242 // destination (but not the source).
6243 Location MarkPending() {
6244 DCHECK(!IsPending());
6245 Location dest = destination_;
6246 destination_ = Location::NoLocation();
6247 return dest;
6248 }
6249
6250 void ClearPending(Location dest) {
6251 DCHECK(IsPending());
6252 destination_ = dest;
6253 }
6254
6255 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006256 DCHECK(source_.IsValid() || destination_.IsInvalid());
6257 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006258 }
6259
6260 // True if this blocks a move from the given location.
6261 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006262 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006263 }
6264
6265 // A move is redundant if it's been eliminated, if its source and
6266 // destination are the same, or if its destination is unneeded.
6267 bool IsRedundant() const {
6268 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6269 }
6270
6271 // We clear both operands to indicate move that's been eliminated.
6272 void Eliminate() {
6273 source_ = destination_ = Location::NoLocation();
6274 }
6275
6276 bool IsEliminated() const {
6277 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6278 return source_.IsInvalid();
6279 }
6280
Alexey Frunze4dda3372015-06-01 18:31:49 -07006281 Primitive::Type GetType() const { return type_; }
6282
Nicolas Geoffray90218252015-04-15 11:56:51 +01006283 bool Is64BitMove() const {
6284 return Primitive::Is64BitType(type_);
6285 }
6286
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006287 HInstruction* GetInstruction() const { return instruction_; }
6288
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006289 private:
6290 Location source_;
6291 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006292 // The type this move is for.
6293 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006294 // The instruction this move is assocatied with. Null when this move is
6295 // for moving an input in the expected locations of user (including a phi user).
6296 // This is only used in debug mode, to ensure we do not connect interval siblings
6297 // in the same parallel move.
6298 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006299};
6300
Roland Levillainc9285912015-12-18 10:38:42 +00006301std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6302
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006303static constexpr size_t kDefaultNumberOfMoves = 4;
6304
6305class HParallelMove : public HTemplateInstruction<0> {
6306 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006307 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006308 : HTemplateInstruction(SideEffects::None(), dex_pc),
6309 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6310 moves_.reserve(kDefaultNumberOfMoves);
6311 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006312
Nicolas Geoffray90218252015-04-15 11:56:51 +01006313 void AddMove(Location source,
6314 Location destination,
6315 Primitive::Type type,
6316 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006317 DCHECK(source.IsValid());
6318 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006319 if (kIsDebugBuild) {
6320 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006321 for (const MoveOperands& move : moves_) {
6322 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006323 // Special case the situation where the move is for the spill slot
6324 // of the instruction.
6325 if ((GetPrevious() == instruction)
6326 || ((GetPrevious() == nullptr)
6327 && instruction->IsPhi()
6328 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006329 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006330 << "Doing parallel moves for the same instruction.";
6331 } else {
6332 DCHECK(false) << "Doing parallel moves for the same instruction.";
6333 }
6334 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006335 }
6336 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006337 for (const MoveOperands& move : moves_) {
6338 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006339 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006340 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006341 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006342 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006343 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006344 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006345 }
6346
Vladimir Marko225b6462015-09-28 12:17:40 +01006347 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006348 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006349 }
6350
Vladimir Marko225b6462015-09-28 12:17:40 +01006351 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006352
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006353 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006354
6355 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006356 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006357
6358 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6359};
6360
Mark Mendell0616ae02015-04-17 12:49:27 -04006361} // namespace art
6362
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006363#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6364#include "nodes_shared.h"
6365#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006366#ifdef ART_ENABLE_CODEGEN_arm
6367#include "nodes_arm.h"
6368#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006369#ifdef ART_ENABLE_CODEGEN_arm64
6370#include "nodes_arm64.h"
6371#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006372#ifdef ART_ENABLE_CODEGEN_x86
6373#include "nodes_x86.h"
6374#endif
6375
6376namespace art {
6377
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006378class HGraphVisitor : public ValueObject {
6379 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006380 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6381 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006382
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006383 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006384 virtual void VisitBasicBlock(HBasicBlock* block);
6385
Roland Levillain633021e2014-10-01 14:12:25 +01006386 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006387 void VisitInsertionOrder();
6388
Roland Levillain633021e2014-10-01 14:12:25 +01006389 // Visit the graph following dominator tree reverse post-order.
6390 void VisitReversePostOrder();
6391
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006392 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006393
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006394 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006395#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006396 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6397
6398 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6399
6400#undef DECLARE_VISIT_INSTRUCTION
6401
6402 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006403 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006404
6405 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6406};
6407
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006408class HGraphDelegateVisitor : public HGraphVisitor {
6409 public:
6410 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6411 virtual ~HGraphDelegateVisitor() {}
6412
6413 // Visit functions that delegate to to super class.
6414#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006415 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006416
6417 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6418
6419#undef DECLARE_VISIT_INSTRUCTION
6420
6421 private:
6422 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6423};
6424
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006425class HInsertionOrderIterator : public ValueObject {
6426 public:
6427 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6428
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006429 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006430 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006431 void Advance() { ++index_; }
6432
6433 private:
6434 const HGraph& graph_;
6435 size_t index_;
6436
6437 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6438};
6439
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006440class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006441 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006442 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6443 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006444 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006445 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006446
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006447 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6448 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006449 void Advance() { ++index_; }
6450
6451 private:
6452 const HGraph& graph_;
6453 size_t index_;
6454
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006455 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006456};
6457
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006458class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006459 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006460 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006461 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006462 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006463 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006464 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006465
6466 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006467 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006468 void Advance() { --index_; }
6469
6470 private:
6471 const HGraph& graph_;
6472 size_t index_;
6473
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006474 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006475};
6476
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006477class HLinearPostOrderIterator : public ValueObject {
6478 public:
6479 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006480 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006481
6482 bool Done() const { return index_ == 0; }
6483
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006484 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006485
6486 void Advance() {
6487 --index_;
6488 DCHECK_GE(index_, 0U);
6489 }
6490
6491 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006492 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006493 size_t index_;
6494
6495 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6496};
6497
6498class HLinearOrderIterator : public ValueObject {
6499 public:
6500 explicit HLinearOrderIterator(const HGraph& graph)
6501 : order_(graph.GetLinearOrder()), index_(0) {}
6502
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006503 bool Done() const { return index_ == order_.size(); }
6504 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006505 void Advance() { ++index_; }
6506
6507 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006508 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006509 size_t index_;
6510
6511 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6512};
6513
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006514// Iterator over the blocks that art part of the loop. Includes blocks part
6515// of an inner loop. The order in which the blocks are iterated is on their
6516// block id.
6517class HBlocksInLoopIterator : public ValueObject {
6518 public:
6519 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6520 : blocks_in_loop_(info.GetBlocks()),
6521 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6522 index_(0) {
6523 if (!blocks_in_loop_.IsBitSet(index_)) {
6524 Advance();
6525 }
6526 }
6527
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006528 bool Done() const { return index_ == blocks_.size(); }
6529 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006530 void Advance() {
6531 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006532 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006533 if (blocks_in_loop_.IsBitSet(index_)) {
6534 break;
6535 }
6536 }
6537 }
6538
6539 private:
6540 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006541 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006542 size_t index_;
6543
6544 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6545};
6546
Mingyao Yang3584bce2015-05-19 16:01:59 -07006547// Iterator over the blocks that art part of the loop. Includes blocks part
6548// of an inner loop. The order in which the blocks are iterated is reverse
6549// post order.
6550class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6551 public:
6552 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6553 : blocks_in_loop_(info.GetBlocks()),
6554 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6555 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006556 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006557 Advance();
6558 }
6559 }
6560
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006561 bool Done() const { return index_ == blocks_.size(); }
6562 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006563 void Advance() {
6564 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006565 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6566 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006567 break;
6568 }
6569 }
6570 }
6571
6572 private:
6573 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006574 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006575 size_t index_;
6576
6577 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6578};
6579
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006580inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006581 if (constant->IsIntConstant()) {
6582 return constant->AsIntConstant()->GetValue();
6583 } else if (constant->IsLongConstant()) {
6584 return constant->AsLongConstant()->GetValue();
6585 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006586 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006587 return 0;
6588 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006589}
6590
Vladimir Marko58155012015-08-19 12:49:41 +00006591inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6592 // For the purposes of the compiler, the dex files must actually be the same object
6593 // if we want to safely treat them as the same. This is especially important for JIT
6594 // as custom class loaders can open the same underlying file (or memory) multiple
6595 // times and provide different class resolution but no two class loaders should ever
6596 // use the same DexFile object - doing so is an unsupported hack that can lead to
6597 // all sorts of weird failures.
6598 return &lhs == &rhs;
6599}
6600
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006601#define INSTRUCTION_TYPE_CHECK(type, super) \
6602 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6603 inline const H##type* HInstruction::As##type() const { \
6604 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6605 } \
6606 inline H##type* HInstruction::As##type() { \
6607 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6608 }
6609
6610 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6611#undef INSTRUCTION_TYPE_CHECK
6612
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006613// Create space in `blocks` for adding `number_of_new_blocks` entries
6614// starting at location `at`. Blocks after `at` are moved accordingly.
6615inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6616 size_t number_of_new_blocks,
6617 size_t after) {
6618 DCHECK_LT(after, blocks->size());
6619 size_t old_size = blocks->size();
6620 size_t new_size = old_size + number_of_new_blocks;
6621 blocks->resize(new_size);
6622 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6623}
6624
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006625} // namespace art
6626
6627#endif // ART_COMPILER_OPTIMIZING_NODES_H_