blob: 6960ceb9656bf54f7e21b21b605f0c53e037b436 [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"
Vladimir Markod59f3b12016-03-29 12:21:58 +010039#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040
41namespace art {
42
David Brazdil1abb4192015-02-17 18:33:36 +000043class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010045class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000046class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010047class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000049class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000052class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000053class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000055class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010056class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010057class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010058class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010059class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000060class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010061class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000062class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000063
Mathieu Chartier736b5602015-09-02 14:54:11 -070064namespace mirror {
65class DexCache;
66} // namespace mirror
67
Nicolas Geoffray818f2102014-02-18 16:43:35 +000068static const int kDefaultNumberOfBlocks = 8;
69static const int kDefaultNumberOfSuccessors = 2;
70static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010071static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010072static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000073static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000074
Roland Levillain5b5b9312016-03-22 14:57:31 +000075// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
76static constexpr int32_t kMaxIntShiftDistance = 0x1f;
77// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
78static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000079
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010080static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070081static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010082
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010083static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
84
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060085static constexpr uint32_t kNoDexPc = -1;
86
Dave Allison20dfc792014-06-16 20:44:29 -070087enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070088 // All types.
89 kCondEQ, // ==
90 kCondNE, // !=
91 // Signed integers and floating-point numbers.
92 kCondLT, // <
93 kCondLE, // <=
94 kCondGT, // >
95 kCondGE, // >=
96 // Unsigned integers.
97 kCondB, // <
98 kCondBE, // <=
99 kCondA, // >
100 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -0700101};
102
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000103enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000104 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000105 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000106 kAnalysisFailThrowCatchLoop,
107 kAnalysisFailAmbiguousArrayOp,
108 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000109};
110
Vladimir Markof9f64412015-09-02 14:05:49 +0100111class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100112 public:
113 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
114
115 void AddInstruction(HInstruction* instruction);
116 void RemoveInstruction(HInstruction* instruction);
117
David Brazdilc3d743f2015-04-22 13:40:50 +0100118 // Insert `instruction` before/after an existing instruction `cursor`.
119 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
120 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
121
Roland Levillain6b469232014-09-25 10:10:38 +0100122 // Return true if this list contains `instruction`.
123 bool Contains(HInstruction* instruction) const;
124
Roland Levillainccc07a92014-09-16 14:48:16 +0100125 // Return true if `instruction1` is found before `instruction2` in
126 // this instruction list and false otherwise. Abort if none
127 // of these instructions is found.
128 bool FoundBefore(const HInstruction* instruction1,
129 const HInstruction* instruction2) const;
130
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000131 bool IsEmpty() const { return first_instruction_ == nullptr; }
132 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
133
134 // Update the block of all instructions to be `block`.
135 void SetBlockOfInstructions(HBasicBlock* block) const;
136
137 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000138 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000139 void Add(const HInstructionList& instruction_list);
140
David Brazdil2d7352b2015-04-20 14:52:42 +0100141 // Return the number of instructions in the list. This is an expensive operation.
142 size_t CountSize() const;
143
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100144 private:
145 HInstruction* first_instruction_;
146 HInstruction* last_instruction_;
147
148 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000149 friend class HGraph;
150 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100151 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100152 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100153
154 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
155};
156
David Brazdil4833f5a2015-12-16 10:37:39 +0000157class ReferenceTypeInfo : ValueObject {
158 public:
159 typedef Handle<mirror::Class> TypeHandle;
160
Vladimir Markoa1de9182016-02-25 11:37:38 +0000161 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
162
163 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000164 return ReferenceTypeInfo(type_handle, is_exact);
165 }
166
167 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
168
Vladimir Markof39745e2016-01-26 12:16:55 +0000169 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000170 return handle.GetReference() != nullptr;
171 }
172
Vladimir Markoa4336d22016-04-19 14:12:13 +0000173 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000174 return IsValidHandle(type_handle_);
175 }
176
177 bool IsExact() const { return is_exact_; }
178
179 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
180 DCHECK(IsValid());
181 return GetTypeHandle()->IsObjectClass();
182 }
183
184 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
185 DCHECK(IsValid());
186 return GetTypeHandle()->IsStringClass();
187 }
188
189 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
190 DCHECK(IsValid());
191 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
192 }
193
194 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
195 DCHECK(IsValid());
196 return GetTypeHandle()->IsInterface();
197 }
198
199 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
200 DCHECK(IsValid());
201 return GetTypeHandle()->IsArrayClass();
202 }
203
204 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
205 DCHECK(IsValid());
206 return GetTypeHandle()->IsPrimitiveArray();
207 }
208
209 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
210 DCHECK(IsValid());
211 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
212 }
213
214 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
215 DCHECK(IsValid());
216 if (!IsExact()) return false;
217 if (!IsArrayClass()) return false;
218 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
219 }
220
221 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
222 DCHECK(IsValid());
223 if (!IsExact()) return false;
224 if (!IsArrayClass()) return false;
225 if (!rti.IsArrayClass()) return false;
226 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
227 rti.GetTypeHandle()->GetComponentType());
228 }
229
230 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
231
232 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
233 DCHECK(IsValid());
234 DCHECK(rti.IsValid());
235 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
236 }
237
238 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
239 DCHECK(IsValid());
240 DCHECK(rti.IsValid());
241 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
242 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
243 }
244
245 // Returns true if the type information provide the same amount of details.
246 // Note that it does not mean that the instructions have the same actual type
247 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000248 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000249 if (!IsValid() && !rti.IsValid()) {
250 // Invalid types are equal.
251 return true;
252 }
253 if (!IsValid() || !rti.IsValid()) {
254 // One is valid, the other not.
255 return false;
256 }
257 return IsExact() == rti.IsExact()
258 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
259 }
260
261 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000262 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
263 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
264 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000265
266 // The class of the object.
267 TypeHandle type_handle_;
268 // Whether or not the type is exact or a superclass of the actual type.
269 // Whether or not we have any information about this type.
270 bool is_exact_;
271};
272
273std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
274
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000275// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100276class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000277 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100278 HGraph(ArenaAllocator* arena,
279 const DexFile& dex_file,
280 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100281 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700282 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100283 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100284 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000285 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100286 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000287 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100288 blocks_(arena->Adapter(kArenaAllocBlockList)),
289 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
290 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700291 entry_block_(nullptr),
292 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100293 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100294 number_of_vregs_(0),
295 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000296 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400297 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000298 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000299 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000300 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000301 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100302 dex_file_(dex_file),
303 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100304 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100305 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100306 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700307 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000308 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100309 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
310 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
311 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
312 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000313 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000314 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
315 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100316 blocks_.reserve(kDefaultNumberOfBlocks);
317 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000318
David Brazdilbadd8262016-02-02 16:28:56 +0000319 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
320 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
321
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000322 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100323 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
324
David Brazdil69ba7b72015-06-23 18:27:30 +0100325 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000326 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100327
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000328 HBasicBlock* GetEntryBlock() const { return entry_block_; }
329 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100330 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000331
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000332 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
333 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000334
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000335 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100336
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100337 void ComputeDominanceInformation();
338 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000339 void ClearLoopInformation();
340 void FindBackEdges(ArenaBitVector* visited);
341 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100342 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100343 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000344
David Brazdil4833f5a2015-12-16 10:37:39 +0000345 // Analyze all natural loops in this graph. Returns a code specifying that it
346 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000347 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000348 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100349
David Brazdilffee3d32015-07-06 11:48:53 +0100350 // Iterate over blocks to compute try block membership. Needs reverse post
351 // order and loop information.
352 void ComputeTryBlockInformation();
353
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000354 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000355 // Returns the instruction to replace the invoke expression or null if the
356 // invoke is for a void method. Note that the caller is responsible for replacing
357 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000358 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000359
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000360 // Update the loop and try membership of `block`, which was spawned from `reference`.
361 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
362 // should be the new back edge.
363 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
364 HBasicBlock* reference,
365 bool replace_if_back_edge);
366
Mingyao Yang3584bce2015-05-19 16:01:59 -0700367 // Need to add a couple of blocks to test if the loop body is entered and
368 // put deoptimization instructions, etc.
369 void TransformLoopHeaderForBCE(HBasicBlock* header);
370
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000371 // Removes `block` from the graph. Assumes `block` has been disconnected from
372 // other blocks and has no instructions or phis.
373 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000374
David Brazdilfc6a86a2015-06-26 10:33:45 +0000375 // Splits the edge between `block` and `successor` while preserving the
376 // indices in the predecessor/successor lists. If there are multiple edges
377 // between the blocks, the lowest indices are used.
378 // Returns the new block which is empty and has the same dex pc as `successor`.
379 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
380
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100381 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
382 void SimplifyLoop(HBasicBlock* header);
383
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000384 int32_t GetNextInstructionId() {
385 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000386 return current_instruction_id_++;
387 }
388
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000389 int32_t GetCurrentInstructionId() const {
390 return current_instruction_id_;
391 }
392
393 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000394 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000395 current_instruction_id_ = id;
396 }
397
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100398 uint16_t GetMaximumNumberOfOutVRegs() const {
399 return maximum_number_of_out_vregs_;
400 }
401
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000402 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
403 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100404 }
405
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100406 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
407 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
408 }
409
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000410 void UpdateTemporariesVRegSlots(size_t slots) {
411 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 }
413
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000414 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100415 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000416 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100417 }
418
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100419 void SetNumberOfVRegs(uint16_t number_of_vregs) {
420 number_of_vregs_ = number_of_vregs;
421 }
422
423 uint16_t GetNumberOfVRegs() const {
424 return number_of_vregs_;
425 }
426
427 void SetNumberOfInVRegs(uint16_t value) {
428 number_of_in_vregs_ = value;
429 }
430
David Brazdildee58d62016-04-07 09:54:26 +0000431 uint16_t GetNumberOfInVRegs() const {
432 return number_of_in_vregs_;
433 }
434
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100435 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100436 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100437 return number_of_vregs_ - number_of_in_vregs_;
438 }
439
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100440 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100441 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100442 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100443
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100444 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100445 return linear_order_;
446 }
447
Mark Mendell1152c922015-04-24 17:06:35 -0400448 bool HasBoundsChecks() const {
449 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800450 }
451
Mark Mendell1152c922015-04-24 17:06:35 -0400452 void SetHasBoundsChecks(bool value) {
453 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800454 }
455
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100456 bool ShouldGenerateConstructorBarrier() const {
457 return should_generate_constructor_barrier_;
458 }
459
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000460 bool IsDebuggable() const { return debuggable_; }
461
David Brazdil8d5b8b22015-03-24 10:51:52 +0000462 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000463 // already, it is created and inserted into the graph. This method is only for
464 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600465 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000466
467 // TODO: This is problematic for the consistency of reference type propagation
468 // because it can be created anytime after the pass and thus it will be left
469 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600470 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000471
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600472 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
473 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000474 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600475 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
476 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000477 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600478 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
479 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000480 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600481 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
482 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000483 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000484
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100485 HCurrentMethod* GetCurrentMethod();
486
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100487 const DexFile& GetDexFile() const {
488 return dex_file_;
489 }
490
491 uint32_t GetMethodIdx() const {
492 return method_idx_;
493 }
494
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100495 InvokeType GetInvokeType() const {
496 return invoke_type_;
497 }
498
Mark Mendellc4701932015-04-10 13:18:51 -0400499 InstructionSet GetInstructionSet() const {
500 return instruction_set_;
501 }
502
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000503 bool IsCompilingOsr() const { return osr_; }
504
David Brazdil77a48ae2015-09-15 12:34:04 +0000505 bool HasTryCatch() const { return has_try_catch_; }
506 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100507
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000508 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
509 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
510
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100511 ArtMethod* GetArtMethod() const { return art_method_; }
512 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
513
Mark Mendellf6529172015-11-17 11:16:56 -0500514 // Returns an instruction with the opposite boolean value from 'cond'.
515 // The instruction has been inserted into the graph, either as a constant, or
516 // before cursor.
517 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
518
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000519 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
520
David Brazdil2d7352b2015-04-20 14:52:42 +0100521 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000522 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100523 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000524
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000525 template <class InstructionType, typename ValueType>
526 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600527 ArenaSafeMap<ValueType, InstructionType*>* cache,
528 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000529 // Try to find an existing constant of the given value.
530 InstructionType* constant = nullptr;
531 auto cached_constant = cache->find(value);
532 if (cached_constant != cache->end()) {
533 constant = cached_constant->second;
534 }
535
536 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100537 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000538 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600539 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000540 cache->Overwrite(value, constant);
541 InsertConstant(constant);
542 }
543 return constant;
544 }
545
David Brazdil8d5b8b22015-03-24 10:51:52 +0000546 void InsertConstant(HConstant* instruction);
547
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000548 // Cache a float constant into the graph. This method should only be
549 // called by the SsaBuilder when creating "equivalent" instructions.
550 void CacheFloatConstant(HFloatConstant* constant);
551
552 // See CacheFloatConstant comment.
553 void CacheDoubleConstant(HDoubleConstant* constant);
554
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000555 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000556
557 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100558 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000559
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100560 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100561 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000562
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100563 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100564 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100565
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000566 HBasicBlock* entry_block_;
567 HBasicBlock* exit_block_;
568
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100569 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100570 uint16_t maximum_number_of_out_vregs_;
571
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100572 // The number of virtual registers in this method. Contains the parameters.
573 uint16_t number_of_vregs_;
574
575 // The number of virtual registers used by parameters of this method.
576 uint16_t number_of_in_vregs_;
577
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000578 // Number of vreg size slots that the temporaries use (used in baseline compiler).
579 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100580
Mark Mendell1152c922015-04-24 17:06:35 -0400581 // Has bounds checks. We can totally skip BCE if it's false.
582 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800583
David Brazdil77a48ae2015-09-15 12:34:04 +0000584 // Flag whether there are any try/catch blocks in the graph. We will skip
585 // try/catch-related passes if false.
586 bool has_try_catch_;
587
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000588 // Flag whether there are any irreducible loops in the graph.
589 bool has_irreducible_loops_;
590
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000591 // Indicates whether the graph should be compiled in a way that
592 // ensures full debuggability. If false, we can apply more
593 // aggressive optimizations that may limit the level of debugging.
594 const bool debuggable_;
595
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000596 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000597 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000598
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100599 // The dex file from which the method is from.
600 const DexFile& dex_file_;
601
602 // The method index in the dex file.
603 const uint32_t method_idx_;
604
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100605 // If inlined, this encodes how the callee is being invoked.
606 const InvokeType invoke_type_;
607
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100608 // Whether the graph has been transformed to SSA form. Only used
609 // in debug mode to ensure we are not using properties only valid
610 // for non-SSA form (like the number of temporaries).
611 bool in_ssa_form_;
612
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100613 const bool should_generate_constructor_barrier_;
614
Mathieu Chartiere401d142015-04-22 13:56:20 -0700615 const InstructionSet instruction_set_;
616
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000617 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000618 HNullConstant* cached_null_constant_;
619 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000620 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000621 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000622 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000623
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100624 HCurrentMethod* cached_current_method_;
625
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100626 // The ArtMethod this graph is for. Note that for AOT, it may be null,
627 // for example for methods whose declaring class could not be resolved
628 // (such as when the superclass could not be found).
629 ArtMethod* art_method_;
630
David Brazdil4833f5a2015-12-16 10:37:39 +0000631 // Keep the RTI of inexact Object to avoid having to pass stack handle
632 // collection pointer to passes which may create NullConstant.
633 ReferenceTypeInfo inexact_object_rti_;
634
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000635 // Whether we are compiling this graph for on stack replacement: this will
636 // make all loops seen as irreducible and emit special stack maps to mark
637 // compiled code entries which the interpreter can directly jump to.
638 const bool osr_;
639
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000640 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100641 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000642 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000643 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000644 DISALLOW_COPY_AND_ASSIGN(HGraph);
645};
646
Vladimir Markof9f64412015-09-02 14:05:49 +0100647class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000648 public:
649 HLoopInformation(HBasicBlock* header, HGraph* graph)
650 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100651 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000652 irreducible_(false),
Nicolas Geoffray77ce6432016-05-10 14:35:34 +0100653 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100654 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100655 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000656 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100657 back_edges_.reserve(kDefaultNumberOfBackEdges);
658 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100659
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000660 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffray77ce6432016-05-10 14:35:34 +0100661 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000662
663 void Dump(std::ostream& os);
664
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100665 HBasicBlock* GetHeader() const {
666 return header_;
667 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000668
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000669 void SetHeader(HBasicBlock* block) {
670 header_ = block;
671 }
672
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100673 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
674 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
675 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
676
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000677 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100678 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000679 }
680
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100681 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100682 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100683 }
684
David Brazdil46e2a392015-03-16 17:31:52 +0000685 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100686 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100687 }
688
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000689 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100690 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000691 }
692
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100693 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100694
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100695 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100696 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100697 }
698
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100699 // Returns the lifetime position of the back edge that has the
700 // greatest lifetime position.
701 size_t GetLifetimeEnd() const;
702
703 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100704 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100705 }
706
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000707 // Finds blocks that are part of this loop.
708 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100709
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100710 // Returns whether this loop information contains `block`.
711 // Note that this loop information *must* be populated before entering this function.
712 bool Contains(const HBasicBlock& block) const;
713
714 // Returns whether this loop information is an inner loop of `other`.
715 // Note that `other` *must* be populated before entering this function.
716 bool IsIn(const HLoopInformation& other) const;
717
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800718 // Returns true if instruction is not defined within this loop.
719 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700720
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100721 const ArenaBitVector& GetBlocks() const { return blocks_; }
722
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000723 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000724 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000725
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000726 void ClearAllBlocks() {
727 blocks_.ClearAllBits();
728 }
729
David Brazdil7e589fe2016-05-06 12:46:21 +0100730 bool HasBackEdgeNotDominatedByHeader() const;
731
Nicolas Geoffray77ce6432016-05-10 14:35:34 +0100732 bool IsPopulated() const {
733 return blocks_.GetHighestBitSet() != -1;
734 }
735
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000736 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100737 // Internal recursive implementation of `Populate`.
738 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100739 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100740
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000741 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100742 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000743 bool irreducible_;
Nicolas Geoffray77ce6432016-05-10 14:35:34 +0100744 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100745 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100746 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000747
748 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
749};
750
David Brazdilec16f792015-08-19 15:04:01 +0100751// Stores try/catch information for basic blocks.
752// Note that HGraph is constructed so that catch blocks cannot simultaneously
753// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100754class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100755 public:
756 // Try block information constructor.
757 explicit TryCatchInformation(const HTryBoundary& try_entry)
758 : try_entry_(&try_entry),
759 catch_dex_file_(nullptr),
760 catch_type_index_(DexFile::kDexNoIndex16) {
761 DCHECK(try_entry_ != nullptr);
762 }
763
764 // Catch block information constructor.
765 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
766 : try_entry_(nullptr),
767 catch_dex_file_(&dex_file),
768 catch_type_index_(catch_type_index) {}
769
770 bool IsTryBlock() const { return try_entry_ != nullptr; }
771
772 const HTryBoundary& GetTryEntry() const {
773 DCHECK(IsTryBlock());
774 return *try_entry_;
775 }
776
777 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
778
779 bool IsCatchAllTypeIndex() const {
780 DCHECK(IsCatchBlock());
781 return catch_type_index_ == DexFile::kDexNoIndex16;
782 }
783
784 uint16_t GetCatchTypeIndex() const {
785 DCHECK(IsCatchBlock());
786 return catch_type_index_;
787 }
788
789 const DexFile& GetCatchDexFile() const {
790 DCHECK(IsCatchBlock());
791 return *catch_dex_file_;
792 }
793
794 private:
795 // One of possibly several TryBoundary instructions entering the block's try.
796 // Only set for try blocks.
797 const HTryBoundary* try_entry_;
798
799 // Exception type information. Only set for catch blocks.
800 const DexFile* catch_dex_file_;
801 const uint16_t catch_type_index_;
802};
803
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100804static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100805static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100806
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000807// A block in a method. Contains the list of instructions represented
808// as a double linked list. Each block knows its predecessors and
809// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100810
Vladimir Markof9f64412015-09-02 14:05:49 +0100811class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000812 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600813 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000814 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000815 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
816 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000817 loop_information_(nullptr),
818 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000819 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100820 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100821 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100822 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000823 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000824 try_catch_information_(nullptr) {
825 predecessors_.reserve(kDefaultNumberOfPredecessors);
826 successors_.reserve(kDefaultNumberOfSuccessors);
827 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
828 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000829
Vladimir Marko60584552015-09-03 13:35:12 +0000830 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100831 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000832 }
833
Vladimir Marko60584552015-09-03 13:35:12 +0000834 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100835 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000836 }
837
David Brazdild26a4112015-11-10 11:07:31 +0000838 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
839 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
840
Vladimir Marko60584552015-09-03 13:35:12 +0000841 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
842 return ContainsElement(successors_, block, start_from);
843 }
844
845 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100846 return dominated_blocks_;
847 }
848
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100849 bool IsEntryBlock() const {
850 return graph_->GetEntryBlock() == this;
851 }
852
853 bool IsExitBlock() const {
854 return graph_->GetExitBlock() == this;
855 }
856
David Brazdil46e2a392015-03-16 17:31:52 +0000857 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000858 bool IsSingleTryBoundary() const;
859
860 // Returns true if this block emits nothing but a jump.
861 bool IsSingleJump() const {
862 HLoopInformation* loop_info = GetLoopInformation();
863 return (IsSingleGoto() || IsSingleTryBoundary())
864 // Back edges generate a suspend check.
865 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
866 }
David Brazdil46e2a392015-03-16 17:31:52 +0000867
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000868 void AddBackEdge(HBasicBlock* back_edge) {
869 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000870 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000871 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100872 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000873 loop_information_->AddBackEdge(back_edge);
874 }
875
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000876 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000877 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000878
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100879 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000880 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600881 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000882
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000883 HBasicBlock* GetDominator() const { return dominator_; }
884 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000885 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
886
887 void RemoveDominatedBlock(HBasicBlock* block) {
888 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100889 }
Vladimir Marko60584552015-09-03 13:35:12 +0000890
891 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
892 ReplaceElement(dominated_blocks_, existing, new_block);
893 }
894
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100895 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000896
897 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100898 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000899 }
900
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100901 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
902 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100903 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100904 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100905 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
906 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000907
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000908 HInstruction* GetFirstInstructionDisregardMoves() const;
909
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000910 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000911 successors_.push_back(block);
912 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000913 }
914
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100915 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
916 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100917 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000918 new_block->predecessors_.push_back(this);
919 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000920 }
921
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000922 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
923 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000924 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000925 new_block->successors_.push_back(this);
926 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000927 }
928
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100929 // Insert `this` between `predecessor` and `successor. This method
930 // preserves the indicies, and will update the first edge found between
931 // `predecessor` and `successor`.
932 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
933 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100934 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000935 successor->predecessors_[predecessor_index] = this;
936 predecessor->successors_[successor_index] = this;
937 successors_.push_back(successor);
938 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100939 }
940
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100941 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000942 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100943 }
944
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000945 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000946 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000947 }
948
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100949 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000950 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100951 }
952
953 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000954 predecessors_.push_back(block);
955 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100956 }
957
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100958 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000959 DCHECK_EQ(predecessors_.size(), 2u);
960 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100961 }
962
David Brazdil769c9e52015-04-27 13:54:09 +0100963 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000964 DCHECK_EQ(successors_.size(), 2u);
965 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100966 }
967
David Brazdilfc6a86a2015-06-26 10:33:45 +0000968 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000969 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100970 }
971
David Brazdilfc6a86a2015-06-26 10:33:45 +0000972 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000973 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100974 }
975
David Brazdilfc6a86a2015-06-26 10:33:45 +0000976 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000977 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100978 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000979 }
980
981 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000982 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100983 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000984 }
985
986 // Returns whether the first occurrence of `predecessor` in the list of
987 // predecessors is at index `idx`.
988 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100989 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000990 return GetPredecessorIndexOf(predecessor) == idx;
991 }
992
David Brazdild7558da2015-09-22 13:04:14 +0100993 // Create a new block between this block and its predecessors. The new block
994 // is added to the graph, all predecessor edges are relinked to it and an edge
995 // is created to `this`. Returns the new empty block. Reverse post order or
996 // loop and try/catch information are not updated.
997 HBasicBlock* CreateImmediateDominator();
998
David Brazdilfc6a86a2015-06-26 10:33:45 +0000999 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001000 // created, latter block. Note that this method will add the block to the
1001 // graph, create a Goto at the end of the former block and will create an edge
1002 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001003 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001004 HBasicBlock* SplitBefore(HInstruction* cursor);
1005
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001006 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001007 // created block. Note that this method just updates raw block information,
1008 // like predecessors, successors, dominators, and instruction list. It does not
1009 // update the graph, reverse post order, loop information, nor make sure the
1010 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001011 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1012
1013 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1014 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001015
1016 // Merge `other` at the end of `this`. Successors and dominated blocks of
1017 // `other` are changed to be successors and dominated blocks of `this`. Note
1018 // that this method does not update the graph, reverse post order, loop
1019 // information, nor make sure the blocks are consistent (for example ending
1020 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001021 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001022
1023 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1024 // of `this` are moved to `other`.
1025 // Note that this method does not update the graph, reverse post order, loop
1026 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001027 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001028 void ReplaceWith(HBasicBlock* other);
1029
David Brazdil2d7352b2015-04-20 14:52:42 +01001030 // Merge `other` at the end of `this`. This method updates loops, reverse post
1031 // order, links to predecessors, successors, dominators and deletes the block
1032 // from the graph. The two blocks must be successive, i.e. `this` the only
1033 // predecessor of `other` and vice versa.
1034 void MergeWith(HBasicBlock* other);
1035
1036 // Disconnects `this` from all its predecessors, successors and dominator,
1037 // removes it from all loops it is included in and eventually from the graph.
1038 // The block must not dominate any other block. Predecessors and successors
1039 // are safely updated.
1040 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001041
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001042 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001043 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001044 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001045 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001046 // Replace instruction `initial` with `replacement` within this block.
1047 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1048 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001049 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001050 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001051 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001052 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1053 // instruction list. With 'ensure_safety' set to true, it verifies that the
1054 // instruction is not in use and removes it from the use lists of its inputs.
1055 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1056 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001057 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001058
1059 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001060 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001061 }
1062
Roland Levillain6b879dd2014-09-22 17:13:44 +01001063 bool IsLoopPreHeaderFirstPredecessor() const {
1064 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001065 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001066 }
1067
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001068 bool IsFirstPredecessorBackEdge() const {
1069 DCHECK(IsLoopHeader());
1070 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1071 }
1072
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001073 HLoopInformation* GetLoopInformation() const {
1074 return loop_information_;
1075 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001076
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001077 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001078 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001079 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001080 void SetInLoop(HLoopInformation* info) {
1081 if (IsLoopHeader()) {
1082 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001083 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001084 loop_information_ = info;
1085 } else if (loop_information_->Contains(*info->GetHeader())) {
1086 // Block is currently part of an outer loop. Make it part of this inner loop.
1087 // Note that a non loop header having a loop information means this loop information
1088 // has already been populated
1089 loop_information_ = info;
1090 } else {
1091 // Block is part of an inner loop. Do not update the loop information.
1092 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1093 // at this point, because this method is being called while populating `info`.
1094 }
1095 }
1096
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001097 // Raw update of the loop information.
1098 void SetLoopInformation(HLoopInformation* info) {
1099 loop_information_ = info;
1100 }
1101
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001102 bool IsInLoop() const { return loop_information_ != nullptr; }
1103
David Brazdilec16f792015-08-19 15:04:01 +01001104 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1105
1106 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1107 try_catch_information_ = try_catch_information;
1108 }
1109
1110 bool IsTryBlock() const {
1111 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1112 }
1113
1114 bool IsCatchBlock() const {
1115 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1116 }
David Brazdilffee3d32015-07-06 11:48:53 +01001117
1118 // Returns the try entry that this block's successors should have. They will
1119 // be in the same try, unless the block ends in a try boundary. In that case,
1120 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001121 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001122
David Brazdild7558da2015-09-22 13:04:14 +01001123 bool HasThrowingInstructions() const;
1124
David Brazdila4b8c212015-05-07 09:59:30 +01001125 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001126 bool Dominates(HBasicBlock* block) const;
1127
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001128 size_t GetLifetimeStart() const { return lifetime_start_; }
1129 size_t GetLifetimeEnd() const { return lifetime_end_; }
1130
1131 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1132 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1133
David Brazdil8d5b8b22015-03-24 10:51:52 +00001134 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001135 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001136 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001137 bool HasSinglePhi() const;
1138
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001139 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001140 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001141 ArenaVector<HBasicBlock*> predecessors_;
1142 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001143 HInstructionList instructions_;
1144 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001145 HLoopInformation* loop_information_;
1146 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001147 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001148 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001149 // The dex program counter of the first instruction of this block.
1150 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001151 size_t lifetime_start_;
1152 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001153 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001154
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001155 friend class HGraph;
1156 friend class HInstruction;
1157
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001158 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1159};
1160
David Brazdilb2bd1c52015-03-25 11:17:37 +00001161// Iterates over the LoopInformation of all loops which contain 'block'
1162// from the innermost to the outermost.
1163class HLoopInformationOutwardIterator : public ValueObject {
1164 public:
1165 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1166 : current_(block.GetLoopInformation()) {}
1167
1168 bool Done() const { return current_ == nullptr; }
1169
1170 void Advance() {
1171 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001172 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001173 }
1174
1175 HLoopInformation* Current() const {
1176 DCHECK(!Done());
1177 return current_;
1178 }
1179
1180 private:
1181 HLoopInformation* current_;
1182
1183 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1184};
1185
Alexandre Ramesef20f712015-06-09 10:29:30 +01001186#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001187 M(Above, Condition) \
1188 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001189 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001190 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001191 M(ArrayGet, Instruction) \
1192 M(ArrayLength, Instruction) \
1193 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001194 M(Below, Condition) \
1195 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001196 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001197 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001198 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001199 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001200 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001201 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001202 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001203 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001204 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001205 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001206 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001207 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001208 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001209 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001210 M(Exit, Instruction) \
1211 M(FloatConstant, Constant) \
1212 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001213 M(GreaterThan, Condition) \
1214 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001215 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001216 M(InstanceFieldGet, Instruction) \
1217 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001218 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001219 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001220 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001221 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001222 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001223 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001224 M(LessThan, Condition) \
1225 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001226 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001227 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001228 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001229 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001230 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001231 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001232 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001233 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001234 M(Neg, UnaryOperation) \
1235 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001236 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001237 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001238 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001239 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001240 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001241 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001242 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001243 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001244 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001245 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001246 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001247 M(Return, Instruction) \
1248 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001249 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001250 M(Shl, BinaryOperation) \
1251 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001252 M(StaticFieldGet, Instruction) \
1253 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001254 M(UnresolvedInstanceFieldGet, Instruction) \
1255 M(UnresolvedInstanceFieldSet, Instruction) \
1256 M(UnresolvedStaticFieldGet, Instruction) \
1257 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001258 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001259 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001260 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001261 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001262 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001263 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001264 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001265 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001266
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001267/*
1268 * Instructions, shared across several (not all) architectures.
1269 */
1270#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1271#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1272#else
1273#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001274 M(BitwiseNegatedRight, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001275 M(MultiplyAccumulate, Instruction)
1276#endif
1277
Vladimir Markob4536b72015-11-24 13:45:23 +00001278#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001279#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001280#else
1281#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1282 M(ArmDexCacheArraysBase, Instruction)
1283#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001284
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001285#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001286#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001287#else
1288#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001289 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001290 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001291#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001292
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001293#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1294
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001295#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1296
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001297#ifndef ART_ENABLE_CODEGEN_x86
1298#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1299#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001300#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1301 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001302 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001303 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001304 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001305#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001306
1307#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1308
1309#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1310 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001311 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001312 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1313 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001314 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001315 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001316 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1317 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1318
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001319#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1320 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001321 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001322 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001323 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001324 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001325
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001326#define FOR_EACH_INSTRUCTION(M) \
1327 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1328 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1329
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001330#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001331FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1332#undef FORWARD_DECLARATION
1333
Roland Levillainccc07a92014-09-16 14:48:16 +01001334#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001335 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001336 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001337 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001338 return other->Is##type(); \
1339 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001340 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001341
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001342#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1343 bool Is##type() const { return As##type() != nullptr; } \
1344 const H##type* As##type() const { return this; } \
1345 H##type* As##type() { return this; }
1346
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001347template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001348class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001349 public:
David Brazdiled596192015-01-23 10:39:45 +00001350 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001351 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001352 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001353
Vladimir Markod59f3b12016-03-29 12:21:58 +01001354 // Hook for the IntrusiveForwardList<>.
1355 // TODO: Hide this better.
1356 IntrusiveForwardListHook hook;
1357
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001358 private:
David Brazdiled596192015-01-23 10:39:45 +00001359 HUseListNode(T user, size_t index)
Vladimir Markod59f3b12016-03-29 12:21:58 +01001360 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001361
1362 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001363 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001364
Vladimir Markod59f3b12016-03-29 12:21:58 +01001365 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001366
1367 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1368};
1369
David Brazdiled596192015-01-23 10:39:45 +00001370template <typename T>
Vladimir Markod59f3b12016-03-29 12:21:58 +01001371using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001372
David Brazdil1abb4192015-02-17 18:33:36 +00001373// This class is used by HEnvironment and HInstruction classes to record the
1374// instructions they use and pointers to the corresponding HUseListNodes kept
1375// by the used instructions.
1376template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001377class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001378 public:
Vladimir Markod59f3b12016-03-29 12:21:58 +01001379 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1380 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001381
Vladimir Markod59f3b12016-03-29 12:21:58 +01001382 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1383 : HUserRecord(old_record.instruction_, before_use_node) {}
1384 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1385 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001386 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001387 }
1388
1389 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Markod59f3b12016-03-29 12:21:58 +01001390 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1391 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001392
1393 private:
1394 // Instruction used by the user.
1395 HInstruction* instruction_;
1396
Vladimir Markod59f3b12016-03-29 12:21:58 +01001397 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1398 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001399};
1400
Aart Bik854a02b2015-07-14 16:07:00 -07001401/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001402 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001403 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001404 * For write/read dependences on fields/arrays, the dependence analysis uses
1405 * type disambiguation (e.g. a float field write cannot modify the value of an
1406 * integer field read) and the access type (e.g. a reference array write cannot
1407 * modify the value of a reference field read [although it may modify the
1408 * reference fetch prior to reading the field, which is represented by its own
1409 * write/read dependence]). The analysis makes conservative points-to
1410 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1411 * the same, and any reference read depends on any reference read without
1412 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001413 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001414 * The internal representation uses 38-bit and is described in the table below.
1415 * The first line indicates the side effect, and for field/array accesses the
1416 * second line indicates the type of the access (in the order of the
1417 * Primitive::Type enum).
1418 * The two numbered lines below indicate the bit position in the bitfield (read
1419 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001420 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001421 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1422 * +-------------+---------+---------+--------------+---------+---------+
1423 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1424 * | 3 |333333322|222222221| 1 |111111110|000000000|
1425 * | 7 |654321098|765432109| 8 |765432109|876543210|
1426 *
1427 * Note that, to ease the implementation, 'changes' bits are least significant
1428 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001429 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001430class SideEffects : public ValueObject {
1431 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001432 SideEffects() : flags_(0) {}
1433
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001434 static SideEffects None() {
1435 return SideEffects(0);
1436 }
1437
1438 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001439 return SideEffects(kAllChangeBits | kAllDependOnBits);
1440 }
1441
1442 static SideEffects AllChanges() {
1443 return SideEffects(kAllChangeBits);
1444 }
1445
1446 static SideEffects AllDependencies() {
1447 return SideEffects(kAllDependOnBits);
1448 }
1449
1450 static SideEffects AllExceptGCDependency() {
1451 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1452 }
1453
1454 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001455 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001456 }
1457
Aart Bik34c3ba92015-07-20 14:08:59 -07001458 static SideEffects AllWrites() {
1459 return SideEffects(kAllWrites);
1460 }
1461
1462 static SideEffects AllReads() {
1463 return SideEffects(kAllReads);
1464 }
1465
1466 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1467 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001468 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001469 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001470 }
1471
Aart Bik854a02b2015-07-14 16:07:00 -07001472 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1473 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001474 }
1475
Aart Bik34c3ba92015-07-20 14:08:59 -07001476 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1477 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001478 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001479 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001480 }
1481
1482 static SideEffects ArrayReadOfType(Primitive::Type type) {
1483 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1484 }
1485
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001486 static SideEffects CanTriggerGC() {
1487 return SideEffects(1ULL << kCanTriggerGCBit);
1488 }
1489
1490 static SideEffects DependsOnGC() {
1491 return SideEffects(1ULL << kDependsOnGCBit);
1492 }
1493
Aart Bik854a02b2015-07-14 16:07:00 -07001494 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001495 SideEffects Union(SideEffects other) const {
1496 return SideEffects(flags_ | other.flags_);
1497 }
1498
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001499 SideEffects Exclusion(SideEffects other) const {
1500 return SideEffects(flags_ & ~other.flags_);
1501 }
1502
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001503 void Add(SideEffects other) {
1504 flags_ |= other.flags_;
1505 }
1506
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001507 bool Includes(SideEffects other) const {
1508 return (other.flags_ & flags_) == other.flags_;
1509 }
1510
1511 bool HasSideEffects() const {
1512 return (flags_ & kAllChangeBits);
1513 }
1514
1515 bool HasDependencies() const {
1516 return (flags_ & kAllDependOnBits);
1517 }
1518
1519 // Returns true if there are no side effects or dependencies.
1520 bool DoesNothing() const {
1521 return flags_ == 0;
1522 }
1523
Aart Bik854a02b2015-07-14 16:07:00 -07001524 // Returns true if something is written.
1525 bool DoesAnyWrite() const {
1526 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001527 }
1528
Aart Bik854a02b2015-07-14 16:07:00 -07001529 // Returns true if something is read.
1530 bool DoesAnyRead() const {
1531 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001532 }
1533
Aart Bik854a02b2015-07-14 16:07:00 -07001534 // Returns true if potentially everything is written and read
1535 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001536 bool DoesAllReadWrite() const {
1537 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1538 }
1539
Aart Bik854a02b2015-07-14 16:07:00 -07001540 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001541 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001542 }
1543
Roland Levillain0d5a2812015-11-13 10:07:31 +00001544 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001545 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001546 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1547 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001548 }
1549
1550 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001551 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001552 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001553 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001554 for (int s = kLastBit; s >= 0; s--) {
1555 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1556 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1557 // This is a bit for the GC side effect.
1558 if (current_bit_is_set) {
1559 flags += "GC";
1560 }
Aart Bik854a02b2015-07-14 16:07:00 -07001561 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001562 } else {
1563 // This is a bit for the array/field analysis.
1564 // The underscore character stands for the 'can trigger GC' bit.
1565 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1566 if (current_bit_is_set) {
1567 flags += kDebug[s];
1568 }
1569 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1570 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1571 flags += "|";
1572 }
1573 }
Aart Bik854a02b2015-07-14 16:07:00 -07001574 }
1575 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001576 }
1577
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001578 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001579
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001580 private:
1581 static constexpr int kFieldArrayAnalysisBits = 9;
1582
1583 static constexpr int kFieldWriteOffset = 0;
1584 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1585 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1586 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1587
1588 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1589
1590 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1591 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1592 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1593 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1594
1595 static constexpr int kLastBit = kDependsOnGCBit;
1596 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1597
1598 // Aliases.
1599
1600 static_assert(kChangeBits == kDependOnBits,
1601 "the 'change' bits should match the 'depend on' bits.");
1602
1603 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1604 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1605 static constexpr uint64_t kAllWrites =
1606 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1607 static constexpr uint64_t kAllReads =
1608 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001609
Aart Bik854a02b2015-07-14 16:07:00 -07001610 // Work around the fact that HIR aliases I/F and J/D.
1611 // TODO: remove this interceptor once HIR types are clean
1612 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1613 switch (type) {
1614 case Primitive::kPrimInt:
1615 case Primitive::kPrimFloat:
1616 return TypeFlag(Primitive::kPrimInt, offset) |
1617 TypeFlag(Primitive::kPrimFloat, offset);
1618 case Primitive::kPrimLong:
1619 case Primitive::kPrimDouble:
1620 return TypeFlag(Primitive::kPrimLong, offset) |
1621 TypeFlag(Primitive::kPrimDouble, offset);
1622 default:
1623 return TypeFlag(type, offset);
1624 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001625 }
1626
Aart Bik854a02b2015-07-14 16:07:00 -07001627 // Translates type to bit flag.
1628 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1629 CHECK_NE(type, Primitive::kPrimVoid);
1630 const uint64_t one = 1;
1631 const int shift = type; // 0-based consecutive enum
1632 DCHECK_LE(kFieldWriteOffset, shift);
1633 DCHECK_LT(shift, kArrayWriteOffset);
1634 return one << (type + offset);
1635 }
1636
1637 // Private constructor on direct flags value.
1638 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1639
1640 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001641};
1642
David Brazdiled596192015-01-23 10:39:45 +00001643// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001644class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001645 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001646 HEnvironment(ArenaAllocator* arena,
1647 size_t number_of_vregs,
1648 const DexFile& dex_file,
1649 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001650 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001651 InvokeType invoke_type,
1652 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001653 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1654 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001655 parent_(nullptr),
1656 dex_file_(dex_file),
1657 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001658 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001659 invoke_type_(invoke_type),
1660 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001661 }
1662
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001663 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001664 : HEnvironment(arena,
1665 to_copy.Size(),
1666 to_copy.GetDexFile(),
1667 to_copy.GetMethodIdx(),
1668 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001669 to_copy.GetInvokeType(),
1670 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001671
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001672 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001673 if (parent_ != nullptr) {
1674 parent_->SetAndCopyParentChain(allocator, parent);
1675 } else {
1676 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1677 parent_->CopyFrom(parent);
1678 if (parent->GetParent() != nullptr) {
1679 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1680 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001681 }
David Brazdiled596192015-01-23 10:39:45 +00001682 }
1683
Vladimir Marko71bf8092015-09-15 15:33:14 +01001684 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001685 void CopyFrom(HEnvironment* environment);
1686
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001687 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1688 // input to the loop phi instead. This is for inserting instructions that
1689 // require an environment (like HDeoptimization) in the loop pre-header.
1690 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001691
1692 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001693 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001694 }
1695
1696 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001697 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001698 }
1699
David Brazdil1abb4192015-02-17 18:33:36 +00001700 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001701
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001702 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001703
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001704 HEnvironment* GetParent() const { return parent_; }
1705
1706 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001707 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001708 }
1709
1710 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001711 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001712 }
1713
1714 uint32_t GetDexPc() const {
1715 return dex_pc_;
1716 }
1717
1718 uint32_t GetMethodIdx() const {
1719 return method_idx_;
1720 }
1721
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001722 InvokeType GetInvokeType() const {
1723 return invoke_type_;
1724 }
1725
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001726 const DexFile& GetDexFile() const {
1727 return dex_file_;
1728 }
1729
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001730 HInstruction* GetHolder() const {
1731 return holder_;
1732 }
1733
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001734
1735 bool IsFromInlinedInvoke() const {
1736 return GetParent() != nullptr;
1737 }
1738
David Brazdiled596192015-01-23 10:39:45 +00001739 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001740 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1741 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001742 HEnvironment* parent_;
1743 const DexFile& dex_file_;
1744 const uint32_t method_idx_;
1745 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001746 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001747
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001748 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001749 HInstruction* const holder_;
1750
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001751 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001752
1753 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1754};
1755
Vladimir Markof9f64412015-09-02 14:05:49 +01001756class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001757 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001758 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001759 : previous_(nullptr),
1760 next_(nullptr),
1761 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001762 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001763 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001764 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001765 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001766 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001767 locations_(nullptr),
1768 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001769 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001770 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001771 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1772 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1773 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001774
Dave Allison20dfc792014-06-16 20:44:29 -07001775 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001776
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001777#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001778 enum InstructionKind {
1779 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1780 };
1781#undef DECLARE_KIND
1782
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001783 HInstruction* GetNext() const { return next_; }
1784 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001785
Calin Juravle77520bc2015-01-12 18:45:46 +00001786 HInstruction* GetNextDisregardingMoves() const;
1787 HInstruction* GetPreviousDisregardingMoves() const;
1788
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001789 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001790 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001791 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001792 bool IsInBlock() const { return block_ != nullptr; }
1793 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001794 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1795 bool IsIrreducibleLoopHeaderPhi() const {
1796 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1797 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001798
Roland Levillain6b879dd2014-09-22 17:13:44 +01001799 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001800 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001801
1802 virtual void Accept(HGraphVisitor* visitor) = 0;
1803 virtual const char* DebugName() const = 0;
1804
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001805 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001806 void SetRawInputAt(size_t index, HInstruction* input) {
1807 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1808 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001809
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001810 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001811
1812 uint32_t GetDexPc() const { return dex_pc_; }
1813
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001814 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001815
Roland Levillaine161a2a2014-10-03 12:45:18 +01001816 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001817 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001818
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001819 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001820 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001821
Calin Juravle10e244f2015-01-26 18:54:32 +00001822 // Does not apply for all instructions, but having this at top level greatly
1823 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001824 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001825 virtual bool CanBeNull() const {
1826 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1827 return true;
1828 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001829
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001830 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001831 return false;
1832 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001833
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001834 virtual bool IsActualObject() const {
1835 return GetType() == Primitive::kPrimNot;
1836 }
1837
Calin Juravle2e768302015-07-28 14:41:11 +00001838 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001839
Calin Juravle61d544b2015-02-23 16:46:57 +00001840 ReferenceTypeInfo GetReferenceTypeInfo() const {
1841 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001842 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Markoa4336d22016-04-19 14:12:13 +00001843 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001844 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001845
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001846 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001847 DCHECK(user != nullptr);
Vladimir Markod59f3b12016-03-29 12:21:58 +01001848 // Note: fixup_end remains valid across push_front().
1849 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1850 HUseListNode<HInstruction*>* new_node =
1851 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1852 uses_.push_front(*new_node);
1853 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001854 }
1855
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001856 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001857 DCHECK(user != nullptr);
Vladimir Markod59f3b12016-03-29 12:21:58 +01001858 // Note: env_fixup_end remains valid across push_front().
1859 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1860 HUseListNode<HEnvironment*>* new_node =
1861 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1862 env_uses_.push_front(*new_node);
1863 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001864 }
1865
David Brazdil1abb4192015-02-17 18:33:36 +00001866 void RemoveAsUserOfInput(size_t input) {
1867 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Markod59f3b12016-03-29 12:21:58 +01001868 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1869 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1870 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001871 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001872
David Brazdil1abb4192015-02-17 18:33:36 +00001873 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1874 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001875
Vladimir Markod59f3b12016-03-29 12:21:58 +01001876 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1877 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1878 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001879 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Markod59f3b12016-03-29 12:21:58 +01001880 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001881 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001882
Roland Levillain6c82d402014-10-13 16:10:27 +01001883 // Does this instruction strictly dominate `other_instruction`?
1884 // Returns false if this instruction and `other_instruction` are the same.
1885 // Aborts if this instruction and `other_instruction` are both phis.
1886 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001887
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001888 int GetId() const { return id_; }
1889 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001890
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001891 int GetSsaIndex() const { return ssa_index_; }
1892 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1893 bool HasSsaIndex() const { return ssa_index_ != -1; }
1894
1895 bool HasEnvironment() const { return environment_ != nullptr; }
1896 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001897 // Set the `environment_` field. Raw because this method does not
1898 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001899 void SetRawEnvironment(HEnvironment* environment) {
1900 DCHECK(environment_ == nullptr);
1901 DCHECK_EQ(environment->GetHolder(), this);
1902 environment_ = environment;
1903 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001904
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001905 void RemoveEnvironment();
1906
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001907 // Set the environment of this instruction, copying it from `environment`. While
1908 // copying, the uses lists are being updated.
1909 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001910 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001911 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001912 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001913 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001914 if (environment->GetParent() != nullptr) {
1915 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1916 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001917 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001918
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001919 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1920 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001921 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001922 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001923 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001924 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001925 if (environment->GetParent() != nullptr) {
1926 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1927 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001928 }
1929
Nicolas Geoffray39468442014-09-02 15:17:15 +01001930 // Returns the number of entries in the environment. Typically, that is the
1931 // number of dex registers in a method. It could be more in case of inlining.
1932 size_t EnvironmentSize() const;
1933
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001934 LocationSummary* GetLocations() const { return locations_; }
1935 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001936
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001937 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001938 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001939
Alexandre Rames188d4312015-04-09 18:30:21 +01001940 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1941 // uses of this instruction by `other` are *not* updated.
1942 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1943 ReplaceWith(other);
1944 other->ReplaceInput(this, use_index);
1945 }
1946
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001947 // Move `this` instruction before `cursor`.
1948 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001949
Vladimir Markofb337ea2015-11-25 15:25:10 +00001950 // Move `this` before its first user and out of any loops. If there is no
1951 // out-of-loop user that dominates all other users, move the instruction
1952 // to the end of the out-of-loop common dominator of the user's blocks.
1953 //
1954 // This can be used only on non-throwing instructions with no side effects that
1955 // have at least one use but no environment uses.
1956 void MoveBeforeFirstUserAndOutOfLoops();
1957
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001958#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001959 bool Is##type() const; \
1960 const H##type* As##type() const; \
1961 H##type* As##type();
1962
1963 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1964#undef INSTRUCTION_TYPE_CHECK
1965
1966#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001967 bool Is##type() const { return (As##type() != nullptr); } \
1968 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001969 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001970 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001971#undef INSTRUCTION_TYPE_CHECK
1972
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001973 // Returns whether the instruction can be moved within the graph.
1974 virtual bool CanBeMoved() const { return false; }
1975
1976 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001977 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001978 return false;
1979 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001980
1981 // Returns whether any data encoded in the two instructions is equal.
1982 // This method does not look at the inputs. Both instructions must be
1983 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001984 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001985 return false;
1986 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001987
1988 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001989 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001990 // 2) Their inputs are identical.
1991 bool Equals(HInstruction* other) const;
1992
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001993 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
1994 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
1995 // the virtual function because the __attribute__((__pure__)) doesn't really
1996 // apply the strong requirement for virtual functions, preventing optimizations.
1997 InstructionKind GetKind() const PURE;
1998 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001999
2000 virtual size_t ComputeHashCode() const {
2001 size_t result = GetKind();
2002 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2003 result = (result * 31) + InputAt(i)->GetId();
2004 }
2005 return result;
2006 }
2007
2008 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002009 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002010 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002011
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002012 size_t GetLifetimePosition() const { return lifetime_position_; }
2013 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2014 LiveInterval* GetLiveInterval() const { return live_interval_; }
2015 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2016 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2017
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002018 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2019
2020 // Returns whether the code generation of the instruction will require to have access
2021 // to the current method. Such instructions are:
2022 // (1): Instructions that require an environment, as calling the runtime requires
2023 // to walk the stack and have the current method stored at a specific stack address.
2024 // (2): Object literals like classes and strings, that are loaded from the dex cache
2025 // fields of the current method.
2026 bool NeedsCurrentMethod() const {
2027 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2028 }
2029
Vladimir Markodc151b22015-10-15 18:02:30 +01002030 // Returns whether the code generation of the instruction will require to have access
2031 // to the dex cache of the current method's declaring class via the current method.
2032 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002033
Mark Mendellc4701932015-04-10 13:18:51 -04002034 // Does this instruction have any use in an environment before
2035 // control flow hits 'other'?
2036 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2037
2038 // Remove all references to environment uses of this instruction.
2039 // The caller must ensure that this is safe to do.
2040 void RemoveEnvironmentUsers();
2041
Vladimir Markoa1de9182016-02-25 11:37:38 +00002042 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2043 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002044
David Brazdil1abb4192015-02-17 18:33:36 +00002045 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002046 // If set, the machine code for this instruction is assumed to be generated by
2047 // its users. Used by liveness analysis to compute use positions accordingly.
2048 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2049 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2050 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2051 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2052
David Brazdil1abb4192015-02-17 18:33:36 +00002053 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2054 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2055
Vladimir Markoa1de9182016-02-25 11:37:38 +00002056 uint32_t GetPackedFields() const {
2057 return packed_fields_;
2058 }
2059
2060 template <size_t flag>
2061 bool GetPackedFlag() const {
2062 return (packed_fields_ & (1u << flag)) != 0u;
2063 }
2064
2065 template <size_t flag>
2066 void SetPackedFlag(bool value = true) {
2067 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2068 }
2069
2070 template <typename BitFieldType>
2071 typename BitFieldType::value_type GetPackedField() const {
2072 return BitFieldType::Decode(packed_fields_);
2073 }
2074
2075 template <typename BitFieldType>
2076 void SetPackedField(typename BitFieldType::value_type value) {
2077 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2078 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2079 }
2080
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002081 private:
Vladimir Markod59f3b12016-03-29 12:21:58 +01002082 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2083 auto before_use_node = uses_.before_begin();
2084 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2085 HInstruction* user = use_node->GetUser();
2086 size_t input_index = use_node->GetIndex();
2087 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2088 before_use_node = use_node;
2089 }
2090 }
2091
2092 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2093 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2094 if (next != uses_.end()) {
2095 HInstruction* next_user = next->GetUser();
2096 size_t next_index = next->GetIndex();
2097 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2098 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2099 }
2100 }
2101
2102 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2103 auto before_env_use_node = env_uses_.before_begin();
2104 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2105 HEnvironment* user = env_use_node->GetUser();
2106 size_t input_index = env_use_node->GetIndex();
2107 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2108 before_env_use_node = env_use_node;
2109 }
2110 }
2111
2112 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2113 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2114 if (next != env_uses_.end()) {
2115 HEnvironment* next_user = next->GetUser();
2116 size_t next_index = next->GetIndex();
2117 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2118 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2119 }
2120 }
David Brazdil1abb4192015-02-17 18:33:36 +00002121
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002122 HInstruction* previous_;
2123 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002124 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002125 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002126
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002127 // An instruction gets an id when it is added to the graph.
2128 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002129 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002130 int id_;
2131
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002132 // When doing liveness analysis, instructions that have uses get an SSA index.
2133 int ssa_index_;
2134
Vladimir Markoa1de9182016-02-25 11:37:38 +00002135 // Packed fields.
2136 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002137
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002138 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002139 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002140
2141 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002142 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002143
Nicolas Geoffray39468442014-09-02 15:17:15 +01002144 // The environment associated with this instruction. Not null if the instruction
2145 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002146 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002147
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002148 // Set by the code generator.
2149 LocationSummary* locations_;
2150
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002151 // Set by the liveness analysis.
2152 LiveInterval* live_interval_;
2153
2154 // Set by the liveness analysis, this is the position in a linear
2155 // order of blocks where this instruction's live interval start.
2156 size_t lifetime_position_;
2157
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002158 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002159
Vladimir Markoa1de9182016-02-25 11:37:38 +00002160 // The reference handle part of the reference type info.
2161 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002162 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002163 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002164
David Brazdil1abb4192015-02-17 18:33:36 +00002165 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002166 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002167 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002168 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002169 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002170
2171 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2172};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002173std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002174
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002175class HInputIterator : public ValueObject {
2176 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002177 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002178
2179 bool Done() const { return index_ == instruction_->InputCount(); }
2180 HInstruction* Current() const { return instruction_->InputAt(index_); }
2181 void Advance() { index_++; }
2182
2183 private:
2184 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002185 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002186
2187 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2188};
2189
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002190class HInstructionIterator : public ValueObject {
2191 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002192 explicit HInstructionIterator(const HInstructionList& instructions)
2193 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002194 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002195 }
2196
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002197 bool Done() const { return instruction_ == nullptr; }
2198 HInstruction* Current() const { return instruction_; }
2199 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002200 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002201 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002202 }
2203
2204 private:
2205 HInstruction* instruction_;
2206 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002207
2208 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002209};
2210
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002211class HBackwardInstructionIterator : public ValueObject {
2212 public:
2213 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2214 : instruction_(instructions.last_instruction_) {
2215 next_ = Done() ? nullptr : instruction_->GetPrevious();
2216 }
2217
2218 bool Done() const { return instruction_ == nullptr; }
2219 HInstruction* Current() const { return instruction_; }
2220 void Advance() {
2221 instruction_ = next_;
2222 next_ = Done() ? nullptr : instruction_->GetPrevious();
2223 }
2224
2225 private:
2226 HInstruction* instruction_;
2227 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002228
2229 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002230};
2231
Vladimir Markof9f64412015-09-02 14:05:49 +01002232template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002233class HTemplateInstruction: public HInstruction {
2234 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002235 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002236 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002237 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002238
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002239 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002240
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002241 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002242 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2243 DCHECK_LT(i, N);
2244 return inputs_[i];
2245 }
David Brazdil1abb4192015-02-17 18:33:36 +00002246
2247 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002248 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002249 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002250 }
2251
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002252 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002253 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002254
2255 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002256};
2257
Vladimir Markof9f64412015-09-02 14:05:49 +01002258// HTemplateInstruction specialization for N=0.
2259template<>
2260class HTemplateInstruction<0>: public HInstruction {
2261 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002262 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002263 : HInstruction(side_effects, dex_pc) {}
2264
Vladimir Markof9f64412015-09-02 14:05:49 +01002265 virtual ~HTemplateInstruction() {}
2266
2267 size_t InputCount() const OVERRIDE { return 0; }
2268
2269 protected:
2270 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2271 LOG(FATAL) << "Unreachable";
2272 UNREACHABLE();
2273 }
2274
2275 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2276 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2277 LOG(FATAL) << "Unreachable";
2278 UNREACHABLE();
2279 }
2280
2281 private:
2282 friend class SsaBuilder;
2283};
2284
Dave Allison20dfc792014-06-16 20:44:29 -07002285template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002286class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002287 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002288 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002289 : HTemplateInstruction<N>(side_effects, dex_pc) {
2290 this->template SetPackedField<TypeField>(type);
2291 }
Dave Allison20dfc792014-06-16 20:44:29 -07002292 virtual ~HExpression() {}
2293
Vladimir Markoa1de9182016-02-25 11:37:38 +00002294 Primitive::Type GetType() const OVERRIDE {
2295 return TypeField::Decode(this->GetPackedFields());
2296 }
Dave Allison20dfc792014-06-16 20:44:29 -07002297
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002298 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002299 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2300 static constexpr size_t kFieldTypeSize =
2301 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2302 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2303 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2304 "Too many packed fields.");
2305 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002306};
2307
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002308// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2309// instruction that branches to the exit block.
2310class HReturnVoid : public HTemplateInstruction<0> {
2311 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002312 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2313 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002314
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002315 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002316
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002317 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002318
2319 private:
2320 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2321};
2322
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002323// Represents dex's RETURN opcodes. A HReturn is a control flow
2324// instruction that branches to the exit block.
2325class HReturn : public HTemplateInstruction<1> {
2326 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002327 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2328 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002329 SetRawInputAt(0, value);
2330 }
2331
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002332 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002333
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002334 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002335
2336 private:
2337 DISALLOW_COPY_AND_ASSIGN(HReturn);
2338};
2339
David Brazdildee58d62016-04-07 09:54:26 +00002340class HPhi : public HInstruction {
2341 public:
2342 HPhi(ArenaAllocator* arena,
2343 uint32_t reg_number,
2344 size_t number_of_inputs,
2345 Primitive::Type type,
2346 uint32_t dex_pc = kNoDexPc)
2347 : HInstruction(SideEffects::None(), dex_pc),
2348 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2349 reg_number_(reg_number) {
2350 SetPackedField<TypeField>(ToPhiType(type));
2351 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2352 // Phis are constructed live and marked dead if conflicting or unused.
2353 // Individual steps of SsaBuilder should assume that if a phi has been
2354 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2355 SetPackedFlag<kFlagIsLive>(true);
2356 SetPackedFlag<kFlagCanBeNull>(true);
2357 }
2358
2359 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2360 static Primitive::Type ToPhiType(Primitive::Type type) {
2361 return Primitive::PrimitiveKind(type);
2362 }
2363
2364 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2365
2366 size_t InputCount() const OVERRIDE { return inputs_.size(); }
2367
2368 void AddInput(HInstruction* input);
2369 void RemoveInputAt(size_t index);
2370
2371 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2372 void SetType(Primitive::Type new_type) {
2373 // Make sure that only valid type changes occur. The following are allowed:
2374 // (1) int -> float/ref (primitive type propagation),
2375 // (2) long -> double (primitive type propagation).
2376 DCHECK(GetType() == new_type ||
2377 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2378 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2379 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2380 SetPackedField<TypeField>(new_type);
2381 }
2382
2383 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2384 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2385
2386 uint32_t GetRegNumber() const { return reg_number_; }
2387
2388 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2389 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2390 bool IsDead() const { return !IsLive(); }
2391 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2392
2393 bool IsVRegEquivalentOf(HInstruction* other) const {
2394 return other != nullptr
2395 && other->IsPhi()
2396 && other->AsPhi()->GetBlock() == GetBlock()
2397 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2398 }
2399
2400 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2401 // An equivalent phi is a phi having the same dex register and type.
2402 // It assumes that phis with the same dex register are adjacent.
2403 HPhi* GetNextEquivalentPhiWithSameType() {
2404 HInstruction* next = GetNext();
2405 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2406 if (next->GetType() == GetType()) {
2407 return next->AsPhi();
2408 }
2409 next = next->GetNext();
2410 }
2411 return nullptr;
2412 }
2413
2414 DECLARE_INSTRUCTION(Phi);
2415
2416 protected:
2417 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
2418 return inputs_[index];
2419 }
2420
2421 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2422 inputs_[index] = input;
2423 }
2424
2425 private:
2426 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2427 static constexpr size_t kFieldTypeSize =
2428 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2429 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2430 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2431 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2432 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2433 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2434
2435 ArenaVector<HUserRecord<HInstruction*> > inputs_;
2436 const uint32_t reg_number_;
2437
2438 DISALLOW_COPY_AND_ASSIGN(HPhi);
2439};
2440
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002441// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002442// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002443// exit block.
2444class HExit : public HTemplateInstruction<0> {
2445 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002446 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002447
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002448 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002449
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002450 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002451
2452 private:
2453 DISALLOW_COPY_AND_ASSIGN(HExit);
2454};
2455
2456// Jumps from one block to another.
2457class HGoto : public HTemplateInstruction<0> {
2458 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002459 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002460
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002461 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002462
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002463 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002464 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002465 }
2466
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002467 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002468
2469 private:
2470 DISALLOW_COPY_AND_ASSIGN(HGoto);
2471};
2472
Roland Levillain9867bc72015-08-05 10:21:34 +01002473class HConstant : public HExpression<0> {
2474 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002475 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2476 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002477
2478 bool CanBeMoved() const OVERRIDE { return true; }
2479
Roland Levillain1a653882016-03-18 18:05:57 +00002480 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002481 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002482 // Is this constant 0 in the arithmetic sense?
2483 virtual bool IsArithmeticZero() const { return false; }
2484 // Is this constant a 0-bit pattern?
2485 virtual bool IsZeroBitPattern() const { return false; }
2486 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002487 virtual bool IsOne() const { return false; }
2488
David Brazdil77a48ae2015-09-15 12:34:04 +00002489 virtual uint64_t GetValueAsUint64() const = 0;
2490
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002491 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002492
2493 private:
2494 DISALLOW_COPY_AND_ASSIGN(HConstant);
2495};
2496
2497class HNullConstant : public HConstant {
2498 public:
2499 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2500 return true;
2501 }
2502
David Brazdil77a48ae2015-09-15 12:34:04 +00002503 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2504
Roland Levillain9867bc72015-08-05 10:21:34 +01002505 size_t ComputeHashCode() const OVERRIDE { return 0; }
2506
Roland Levillain1a653882016-03-18 18:05:57 +00002507 // The null constant representation is a 0-bit pattern.
2508 virtual bool IsZeroBitPattern() const { return true; }
2509
Roland Levillain9867bc72015-08-05 10:21:34 +01002510 DECLARE_INSTRUCTION(NullConstant);
2511
2512 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002513 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002514
2515 friend class HGraph;
2516 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2517};
2518
2519// Constants of the type int. Those can be from Dex instructions, or
2520// synthesized (for example with the if-eqz instruction).
2521class HIntConstant : public HConstant {
2522 public:
2523 int32_t GetValue() const { return value_; }
2524
David Brazdil9f389d42015-10-01 14:32:56 +01002525 uint64_t GetValueAsUint64() const OVERRIDE {
2526 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2527 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002528
Roland Levillain9867bc72015-08-05 10:21:34 +01002529 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002530 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002531 return other->AsIntConstant()->value_ == value_;
2532 }
2533
2534 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2535
2536 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002537 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2538 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002539 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2540
Roland Levillain1a653882016-03-18 18:05:57 +00002541 // Integer constants are used to encode Boolean values as well,
2542 // where 1 means true and 0 means false.
2543 bool IsTrue() const { return GetValue() == 1; }
2544 bool IsFalse() const { return GetValue() == 0; }
2545
Roland Levillain9867bc72015-08-05 10:21:34 +01002546 DECLARE_INSTRUCTION(IntConstant);
2547
2548 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002549 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2550 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2551 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2552 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002553
2554 const int32_t value_;
2555
2556 friend class HGraph;
2557 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2558 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2559 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2560};
2561
2562class HLongConstant : public HConstant {
2563 public:
2564 int64_t GetValue() const { return value_; }
2565
David Brazdil77a48ae2015-09-15 12:34:04 +00002566 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2567
Roland Levillain9867bc72015-08-05 10:21:34 +01002568 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002569 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002570 return other->AsLongConstant()->value_ == value_;
2571 }
2572
2573 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2574
2575 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002576 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2577 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002578 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2579
2580 DECLARE_INSTRUCTION(LongConstant);
2581
2582 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002583 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2584 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002585
2586 const int64_t value_;
2587
2588 friend class HGraph;
2589 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2590};
Dave Allison20dfc792014-06-16 20:44:29 -07002591
Roland Levillain31dd3d62016-02-16 12:21:02 +00002592class HFloatConstant : public HConstant {
2593 public:
2594 float GetValue() const { return value_; }
2595
2596 uint64_t GetValueAsUint64() const OVERRIDE {
2597 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2598 }
2599
2600 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2601 DCHECK(other->IsFloatConstant()) << other->DebugName();
2602 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2603 }
2604
2605 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2606
2607 bool IsMinusOne() const OVERRIDE {
2608 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2609 }
Roland Levillain1a653882016-03-18 18:05:57 +00002610 bool IsArithmeticZero() const OVERRIDE {
2611 return std::fpclassify(value_) == FP_ZERO;
2612 }
2613 bool IsArithmeticPositiveZero() const {
2614 return IsArithmeticZero() && !std::signbit(value_);
2615 }
2616 bool IsArithmeticNegativeZero() const {
2617 return IsArithmeticZero() && std::signbit(value_);
2618 }
2619 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002620 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002621 }
2622 bool IsOne() const OVERRIDE {
2623 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2624 }
2625 bool IsNaN() const {
2626 return std::isnan(value_);
2627 }
2628
2629 DECLARE_INSTRUCTION(FloatConstant);
2630
2631 private:
2632 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2633 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2634 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2635 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2636
2637 const float value_;
2638
2639 // Only the SsaBuilder and HGraph can create floating-point constants.
2640 friend class SsaBuilder;
2641 friend class HGraph;
2642 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2643};
2644
2645class HDoubleConstant : public HConstant {
2646 public:
2647 double GetValue() const { return value_; }
2648
2649 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2650
2651 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2652 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2653 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2654 }
2655
2656 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2657
2658 bool IsMinusOne() const OVERRIDE {
2659 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2660 }
Roland Levillain1a653882016-03-18 18:05:57 +00002661 bool IsArithmeticZero() const OVERRIDE {
2662 return std::fpclassify(value_) == FP_ZERO;
2663 }
2664 bool IsArithmeticPositiveZero() const {
2665 return IsArithmeticZero() && !std::signbit(value_);
2666 }
2667 bool IsArithmeticNegativeZero() const {
2668 return IsArithmeticZero() && std::signbit(value_);
2669 }
2670 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002671 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002672 }
2673 bool IsOne() const OVERRIDE {
2674 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2675 }
2676 bool IsNaN() const {
2677 return std::isnan(value_);
2678 }
2679
2680 DECLARE_INSTRUCTION(DoubleConstant);
2681
2682 private:
2683 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2684 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2685 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2686 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2687
2688 const double value_;
2689
2690 // Only the SsaBuilder and HGraph can create floating-point constants.
2691 friend class SsaBuilder;
2692 friend class HGraph;
2693 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2694};
2695
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002696// Conditional branch. A block ending with an HIf instruction must have
2697// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002698class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002699 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002700 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2701 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002702 SetRawInputAt(0, input);
2703 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002704
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002705 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002706
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002707 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002708 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002709 }
2710
2711 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002712 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002713 }
2714
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002715 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002716
2717 private:
2718 DISALLOW_COPY_AND_ASSIGN(HIf);
2719};
2720
David Brazdilfc6a86a2015-06-26 10:33:45 +00002721
2722// Abstract instruction which marks the beginning and/or end of a try block and
2723// links it to the respective exception handlers. Behaves the same as a Goto in
2724// non-exceptional control flow.
2725// Normal-flow successor is stored at index zero, exception handlers under
2726// higher indices in no particular order.
2727class HTryBoundary : public HTemplateInstruction<0> {
2728 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002729 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002730 kEntry,
2731 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002732 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002733 };
2734
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002735 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002736 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2737 SetPackedField<BoundaryKindField>(kind);
2738 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002739
2740 bool IsControlFlow() const OVERRIDE { return true; }
2741
2742 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002743 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002744
David Brazdild26a4112015-11-10 11:07:31 +00002745 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2746 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2747 }
2748
David Brazdilfc6a86a2015-06-26 10:33:45 +00002749 // Returns whether `handler` is among its exception handlers (non-zero index
2750 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002751 bool HasExceptionHandler(const HBasicBlock& handler) const {
2752 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002753 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002754 }
2755
2756 // If not present already, adds `handler` to its block's list of exception
2757 // handlers.
2758 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002759 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002760 GetBlock()->AddSuccessor(handler);
2761 }
2762 }
2763
Vladimir Markoa1de9182016-02-25 11:37:38 +00002764 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2765 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002766
David Brazdilffee3d32015-07-06 11:48:53 +01002767 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2768
David Brazdilfc6a86a2015-06-26 10:33:45 +00002769 DECLARE_INSTRUCTION(TryBoundary);
2770
2771 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002772 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2773 static constexpr size_t kFieldBoundaryKindSize =
2774 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2775 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2776 kFieldBoundaryKind + kFieldBoundaryKindSize;
2777 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2778 "Too many packed fields.");
2779 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002780
2781 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2782};
2783
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002784// Deoptimize to interpreter, upon checking a condition.
2785class HDeoptimize : public HTemplateInstruction<1> {
2786 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002787 // We set CanTriggerGC to prevent any intermediate address to be live
2788 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002789 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002790 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002791 SetRawInputAt(0, cond);
2792 }
2793
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002794 bool CanBeMoved() const OVERRIDE { return true; }
2795 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2796 return true;
2797 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002798 bool NeedsEnvironment() const OVERRIDE { return true; }
2799 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002800
2801 DECLARE_INSTRUCTION(Deoptimize);
2802
2803 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002804 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2805};
2806
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002807// Represents the ArtMethod that was passed as a first argument to
2808// the method. It is used by instructions that depend on it, like
2809// instructions that work with the dex cache.
2810class HCurrentMethod : public HExpression<0> {
2811 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002812 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2813 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002814
2815 DECLARE_INSTRUCTION(CurrentMethod);
2816
2817 private:
2818 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2819};
2820
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002821// Fetches an ArtMethod from the virtual table or the interface method table
2822// of a class.
2823class HClassTableGet : public HExpression<1> {
2824 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002825 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002826 kVTable,
2827 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002828 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002829 };
2830 HClassTableGet(HInstruction* cls,
2831 Primitive::Type type,
2832 TableKind kind,
2833 size_t index,
2834 uint32_t dex_pc)
2835 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002836 index_(index) {
2837 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002838 SetRawInputAt(0, cls);
2839 }
2840
2841 bool CanBeMoved() const OVERRIDE { return true; }
2842 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2843 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002844 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002845 }
2846
Vladimir Markoa1de9182016-02-25 11:37:38 +00002847 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002848 size_t GetIndex() const { return index_; }
2849
2850 DECLARE_INSTRUCTION(ClassTableGet);
2851
2852 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002853 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2854 static constexpr size_t kFieldTableKindSize =
2855 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2856 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2857 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2858 "Too many packed fields.");
2859 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2860
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002861 // The index of the ArtMethod in the table.
2862 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002863
2864 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2865};
2866
Mark Mendellfe57faa2015-09-18 09:26:15 -04002867// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2868// have one successor for each entry in the switch table, and the final successor
2869// will be the block containing the next Dex opcode.
2870class HPackedSwitch : public HTemplateInstruction<1> {
2871 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002872 HPackedSwitch(int32_t start_value,
2873 uint32_t num_entries,
2874 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002875 uint32_t dex_pc = kNoDexPc)
2876 : HTemplateInstruction(SideEffects::None(), dex_pc),
2877 start_value_(start_value),
2878 num_entries_(num_entries) {
2879 SetRawInputAt(0, input);
2880 }
2881
2882 bool IsControlFlow() const OVERRIDE { return true; }
2883
2884 int32_t GetStartValue() const { return start_value_; }
2885
Vladimir Marko211c2112015-09-24 16:52:33 +01002886 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002887
2888 HBasicBlock* GetDefaultBlock() const {
2889 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002890 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002891 }
2892 DECLARE_INSTRUCTION(PackedSwitch);
2893
2894 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002895 const int32_t start_value_;
2896 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002897
2898 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2899};
2900
Roland Levillain88cb1752014-10-20 16:36:47 +01002901class HUnaryOperation : public HExpression<1> {
2902 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002903 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2904 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002905 SetRawInputAt(0, input);
2906 }
2907
2908 HInstruction* GetInput() const { return InputAt(0); }
2909 Primitive::Type GetResultType() const { return GetType(); }
2910
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002911 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002912 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002913 return true;
2914 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002915
Roland Levillain31dd3d62016-02-16 12:21:02 +00002916 // Try to statically evaluate `this` and return a HConstant
2917 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002918 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002919 HConstant* TryStaticEvaluation() const;
2920
2921 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002922 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2923 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002924 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2925 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002926
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002927 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002928
2929 private:
2930 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2931};
2932
Dave Allison20dfc792014-06-16 20:44:29 -07002933class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002934 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002935 HBinaryOperation(Primitive::Type result_type,
2936 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002937 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002938 SideEffects side_effects = SideEffects::None(),
2939 uint32_t dex_pc = kNoDexPc)
2940 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002941 SetRawInputAt(0, left);
2942 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002943 }
2944
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002945 HInstruction* GetLeft() const { return InputAt(0); }
2946 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002947 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002948
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002949 virtual bool IsCommutative() const { return false; }
2950
2951 // Put constant on the right.
2952 // Returns whether order is changed.
2953 bool OrderInputsWithConstantOnTheRight() {
2954 HInstruction* left = InputAt(0);
2955 HInstruction* right = InputAt(1);
2956 if (left->IsConstant() && !right->IsConstant()) {
2957 ReplaceInput(right, 0);
2958 ReplaceInput(left, 1);
2959 return true;
2960 }
2961 return false;
2962 }
2963
2964 // Order inputs by instruction id, but favor constant on the right side.
2965 // This helps GVN for commutative ops.
2966 void OrderInputs() {
2967 DCHECK(IsCommutative());
2968 HInstruction* left = InputAt(0);
2969 HInstruction* right = InputAt(1);
2970 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2971 return;
2972 }
2973 if (OrderInputsWithConstantOnTheRight()) {
2974 return;
2975 }
2976 // Order according to instruction id.
2977 if (left->GetId() > right->GetId()) {
2978 ReplaceInput(right, 0);
2979 ReplaceInput(left, 1);
2980 }
2981 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002982
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002983 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002984 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002985 return true;
2986 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002987
Roland Levillain31dd3d62016-02-16 12:21:02 +00002988 // Try to statically evaluate `this` and return a HConstant
2989 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002990 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002991 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002992
2993 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002994 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2995 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002996 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2997 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002998 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002999 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3000 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003001 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3002 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003003 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3004 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003005 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003006 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3007 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003008
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003009 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003010 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003011 HConstant* GetConstantRight() const;
3012
3013 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003014 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003015 HInstruction* GetLeastConstantLeft() const;
3016
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003017 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003018
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003019 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003020 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3021};
3022
Mark Mendellc4701932015-04-10 13:18:51 -04003023// The comparison bias applies for floating point operations and indicates how NaN
3024// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003025enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003026 kNoBias, // bias is not applicable (i.e. for long operation)
3027 kGtBias, // return 1 for NaN comparisons
3028 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003029 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003030};
3031
Roland Levillain31dd3d62016-02-16 12:21:02 +00003032std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3033
Dave Allison20dfc792014-06-16 20:44:29 -07003034class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003035 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003036 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003037 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3038 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3039 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003040
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003041 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003042 // `instruction`, and disregard moves in between.
3043 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003044
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003045 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003046
3047 virtual IfCondition GetCondition() const = 0;
3048
Mark Mendellc4701932015-04-10 13:18:51 -04003049 virtual IfCondition GetOppositeCondition() const = 0;
3050
Vladimir Markoa1de9182016-02-25 11:37:38 +00003051 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003052 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3053
Vladimir Markoa1de9182016-02-25 11:37:38 +00003054 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3055 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003056
3057 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003058 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003059 }
3060
Roland Levillain4fa13f62015-07-06 18:11:54 +01003061 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003062 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003063 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003064 if (if_cond == kCondNE) {
3065 return true;
3066 } else if (if_cond == kCondEQ) {
3067 return false;
3068 }
3069 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003070 }
3071
3072 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003073 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003074 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003075 if (if_cond == kCondEQ) {
3076 return true;
3077 } else if (if_cond == kCondNE) {
3078 return false;
3079 }
3080 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003081 }
3082
Roland Levillain31dd3d62016-02-16 12:21:02 +00003083 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003084 // Needed if we merge a HCompare into a HCondition.
3085 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3086 static constexpr size_t kFieldComparisonBiasSize =
3087 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3088 static constexpr size_t kNumberOfConditionPackedBits =
3089 kFieldComparisonBias + kFieldComparisonBiasSize;
3090 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3091 using ComparisonBiasField =
3092 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3093
Roland Levillain31dd3d62016-02-16 12:21:02 +00003094 template <typename T>
3095 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3096
3097 template <typename T>
3098 int32_t CompareFP(T x, T y) const {
3099 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3100 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3101 // Handle the bias.
3102 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3103 }
3104
3105 // Return an integer constant containing the result of a condition evaluated at compile time.
3106 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3107 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3108 }
3109
Dave Allison20dfc792014-06-16 20:44:29 -07003110 private:
3111 DISALLOW_COPY_AND_ASSIGN(HCondition);
3112};
3113
3114// Instruction to check if two inputs are equal to each other.
3115class HEqual : public HCondition {
3116 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003117 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3118 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003119
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003120 bool IsCommutative() const OVERRIDE { return true; }
3121
Vladimir Marko9e23df52015-11-10 17:14:35 +00003122 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3123 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003124 return MakeConstantCondition(true, GetDexPc());
3125 }
3126 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3127 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3128 }
3129 // In the following Evaluate methods, a HCompare instruction has
3130 // been merged into this HEqual instruction; evaluate it as
3131 // `Compare(x, y) == 0`.
3132 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3133 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3134 GetDexPc());
3135 }
3136 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3137 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3138 }
3139 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3140 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003141 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003142
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003143 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003144
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003145 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003146 return kCondEQ;
3147 }
3148
Mark Mendellc4701932015-04-10 13:18:51 -04003149 IfCondition GetOppositeCondition() const OVERRIDE {
3150 return kCondNE;
3151 }
3152
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003153 private:
Aart Bike9f37602015-10-09 11:15:55 -07003154 template <typename T> bool Compute(T x, T y) const { return x == y; }
3155
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003156 DISALLOW_COPY_AND_ASSIGN(HEqual);
3157};
3158
Dave Allison20dfc792014-06-16 20:44:29 -07003159class HNotEqual : public HCondition {
3160 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003161 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3162 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003163
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003164 bool IsCommutative() const OVERRIDE { return true; }
3165
Vladimir Marko9e23df52015-11-10 17:14:35 +00003166 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3167 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003168 return MakeConstantCondition(false, GetDexPc());
3169 }
3170 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3171 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3172 }
3173 // In the following Evaluate methods, a HCompare instruction has
3174 // been merged into this HNotEqual instruction; evaluate it as
3175 // `Compare(x, y) != 0`.
3176 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3177 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3178 }
3179 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3180 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3181 }
3182 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3183 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003184 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003185
Dave Allison20dfc792014-06-16 20:44:29 -07003186 DECLARE_INSTRUCTION(NotEqual);
3187
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003188 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003189 return kCondNE;
3190 }
3191
Mark Mendellc4701932015-04-10 13:18:51 -04003192 IfCondition GetOppositeCondition() const OVERRIDE {
3193 return kCondEQ;
3194 }
3195
Dave Allison20dfc792014-06-16 20:44:29 -07003196 private:
Aart Bike9f37602015-10-09 11:15:55 -07003197 template <typename T> bool Compute(T x, T y) const { return x != y; }
3198
Dave Allison20dfc792014-06-16 20:44:29 -07003199 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3200};
3201
3202class HLessThan : public HCondition {
3203 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003204 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3205 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003206
Roland Levillain9867bc72015-08-05 10:21:34 +01003207 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003208 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003209 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003210 // In the following Evaluate methods, a HCompare instruction has
3211 // been merged into this HLessThan instruction; evaluate it as
3212 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003213 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003214 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3215 }
3216 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3217 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3218 }
3219 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3220 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003221 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003222
Dave Allison20dfc792014-06-16 20:44:29 -07003223 DECLARE_INSTRUCTION(LessThan);
3224
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003225 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003226 return kCondLT;
3227 }
3228
Mark Mendellc4701932015-04-10 13:18:51 -04003229 IfCondition GetOppositeCondition() const OVERRIDE {
3230 return kCondGE;
3231 }
3232
Dave Allison20dfc792014-06-16 20:44:29 -07003233 private:
Aart Bike9f37602015-10-09 11:15:55 -07003234 template <typename T> bool Compute(T x, T y) const { return x < y; }
3235
Dave Allison20dfc792014-06-16 20:44:29 -07003236 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3237};
3238
3239class HLessThanOrEqual : public HCondition {
3240 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003241 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3242 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003243
Roland Levillain9867bc72015-08-05 10:21:34 +01003244 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003245 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003246 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003247 // In the following Evaluate methods, a HCompare instruction has
3248 // been merged into this HLessThanOrEqual instruction; evaluate it as
3249 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003250 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003251 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3252 }
3253 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3254 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3255 }
3256 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3257 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003258 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003259
Dave Allison20dfc792014-06-16 20:44:29 -07003260 DECLARE_INSTRUCTION(LessThanOrEqual);
3261
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003262 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003263 return kCondLE;
3264 }
3265
Mark Mendellc4701932015-04-10 13:18:51 -04003266 IfCondition GetOppositeCondition() const OVERRIDE {
3267 return kCondGT;
3268 }
3269
Dave Allison20dfc792014-06-16 20:44:29 -07003270 private:
Aart Bike9f37602015-10-09 11:15:55 -07003271 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3272
Dave Allison20dfc792014-06-16 20:44:29 -07003273 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3274};
3275
3276class HGreaterThan : public HCondition {
3277 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003278 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3279 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003280
Roland Levillain9867bc72015-08-05 10:21:34 +01003281 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003282 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003283 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003284 // In the following Evaluate methods, a HCompare instruction has
3285 // been merged into this HGreaterThan instruction; evaluate it as
3286 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003287 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003288 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3289 }
3290 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3291 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3292 }
3293 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3294 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003295 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003296
Dave Allison20dfc792014-06-16 20:44:29 -07003297 DECLARE_INSTRUCTION(GreaterThan);
3298
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003299 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003300 return kCondGT;
3301 }
3302
Mark Mendellc4701932015-04-10 13:18:51 -04003303 IfCondition GetOppositeCondition() const OVERRIDE {
3304 return kCondLE;
3305 }
3306
Dave Allison20dfc792014-06-16 20:44:29 -07003307 private:
Aart Bike9f37602015-10-09 11:15:55 -07003308 template <typename T> bool Compute(T x, T y) const { return x > y; }
3309
Dave Allison20dfc792014-06-16 20:44:29 -07003310 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3311};
3312
3313class HGreaterThanOrEqual : public HCondition {
3314 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003315 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3316 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003317
Roland Levillain9867bc72015-08-05 10:21:34 +01003318 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003319 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003320 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003321 // In the following Evaluate methods, a HCompare instruction has
3322 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3323 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003324 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003325 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3326 }
3327 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3328 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3329 }
3330 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3331 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003332 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003333
Dave Allison20dfc792014-06-16 20:44:29 -07003334 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3335
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003336 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003337 return kCondGE;
3338 }
3339
Mark Mendellc4701932015-04-10 13:18:51 -04003340 IfCondition GetOppositeCondition() const OVERRIDE {
3341 return kCondLT;
3342 }
3343
Dave Allison20dfc792014-06-16 20:44:29 -07003344 private:
Aart Bike9f37602015-10-09 11:15:55 -07003345 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3346
Dave Allison20dfc792014-06-16 20:44:29 -07003347 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3348};
3349
Aart Bike9f37602015-10-09 11:15:55 -07003350class HBelow : public HCondition {
3351 public:
3352 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3353 : HCondition(first, second, dex_pc) {}
3354
3355 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003356 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003357 }
3358 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003359 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3360 }
3361 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3362 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3363 LOG(FATAL) << DebugName() << " is not defined for float values";
3364 UNREACHABLE();
3365 }
3366 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3367 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3368 LOG(FATAL) << DebugName() << " is not defined for double values";
3369 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003370 }
3371
3372 DECLARE_INSTRUCTION(Below);
3373
3374 IfCondition GetCondition() const OVERRIDE {
3375 return kCondB;
3376 }
3377
3378 IfCondition GetOppositeCondition() const OVERRIDE {
3379 return kCondAE;
3380 }
3381
3382 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003383 template <typename T> bool Compute(T x, T y) const {
3384 return MakeUnsigned(x) < MakeUnsigned(y);
3385 }
Aart Bike9f37602015-10-09 11:15:55 -07003386
3387 DISALLOW_COPY_AND_ASSIGN(HBelow);
3388};
3389
3390class HBelowOrEqual : public HCondition {
3391 public:
3392 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3393 : HCondition(first, second, dex_pc) {}
3394
3395 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003396 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003397 }
3398 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003399 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3400 }
3401 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3402 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3403 LOG(FATAL) << DebugName() << " is not defined for float values";
3404 UNREACHABLE();
3405 }
3406 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3407 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3408 LOG(FATAL) << DebugName() << " is not defined for double values";
3409 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003410 }
3411
3412 DECLARE_INSTRUCTION(BelowOrEqual);
3413
3414 IfCondition GetCondition() const OVERRIDE {
3415 return kCondBE;
3416 }
3417
3418 IfCondition GetOppositeCondition() const OVERRIDE {
3419 return kCondA;
3420 }
3421
3422 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003423 template <typename T> bool Compute(T x, T y) const {
3424 return MakeUnsigned(x) <= MakeUnsigned(y);
3425 }
Aart Bike9f37602015-10-09 11:15:55 -07003426
3427 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3428};
3429
3430class HAbove : public HCondition {
3431 public:
3432 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3433 : HCondition(first, second, dex_pc) {}
3434
3435 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003436 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003437 }
3438 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003439 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3440 }
3441 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3442 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3443 LOG(FATAL) << DebugName() << " is not defined for float values";
3444 UNREACHABLE();
3445 }
3446 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3447 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3448 LOG(FATAL) << DebugName() << " is not defined for double values";
3449 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003450 }
3451
3452 DECLARE_INSTRUCTION(Above);
3453
3454 IfCondition GetCondition() const OVERRIDE {
3455 return kCondA;
3456 }
3457
3458 IfCondition GetOppositeCondition() const OVERRIDE {
3459 return kCondBE;
3460 }
3461
3462 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003463 template <typename T> bool Compute(T x, T y) const {
3464 return MakeUnsigned(x) > MakeUnsigned(y);
3465 }
Aart Bike9f37602015-10-09 11:15:55 -07003466
3467 DISALLOW_COPY_AND_ASSIGN(HAbove);
3468};
3469
3470class HAboveOrEqual : public HCondition {
3471 public:
3472 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3473 : HCondition(first, second, dex_pc) {}
3474
3475 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003476 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003477 }
3478 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003479 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3480 }
3481 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3482 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3483 LOG(FATAL) << DebugName() << " is not defined for float values";
3484 UNREACHABLE();
3485 }
3486 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3487 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3488 LOG(FATAL) << DebugName() << " is not defined for double values";
3489 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003490 }
3491
3492 DECLARE_INSTRUCTION(AboveOrEqual);
3493
3494 IfCondition GetCondition() const OVERRIDE {
3495 return kCondAE;
3496 }
3497
3498 IfCondition GetOppositeCondition() const OVERRIDE {
3499 return kCondB;
3500 }
3501
3502 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003503 template <typename T> bool Compute(T x, T y) const {
3504 return MakeUnsigned(x) >= MakeUnsigned(y);
3505 }
Aart Bike9f37602015-10-09 11:15:55 -07003506
3507 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3508};
Dave Allison20dfc792014-06-16 20:44:29 -07003509
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003510// Instruction to check how two inputs compare to each other.
3511// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3512class HCompare : public HBinaryOperation {
3513 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003514 // Note that `comparison_type` is the type of comparison performed
3515 // between the comparison's inputs, not the type of the instantiated
3516 // HCompare instruction (which is always Primitive::kPrimInt).
3517 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003518 HInstruction* first,
3519 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003520 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003521 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003522 : HBinaryOperation(Primitive::kPrimInt,
3523 first,
3524 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003525 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003526 dex_pc) {
3527 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003528 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3529 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003530 }
3531
Roland Levillain9867bc72015-08-05 10:21:34 +01003532 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003533 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3534
3535 template <typename T>
3536 int32_t ComputeFP(T x, T y) const {
3537 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3538 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3539 // Handle the bias.
3540 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3541 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003542
Roland Levillain9867bc72015-08-05 10:21:34 +01003543 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003544 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3545 // reach this code path when processing a freshly built HIR
3546 // graph. However HCompare integer instructions can be synthesized
3547 // by the instruction simplifier to implement IntegerCompare and
3548 // IntegerSignum intrinsics, so we have to handle this case.
3549 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003550 }
3551 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003552 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3553 }
3554 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3555 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3556 }
3557 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3558 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003559 }
3560
Calin Juravleddb7df22014-11-25 20:56:51 +00003561 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003562 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003563 }
3564
Vladimir Markoa1de9182016-02-25 11:37:38 +00003565 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003566
Roland Levillain31dd3d62016-02-16 12:21:02 +00003567 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003568 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003569 bool IsGtBias() const {
3570 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003571 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003572 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003573
Roland Levillain1693a1f2016-03-15 14:57:31 +00003574 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3575 // Comparisons do not require a runtime call in any back end.
3576 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003577 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003578
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003579 DECLARE_INSTRUCTION(Compare);
3580
Roland Levillain31dd3d62016-02-16 12:21:02 +00003581 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003582 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3583 static constexpr size_t kFieldComparisonBiasSize =
3584 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3585 static constexpr size_t kNumberOfComparePackedBits =
3586 kFieldComparisonBias + kFieldComparisonBiasSize;
3587 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3588 using ComparisonBiasField =
3589 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3590
Roland Levillain31dd3d62016-02-16 12:21:02 +00003591 // Return an integer constant containing the result of a comparison evaluated at compile time.
3592 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3593 DCHECK(value == -1 || value == 0 || value == 1) << value;
3594 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3595 }
3596
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003597 private:
3598 DISALLOW_COPY_AND_ASSIGN(HCompare);
3599};
3600
David Brazdil6de19382016-01-08 17:37:10 +00003601class HNewInstance : public HExpression<2> {
3602 public:
3603 HNewInstance(HInstruction* cls,
3604 HCurrentMethod* current_method,
3605 uint32_t dex_pc,
3606 uint16_t type_index,
3607 const DexFile& dex_file,
3608 bool can_throw,
3609 bool finalizable,
3610 QuickEntrypointEnum entrypoint)
3611 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3612 type_index_(type_index),
3613 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003614 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003615 SetPackedFlag<kFlagCanThrow>(can_throw);
3616 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003617 SetRawInputAt(0, cls);
3618 SetRawInputAt(1, current_method);
3619 }
3620
3621 uint16_t GetTypeIndex() const { return type_index_; }
3622 const DexFile& GetDexFile() const { return dex_file_; }
3623
3624 // Calls runtime so needs an environment.
3625 bool NeedsEnvironment() const OVERRIDE { return true; }
3626
3627 // It may throw when called on type that's not instantiable/accessible.
3628 // It can throw OOME.
3629 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003630 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>() || true; }
David Brazdil6de19382016-01-08 17:37:10 +00003631
Vladimir Markoa1de9182016-02-25 11:37:38 +00003632 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003633
3634 bool CanBeNull() const OVERRIDE { return false; }
3635
3636 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3637
3638 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3639 entrypoint_ = entrypoint;
3640 }
3641
3642 bool IsStringAlloc() const;
3643
3644 DECLARE_INSTRUCTION(NewInstance);
3645
3646 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003647 static constexpr size_t kFlagCanThrow = kNumberOfExpressionPackedBits;
3648 static constexpr size_t kFlagFinalizable = kFlagCanThrow + 1;
3649 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3650 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3651 "Too many packed fields.");
3652
David Brazdil6de19382016-01-08 17:37:10 +00003653 const uint16_t type_index_;
3654 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003655 QuickEntrypointEnum entrypoint_;
3656
3657 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3658};
3659
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003660enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003661#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3662 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003663#include "intrinsics_list.h"
3664 kNone,
3665 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3666#undef INTRINSICS_LIST
3667#undef OPTIMIZING_INTRINSICS
3668};
3669std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3670
Agi Csaki05f20562015-08-19 14:58:14 -07003671enum IntrinsicNeedsEnvironmentOrCache {
3672 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3673 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003674};
3675
Aart Bik5d75afe2015-12-14 11:57:01 -08003676enum IntrinsicSideEffects {
3677 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3678 kReadSideEffects, // Intrinsic may read heap memory.
3679 kWriteSideEffects, // Intrinsic may write heap memory.
3680 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3681};
3682
3683enum IntrinsicExceptions {
3684 kNoThrow, // Intrinsic does not throw any exceptions.
3685 kCanThrow // Intrinsic may throw exceptions.
3686};
3687
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003688class HInvoke : public HInstruction {
3689 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003690 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003691
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003692 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003693
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003694 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003695 SetRawInputAt(index, argument);
3696 }
3697
Roland Levillain3e3d7332015-04-28 11:00:54 +01003698 // Return the number of arguments. This number can be lower than
3699 // the number of inputs returned by InputCount(), as some invoke
3700 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3701 // inputs at the end of their list of inputs.
3702 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3703
Vladimir Markoa1de9182016-02-25 11:37:38 +00003704 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003705
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003706 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003707 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003708
Vladimir Markoa1de9182016-02-25 11:37:38 +00003709 InvokeType GetOriginalInvokeType() const {
3710 return GetPackedField<OriginalInvokeTypeField>();
3711 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003712
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003713 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003714 return intrinsic_;
3715 }
3716
Aart Bik5d75afe2015-12-14 11:57:01 -08003717 void SetIntrinsic(Intrinsics intrinsic,
3718 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3719 IntrinsicSideEffects side_effects,
3720 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003721
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003722 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003723 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003724 }
3725
Vladimir Markoa1de9182016-02-25 11:37:38 +00003726 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003727
3728 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3729
3730 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3731 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3732 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003733
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003734 uint32_t* GetIntrinsicOptimizations() {
3735 return &intrinsic_optimizations_;
3736 }
3737
3738 const uint32_t* GetIntrinsicOptimizations() const {
3739 return &intrinsic_optimizations_;
3740 }
3741
3742 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3743
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003744 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003745
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003746 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003747 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3748 static constexpr size_t kFieldOriginalInvokeTypeSize =
3749 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3750 static constexpr size_t kFieldReturnType =
3751 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3752 static constexpr size_t kFieldReturnTypeSize =
3753 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3754 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3755 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3756 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3757 using OriginalInvokeTypeField =
3758 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3759 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3760
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003761 HInvoke(ArenaAllocator* arena,
3762 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003763 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003764 Primitive::Type return_type,
3765 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003766 uint32_t dex_method_index,
3767 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003768 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003769 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003770 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003771 inputs_(number_of_arguments + number_of_other_inputs,
3772 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003773 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003774 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003775 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003776 SetPackedField<ReturnTypeField>(return_type);
3777 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3778 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003779 }
3780
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003781 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003782 return inputs_[index];
3783 }
3784
David Brazdil1abb4192015-02-17 18:33:36 +00003785 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003786 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003787 }
3788
Vladimir Markoa1de9182016-02-25 11:37:38 +00003789 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003790
Roland Levillain3e3d7332015-04-28 11:00:54 +01003791 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003792 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003793 const uint32_t dex_method_index_;
3794 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003795
3796 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3797 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003798
3799 private:
3800 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3801};
3802
Calin Juravle175dc732015-08-25 15:42:32 +01003803class HInvokeUnresolved : public HInvoke {
3804 public:
3805 HInvokeUnresolved(ArenaAllocator* arena,
3806 uint32_t number_of_arguments,
3807 Primitive::Type return_type,
3808 uint32_t dex_pc,
3809 uint32_t dex_method_index,
3810 InvokeType invoke_type)
3811 : HInvoke(arena,
3812 number_of_arguments,
3813 0u /* number_of_other_inputs */,
3814 return_type,
3815 dex_pc,
3816 dex_method_index,
3817 invoke_type) {
3818 }
3819
3820 DECLARE_INSTRUCTION(InvokeUnresolved);
3821
3822 private:
3823 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3824};
3825
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003826class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003827 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003828 // Requirements of this method call regarding the class
3829 // initialization (clinit) check of its declaring class.
3830 enum class ClinitCheckRequirement {
3831 kNone, // Class already initialized.
3832 kExplicit, // Static call having explicit clinit check as last input.
3833 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003834 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003835 };
3836
Vladimir Marko58155012015-08-19 12:49:41 +00003837 // Determines how to load the target ArtMethod*.
3838 enum class MethodLoadKind {
3839 // Use a String init ArtMethod* loaded from Thread entrypoints.
3840 kStringInit,
3841
3842 // Use the method's own ArtMethod* loaded by the register allocator.
3843 kRecursive,
3844
3845 // Use ArtMethod* at a known address, embed the direct address in the code.
3846 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3847 kDirectAddress,
3848
3849 // Use ArtMethod* at an address that will be known at link time, embed the direct
3850 // address in the code. If the image is relocatable, emit .patch_oat entry.
3851 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3852 // the image relocatable or not.
3853 kDirectAddressWithFixup,
3854
Vladimir Markoa1de9182016-02-25 11:37:38 +00003855 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003856 // Used when we need to use the dex cache, for example for invoke-static that
3857 // may cause class initialization (the entry may point to a resolution method),
3858 // and we know that we can access the dex cache arrays using a PC-relative load.
3859 kDexCachePcRelative,
3860
3861 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3862 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3863 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3864 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3865 kDexCacheViaMethod,
3866 };
3867
3868 // Determines the location of the code pointer.
3869 enum class CodePtrLocation {
3870 // Recursive call, use local PC-relative call instruction.
3871 kCallSelf,
3872
3873 // Use PC-relative call instruction patched at link time.
3874 // Used for calls within an oat file, boot->boot or app->app.
3875 kCallPCRelative,
3876
3877 // Call to a known target address, embed the direct address in code.
3878 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3879 kCallDirect,
3880
3881 // Call to a target address that will be known at link time, embed the direct
3882 // address in code. If the image is relocatable, emit .patch_oat entry.
3883 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3884 // the image relocatable or not.
3885 kCallDirectWithFixup,
3886
3887 // Use code pointer from the ArtMethod*.
3888 // Used when we don't know the target code. This is also the last-resort-kind used when
3889 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3890 kCallArtMethod,
3891 };
3892
3893 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003894 MethodLoadKind method_load_kind;
3895 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003896 // The method load data holds
3897 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3898 // Note that there are multiple string init methods, each having its own offset.
3899 // - the method address for kDirectAddress
3900 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003901 uint64_t method_load_data;
3902 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003903 };
3904
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003905 HInvokeStaticOrDirect(ArenaAllocator* arena,
3906 uint32_t number_of_arguments,
3907 Primitive::Type return_type,
3908 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003909 uint32_t method_index,
3910 MethodReference target_method,
3911 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003912 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003913 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003914 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003915 : HInvoke(arena,
3916 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003917 // There is potentially one extra argument for the HCurrentMethod node, and
3918 // potentially one other if the clinit check is explicit, and potentially
3919 // one other if the method is a string factory.
3920 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003921 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003922 return_type,
3923 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003924 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003925 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003926 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003927 dispatch_info_(dispatch_info) {
3928 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3929 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3930 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003931
Vladimir Markodc151b22015-10-15 18:02:30 +01003932 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003933 bool had_current_method_input = HasCurrentMethodInput();
3934 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3935
3936 // Using the current method is the default and once we find a better
3937 // method load kind, we should not go back to using the current method.
3938 DCHECK(had_current_method_input || !needs_current_method_input);
3939
3940 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003941 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3942 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003943 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003944 dispatch_info_ = dispatch_info;
3945 }
3946
Vladimir Markoc53c0792015-11-19 15:48:33 +00003947 void AddSpecialInput(HInstruction* input) {
3948 // We allow only one special input.
3949 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3950 DCHECK(InputCount() == GetSpecialInputIndex() ||
3951 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3952 InsertInputAt(GetSpecialInputIndex(), input);
3953 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003954
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003955 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003956 // We access the method via the dex cache so we can't do an implicit null check.
3957 // TODO: for intrinsics we can generate implicit null checks.
3958 return false;
3959 }
3960
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003961 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003962 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003963 }
3964
Vladimir Markoc53c0792015-11-19 15:48:33 +00003965 // Get the index of the special input, if any.
3966 //
David Brazdil6de19382016-01-08 17:37:10 +00003967 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3968 // method pointer; otherwise there may be one platform-specific special input,
3969 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003970 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003971 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003972
Vladimir Markoa1de9182016-02-25 11:37:38 +00003973 InvokeType GetOptimizedInvokeType() const {
3974 return GetPackedField<OptimizedInvokeTypeField>();
3975 }
3976
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003977 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003978 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003979 }
3980
Vladimir Marko58155012015-08-19 12:49:41 +00003981 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3982 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3983 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003984 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003985 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003986 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003987 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003988 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3989 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003990 bool HasCurrentMethodInput() const {
3991 // This function can be called only after the invoke has been fully initialized by the builder.
3992 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003993 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003994 return true;
3995 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003996 DCHECK(InputCount() == GetSpecialInputIndex() ||
3997 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003998 return false;
3999 }
4000 }
Vladimir Marko58155012015-08-19 12:49:41 +00004001 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
4002 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004003 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00004004
4005 int32_t GetStringInitOffset() const {
4006 DCHECK(IsStringInit());
4007 return dispatch_info_.method_load_data;
4008 }
4009
4010 uint64_t GetMethodAddress() const {
4011 DCHECK(HasMethodAddress());
4012 return dispatch_info_.method_load_data;
4013 }
4014
4015 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004016 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004017 return dispatch_info_.method_load_data;
4018 }
4019
4020 uint64_t GetDirectCodePtr() const {
4021 DCHECK(HasDirectCodePtr());
4022 return dispatch_info_.direct_code_ptr;
4023 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004024
Vladimir Markoa1de9182016-02-25 11:37:38 +00004025 ClinitCheckRequirement GetClinitCheckRequirement() const {
4026 return GetPackedField<ClinitCheckRequirementField>();
4027 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004028
Roland Levillain4c0eb422015-04-24 16:43:49 +01004029 // Is this instruction a call to a static method?
4030 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004031 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004032 }
4033
Vladimir Markofbb184a2015-11-13 14:47:00 +00004034 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4035 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4036 // instruction; only relevant for static calls with explicit clinit check.
4037 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004038 DCHECK(IsStaticWithExplicitClinitCheck());
4039 size_t last_input_index = InputCount() - 1;
4040 HInstruction* last_input = InputAt(last_input_index);
4041 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004042 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004043 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004044 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004045 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004046 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004047 }
4048
4049 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004050 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004051 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004052 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004053 }
4054
4055 // Is this a call to a static method whose declaring class has an
4056 // implicit intialization check requirement?
4057 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004058 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004059 }
4060
Vladimir Markob554b5a2015-11-06 12:57:55 +00004061 // Does this method load kind need the current method as an input?
4062 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4063 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4064 }
4065
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004066 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004067
Roland Levillain4c0eb422015-04-24 16:43:49 +01004068 protected:
4069 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
4070 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
4071 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
4072 HInstruction* input = input_record.GetInstruction();
4073 // `input` is the last input of a static invoke marked as having
4074 // an explicit clinit check. It must either be:
4075 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4076 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4077 DCHECK(input != nullptr);
4078 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
4079 }
4080 return input_record;
4081 }
4082
Vladimir Markoc53c0792015-11-19 15:48:33 +00004083 void InsertInputAt(size_t index, HInstruction* input);
4084 void RemoveInputAt(size_t index);
4085
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004086 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004087 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4088 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4089 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4090 static constexpr size_t kFieldClinitCheckRequirement =
4091 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4092 static constexpr size_t kFieldClinitCheckRequirementSize =
4093 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4094 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4095 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4096 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4097 "Too many packed fields.");
4098 using OptimizedInvokeTypeField =
4099 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4100 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4101 kFieldClinitCheckRequirement,
4102 kFieldClinitCheckRequirementSize>;
4103
Vladimir Marko58155012015-08-19 12:49:41 +00004104 // The target method may refer to different dex file or method index than the original
4105 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4106 // in derived class to increase visibility.
4107 MethodReference target_method_;
4108 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004109
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004110 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004111};
Vladimir Markof64242a2015-12-01 14:58:23 +00004112std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004113std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004114
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004115class HInvokeVirtual : public HInvoke {
4116 public:
4117 HInvokeVirtual(ArenaAllocator* arena,
4118 uint32_t number_of_arguments,
4119 Primitive::Type return_type,
4120 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004121 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004122 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004123 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004124 vtable_index_(vtable_index) {}
4125
Calin Juravle641547a2015-04-21 22:08:51 +01004126 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004127 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004128 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004129 }
4130
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004131 uint32_t GetVTableIndex() const { return vtable_index_; }
4132
4133 DECLARE_INSTRUCTION(InvokeVirtual);
4134
4135 private:
4136 const uint32_t vtable_index_;
4137
4138 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4139};
4140
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004141class HInvokeInterface : public HInvoke {
4142 public:
4143 HInvokeInterface(ArenaAllocator* arena,
4144 uint32_t number_of_arguments,
4145 Primitive::Type return_type,
4146 uint32_t dex_pc,
4147 uint32_t dex_method_index,
4148 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004149 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004150 imt_index_(imt_index) {}
4151
Calin Juravle641547a2015-04-21 22:08:51 +01004152 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004153 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004154 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004155 }
4156
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004157 uint32_t GetImtIndex() const { return imt_index_; }
4158 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4159
4160 DECLARE_INSTRUCTION(InvokeInterface);
4161
4162 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004163 const uint32_t imt_index_;
4164
4165 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4166};
4167
Roland Levillain88cb1752014-10-20 16:36:47 +01004168class HNeg : public HUnaryOperation {
4169 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004170 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004171 : HUnaryOperation(result_type, input, dex_pc) {
4172 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4173 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004174
Roland Levillain9867bc72015-08-05 10:21:34 +01004175 template <typename T> T Compute(T x) const { return -x; }
4176
4177 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004178 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004179 }
4180 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004181 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004182 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004183 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4184 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4185 }
4186 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4187 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4188 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004189
Roland Levillain88cb1752014-10-20 16:36:47 +01004190 DECLARE_INSTRUCTION(Neg);
4191
4192 private:
4193 DISALLOW_COPY_AND_ASSIGN(HNeg);
4194};
4195
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004196class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004197 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004198 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004199 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004200 uint32_t dex_pc,
4201 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004202 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004203 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004204 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004205 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004206 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004207 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004208 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004209 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004210 }
4211
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004212 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004213 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004214
4215 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004216 bool NeedsEnvironment() const OVERRIDE { return true; }
4217
Mingyao Yang0c365e62015-03-31 15:09:29 -07004218 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4219 bool CanThrow() const OVERRIDE { return true; }
4220
Calin Juravle10e244f2015-01-26 18:54:32 +00004221 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004222
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004223 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4224
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004225 DECLARE_INSTRUCTION(NewArray);
4226
4227 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004228 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004229 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004230 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004231
4232 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4233};
4234
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004235class HAdd : public HBinaryOperation {
4236 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004237 HAdd(Primitive::Type result_type,
4238 HInstruction* left,
4239 HInstruction* right,
4240 uint32_t dex_pc = kNoDexPc)
4241 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004242
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004243 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004244
Roland Levillain9867bc72015-08-05 10:21:34 +01004245 template <typename T> T Compute(T x, T y) const { return x + y; }
4246
4247 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004248 return GetBlock()->GetGraph()->GetIntConstant(
4249 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004250 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004251 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004252 return GetBlock()->GetGraph()->GetLongConstant(
4253 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004254 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004255 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4256 return GetBlock()->GetGraph()->GetFloatConstant(
4257 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4258 }
4259 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4260 return GetBlock()->GetGraph()->GetDoubleConstant(
4261 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4262 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004263
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004264 DECLARE_INSTRUCTION(Add);
4265
4266 private:
4267 DISALLOW_COPY_AND_ASSIGN(HAdd);
4268};
4269
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004270class HSub : public HBinaryOperation {
4271 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004272 HSub(Primitive::Type result_type,
4273 HInstruction* left,
4274 HInstruction* right,
4275 uint32_t dex_pc = kNoDexPc)
4276 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004277
Roland Levillain9867bc72015-08-05 10:21:34 +01004278 template <typename T> T Compute(T x, T y) const { return x - y; }
4279
4280 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004281 return GetBlock()->GetGraph()->GetIntConstant(
4282 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004283 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004284 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004285 return GetBlock()->GetGraph()->GetLongConstant(
4286 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004287 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004288 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4289 return GetBlock()->GetGraph()->GetFloatConstant(
4290 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4291 }
4292 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4293 return GetBlock()->GetGraph()->GetDoubleConstant(
4294 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4295 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004296
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004297 DECLARE_INSTRUCTION(Sub);
4298
4299 private:
4300 DISALLOW_COPY_AND_ASSIGN(HSub);
4301};
4302
Calin Juravle34bacdf2014-10-07 20:23:36 +01004303class HMul : public HBinaryOperation {
4304 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004305 HMul(Primitive::Type result_type,
4306 HInstruction* left,
4307 HInstruction* right,
4308 uint32_t dex_pc = kNoDexPc)
4309 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004310
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004311 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004312
Roland Levillain9867bc72015-08-05 10:21:34 +01004313 template <typename T> T Compute(T x, T y) const { return x * y; }
4314
4315 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004316 return GetBlock()->GetGraph()->GetIntConstant(
4317 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004318 }
4319 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004320 return GetBlock()->GetGraph()->GetLongConstant(
4321 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004322 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004323 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4324 return GetBlock()->GetGraph()->GetFloatConstant(
4325 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4326 }
4327 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4328 return GetBlock()->GetGraph()->GetDoubleConstant(
4329 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4330 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004331
4332 DECLARE_INSTRUCTION(Mul);
4333
4334 private:
4335 DISALLOW_COPY_AND_ASSIGN(HMul);
4336};
4337
Calin Juravle7c4954d2014-10-28 16:57:40 +00004338class HDiv : public HBinaryOperation {
4339 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004340 HDiv(Primitive::Type result_type,
4341 HInstruction* left,
4342 HInstruction* right,
4343 uint32_t dex_pc)
4344 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004345
Roland Levillain9867bc72015-08-05 10:21:34 +01004346 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004347 T ComputeIntegral(T x, T y) const {
4348 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004349 // Our graph structure ensures we never have 0 for `y` during
4350 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004351 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004352 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004353 return (y == -1) ? -x : x / y;
4354 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004355
Roland Levillain31dd3d62016-02-16 12:21:02 +00004356 template <typename T>
4357 T ComputeFP(T x, T y) const {
4358 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4359 return x / y;
4360 }
4361
Roland Levillain9867bc72015-08-05 10:21:34 +01004362 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004363 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004364 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004365 }
4366 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004367 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004368 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4369 }
4370 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4371 return GetBlock()->GetGraph()->GetFloatConstant(
4372 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4373 }
4374 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4375 return GetBlock()->GetGraph()->GetDoubleConstant(
4376 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004377 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004378
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004379 static SideEffects SideEffectsForArchRuntimeCalls() {
4380 // The generated code can use a runtime call.
4381 return SideEffects::CanTriggerGC();
4382 }
4383
Calin Juravle7c4954d2014-10-28 16:57:40 +00004384 DECLARE_INSTRUCTION(Div);
4385
4386 private:
4387 DISALLOW_COPY_AND_ASSIGN(HDiv);
4388};
4389
Calin Juravlebacfec32014-11-14 15:54:36 +00004390class HRem : public HBinaryOperation {
4391 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004392 HRem(Primitive::Type result_type,
4393 HInstruction* left,
4394 HInstruction* right,
4395 uint32_t dex_pc)
4396 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004397
Roland Levillain9867bc72015-08-05 10:21:34 +01004398 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004399 T ComputeIntegral(T x, T y) const {
4400 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004401 // Our graph structure ensures we never have 0 for `y` during
4402 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004403 DCHECK_NE(y, 0);
4404 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4405 return (y == -1) ? 0 : x % y;
4406 }
4407
Roland Levillain31dd3d62016-02-16 12:21:02 +00004408 template <typename T>
4409 T ComputeFP(T x, T y) const {
4410 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4411 return std::fmod(x, y);
4412 }
4413
Roland Levillain9867bc72015-08-05 10:21:34 +01004414 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004415 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004416 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004417 }
4418 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004419 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004420 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004421 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004422 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4423 return GetBlock()->GetGraph()->GetFloatConstant(
4424 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4425 }
4426 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4427 return GetBlock()->GetGraph()->GetDoubleConstant(
4428 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4429 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004430
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004431 static SideEffects SideEffectsForArchRuntimeCalls() {
4432 return SideEffects::CanTriggerGC();
4433 }
4434
Calin Juravlebacfec32014-11-14 15:54:36 +00004435 DECLARE_INSTRUCTION(Rem);
4436
4437 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004438 DISALLOW_COPY_AND_ASSIGN(HRem);
4439};
4440
Calin Juravled0d48522014-11-04 16:40:20 +00004441class HDivZeroCheck : public HExpression<1> {
4442 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004443 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4444 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004445 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004446 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004447 SetRawInputAt(0, value);
4448 }
4449
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004450 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4451
Calin Juravled0d48522014-11-04 16:40:20 +00004452 bool CanBeMoved() const OVERRIDE { return true; }
4453
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004454 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004455 return true;
4456 }
4457
4458 bool NeedsEnvironment() const OVERRIDE { return true; }
4459 bool CanThrow() const OVERRIDE { return true; }
4460
Calin Juravled0d48522014-11-04 16:40:20 +00004461 DECLARE_INSTRUCTION(DivZeroCheck);
4462
4463 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004464 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4465};
4466
Calin Juravle9aec02f2014-11-18 23:06:35 +00004467class HShl : public HBinaryOperation {
4468 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004469 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004470 HInstruction* value,
4471 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004472 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004473 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4474 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4475 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004476 }
4477
Roland Levillain5b5b9312016-03-22 14:57:31 +00004478 template <typename T>
4479 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4480 return value << (distance & max_shift_distance);
4481 }
4482
4483 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004484 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004485 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004486 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004487 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004488 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004489 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004490 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004491 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4492 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4493 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4494 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004495 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004496 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4497 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004498 LOG(FATAL) << DebugName() << " is not defined for float values";
4499 UNREACHABLE();
4500 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004501 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4502 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004503 LOG(FATAL) << DebugName() << " is not defined for double values";
4504 UNREACHABLE();
4505 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004506
4507 DECLARE_INSTRUCTION(Shl);
4508
4509 private:
4510 DISALLOW_COPY_AND_ASSIGN(HShl);
4511};
4512
4513class HShr : public HBinaryOperation {
4514 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004515 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004516 HInstruction* value,
4517 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004518 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004519 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4520 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4521 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004522 }
4523
Roland Levillain5b5b9312016-03-22 14:57:31 +00004524 template <typename T>
4525 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4526 return value >> (distance & max_shift_distance);
4527 }
4528
4529 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004530 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004531 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004532 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004533 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004534 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004535 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004536 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004537 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4538 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4539 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4540 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004541 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004542 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4543 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004544 LOG(FATAL) << DebugName() << " is not defined for float values";
4545 UNREACHABLE();
4546 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004547 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4548 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004549 LOG(FATAL) << DebugName() << " is not defined for double values";
4550 UNREACHABLE();
4551 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004552
4553 DECLARE_INSTRUCTION(Shr);
4554
4555 private:
4556 DISALLOW_COPY_AND_ASSIGN(HShr);
4557};
4558
4559class HUShr : public HBinaryOperation {
4560 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004561 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004562 HInstruction* value,
4563 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004564 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004565 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4566 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4567 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004568 }
4569
Roland Levillain5b5b9312016-03-22 14:57:31 +00004570 template <typename T>
4571 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4572 typedef typename std::make_unsigned<T>::type V;
4573 V ux = static_cast<V>(value);
4574 return static_cast<T>(ux >> (distance & max_shift_distance));
4575 }
4576
4577 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004578 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004579 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004580 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004581 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004582 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004583 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004584 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004585 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4586 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4587 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4588 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004589 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004590 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4591 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004592 LOG(FATAL) << DebugName() << " is not defined for float values";
4593 UNREACHABLE();
4594 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004595 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4596 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004597 LOG(FATAL) << DebugName() << " is not defined for double values";
4598 UNREACHABLE();
4599 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004600
4601 DECLARE_INSTRUCTION(UShr);
4602
4603 private:
4604 DISALLOW_COPY_AND_ASSIGN(HUShr);
4605};
4606
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004607class HAnd : public HBinaryOperation {
4608 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004609 HAnd(Primitive::Type result_type,
4610 HInstruction* left,
4611 HInstruction* right,
4612 uint32_t dex_pc = kNoDexPc)
4613 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004614
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004615 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004616
Roland Levillaine53bd812016-02-24 14:54:18 +00004617 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004618
4619 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004620 return GetBlock()->GetGraph()->GetIntConstant(
4621 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004622 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004623 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004624 return GetBlock()->GetGraph()->GetLongConstant(
4625 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004626 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004627 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4628 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4629 LOG(FATAL) << DebugName() << " is not defined for float values";
4630 UNREACHABLE();
4631 }
4632 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4633 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4634 LOG(FATAL) << DebugName() << " is not defined for double values";
4635 UNREACHABLE();
4636 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004637
4638 DECLARE_INSTRUCTION(And);
4639
4640 private:
4641 DISALLOW_COPY_AND_ASSIGN(HAnd);
4642};
4643
4644class HOr : public HBinaryOperation {
4645 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004646 HOr(Primitive::Type result_type,
4647 HInstruction* left,
4648 HInstruction* right,
4649 uint32_t dex_pc = kNoDexPc)
4650 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004651
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004652 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004653
Roland Levillaine53bd812016-02-24 14:54:18 +00004654 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004655
4656 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004657 return GetBlock()->GetGraph()->GetIntConstant(
4658 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004659 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004660 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004661 return GetBlock()->GetGraph()->GetLongConstant(
4662 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004663 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004664 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4665 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4666 LOG(FATAL) << DebugName() << " is not defined for float values";
4667 UNREACHABLE();
4668 }
4669 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4670 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4671 LOG(FATAL) << DebugName() << " is not defined for double values";
4672 UNREACHABLE();
4673 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004674
4675 DECLARE_INSTRUCTION(Or);
4676
4677 private:
4678 DISALLOW_COPY_AND_ASSIGN(HOr);
4679};
4680
4681class HXor : public HBinaryOperation {
4682 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004683 HXor(Primitive::Type result_type,
4684 HInstruction* left,
4685 HInstruction* right,
4686 uint32_t dex_pc = kNoDexPc)
4687 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004688
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004689 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004690
Roland Levillaine53bd812016-02-24 14:54:18 +00004691 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004692
4693 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004694 return GetBlock()->GetGraph()->GetIntConstant(
4695 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004696 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004697 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004698 return GetBlock()->GetGraph()->GetLongConstant(
4699 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004700 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004701 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4702 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4703 LOG(FATAL) << DebugName() << " is not defined for float values";
4704 UNREACHABLE();
4705 }
4706 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4707 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4708 LOG(FATAL) << DebugName() << " is not defined for double values";
4709 UNREACHABLE();
4710 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004711
4712 DECLARE_INSTRUCTION(Xor);
4713
4714 private:
4715 DISALLOW_COPY_AND_ASSIGN(HXor);
4716};
4717
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004718class HRor : public HBinaryOperation {
4719 public:
4720 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004721 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004722 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4723 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004724 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004725
Roland Levillain5b5b9312016-03-22 14:57:31 +00004726 template <typename T>
4727 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4728 typedef typename std::make_unsigned<T>::type V;
4729 V ux = static_cast<V>(value);
4730 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004731 return static_cast<T>(ux);
4732 } else {
4733 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004734 return static_cast<T>(ux >> (distance & max_shift_value)) |
4735 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004736 }
4737 }
4738
Roland Levillain5b5b9312016-03-22 14:57:31 +00004739 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004740 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004741 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004742 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004743 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004744 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004745 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004746 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004747 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4748 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4749 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4750 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004751 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004752 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4753 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004754 LOG(FATAL) << DebugName() << " is not defined for float values";
4755 UNREACHABLE();
4756 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004757 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4758 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004759 LOG(FATAL) << DebugName() << " is not defined for double values";
4760 UNREACHABLE();
4761 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004762
4763 DECLARE_INSTRUCTION(Ror);
4764
4765 private:
4766 DISALLOW_COPY_AND_ASSIGN(HRor);
4767};
4768
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004769// The value of a parameter in this method. Its location depends on
4770// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004771class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004772 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004773 HParameterValue(const DexFile& dex_file,
4774 uint16_t type_index,
4775 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004776 Primitive::Type parameter_type,
4777 bool is_this = false)
4778 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004779 dex_file_(dex_file),
4780 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004781 index_(index) {
4782 SetPackedFlag<kFlagIsThis>(is_this);
4783 SetPackedFlag<kFlagCanBeNull>(!is_this);
4784 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004785
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004786 const DexFile& GetDexFile() const { return dex_file_; }
4787 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004788 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004789 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004790
Vladimir Markoa1de9182016-02-25 11:37:38 +00004791 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4792 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004793
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004794 DECLARE_INSTRUCTION(ParameterValue);
4795
4796 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004797 // Whether or not the parameter value corresponds to 'this' argument.
4798 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4799 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4800 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4801 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4802 "Too many packed fields.");
4803
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004804 const DexFile& dex_file_;
4805 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004806 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004807 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004808 const uint8_t index_;
4809
4810 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4811};
4812
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004813class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004814 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004815 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4816 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004817
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004818 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004819 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004820 return true;
4821 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004822
Roland Levillain9867bc72015-08-05 10:21:34 +01004823 template <typename T> T Compute(T x) const { return ~x; }
4824
4825 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004826 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004827 }
4828 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004829 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004830 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004831 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4832 LOG(FATAL) << DebugName() << " is not defined for float values";
4833 UNREACHABLE();
4834 }
4835 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4836 LOG(FATAL) << DebugName() << " is not defined for double values";
4837 UNREACHABLE();
4838 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004839
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004840 DECLARE_INSTRUCTION(Not);
4841
4842 private:
4843 DISALLOW_COPY_AND_ASSIGN(HNot);
4844};
4845
David Brazdil66d126e2015-04-03 16:02:44 +01004846class HBooleanNot : public HUnaryOperation {
4847 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004848 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4849 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004850
4851 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004852 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004853 return true;
4854 }
4855
Roland Levillain9867bc72015-08-05 10:21:34 +01004856 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004857 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004858 return !x;
4859 }
4860
Roland Levillain9867bc72015-08-05 10:21:34 +01004861 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004862 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004863 }
4864 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4865 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004866 UNREACHABLE();
4867 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004868 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4869 LOG(FATAL) << DebugName() << " is not defined for float values";
4870 UNREACHABLE();
4871 }
4872 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4873 LOG(FATAL) << DebugName() << " is not defined for double values";
4874 UNREACHABLE();
4875 }
David Brazdil66d126e2015-04-03 16:02:44 +01004876
4877 DECLARE_INSTRUCTION(BooleanNot);
4878
4879 private:
4880 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4881};
4882
Roland Levillaindff1f282014-11-05 14:15:05 +00004883class HTypeConversion : public HExpression<1> {
4884 public:
4885 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004886 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004887 : HExpression(result_type,
4888 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4889 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004890 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004891 // Invariant: We should never generate a conversion to a Boolean value.
4892 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004893 }
4894
4895 HInstruction* GetInput() const { return InputAt(0); }
4896 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4897 Primitive::Type GetResultType() const { return GetType(); }
4898
4899 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004900 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004901
Mark Mendelle82549b2015-05-06 10:55:34 -04004902 // Try to statically evaluate the conversion and return a HConstant
4903 // containing the result. If the input cannot be converted, return nullptr.
4904 HConstant* TryStaticEvaluation() const;
4905
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004906 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4907 Primitive::Type result_type) {
4908 // Some architectures may not require the 'GC' side effects, but at this point
4909 // in the compilation process we do not know what architecture we will
4910 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004911 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4912 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004913 return SideEffects::CanTriggerGC();
4914 }
4915 return SideEffects::None();
4916 }
4917
Roland Levillaindff1f282014-11-05 14:15:05 +00004918 DECLARE_INSTRUCTION(TypeConversion);
4919
4920 private:
4921 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4922};
4923
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004924static constexpr uint32_t kNoRegNumber = -1;
4925
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004926class HNullCheck : public HExpression<1> {
4927 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004928 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4929 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004930 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004931 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004932 SetRawInputAt(0, value);
4933 }
4934
Calin Juravle10e244f2015-01-26 18:54:32 +00004935 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004936 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004937 return true;
4938 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004939
Calin Juravle10e244f2015-01-26 18:54:32 +00004940 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004941
Calin Juravle10e244f2015-01-26 18:54:32 +00004942 bool CanThrow() const OVERRIDE { return true; }
4943
4944 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004945
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004946
4947 DECLARE_INSTRUCTION(NullCheck);
4948
4949 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004950 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4951};
4952
4953class FieldInfo : public ValueObject {
4954 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004955 FieldInfo(MemberOffset field_offset,
4956 Primitive::Type field_type,
4957 bool is_volatile,
4958 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004959 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004960 const DexFile& dex_file,
4961 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004962 : field_offset_(field_offset),
4963 field_type_(field_type),
4964 is_volatile_(is_volatile),
4965 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004966 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004967 dex_file_(dex_file),
4968 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004969
4970 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004971 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004972 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004973 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004974 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004975 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004976 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004977
4978 private:
4979 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004980 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004981 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004982 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004983 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004984 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004985 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004986};
4987
4988class HInstanceFieldGet : public HExpression<1> {
4989 public:
4990 HInstanceFieldGet(HInstruction* value,
4991 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004992 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004993 bool is_volatile,
4994 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004995 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004996 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004997 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004998 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004999 : HExpression(field_type,
5000 SideEffects::FieldReadOfType(field_type, is_volatile),
5001 dex_pc),
5002 field_info_(field_offset,
5003 field_type,
5004 is_volatile,
5005 field_idx,
5006 declaring_class_def_index,
5007 dex_file,
5008 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005009 SetRawInputAt(0, value);
5010 }
5011
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005012 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005013
5014 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5015 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
5016 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005017 }
5018
Calin Juravle641547a2015-04-21 22:08:51 +01005019 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5020 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005021 }
5022
5023 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005024 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5025 }
5026
Calin Juravle52c48962014-12-16 17:02:57 +00005027 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005028 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005029 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005030 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005031
5032 DECLARE_INSTRUCTION(InstanceFieldGet);
5033
5034 private:
5035 const FieldInfo field_info_;
5036
5037 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5038};
5039
5040class HInstanceFieldSet : public HTemplateInstruction<2> {
5041 public:
5042 HInstanceFieldSet(HInstruction* object,
5043 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005044 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005045 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005046 bool is_volatile,
5047 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005048 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005049 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005050 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005051 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005052 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5053 dex_pc),
5054 field_info_(field_offset,
5055 field_type,
5056 is_volatile,
5057 field_idx,
5058 declaring_class_def_index,
5059 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005060 dex_cache) {
5061 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005062 SetRawInputAt(0, object);
5063 SetRawInputAt(1, value);
5064 }
5065
Calin Juravle641547a2015-04-21 22:08:51 +01005066 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5067 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005068 }
5069
Calin Juravle52c48962014-12-16 17:02:57 +00005070 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005071 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005072 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005073 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005074 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005075 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5076 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005077
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005078 DECLARE_INSTRUCTION(InstanceFieldSet);
5079
5080 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005081 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5082 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5083 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5084 "Too many packed fields.");
5085
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005086 const FieldInfo field_info_;
5087
5088 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5089};
5090
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005091class HArrayGet : public HExpression<2> {
5092 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005093 HArrayGet(HInstruction* array,
5094 HInstruction* index,
5095 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005096 uint32_t dex_pc,
5097 SideEffects additional_side_effects = SideEffects::None())
5098 : HExpression(type,
5099 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00005100 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005101 SetRawInputAt(0, array);
5102 SetRawInputAt(1, index);
5103 }
5104
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005105 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005106 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005107 return true;
5108 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005109 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005110 // TODO: We can be smarter here.
5111 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5112 // which generates the implicit null check. There are cases when these can be removed
5113 // to produce better code. If we ever add optimizations to do so we should allow an
5114 // implicit check here (as long as the address falls in the first page).
5115 return false;
5116 }
5117
David Brazdil4833f5a2015-12-16 10:37:39 +00005118 bool IsEquivalentOf(HArrayGet* other) const {
5119 bool result = (GetDexPc() == other->GetDexPc());
5120 if (kIsDebugBuild && result) {
5121 DCHECK_EQ(GetBlock(), other->GetBlock());
5122 DCHECK_EQ(GetArray(), other->GetArray());
5123 DCHECK_EQ(GetIndex(), other->GetIndex());
5124 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005125 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005126 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005127 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5128 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005129 }
5130 }
5131 return result;
5132 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005133
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005134 HInstruction* GetArray() const { return InputAt(0); }
5135 HInstruction* GetIndex() const { return InputAt(1); }
5136
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005137 DECLARE_INSTRUCTION(ArrayGet);
5138
5139 private:
5140 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5141};
5142
5143class HArraySet : public HTemplateInstruction<3> {
5144 public:
5145 HArraySet(HInstruction* array,
5146 HInstruction* index,
5147 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005148 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005149 uint32_t dex_pc,
5150 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005151 : HTemplateInstruction(
5152 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005153 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
5154 additional_side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005155 dex_pc) {
5156 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5157 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5158 SetPackedFlag<kFlagValueCanBeNull>(true);
5159 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005160 SetRawInputAt(0, array);
5161 SetRawInputAt(1, index);
5162 SetRawInputAt(2, value);
5163 }
5164
Calin Juravle77520bc2015-01-12 18:45:46 +00005165 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005166 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005167 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005168 }
5169
Mingyao Yang81014cb2015-06-02 03:16:27 -07005170 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005171 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005172
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005173 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005174 // TODO: Same as for ArrayGet.
5175 return false;
5176 }
5177
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005178 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005179 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005180 }
5181
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005182 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005183 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005184 }
5185
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005186 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005187 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005188 }
5189
Vladimir Markoa1de9182016-02-25 11:37:38 +00005190 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5191 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5192 bool StaticTypeOfArrayIsObjectArray() const {
5193 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5194 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005195
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005196 HInstruction* GetArray() const { return InputAt(0); }
5197 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005198 HInstruction* GetValue() const { return InputAt(2); }
5199
5200 Primitive::Type GetComponentType() const {
5201 // The Dex format does not type floating point index operations. Since the
5202 // `expected_component_type_` is set during building and can therefore not
5203 // be correct, we also check what is the value type. If it is a floating
5204 // point type, we must use that type.
5205 Primitive::Type value_type = GetValue()->GetType();
5206 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5207 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005208 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005209 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005210
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005211 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005212 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005213 }
5214
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005215 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5216 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5217 }
5218
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005219 DECLARE_INSTRUCTION(ArraySet);
5220
5221 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005222 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5223 static constexpr size_t kFieldExpectedComponentTypeSize =
5224 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5225 static constexpr size_t kFlagNeedsTypeCheck =
5226 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5227 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005228 // Cached information for the reference_type_info_ so that codegen
5229 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005230 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5231 static constexpr size_t kNumberOfArraySetPackedBits =
5232 kFlagStaticTypeOfArrayIsObjectArray + 1;
5233 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5234 using ExpectedComponentTypeField =
5235 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005236
5237 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5238};
5239
5240class HArrayLength : public HExpression<1> {
5241 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005242 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005243 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005244 // Note that arrays do not change length, so the instruction does not
5245 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005246 SetRawInputAt(0, array);
5247 }
5248
Calin Juravle77520bc2015-01-12 18:45:46 +00005249 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005250 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005251 return true;
5252 }
Calin Juravle641547a2015-04-21 22:08:51 +01005253 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5254 return obj == InputAt(0);
5255 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005256
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005257 DECLARE_INSTRUCTION(ArrayLength);
5258
5259 private:
5260 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5261};
5262
5263class HBoundsCheck : public HExpression<2> {
5264 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005265 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5266 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005267 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005268 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005269 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005270 SetRawInputAt(0, index);
5271 SetRawInputAt(1, length);
5272 }
5273
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005274 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005275 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005276 return true;
5277 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005278
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005279 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005280
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005281 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005282
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005283 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005284
5285 DECLARE_INSTRUCTION(BoundsCheck);
5286
5287 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005288 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5289};
5290
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005291class HSuspendCheck : public HTemplateInstruction<0> {
5292 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005293 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005294 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005295
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005296 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005297 return true;
5298 }
5299
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005300 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5301 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005302
5303 DECLARE_INSTRUCTION(SuspendCheck);
5304
5305 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005306 // Only used for code generation, in order to share the same slow path between back edges
5307 // of a same loop.
5308 SlowPathCode* slow_path_;
5309
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005310 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5311};
5312
David Srbecky0cf44932015-12-09 14:09:59 +00005313// Pseudo-instruction which provides the native debugger with mapping information.
5314// It ensures that we can generate line number and local variables at this point.
5315class HNativeDebugInfo : public HTemplateInstruction<0> {
5316 public:
5317 explicit HNativeDebugInfo(uint32_t dex_pc)
5318 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5319
5320 bool NeedsEnvironment() const OVERRIDE {
5321 return true;
5322 }
5323
5324 DECLARE_INSTRUCTION(NativeDebugInfo);
5325
5326 private:
5327 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5328};
5329
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005330/**
5331 * Instruction to load a Class object.
5332 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005333class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005334 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005335 HLoadClass(HCurrentMethod* current_method,
5336 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005337 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005338 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005339 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005340 bool needs_access_check,
5341 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005342 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005343 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005344 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005345 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005346 // Referrers class should not need access check. We never inline unverified
5347 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005348 DCHECK(!is_referrers_class || !needs_access_check);
5349
5350 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5351 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5352 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5353 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005354 SetRawInputAt(0, current_method);
5355 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005356
5357 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005358
5359 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005360 // Note that we don't need to test for generate_clinit_check_.
5361 // Whether or not we need to generate the clinit check is processed in
5362 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005363 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005364 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005365 }
5366
5367 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5368
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005369 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005370 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005371
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005372 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005373 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005374 }
5375
Calin Juravle0ba218d2015-05-19 18:46:01 +01005376 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005377 // The entrypoint the code generator is going to call does not do
5378 // clinit of the class.
5379 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005380 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005381 }
5382
5383 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005384 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005385 (!IsReferrersClass() && !IsInDexCache()) ||
5386 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005387 }
5388
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005389
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005390 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005391 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005392 }
5393
Calin Juravleacf735c2015-02-12 15:25:22 +00005394 ReferenceTypeInfo GetLoadedClassRTI() {
5395 return loaded_class_rti_;
5396 }
5397
5398 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5399 // Make sure we only set exact types (the loaded class should never be merged).
5400 DCHECK(rti.IsExact());
5401 loaded_class_rti_ = rti;
5402 }
5403
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005404 const DexFile& GetDexFile() { return dex_file_; }
5405
Vladimir Markoa1de9182016-02-25 11:37:38 +00005406 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005407
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005408 static SideEffects SideEffectsForArchRuntimeCalls() {
5409 return SideEffects::CanTriggerGC();
5410 }
5411
Vladimir Markoa1de9182016-02-25 11:37:38 +00005412 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5413 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5414 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5415 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005416
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005417 DECLARE_INSTRUCTION(LoadClass);
5418
5419 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005420 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5421 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5422 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005423 // Whether this instruction must generate the initialization check.
5424 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005425 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5426 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5427 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5428
5429 const uint16_t type_index_;
5430 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005431
Calin Juravleacf735c2015-02-12 15:25:22 +00005432 ReferenceTypeInfo loaded_class_rti_;
5433
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005434 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5435};
5436
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005437class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005438 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005439 // Determines how to load the String.
5440 enum class LoadKind {
5441 // Use boot image String* address that will be known at link time.
5442 // Used for boot image strings referenced by boot image code in non-PIC mode.
5443 kBootImageLinkTimeAddress,
5444
5445 // Use PC-relative boot image String* address that will be known at link time.
5446 // Used for boot image strings referenced by boot image code in PIC mode.
5447 kBootImageLinkTimePcRelative,
5448
5449 // Use a known boot image String* address, embedded in the code by the codegen.
5450 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5451 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5452 // GetIncludePatchInformation().
5453 kBootImageAddress,
5454
5455 // Load from the resolved strings array at an absolute address.
5456 // Used for strings outside the boot image referenced by JIT-compiled code.
5457 kDexCacheAddress,
5458
5459 // Load from resolved strings array in the dex cache using a PC-relative load.
5460 // Used for strings outside boot image when we know that we can access
5461 // the dex cache arrays using a PC-relative load.
5462 kDexCachePcRelative,
5463
5464 // Load from resolved strings array accessed through the class loaded from
5465 // the compiled method's own ArtMethod*. This is the default access type when
5466 // all other types are unavailable.
5467 kDexCacheViaMethod,
5468
5469 kLast = kDexCacheViaMethod
5470 };
5471
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005472 HLoadString(HCurrentMethod* current_method,
5473 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005474 const DexFile& dex_file,
5475 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005476 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005477 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005478 SetPackedFlag<kFlagIsInDexCache>(false);
5479 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5480 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005481 SetRawInputAt(0, current_method);
5482 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005483
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005484 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5485 DCHECK(HasAddress(load_kind));
5486 load_data_.address = address;
5487 SetLoadKindInternal(load_kind);
5488 }
5489
5490 void SetLoadKindWithStringReference(LoadKind load_kind,
5491 const DexFile& dex_file,
5492 uint32_t string_index) {
5493 DCHECK(HasStringReference(load_kind));
5494 load_data_.ref.dex_file = &dex_file;
5495 string_index_ = string_index;
5496 SetLoadKindInternal(load_kind);
5497 }
5498
5499 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5500 const DexFile& dex_file,
5501 uint32_t element_index) {
5502 DCHECK(HasDexCacheReference(load_kind));
5503 load_data_.ref.dex_file = &dex_file;
5504 load_data_.ref.dex_cache_element_index = element_index;
5505 SetLoadKindInternal(load_kind);
5506 }
5507
5508 LoadKind GetLoadKind() const {
5509 return GetPackedField<LoadKindField>();
5510 }
5511
5512 const DexFile& GetDexFile() const;
5513
5514 uint32_t GetStringIndex() const {
5515 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5516 return string_index_;
5517 }
5518
5519 uint32_t GetDexCacheElementOffset() const;
5520
5521 uint64_t GetAddress() const {
5522 DCHECK(HasAddress(GetLoadKind()));
5523 return load_data_.address;
5524 }
5525
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005526 bool CanBeMoved() const OVERRIDE { return true; }
5527
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005528 bool InstructionDataEquals(HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005529
5530 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5531
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005532 // Will call the runtime if we need to load the string through
5533 // the dex cache and the string is not guaranteed to be there yet.
5534 bool NeedsEnvironment() const OVERRIDE {
5535 LoadKind load_kind = GetLoadKind();
5536 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5537 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5538 load_kind == LoadKind::kBootImageAddress) {
5539 return false;
5540 }
5541 return !IsInDexCache();
5542 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005543
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005544 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5545 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5546 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005547
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005548 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005549 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005550
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005551 static SideEffects SideEffectsForArchRuntimeCalls() {
5552 return SideEffects::CanTriggerGC();
5553 }
5554
Vladimir Markoa1de9182016-02-25 11:37:38 +00005555 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5556
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005557 void MarkInDexCache() {
5558 SetPackedFlag<kFlagIsInDexCache>(true);
5559 DCHECK(!NeedsEnvironment());
5560 RemoveEnvironment();
Vladimir Markobf12e4d2016-04-05 11:18:49 +01005561 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005562 }
5563
5564 size_t InputCount() const OVERRIDE {
5565 return (InputAt(0) != nullptr) ? 1u : 0u;
5566 }
5567
5568 void AddSpecialInput(HInstruction* special_input);
5569
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005570 DECLARE_INSTRUCTION(LoadString);
5571
5572 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005573 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005574 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5575 static constexpr size_t kFieldLoadKindSize =
5576 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5577 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005578 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005579 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005580
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005581 static bool HasStringReference(LoadKind load_kind) {
5582 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5583 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5584 load_kind == LoadKind::kDexCacheViaMethod;
5585 }
5586
5587 static bool HasAddress(LoadKind load_kind) {
5588 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5589 }
5590
5591 static bool HasDexCacheReference(LoadKind load_kind) {
5592 return load_kind == LoadKind::kDexCachePcRelative;
5593 }
5594
5595 void SetLoadKindInternal(LoadKind load_kind);
5596
5597 // String index serves also as the hash code and it's also needed for slow-paths,
5598 // so it must not be overwritten with other load data.
5599 uint32_t string_index_;
5600
5601 union {
5602 struct {
5603 const DexFile* dex_file; // For string reference and dex cache reference.
5604 uint32_t dex_cache_element_index; // Only for dex cache reference.
5605 } ref;
5606 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5607 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005608
5609 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5610};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005611std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5612
5613// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5614inline const DexFile& HLoadString::GetDexFile() const {
5615 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5616 << GetLoadKind();
5617 return *load_data_.ref.dex_file;
5618}
5619
5620// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5621inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5622 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5623 return load_data_.ref.dex_cache_element_index;
5624}
5625
5626// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5627inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5628 // The special input is used for PC-relative loads on some architectures.
5629 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5630 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
5631 DCHECK(InputAt(0) == nullptr);
5632 SetRawInputAt(0u, special_input);
5633 special_input->AddUseAt(this, 0);
5634}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005635
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005636/**
5637 * Performs an initialization check on its Class object input.
5638 */
5639class HClinitCheck : public HExpression<1> {
5640 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005641 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005642 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005643 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005644 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5645 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005646 SetRawInputAt(0, constant);
5647 }
5648
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005649 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005650 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005651 return true;
5652 }
5653
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005654 bool NeedsEnvironment() const OVERRIDE {
5655 // May call runtime to initialize the class.
5656 return true;
5657 }
5658
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005659 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005660
5661 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5662
5663 DECLARE_INSTRUCTION(ClinitCheck);
5664
5665 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005666 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5667};
5668
5669class HStaticFieldGet : public HExpression<1> {
5670 public:
5671 HStaticFieldGet(HInstruction* cls,
5672 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005673 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005674 bool is_volatile,
5675 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005676 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005677 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005678 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005679 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005680 : HExpression(field_type,
5681 SideEffects::FieldReadOfType(field_type, is_volatile),
5682 dex_pc),
5683 field_info_(field_offset,
5684 field_type,
5685 is_volatile,
5686 field_idx,
5687 declaring_class_def_index,
5688 dex_file,
5689 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005690 SetRawInputAt(0, cls);
5691 }
5692
Calin Juravle52c48962014-12-16 17:02:57 +00005693
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005694 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005695
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005696 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005697 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5698 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005699 }
5700
5701 size_t ComputeHashCode() const OVERRIDE {
5702 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5703 }
5704
Calin Juravle52c48962014-12-16 17:02:57 +00005705 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005706 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5707 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005708 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005709
5710 DECLARE_INSTRUCTION(StaticFieldGet);
5711
5712 private:
5713 const FieldInfo field_info_;
5714
5715 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5716};
5717
5718class HStaticFieldSet : public HTemplateInstruction<2> {
5719 public:
5720 HStaticFieldSet(HInstruction* cls,
5721 HInstruction* value,
5722 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005723 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005724 bool is_volatile,
5725 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005726 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005727 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005728 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005729 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005730 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5731 dex_pc),
5732 field_info_(field_offset,
5733 field_type,
5734 is_volatile,
5735 field_idx,
5736 declaring_class_def_index,
5737 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005738 dex_cache) {
5739 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005740 SetRawInputAt(0, cls);
5741 SetRawInputAt(1, value);
5742 }
5743
Calin Juravle52c48962014-12-16 17:02:57 +00005744 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005745 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5746 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005747 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005748
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005749 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005750 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5751 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005752
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005753 DECLARE_INSTRUCTION(StaticFieldSet);
5754
5755 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005756 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5757 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5758 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5759 "Too many packed fields.");
5760
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005761 const FieldInfo field_info_;
5762
5763 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5764};
5765
Calin Juravlee460d1d2015-09-29 04:52:17 +01005766class HUnresolvedInstanceFieldGet : public HExpression<1> {
5767 public:
5768 HUnresolvedInstanceFieldGet(HInstruction* obj,
5769 Primitive::Type field_type,
5770 uint32_t field_index,
5771 uint32_t dex_pc)
5772 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5773 field_index_(field_index) {
5774 SetRawInputAt(0, obj);
5775 }
5776
5777 bool NeedsEnvironment() const OVERRIDE { return true; }
5778 bool CanThrow() const OVERRIDE { return true; }
5779
5780 Primitive::Type GetFieldType() const { return GetType(); }
5781 uint32_t GetFieldIndex() const { return field_index_; }
5782
5783 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5784
5785 private:
5786 const uint32_t field_index_;
5787
5788 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5789};
5790
5791class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5792 public:
5793 HUnresolvedInstanceFieldSet(HInstruction* obj,
5794 HInstruction* value,
5795 Primitive::Type field_type,
5796 uint32_t field_index,
5797 uint32_t dex_pc)
5798 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005799 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005800 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005801 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005802 SetRawInputAt(0, obj);
5803 SetRawInputAt(1, value);
5804 }
5805
5806 bool NeedsEnvironment() const OVERRIDE { return true; }
5807 bool CanThrow() const OVERRIDE { return true; }
5808
Vladimir Markoa1de9182016-02-25 11:37:38 +00005809 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005810 uint32_t GetFieldIndex() const { return field_index_; }
5811
5812 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5813
5814 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005815 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5816 static constexpr size_t kFieldFieldTypeSize =
5817 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5818 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5819 kFieldFieldType + kFieldFieldTypeSize;
5820 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5821 "Too many packed fields.");
5822 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5823
Calin Juravlee460d1d2015-09-29 04:52:17 +01005824 const uint32_t field_index_;
5825
5826 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5827};
5828
5829class HUnresolvedStaticFieldGet : public HExpression<0> {
5830 public:
5831 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5832 uint32_t field_index,
5833 uint32_t dex_pc)
5834 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5835 field_index_(field_index) {
5836 }
5837
5838 bool NeedsEnvironment() const OVERRIDE { return true; }
5839 bool CanThrow() const OVERRIDE { return true; }
5840
5841 Primitive::Type GetFieldType() const { return GetType(); }
5842 uint32_t GetFieldIndex() const { return field_index_; }
5843
5844 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5845
5846 private:
5847 const uint32_t field_index_;
5848
5849 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5850};
5851
5852class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5853 public:
5854 HUnresolvedStaticFieldSet(HInstruction* value,
5855 Primitive::Type field_type,
5856 uint32_t field_index,
5857 uint32_t dex_pc)
5858 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005859 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005860 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005861 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005862 SetRawInputAt(0, value);
5863 }
5864
5865 bool NeedsEnvironment() const OVERRIDE { return true; }
5866 bool CanThrow() const OVERRIDE { return true; }
5867
Vladimir Markoa1de9182016-02-25 11:37:38 +00005868 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005869 uint32_t GetFieldIndex() const { return field_index_; }
5870
5871 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5872
5873 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005874 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5875 static constexpr size_t kFieldFieldTypeSize =
5876 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5877 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5878 kFieldFieldType + kFieldFieldTypeSize;
5879 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5880 "Too many packed fields.");
5881 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5882
Calin Juravlee460d1d2015-09-29 04:52:17 +01005883 const uint32_t field_index_;
5884
5885 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5886};
5887
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005888// Implement the move-exception DEX instruction.
5889class HLoadException : public HExpression<0> {
5890 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005891 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5892 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005893
David Brazdilbbd733e2015-08-18 17:48:17 +01005894 bool CanBeNull() const OVERRIDE { return false; }
5895
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005896 DECLARE_INSTRUCTION(LoadException);
5897
5898 private:
5899 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5900};
5901
David Brazdilcb1c0552015-08-04 16:22:25 +01005902// Implicit part of move-exception which clears thread-local exception storage.
5903// Must not be removed because the runtime expects the TLS to get cleared.
5904class HClearException : public HTemplateInstruction<0> {
5905 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005906 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5907 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005908
5909 DECLARE_INSTRUCTION(ClearException);
5910
5911 private:
5912 DISALLOW_COPY_AND_ASSIGN(HClearException);
5913};
5914
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005915class HThrow : public HTemplateInstruction<1> {
5916 public:
5917 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005918 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005919 SetRawInputAt(0, exception);
5920 }
5921
5922 bool IsControlFlow() const OVERRIDE { return true; }
5923
5924 bool NeedsEnvironment() const OVERRIDE { return true; }
5925
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005926 bool CanThrow() const OVERRIDE { return true; }
5927
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005928
5929 DECLARE_INSTRUCTION(Throw);
5930
5931 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005932 DISALLOW_COPY_AND_ASSIGN(HThrow);
5933};
5934
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005935/**
5936 * Implementation strategies for the code generator of a HInstanceOf
5937 * or `HCheckCast`.
5938 */
5939enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005940 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005941 kExactCheck, // Can do a single class compare.
5942 kClassHierarchyCheck, // Can just walk the super class chain.
5943 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5944 kInterfaceCheck, // No optimization yet when checking against an interface.
5945 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005946 kArrayCheck, // No optimization yet when checking against a generic array.
5947 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005948};
5949
Roland Levillain86503782016-02-11 19:07:30 +00005950std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5951
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005952class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005953 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005954 HInstanceOf(HInstruction* object,
5955 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005956 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005957 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005958 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005959 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005960 dex_pc) {
5961 SetPackedField<TypeCheckKindField>(check_kind);
5962 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005963 SetRawInputAt(0, object);
5964 SetRawInputAt(1, constant);
5965 }
5966
5967 bool CanBeMoved() const OVERRIDE { return true; }
5968
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005969 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005970 return true;
5971 }
5972
5973 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005974 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005975 }
5976
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005977 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005978 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5979 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5980 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5981 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005982
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005983 static bool CanCallRuntime(TypeCheckKind check_kind) {
5984 // Mips currently does runtime calls for any other checks.
5985 return check_kind != TypeCheckKind::kExactCheck;
5986 }
5987
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005988 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005989 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005990 }
5991
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005992 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005993
5994 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005995 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
5996 static constexpr size_t kFieldTypeCheckKindSize =
5997 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
5998 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
5999 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6000 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6001 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006002
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006003 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6004};
6005
Calin Juravleb1498f62015-02-16 13:13:29 +00006006class HBoundType : public HExpression<1> {
6007 public:
David Brazdilf5552582015-12-27 13:36:12 +00006008 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006009 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006010 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6011 SetPackedFlag<kFlagUpperCanBeNull>(true);
6012 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006013 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006014 SetRawInputAt(0, input);
6015 }
6016
David Brazdilf5552582015-12-27 13:36:12 +00006017 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006018 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006019 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006020 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006021
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006022 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006023 DCHECK(GetUpperCanBeNull() || !can_be_null);
6024 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006025 }
6026
Vladimir Markoa1de9182016-02-25 11:37:38 +00006027 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006028
Calin Juravleb1498f62015-02-16 13:13:29 +00006029 DECLARE_INSTRUCTION(BoundType);
6030
6031 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006032 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6033 // is false then CanBeNull() cannot be true).
6034 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6035 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6036 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6037 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6038
Calin Juravleb1498f62015-02-16 13:13:29 +00006039 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006040 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6041 // It is used to bound the type in cases like:
6042 // if (x instanceof ClassX) {
6043 // // uper_bound_ will be ClassX
6044 // }
David Brazdilf5552582015-12-27 13:36:12 +00006045 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006046
6047 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6048};
6049
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006050class HCheckCast : public HTemplateInstruction<2> {
6051 public:
6052 HCheckCast(HInstruction* object,
6053 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006054 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006055 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006056 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6057 SetPackedField<TypeCheckKindField>(check_kind);
6058 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006059 SetRawInputAt(0, object);
6060 SetRawInputAt(1, constant);
6061 }
6062
6063 bool CanBeMoved() const OVERRIDE { return true; }
6064
6065 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6066 return true;
6067 }
6068
6069 bool NeedsEnvironment() const OVERRIDE {
6070 // Instruction may throw a CheckCastError.
6071 return true;
6072 }
6073
6074 bool CanThrow() const OVERRIDE { return true; }
6075
Vladimir Markoa1de9182016-02-25 11:37:38 +00006076 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6077 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6078 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6079 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006080
6081 DECLARE_INSTRUCTION(CheckCast);
6082
6083 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006084 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6085 static constexpr size_t kFieldTypeCheckKindSize =
6086 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6087 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6088 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6089 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6090 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006091
6092 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006093};
6094
Calin Juravle27df7582015-04-17 19:12:31 +01006095class HMemoryBarrier : public HTemplateInstruction<0> {
6096 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006097 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006098 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006099 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6100 SetPackedField<BarrierKindField>(barrier_kind);
6101 }
Calin Juravle27df7582015-04-17 19:12:31 +01006102
Vladimir Markoa1de9182016-02-25 11:37:38 +00006103 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006104
6105 DECLARE_INSTRUCTION(MemoryBarrier);
6106
6107 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006108 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6109 static constexpr size_t kFieldBarrierKindSize =
6110 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6111 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6112 kFieldBarrierKind + kFieldBarrierKindSize;
6113 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6114 "Too many packed fields.");
6115 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006116
6117 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6118};
6119
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006120class HMonitorOperation : public HTemplateInstruction<1> {
6121 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006122 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006123 kEnter,
6124 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006125 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006126 };
6127
6128 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006129 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006130 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6131 dex_pc) {
6132 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006133 SetRawInputAt(0, object);
6134 }
6135
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006136 // Instruction may go into runtime, so we need an environment.
6137 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006138
6139 bool CanThrow() const OVERRIDE {
6140 // Verifier guarantees that monitor-exit cannot throw.
6141 // This is important because it allows the HGraphBuilder to remove
6142 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6143 return IsEnter();
6144 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006145
Vladimir Markoa1de9182016-02-25 11:37:38 +00006146 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6147 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006148
6149 DECLARE_INSTRUCTION(MonitorOperation);
6150
Calin Juravle52c48962014-12-16 17:02:57 +00006151 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006152 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6153 static constexpr size_t kFieldOperationKindSize =
6154 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6155 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6156 kFieldOperationKind + kFieldOperationKindSize;
6157 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6158 "Too many packed fields.");
6159 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006160
6161 private:
6162 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6163};
6164
David Brazdil74eb1b22015-12-14 11:44:01 +00006165class HSelect : public HExpression<3> {
6166 public:
6167 HSelect(HInstruction* condition,
6168 HInstruction* true_value,
6169 HInstruction* false_value,
6170 uint32_t dex_pc)
6171 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6172 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6173
6174 // First input must be `true_value` or `false_value` to allow codegens to
6175 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6176 // that architectures which implement HSelect as a conditional move also
6177 // will not need to invert the condition.
6178 SetRawInputAt(0, false_value);
6179 SetRawInputAt(1, true_value);
6180 SetRawInputAt(2, condition);
6181 }
6182
6183 HInstruction* GetFalseValue() const { return InputAt(0); }
6184 HInstruction* GetTrueValue() const { return InputAt(1); }
6185 HInstruction* GetCondition() const { return InputAt(2); }
6186
6187 bool CanBeMoved() const OVERRIDE { return true; }
6188 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6189
6190 bool CanBeNull() const OVERRIDE {
6191 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6192 }
6193
6194 DECLARE_INSTRUCTION(Select);
6195
6196 private:
6197 DISALLOW_COPY_AND_ASSIGN(HSelect);
6198};
6199
Vladimir Markof9f64412015-09-02 14:05:49 +01006200class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006201 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006202 MoveOperands(Location source,
6203 Location destination,
6204 Primitive::Type type,
6205 HInstruction* instruction)
6206 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006207
6208 Location GetSource() const { return source_; }
6209 Location GetDestination() const { return destination_; }
6210
6211 void SetSource(Location value) { source_ = value; }
6212 void SetDestination(Location value) { destination_ = value; }
6213
6214 // The parallel move resolver marks moves as "in-progress" by clearing the
6215 // destination (but not the source).
6216 Location MarkPending() {
6217 DCHECK(!IsPending());
6218 Location dest = destination_;
6219 destination_ = Location::NoLocation();
6220 return dest;
6221 }
6222
6223 void ClearPending(Location dest) {
6224 DCHECK(IsPending());
6225 destination_ = dest;
6226 }
6227
6228 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006229 DCHECK(source_.IsValid() || destination_.IsInvalid());
6230 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006231 }
6232
6233 // True if this blocks a move from the given location.
6234 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006235 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006236 }
6237
6238 // A move is redundant if it's been eliminated, if its source and
6239 // destination are the same, or if its destination is unneeded.
6240 bool IsRedundant() const {
6241 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6242 }
6243
6244 // We clear both operands to indicate move that's been eliminated.
6245 void Eliminate() {
6246 source_ = destination_ = Location::NoLocation();
6247 }
6248
6249 bool IsEliminated() const {
6250 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6251 return source_.IsInvalid();
6252 }
6253
Alexey Frunze4dda3372015-06-01 18:31:49 -07006254 Primitive::Type GetType() const { return type_; }
6255
Nicolas Geoffray90218252015-04-15 11:56:51 +01006256 bool Is64BitMove() const {
6257 return Primitive::Is64BitType(type_);
6258 }
6259
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006260 HInstruction* GetInstruction() const { return instruction_; }
6261
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006262 private:
6263 Location source_;
6264 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006265 // The type this move is for.
6266 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006267 // The instruction this move is assocatied with. Null when this move is
6268 // for moving an input in the expected locations of user (including a phi user).
6269 // This is only used in debug mode, to ensure we do not connect interval siblings
6270 // in the same parallel move.
6271 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006272};
6273
Roland Levillainc9285912015-12-18 10:38:42 +00006274std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6275
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006276static constexpr size_t kDefaultNumberOfMoves = 4;
6277
6278class HParallelMove : public HTemplateInstruction<0> {
6279 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006280 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006281 : HTemplateInstruction(SideEffects::None(), dex_pc),
6282 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6283 moves_.reserve(kDefaultNumberOfMoves);
6284 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006285
Nicolas Geoffray90218252015-04-15 11:56:51 +01006286 void AddMove(Location source,
6287 Location destination,
6288 Primitive::Type type,
6289 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006290 DCHECK(source.IsValid());
6291 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006292 if (kIsDebugBuild) {
6293 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006294 for (const MoveOperands& move : moves_) {
6295 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006296 // Special case the situation where the move is for the spill slot
6297 // of the instruction.
6298 if ((GetPrevious() == instruction)
6299 || ((GetPrevious() == nullptr)
6300 && instruction->IsPhi()
6301 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006302 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006303 << "Doing parallel moves for the same instruction.";
6304 } else {
6305 DCHECK(false) << "Doing parallel moves for the same instruction.";
6306 }
6307 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006308 }
6309 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006310 for (const MoveOperands& move : moves_) {
6311 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006312 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006313 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006314 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006315 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006316 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006317 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006318 }
6319
Vladimir Marko225b6462015-09-28 12:17:40 +01006320 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006321 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006322 }
6323
Vladimir Marko225b6462015-09-28 12:17:40 +01006324 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006325
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006326 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006327
6328 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006329 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006330
6331 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6332};
6333
Mark Mendell0616ae02015-04-17 12:49:27 -04006334} // namespace art
6335
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006336#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6337#include "nodes_shared.h"
6338#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006339#ifdef ART_ENABLE_CODEGEN_arm
6340#include "nodes_arm.h"
6341#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006342#ifdef ART_ENABLE_CODEGEN_arm64
6343#include "nodes_arm64.h"
6344#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006345#ifdef ART_ENABLE_CODEGEN_x86
6346#include "nodes_x86.h"
6347#endif
6348
6349namespace art {
6350
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006351class HGraphVisitor : public ValueObject {
6352 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006353 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6354 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006355
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006356 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006357 virtual void VisitBasicBlock(HBasicBlock* block);
6358
Roland Levillain633021e2014-10-01 14:12:25 +01006359 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006360 void VisitInsertionOrder();
6361
Roland Levillain633021e2014-10-01 14:12:25 +01006362 // Visit the graph following dominator tree reverse post-order.
6363 void VisitReversePostOrder();
6364
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006365 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006366
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006367 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006368#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006369 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6370
6371 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6372
6373#undef DECLARE_VISIT_INSTRUCTION
6374
6375 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006376 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006377
6378 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6379};
6380
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006381class HGraphDelegateVisitor : public HGraphVisitor {
6382 public:
6383 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6384 virtual ~HGraphDelegateVisitor() {}
6385
6386 // Visit functions that delegate to to super class.
6387#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006388 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006389
6390 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6391
6392#undef DECLARE_VISIT_INSTRUCTION
6393
6394 private:
6395 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6396};
6397
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006398class HInsertionOrderIterator : public ValueObject {
6399 public:
6400 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6401
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006402 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006403 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006404 void Advance() { ++index_; }
6405
6406 private:
6407 const HGraph& graph_;
6408 size_t index_;
6409
6410 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6411};
6412
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006413class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006414 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006415 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6416 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006417 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006418 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006419
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006420 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6421 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006422 void Advance() { ++index_; }
6423
6424 private:
6425 const HGraph& graph_;
6426 size_t index_;
6427
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006428 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006429};
6430
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006431class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006432 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006433 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006434 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006435 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006436 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006437 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006438
6439 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006440 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006441 void Advance() { --index_; }
6442
6443 private:
6444 const HGraph& graph_;
6445 size_t index_;
6446
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006447 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006448};
6449
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006450class HLinearPostOrderIterator : public ValueObject {
6451 public:
6452 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006453 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006454
6455 bool Done() const { return index_ == 0; }
6456
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006457 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006458
6459 void Advance() {
6460 --index_;
6461 DCHECK_GE(index_, 0U);
6462 }
6463
6464 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006465 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006466 size_t index_;
6467
6468 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6469};
6470
6471class HLinearOrderIterator : public ValueObject {
6472 public:
6473 explicit HLinearOrderIterator(const HGraph& graph)
6474 : order_(graph.GetLinearOrder()), index_(0) {}
6475
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006476 bool Done() const { return index_ == order_.size(); }
6477 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006478 void Advance() { ++index_; }
6479
6480 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006481 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006482 size_t index_;
6483
6484 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6485};
6486
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006487// Iterator over the blocks that art part of the loop. Includes blocks part
6488// of an inner loop. The order in which the blocks are iterated is on their
6489// block id.
6490class HBlocksInLoopIterator : public ValueObject {
6491 public:
6492 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6493 : blocks_in_loop_(info.GetBlocks()),
6494 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6495 index_(0) {
6496 if (!blocks_in_loop_.IsBitSet(index_)) {
6497 Advance();
6498 }
6499 }
6500
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006501 bool Done() const { return index_ == blocks_.size(); }
6502 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006503 void Advance() {
6504 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006505 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006506 if (blocks_in_loop_.IsBitSet(index_)) {
6507 break;
6508 }
6509 }
6510 }
6511
6512 private:
6513 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006514 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006515 size_t index_;
6516
6517 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6518};
6519
Mingyao Yang3584bce2015-05-19 16:01:59 -07006520// Iterator over the blocks that art part of the loop. Includes blocks part
6521// of an inner loop. The order in which the blocks are iterated is reverse
6522// post order.
6523class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6524 public:
6525 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6526 : blocks_in_loop_(info.GetBlocks()),
6527 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6528 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006529 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006530 Advance();
6531 }
6532 }
6533
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006534 bool Done() const { return index_ == blocks_.size(); }
6535 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006536 void Advance() {
6537 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006538 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6539 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006540 break;
6541 }
6542 }
6543 }
6544
6545 private:
6546 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006547 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006548 size_t index_;
6549
6550 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6551};
6552
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006553inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006554 if (constant->IsIntConstant()) {
6555 return constant->AsIntConstant()->GetValue();
6556 } else if (constant->IsLongConstant()) {
6557 return constant->AsLongConstant()->GetValue();
6558 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006559 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006560 return 0;
6561 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006562}
6563
Vladimir Marko58155012015-08-19 12:49:41 +00006564inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6565 // For the purposes of the compiler, the dex files must actually be the same object
6566 // if we want to safely treat them as the same. This is especially important for JIT
6567 // as custom class loaders can open the same underlying file (or memory) multiple
6568 // times and provide different class resolution but no two class loaders should ever
6569 // use the same DexFile object - doing so is an unsupported hack that can lead to
6570 // all sorts of weird failures.
6571 return &lhs == &rhs;
6572}
6573
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006574#define INSTRUCTION_TYPE_CHECK(type, super) \
6575 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6576 inline const H##type* HInstruction::As##type() const { \
6577 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6578 } \
6579 inline H##type* HInstruction::As##type() { \
6580 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6581 }
6582
6583 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6584#undef INSTRUCTION_TYPE_CHECK
6585
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006586// Create space in `blocks` for adding `number_of_new_blocks` entries
6587// starting at location `at`. Blocks after `at` are moved accordingly.
6588inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6589 size_t number_of_new_blocks,
6590 size_t after) {
6591 DCHECK_LT(after, blocks->size());
6592 size_t old_size = blocks->size();
6593 size_t new_size = old_size + number_of_new_blocks;
6594 blocks->resize(new_size);
6595 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6596}
6597
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006598} // namespace art
6599
6600#endif // ART_COMPILER_OPTIMIZING_NODES_H_