blob: 56d6a9838031a90621b5b41c80f7122289d6c24c [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),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100653 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100654 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000655 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100656 back_edges_.reserve(kDefaultNumberOfBackEdges);
657 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100658
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000659 bool IsIrreducible() const { return irreducible_; }
660
661 void Dump(std::ostream& os);
662
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100663 HBasicBlock* GetHeader() const {
664 return header_;
665 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000666
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000667 void SetHeader(HBasicBlock* block) {
668 header_ = block;
669 }
670
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100671 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
672 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
673 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
674
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000675 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100676 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000677 }
678
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100679 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100680 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100681 }
682
David Brazdil46e2a392015-03-16 17:31:52 +0000683 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100684 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100685 }
686
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000687 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100688 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000689 }
690
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100691 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100692
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100693 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100694 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100695 }
696
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100697 // Returns the lifetime position of the back edge that has the
698 // greatest lifetime position.
699 size_t GetLifetimeEnd() const;
700
701 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100702 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100703 }
704
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000705 // Finds blocks that are part of this loop.
706 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100707
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100708 // Returns whether this loop information contains `block`.
709 // Note that this loop information *must* be populated before entering this function.
710 bool Contains(const HBasicBlock& block) const;
711
712 // Returns whether this loop information is an inner loop of `other`.
713 // Note that `other` *must* be populated before entering this function.
714 bool IsIn(const HLoopInformation& other) const;
715
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800716 // Returns true if instruction is not defined within this loop.
717 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700718
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100719 const ArenaBitVector& GetBlocks() const { return blocks_; }
720
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000721 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000722 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000723
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000724 void ClearAllBlocks() {
725 blocks_.ClearAllBits();
726 }
727
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000728 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100729 // Internal recursive implementation of `Populate`.
730 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100731 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100732
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000733 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100734 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000735 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100736 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100737 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000738
739 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
740};
741
David Brazdilec16f792015-08-19 15:04:01 +0100742// Stores try/catch information for basic blocks.
743// Note that HGraph is constructed so that catch blocks cannot simultaneously
744// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100745class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100746 public:
747 // Try block information constructor.
748 explicit TryCatchInformation(const HTryBoundary& try_entry)
749 : try_entry_(&try_entry),
750 catch_dex_file_(nullptr),
751 catch_type_index_(DexFile::kDexNoIndex16) {
752 DCHECK(try_entry_ != nullptr);
753 }
754
755 // Catch block information constructor.
756 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
757 : try_entry_(nullptr),
758 catch_dex_file_(&dex_file),
759 catch_type_index_(catch_type_index) {}
760
761 bool IsTryBlock() const { return try_entry_ != nullptr; }
762
763 const HTryBoundary& GetTryEntry() const {
764 DCHECK(IsTryBlock());
765 return *try_entry_;
766 }
767
768 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
769
770 bool IsCatchAllTypeIndex() const {
771 DCHECK(IsCatchBlock());
772 return catch_type_index_ == DexFile::kDexNoIndex16;
773 }
774
775 uint16_t GetCatchTypeIndex() const {
776 DCHECK(IsCatchBlock());
777 return catch_type_index_;
778 }
779
780 const DexFile& GetCatchDexFile() const {
781 DCHECK(IsCatchBlock());
782 return *catch_dex_file_;
783 }
784
785 private:
786 // One of possibly several TryBoundary instructions entering the block's try.
787 // Only set for try blocks.
788 const HTryBoundary* try_entry_;
789
790 // Exception type information. Only set for catch blocks.
791 const DexFile* catch_dex_file_;
792 const uint16_t catch_type_index_;
793};
794
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100795static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100796static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100797
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000798// A block in a method. Contains the list of instructions represented
799// as a double linked list. Each block knows its predecessors and
800// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100801
Vladimir Markof9f64412015-09-02 14:05:49 +0100802class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000803 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600804 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000805 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000806 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
807 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000808 loop_information_(nullptr),
809 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000810 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100811 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100812 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100813 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000814 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000815 try_catch_information_(nullptr) {
816 predecessors_.reserve(kDefaultNumberOfPredecessors);
817 successors_.reserve(kDefaultNumberOfSuccessors);
818 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
819 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000820
Vladimir Marko60584552015-09-03 13:35:12 +0000821 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100822 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000823 }
824
Vladimir Marko60584552015-09-03 13:35:12 +0000825 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100826 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000827 }
828
David Brazdild26a4112015-11-10 11:07:31 +0000829 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
830 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
831
Vladimir Marko60584552015-09-03 13:35:12 +0000832 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
833 return ContainsElement(successors_, block, start_from);
834 }
835
836 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100837 return dominated_blocks_;
838 }
839
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100840 bool IsEntryBlock() const {
841 return graph_->GetEntryBlock() == this;
842 }
843
844 bool IsExitBlock() const {
845 return graph_->GetExitBlock() == this;
846 }
847
David Brazdil46e2a392015-03-16 17:31:52 +0000848 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000849 bool IsSingleTryBoundary() const;
850
851 // Returns true if this block emits nothing but a jump.
852 bool IsSingleJump() const {
853 HLoopInformation* loop_info = GetLoopInformation();
854 return (IsSingleGoto() || IsSingleTryBoundary())
855 // Back edges generate a suspend check.
856 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
857 }
David Brazdil46e2a392015-03-16 17:31:52 +0000858
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000859 void AddBackEdge(HBasicBlock* back_edge) {
860 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000861 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000862 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100863 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000864 loop_information_->AddBackEdge(back_edge);
865 }
866
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000867 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000868 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000869
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100870 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000871 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600872 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000873
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000874 HBasicBlock* GetDominator() const { return dominator_; }
875 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000876 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
877
878 void RemoveDominatedBlock(HBasicBlock* block) {
879 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100880 }
Vladimir Marko60584552015-09-03 13:35:12 +0000881
882 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
883 ReplaceElement(dominated_blocks_, existing, new_block);
884 }
885
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100886 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000887
888 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100889 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000890 }
891
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100892 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
893 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100894 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100895 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100896 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
897 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000898
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000899 HInstruction* GetFirstInstructionDisregardMoves() const;
900
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000901 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000902 successors_.push_back(block);
903 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000904 }
905
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100906 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
907 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100908 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000909 new_block->predecessors_.push_back(this);
910 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000911 }
912
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000913 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
914 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000915 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000916 new_block->successors_.push_back(this);
917 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000918 }
919
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100920 // Insert `this` between `predecessor` and `successor. This method
921 // preserves the indicies, and will update the first edge found between
922 // `predecessor` and `successor`.
923 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
924 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100925 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000926 successor->predecessors_[predecessor_index] = this;
927 predecessor->successors_[successor_index] = this;
928 successors_.push_back(successor);
929 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100930 }
931
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100932 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000933 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100934 }
935
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000936 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000937 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000938 }
939
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100940 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000941 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100942 }
943
944 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000945 predecessors_.push_back(block);
946 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100947 }
948
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100949 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000950 DCHECK_EQ(predecessors_.size(), 2u);
951 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100952 }
953
David Brazdil769c9e52015-04-27 13:54:09 +0100954 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000955 DCHECK_EQ(successors_.size(), 2u);
956 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100957 }
958
David Brazdilfc6a86a2015-06-26 10:33:45 +0000959 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000960 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100961 }
962
David Brazdilfc6a86a2015-06-26 10:33:45 +0000963 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000964 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100965 }
966
David Brazdilfc6a86a2015-06-26 10:33:45 +0000967 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000968 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100969 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000970 }
971
972 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000973 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100974 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000975 }
976
977 // Returns whether the first occurrence of `predecessor` in the list of
978 // predecessors is at index `idx`.
979 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100980 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000981 return GetPredecessorIndexOf(predecessor) == idx;
982 }
983
David Brazdild7558da2015-09-22 13:04:14 +0100984 // Create a new block between this block and its predecessors. The new block
985 // is added to the graph, all predecessor edges are relinked to it and an edge
986 // is created to `this`. Returns the new empty block. Reverse post order or
987 // loop and try/catch information are not updated.
988 HBasicBlock* CreateImmediateDominator();
989
David Brazdilfc6a86a2015-06-26 10:33:45 +0000990 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100991 // created, latter block. Note that this method will add the block to the
992 // graph, create a Goto at the end of the former block and will create an edge
993 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100994 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000995 HBasicBlock* SplitBefore(HInstruction* cursor);
996
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000997 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000998 // created block. Note that this method just updates raw block information,
999 // like predecessors, successors, dominators, and instruction list. It does not
1000 // update the graph, reverse post order, loop information, nor make sure the
1001 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001002 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1003
1004 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1005 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001006
1007 // Merge `other` at the end of `this`. Successors and dominated blocks of
1008 // `other` are changed to be successors and dominated blocks of `this`. Note
1009 // that this method does not update the graph, reverse post order, loop
1010 // information, nor make sure the blocks are consistent (for example ending
1011 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001012 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001013
1014 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1015 // of `this` are moved to `other`.
1016 // Note that this method does not update the graph, reverse post order, loop
1017 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001018 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001019 void ReplaceWith(HBasicBlock* other);
1020
David Brazdil2d7352b2015-04-20 14:52:42 +01001021 // Merge `other` at the end of `this`. This method updates loops, reverse post
1022 // order, links to predecessors, successors, dominators and deletes the block
1023 // from the graph. The two blocks must be successive, i.e. `this` the only
1024 // predecessor of `other` and vice versa.
1025 void MergeWith(HBasicBlock* other);
1026
1027 // Disconnects `this` from all its predecessors, successors and dominator,
1028 // removes it from all loops it is included in and eventually from the graph.
1029 // The block must not dominate any other block. Predecessors and successors
1030 // are safely updated.
1031 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001032
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001033 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001034 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001035 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001036 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001037 // Replace instruction `initial` with `replacement` within this block.
1038 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1039 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001040 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001041 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001042 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001043 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1044 // instruction list. With 'ensure_safety' set to true, it verifies that the
1045 // instruction is not in use and removes it from the use lists of its inputs.
1046 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1047 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001048 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001049
1050 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001051 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001052 }
1053
Roland Levillain6b879dd2014-09-22 17:13:44 +01001054 bool IsLoopPreHeaderFirstPredecessor() const {
1055 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001056 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001057 }
1058
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001059 bool IsFirstPredecessorBackEdge() const {
1060 DCHECK(IsLoopHeader());
1061 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1062 }
1063
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001064 HLoopInformation* GetLoopInformation() const {
1065 return loop_information_;
1066 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001067
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001068 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001069 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001070 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001071 void SetInLoop(HLoopInformation* info) {
1072 if (IsLoopHeader()) {
1073 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001074 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001075 loop_information_ = info;
1076 } else if (loop_information_->Contains(*info->GetHeader())) {
1077 // Block is currently part of an outer loop. Make it part of this inner loop.
1078 // Note that a non loop header having a loop information means this loop information
1079 // has already been populated
1080 loop_information_ = info;
1081 } else {
1082 // Block is part of an inner loop. Do not update the loop information.
1083 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1084 // at this point, because this method is being called while populating `info`.
1085 }
1086 }
1087
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001088 // Raw update of the loop information.
1089 void SetLoopInformation(HLoopInformation* info) {
1090 loop_information_ = info;
1091 }
1092
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001093 bool IsInLoop() const { return loop_information_ != nullptr; }
1094
David Brazdilec16f792015-08-19 15:04:01 +01001095 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1096
1097 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1098 try_catch_information_ = try_catch_information;
1099 }
1100
1101 bool IsTryBlock() const {
1102 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1103 }
1104
1105 bool IsCatchBlock() const {
1106 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1107 }
David Brazdilffee3d32015-07-06 11:48:53 +01001108
1109 // Returns the try entry that this block's successors should have. They will
1110 // be in the same try, unless the block ends in a try boundary. In that case,
1111 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001112 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001113
David Brazdild7558da2015-09-22 13:04:14 +01001114 bool HasThrowingInstructions() const;
1115
David Brazdila4b8c212015-05-07 09:59:30 +01001116 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001117 bool Dominates(HBasicBlock* block) const;
1118
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001119 size_t GetLifetimeStart() const { return lifetime_start_; }
1120 size_t GetLifetimeEnd() const { return lifetime_end_; }
1121
1122 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1123 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1124
David Brazdil8d5b8b22015-03-24 10:51:52 +00001125 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001126 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001127 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001128 bool HasSinglePhi() const;
1129
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001130 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001131 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001132 ArenaVector<HBasicBlock*> predecessors_;
1133 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001134 HInstructionList instructions_;
1135 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001136 HLoopInformation* loop_information_;
1137 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001138 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001139 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001140 // The dex program counter of the first instruction of this block.
1141 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001142 size_t lifetime_start_;
1143 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001144 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001145
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001146 friend class HGraph;
1147 friend class HInstruction;
1148
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001149 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1150};
1151
David Brazdilb2bd1c52015-03-25 11:17:37 +00001152// Iterates over the LoopInformation of all loops which contain 'block'
1153// from the innermost to the outermost.
1154class HLoopInformationOutwardIterator : public ValueObject {
1155 public:
1156 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1157 : current_(block.GetLoopInformation()) {}
1158
1159 bool Done() const { return current_ == nullptr; }
1160
1161 void Advance() {
1162 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001163 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001164 }
1165
1166 HLoopInformation* Current() const {
1167 DCHECK(!Done());
1168 return current_;
1169 }
1170
1171 private:
1172 HLoopInformation* current_;
1173
1174 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1175};
1176
Alexandre Ramesef20f712015-06-09 10:29:30 +01001177#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001178 M(Above, Condition) \
1179 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001180 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001181 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001182 M(ArrayGet, Instruction) \
1183 M(ArrayLength, Instruction) \
1184 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001185 M(Below, Condition) \
1186 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001187 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001188 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001189 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001190 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001191 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001192 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001193 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001194 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001195 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001196 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001197 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001198 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001199 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001200 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001201 M(Exit, Instruction) \
1202 M(FloatConstant, Constant) \
1203 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001204 M(GreaterThan, Condition) \
1205 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001206 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001207 M(InstanceFieldGet, Instruction) \
1208 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001209 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001210 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001211 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001212 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001213 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001214 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001215 M(LessThan, Condition) \
1216 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001217 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001218 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001219 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001220 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001221 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001222 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001223 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001224 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001225 M(Neg, UnaryOperation) \
1226 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001227 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001228 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001229 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001230 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001231 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001232 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001233 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001234 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001235 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001236 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001237 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001238 M(Return, Instruction) \
1239 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001240 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001241 M(Shl, BinaryOperation) \
1242 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001243 M(StaticFieldGet, Instruction) \
1244 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001245 M(UnresolvedInstanceFieldGet, Instruction) \
1246 M(UnresolvedInstanceFieldSet, Instruction) \
1247 M(UnresolvedStaticFieldGet, Instruction) \
1248 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001249 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001250 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001251 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001252 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001253 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001254 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001255 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001256 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001257
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001258/*
1259 * Instructions, shared across several (not all) architectures.
1260 */
1261#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1262#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1263#else
1264#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001265 M(BitwiseNegatedRight, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001266 M(MultiplyAccumulate, Instruction)
1267#endif
1268
Vladimir Markob4536b72015-11-24 13:45:23 +00001269#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001270#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001271#else
1272#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1273 M(ArmDexCacheArraysBase, Instruction)
1274#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001275
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001276#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001277#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001278#else
1279#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001280 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001281 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001282#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001283
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001284#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1285
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001286#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1287
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001288#ifndef ART_ENABLE_CODEGEN_x86
1289#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1290#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001291#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1292 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001293 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001294 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001295 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001296#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001297
1298#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1299
1300#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1301 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001302 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001303 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1304 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001305 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001306 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001307 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1308 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1309
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001310#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1311 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001312 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001313 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001314 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001315 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001316
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001317#define FOR_EACH_INSTRUCTION(M) \
1318 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1319 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1320
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001321#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001322FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1323#undef FORWARD_DECLARATION
1324
Roland Levillainccc07a92014-09-16 14:48:16 +01001325#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001326 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001327 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001328 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001329 return other->Is##type(); \
1330 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001331 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001332
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001333#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1334 bool Is##type() const { return As##type() != nullptr; } \
1335 const H##type* As##type() const { return this; } \
1336 H##type* As##type() { return this; }
1337
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001338template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001339class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001340 public:
David Brazdiled596192015-01-23 10:39:45 +00001341 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001342 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001343 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001344
Vladimir Markod59f3b12016-03-29 12:21:58 +01001345 // Hook for the IntrusiveForwardList<>.
1346 // TODO: Hide this better.
1347 IntrusiveForwardListHook hook;
1348
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001349 private:
David Brazdiled596192015-01-23 10:39:45 +00001350 HUseListNode(T user, size_t index)
Vladimir Markod59f3b12016-03-29 12:21:58 +01001351 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001352
1353 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001354 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001355
Vladimir Markod59f3b12016-03-29 12:21:58 +01001356 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001357
1358 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1359};
1360
David Brazdiled596192015-01-23 10:39:45 +00001361template <typename T>
Vladimir Markod59f3b12016-03-29 12:21:58 +01001362using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001363
David Brazdil1abb4192015-02-17 18:33:36 +00001364// This class is used by HEnvironment and HInstruction classes to record the
1365// instructions they use and pointers to the corresponding HUseListNodes kept
1366// by the used instructions.
1367template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001368class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001369 public:
Vladimir Markod59f3b12016-03-29 12:21:58 +01001370 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1371 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001372
Vladimir Markod59f3b12016-03-29 12:21:58 +01001373 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1374 : HUserRecord(old_record.instruction_, before_use_node) {}
1375 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1376 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001377 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001378 }
1379
1380 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Markod59f3b12016-03-29 12:21:58 +01001381 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1382 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001383
1384 private:
1385 // Instruction used by the user.
1386 HInstruction* instruction_;
1387
Vladimir Markod59f3b12016-03-29 12:21:58 +01001388 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1389 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001390};
1391
Aart Bik854a02b2015-07-14 16:07:00 -07001392/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001393 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001394 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001395 * For write/read dependences on fields/arrays, the dependence analysis uses
1396 * type disambiguation (e.g. a float field write cannot modify the value of an
1397 * integer field read) and the access type (e.g. a reference array write cannot
1398 * modify the value of a reference field read [although it may modify the
1399 * reference fetch prior to reading the field, which is represented by its own
1400 * write/read dependence]). The analysis makes conservative points-to
1401 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1402 * the same, and any reference read depends on any reference read without
1403 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001404 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001405 * The internal representation uses 38-bit and is described in the table below.
1406 * The first line indicates the side effect, and for field/array accesses the
1407 * second line indicates the type of the access (in the order of the
1408 * Primitive::Type enum).
1409 * The two numbered lines below indicate the bit position in the bitfield (read
1410 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001411 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001412 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1413 * +-------------+---------+---------+--------------+---------+---------+
1414 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1415 * | 3 |333333322|222222221| 1 |111111110|000000000|
1416 * | 7 |654321098|765432109| 8 |765432109|876543210|
1417 *
1418 * Note that, to ease the implementation, 'changes' bits are least significant
1419 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001420 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001421class SideEffects : public ValueObject {
1422 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001423 SideEffects() : flags_(0) {}
1424
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001425 static SideEffects None() {
1426 return SideEffects(0);
1427 }
1428
1429 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001430 return SideEffects(kAllChangeBits | kAllDependOnBits);
1431 }
1432
1433 static SideEffects AllChanges() {
1434 return SideEffects(kAllChangeBits);
1435 }
1436
1437 static SideEffects AllDependencies() {
1438 return SideEffects(kAllDependOnBits);
1439 }
1440
1441 static SideEffects AllExceptGCDependency() {
1442 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1443 }
1444
1445 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001446 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001447 }
1448
Aart Bik34c3ba92015-07-20 14:08:59 -07001449 static SideEffects AllWrites() {
1450 return SideEffects(kAllWrites);
1451 }
1452
1453 static SideEffects AllReads() {
1454 return SideEffects(kAllReads);
1455 }
1456
1457 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1458 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001459 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001460 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001461 }
1462
Aart Bik854a02b2015-07-14 16:07:00 -07001463 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1464 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001465 }
1466
Aart Bik34c3ba92015-07-20 14:08:59 -07001467 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1468 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001469 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001470 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001471 }
1472
1473 static SideEffects ArrayReadOfType(Primitive::Type type) {
1474 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1475 }
1476
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001477 static SideEffects CanTriggerGC() {
1478 return SideEffects(1ULL << kCanTriggerGCBit);
1479 }
1480
1481 static SideEffects DependsOnGC() {
1482 return SideEffects(1ULL << kDependsOnGCBit);
1483 }
1484
Aart Bik854a02b2015-07-14 16:07:00 -07001485 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001486 SideEffects Union(SideEffects other) const {
1487 return SideEffects(flags_ | other.flags_);
1488 }
1489
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001490 SideEffects Exclusion(SideEffects other) const {
1491 return SideEffects(flags_ & ~other.flags_);
1492 }
1493
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001494 void Add(SideEffects other) {
1495 flags_ |= other.flags_;
1496 }
1497
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001498 bool Includes(SideEffects other) const {
1499 return (other.flags_ & flags_) == other.flags_;
1500 }
1501
1502 bool HasSideEffects() const {
1503 return (flags_ & kAllChangeBits);
1504 }
1505
1506 bool HasDependencies() const {
1507 return (flags_ & kAllDependOnBits);
1508 }
1509
1510 // Returns true if there are no side effects or dependencies.
1511 bool DoesNothing() const {
1512 return flags_ == 0;
1513 }
1514
Aart Bik854a02b2015-07-14 16:07:00 -07001515 // Returns true if something is written.
1516 bool DoesAnyWrite() const {
1517 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001518 }
1519
Aart Bik854a02b2015-07-14 16:07:00 -07001520 // Returns true if something is read.
1521 bool DoesAnyRead() const {
1522 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001523 }
1524
Aart Bik854a02b2015-07-14 16:07:00 -07001525 // Returns true if potentially everything is written and read
1526 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001527 bool DoesAllReadWrite() const {
1528 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1529 }
1530
Aart Bik854a02b2015-07-14 16:07:00 -07001531 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001532 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001533 }
1534
Roland Levillain0d5a2812015-11-13 10:07:31 +00001535 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001536 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001537 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1538 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001539 }
1540
1541 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001542 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001543 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001544 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001545 for (int s = kLastBit; s >= 0; s--) {
1546 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1547 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1548 // This is a bit for the GC side effect.
1549 if (current_bit_is_set) {
1550 flags += "GC";
1551 }
Aart Bik854a02b2015-07-14 16:07:00 -07001552 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001553 } else {
1554 // This is a bit for the array/field analysis.
1555 // The underscore character stands for the 'can trigger GC' bit.
1556 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1557 if (current_bit_is_set) {
1558 flags += kDebug[s];
1559 }
1560 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1561 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1562 flags += "|";
1563 }
1564 }
Aart Bik854a02b2015-07-14 16:07:00 -07001565 }
1566 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001567 }
1568
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001569 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001570
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001571 private:
1572 static constexpr int kFieldArrayAnalysisBits = 9;
1573
1574 static constexpr int kFieldWriteOffset = 0;
1575 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1576 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1577 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1578
1579 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1580
1581 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1582 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1583 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1584 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1585
1586 static constexpr int kLastBit = kDependsOnGCBit;
1587 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1588
1589 // Aliases.
1590
1591 static_assert(kChangeBits == kDependOnBits,
1592 "the 'change' bits should match the 'depend on' bits.");
1593
1594 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1595 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1596 static constexpr uint64_t kAllWrites =
1597 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1598 static constexpr uint64_t kAllReads =
1599 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001600
Aart Bik854a02b2015-07-14 16:07:00 -07001601 // Work around the fact that HIR aliases I/F and J/D.
1602 // TODO: remove this interceptor once HIR types are clean
1603 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1604 switch (type) {
1605 case Primitive::kPrimInt:
1606 case Primitive::kPrimFloat:
1607 return TypeFlag(Primitive::kPrimInt, offset) |
1608 TypeFlag(Primitive::kPrimFloat, offset);
1609 case Primitive::kPrimLong:
1610 case Primitive::kPrimDouble:
1611 return TypeFlag(Primitive::kPrimLong, offset) |
1612 TypeFlag(Primitive::kPrimDouble, offset);
1613 default:
1614 return TypeFlag(type, offset);
1615 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001616 }
1617
Aart Bik854a02b2015-07-14 16:07:00 -07001618 // Translates type to bit flag.
1619 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1620 CHECK_NE(type, Primitive::kPrimVoid);
1621 const uint64_t one = 1;
1622 const int shift = type; // 0-based consecutive enum
1623 DCHECK_LE(kFieldWriteOffset, shift);
1624 DCHECK_LT(shift, kArrayWriteOffset);
1625 return one << (type + offset);
1626 }
1627
1628 // Private constructor on direct flags value.
1629 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1630
1631 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001632};
1633
David Brazdiled596192015-01-23 10:39:45 +00001634// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001635class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001636 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001637 HEnvironment(ArenaAllocator* arena,
1638 size_t number_of_vregs,
1639 const DexFile& dex_file,
1640 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001641 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001642 InvokeType invoke_type,
1643 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001644 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1645 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001646 parent_(nullptr),
1647 dex_file_(dex_file),
1648 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001649 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001650 invoke_type_(invoke_type),
1651 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001652 }
1653
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001654 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001655 : HEnvironment(arena,
1656 to_copy.Size(),
1657 to_copy.GetDexFile(),
1658 to_copy.GetMethodIdx(),
1659 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001660 to_copy.GetInvokeType(),
1661 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001662
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001663 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001664 if (parent_ != nullptr) {
1665 parent_->SetAndCopyParentChain(allocator, parent);
1666 } else {
1667 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1668 parent_->CopyFrom(parent);
1669 if (parent->GetParent() != nullptr) {
1670 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1671 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001672 }
David Brazdiled596192015-01-23 10:39:45 +00001673 }
1674
Vladimir Marko71bf8092015-09-15 15:33:14 +01001675 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001676 void CopyFrom(HEnvironment* environment);
1677
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001678 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1679 // input to the loop phi instead. This is for inserting instructions that
1680 // require an environment (like HDeoptimization) in the loop pre-header.
1681 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001682
1683 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001684 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001685 }
1686
1687 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001688 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001689 }
1690
David Brazdil1abb4192015-02-17 18:33:36 +00001691 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001692
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001693 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001694
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001695 HEnvironment* GetParent() const { return parent_; }
1696
1697 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001698 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001699 }
1700
1701 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001702 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001703 }
1704
1705 uint32_t GetDexPc() const {
1706 return dex_pc_;
1707 }
1708
1709 uint32_t GetMethodIdx() const {
1710 return method_idx_;
1711 }
1712
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001713 InvokeType GetInvokeType() const {
1714 return invoke_type_;
1715 }
1716
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001717 const DexFile& GetDexFile() const {
1718 return dex_file_;
1719 }
1720
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001721 HInstruction* GetHolder() const {
1722 return holder_;
1723 }
1724
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001725
1726 bool IsFromInlinedInvoke() const {
1727 return GetParent() != nullptr;
1728 }
1729
David Brazdiled596192015-01-23 10:39:45 +00001730 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001731 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1732 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001733 HEnvironment* parent_;
1734 const DexFile& dex_file_;
1735 const uint32_t method_idx_;
1736 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001737 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001738
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001739 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001740 HInstruction* const holder_;
1741
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001742 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001743
1744 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1745};
1746
Vladimir Markof9f64412015-09-02 14:05:49 +01001747class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001748 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001749 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001750 : previous_(nullptr),
1751 next_(nullptr),
1752 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001753 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001754 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001755 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001756 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001757 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001758 locations_(nullptr),
1759 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001760 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001761 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001762 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1763 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1764 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001765
Dave Allison20dfc792014-06-16 20:44:29 -07001766 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001767
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001768#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001769 enum InstructionKind {
1770 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1771 };
1772#undef DECLARE_KIND
1773
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001774 HInstruction* GetNext() const { return next_; }
1775 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001776
Calin Juravle77520bc2015-01-12 18:45:46 +00001777 HInstruction* GetNextDisregardingMoves() const;
1778 HInstruction* GetPreviousDisregardingMoves() const;
1779
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001780 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001781 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001782 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001783 bool IsInBlock() const { return block_ != nullptr; }
1784 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001785 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1786 bool IsIrreducibleLoopHeaderPhi() const {
1787 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1788 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001789
Roland Levillain6b879dd2014-09-22 17:13:44 +01001790 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001791 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001792
1793 virtual void Accept(HGraphVisitor* visitor) = 0;
1794 virtual const char* DebugName() const = 0;
1795
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001796 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001797 void SetRawInputAt(size_t index, HInstruction* input) {
1798 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1799 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001800
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001801 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001802
1803 uint32_t GetDexPc() const { return dex_pc_; }
1804
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001805 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001806
Roland Levillaine161a2a2014-10-03 12:45:18 +01001807 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001808 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001809
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001810 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001811 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001812
Calin Juravle10e244f2015-01-26 18:54:32 +00001813 // Does not apply for all instructions, but having this at top level greatly
1814 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001815 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001816 virtual bool CanBeNull() const {
1817 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1818 return true;
1819 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001820
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001821 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001822 return false;
1823 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001824
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001825 virtual bool IsActualObject() const {
1826 return GetType() == Primitive::kPrimNot;
1827 }
1828
Calin Juravle2e768302015-07-28 14:41:11 +00001829 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001830
Calin Juravle61d544b2015-02-23 16:46:57 +00001831 ReferenceTypeInfo GetReferenceTypeInfo() const {
1832 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001833 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Markoa4336d22016-04-19 14:12:13 +00001834 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001835 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001836
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001837 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001838 DCHECK(user != nullptr);
Vladimir Markod59f3b12016-03-29 12:21:58 +01001839 // Note: fixup_end remains valid across push_front().
1840 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1841 HUseListNode<HInstruction*>* new_node =
1842 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1843 uses_.push_front(*new_node);
1844 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001845 }
1846
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001847 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001848 DCHECK(user != nullptr);
Vladimir Markod59f3b12016-03-29 12:21:58 +01001849 // Note: env_fixup_end remains valid across push_front().
1850 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1851 HUseListNode<HEnvironment*>* new_node =
1852 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1853 env_uses_.push_front(*new_node);
1854 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001855 }
1856
David Brazdil1abb4192015-02-17 18:33:36 +00001857 void RemoveAsUserOfInput(size_t input) {
1858 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Markod59f3b12016-03-29 12:21:58 +01001859 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1860 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1861 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001862 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001863
David Brazdil1abb4192015-02-17 18:33:36 +00001864 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1865 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001866
Vladimir Markod59f3b12016-03-29 12:21:58 +01001867 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1868 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1869 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001870 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Markod59f3b12016-03-29 12:21:58 +01001871 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001872 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001873
Roland Levillain6c82d402014-10-13 16:10:27 +01001874 // Does this instruction strictly dominate `other_instruction`?
1875 // Returns false if this instruction and `other_instruction` are the same.
1876 // Aborts if this instruction and `other_instruction` are both phis.
1877 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001878
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001879 int GetId() const { return id_; }
1880 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001881
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001882 int GetSsaIndex() const { return ssa_index_; }
1883 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1884 bool HasSsaIndex() const { return ssa_index_ != -1; }
1885
1886 bool HasEnvironment() const { return environment_ != nullptr; }
1887 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001888 // Set the `environment_` field. Raw because this method does not
1889 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001890 void SetRawEnvironment(HEnvironment* environment) {
1891 DCHECK(environment_ == nullptr);
1892 DCHECK_EQ(environment->GetHolder(), this);
1893 environment_ = environment;
1894 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001895
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001896 void RemoveEnvironment();
1897
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001898 // Set the environment of this instruction, copying it from `environment`. While
1899 // copying, the uses lists are being updated.
1900 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001901 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001902 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001903 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001904 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001905 if (environment->GetParent() != nullptr) {
1906 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1907 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001908 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001909
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001910 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1911 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001912 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001913 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001914 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001915 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001916 if (environment->GetParent() != nullptr) {
1917 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1918 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001919 }
1920
Nicolas Geoffray39468442014-09-02 15:17:15 +01001921 // Returns the number of entries in the environment. Typically, that is the
1922 // number of dex registers in a method. It could be more in case of inlining.
1923 size_t EnvironmentSize() const;
1924
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001925 LocationSummary* GetLocations() const { return locations_; }
1926 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001927
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001928 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001929 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001930
Alexandre Rames188d4312015-04-09 18:30:21 +01001931 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1932 // uses of this instruction by `other` are *not* updated.
1933 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1934 ReplaceWith(other);
1935 other->ReplaceInput(this, use_index);
1936 }
1937
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001938 // Move `this` instruction before `cursor`.
1939 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001940
Vladimir Markofb337ea2015-11-25 15:25:10 +00001941 // Move `this` before its first user and out of any loops. If there is no
1942 // out-of-loop user that dominates all other users, move the instruction
1943 // to the end of the out-of-loop common dominator of the user's blocks.
1944 //
1945 // This can be used only on non-throwing instructions with no side effects that
1946 // have at least one use but no environment uses.
1947 void MoveBeforeFirstUserAndOutOfLoops();
1948
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001949#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001950 bool Is##type() const; \
1951 const H##type* As##type() const; \
1952 H##type* As##type();
1953
1954 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1955#undef INSTRUCTION_TYPE_CHECK
1956
1957#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001958 bool Is##type() const { return (As##type() != nullptr); } \
1959 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001960 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001961 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001962#undef INSTRUCTION_TYPE_CHECK
1963
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001964 // Returns whether the instruction can be moved within the graph.
1965 virtual bool CanBeMoved() const { return false; }
1966
1967 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001968 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001969 return false;
1970 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001971
1972 // Returns whether any data encoded in the two instructions is equal.
1973 // This method does not look at the inputs. Both instructions must be
1974 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001975 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001976 return false;
1977 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001978
1979 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001980 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001981 // 2) Their inputs are identical.
1982 bool Equals(HInstruction* other) const;
1983
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001984 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
1985 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
1986 // the virtual function because the __attribute__((__pure__)) doesn't really
1987 // apply the strong requirement for virtual functions, preventing optimizations.
1988 InstructionKind GetKind() const PURE;
1989 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001990
1991 virtual size_t ComputeHashCode() const {
1992 size_t result = GetKind();
1993 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1994 result = (result * 31) + InputAt(i)->GetId();
1995 }
1996 return result;
1997 }
1998
1999 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002000 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002001 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002002
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002003 size_t GetLifetimePosition() const { return lifetime_position_; }
2004 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2005 LiveInterval* GetLiveInterval() const { return live_interval_; }
2006 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2007 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2008
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002009 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2010
2011 // Returns whether the code generation of the instruction will require to have access
2012 // to the current method. Such instructions are:
2013 // (1): Instructions that require an environment, as calling the runtime requires
2014 // to walk the stack and have the current method stored at a specific stack address.
2015 // (2): Object literals like classes and strings, that are loaded from the dex cache
2016 // fields of the current method.
2017 bool NeedsCurrentMethod() const {
2018 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2019 }
2020
Vladimir Markodc151b22015-10-15 18:02:30 +01002021 // Returns whether the code generation of the instruction will require to have access
2022 // to the dex cache of the current method's declaring class via the current method.
2023 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002024
Mark Mendellc4701932015-04-10 13:18:51 -04002025 // Does this instruction have any use in an environment before
2026 // control flow hits 'other'?
2027 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2028
2029 // Remove all references to environment uses of this instruction.
2030 // The caller must ensure that this is safe to do.
2031 void RemoveEnvironmentUsers();
2032
Vladimir Markoa1de9182016-02-25 11:37:38 +00002033 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2034 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002035
David Brazdil1abb4192015-02-17 18:33:36 +00002036 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002037 // If set, the machine code for this instruction is assumed to be generated by
2038 // its users. Used by liveness analysis to compute use positions accordingly.
2039 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2040 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2041 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2042 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2043
David Brazdil1abb4192015-02-17 18:33:36 +00002044 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2045 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2046
Vladimir Markoa1de9182016-02-25 11:37:38 +00002047 uint32_t GetPackedFields() const {
2048 return packed_fields_;
2049 }
2050
2051 template <size_t flag>
2052 bool GetPackedFlag() const {
2053 return (packed_fields_ & (1u << flag)) != 0u;
2054 }
2055
2056 template <size_t flag>
2057 void SetPackedFlag(bool value = true) {
2058 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2059 }
2060
2061 template <typename BitFieldType>
2062 typename BitFieldType::value_type GetPackedField() const {
2063 return BitFieldType::Decode(packed_fields_);
2064 }
2065
2066 template <typename BitFieldType>
2067 void SetPackedField(typename BitFieldType::value_type value) {
2068 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2069 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2070 }
2071
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002072 private:
Vladimir Markod59f3b12016-03-29 12:21:58 +01002073 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2074 auto before_use_node = uses_.before_begin();
2075 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2076 HInstruction* user = use_node->GetUser();
2077 size_t input_index = use_node->GetIndex();
2078 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2079 before_use_node = use_node;
2080 }
2081 }
2082
2083 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2084 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2085 if (next != uses_.end()) {
2086 HInstruction* next_user = next->GetUser();
2087 size_t next_index = next->GetIndex();
2088 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2089 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2090 }
2091 }
2092
2093 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2094 auto before_env_use_node = env_uses_.before_begin();
2095 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2096 HEnvironment* user = env_use_node->GetUser();
2097 size_t input_index = env_use_node->GetIndex();
2098 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2099 before_env_use_node = env_use_node;
2100 }
2101 }
2102
2103 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2104 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2105 if (next != env_uses_.end()) {
2106 HEnvironment* next_user = next->GetUser();
2107 size_t next_index = next->GetIndex();
2108 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2109 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2110 }
2111 }
David Brazdil1abb4192015-02-17 18:33:36 +00002112
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002113 HInstruction* previous_;
2114 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002115 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002116 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002117
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002118 // An instruction gets an id when it is added to the graph.
2119 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002120 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002121 int id_;
2122
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002123 // When doing liveness analysis, instructions that have uses get an SSA index.
2124 int ssa_index_;
2125
Vladimir Markoa1de9182016-02-25 11:37:38 +00002126 // Packed fields.
2127 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002128
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002129 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002130 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002131
2132 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002133 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002134
Nicolas Geoffray39468442014-09-02 15:17:15 +01002135 // The environment associated with this instruction. Not null if the instruction
2136 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002137 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002138
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002139 // Set by the code generator.
2140 LocationSummary* locations_;
2141
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002142 // Set by the liveness analysis.
2143 LiveInterval* live_interval_;
2144
2145 // Set by the liveness analysis, this is the position in a linear
2146 // order of blocks where this instruction's live interval start.
2147 size_t lifetime_position_;
2148
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002149 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002150
Vladimir Markoa1de9182016-02-25 11:37:38 +00002151 // The reference handle part of the reference type info.
2152 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002153 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002154 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002155
David Brazdil1abb4192015-02-17 18:33:36 +00002156 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002157 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002158 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002159 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002160 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002161
2162 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2163};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002164std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002165
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002166class HInputIterator : public ValueObject {
2167 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002168 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002169
2170 bool Done() const { return index_ == instruction_->InputCount(); }
2171 HInstruction* Current() const { return instruction_->InputAt(index_); }
2172 void Advance() { index_++; }
2173
2174 private:
2175 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002176 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002177
2178 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2179};
2180
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002181class HInstructionIterator : public ValueObject {
2182 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002183 explicit HInstructionIterator(const HInstructionList& instructions)
2184 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002185 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002186 }
2187
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002188 bool Done() const { return instruction_ == nullptr; }
2189 HInstruction* Current() const { return instruction_; }
2190 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002191 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002192 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002193 }
2194
2195 private:
2196 HInstruction* instruction_;
2197 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002198
2199 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002200};
2201
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002202class HBackwardInstructionIterator : public ValueObject {
2203 public:
2204 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2205 : instruction_(instructions.last_instruction_) {
2206 next_ = Done() ? nullptr : instruction_->GetPrevious();
2207 }
2208
2209 bool Done() const { return instruction_ == nullptr; }
2210 HInstruction* Current() const { return instruction_; }
2211 void Advance() {
2212 instruction_ = next_;
2213 next_ = Done() ? nullptr : instruction_->GetPrevious();
2214 }
2215
2216 private:
2217 HInstruction* instruction_;
2218 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002219
2220 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002221};
2222
Vladimir Markof9f64412015-09-02 14:05:49 +01002223template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002224class HTemplateInstruction: public HInstruction {
2225 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002226 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002227 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002228 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002229
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002230 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002231
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002232 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002233 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2234 DCHECK_LT(i, N);
2235 return inputs_[i];
2236 }
David Brazdil1abb4192015-02-17 18:33:36 +00002237
2238 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002239 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002240 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002241 }
2242
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002243 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002244 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002245
2246 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002247};
2248
Vladimir Markof9f64412015-09-02 14:05:49 +01002249// HTemplateInstruction specialization for N=0.
2250template<>
2251class HTemplateInstruction<0>: public HInstruction {
2252 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002253 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002254 : HInstruction(side_effects, dex_pc) {}
2255
Vladimir Markof9f64412015-09-02 14:05:49 +01002256 virtual ~HTemplateInstruction() {}
2257
2258 size_t InputCount() const OVERRIDE { return 0; }
2259
2260 protected:
2261 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2262 LOG(FATAL) << "Unreachable";
2263 UNREACHABLE();
2264 }
2265
2266 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2267 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2268 LOG(FATAL) << "Unreachable";
2269 UNREACHABLE();
2270 }
2271
2272 private:
2273 friend class SsaBuilder;
2274};
2275
Dave Allison20dfc792014-06-16 20:44:29 -07002276template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002277class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002278 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002279 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002280 : HTemplateInstruction<N>(side_effects, dex_pc) {
2281 this->template SetPackedField<TypeField>(type);
2282 }
Dave Allison20dfc792014-06-16 20:44:29 -07002283 virtual ~HExpression() {}
2284
Vladimir Markoa1de9182016-02-25 11:37:38 +00002285 Primitive::Type GetType() const OVERRIDE {
2286 return TypeField::Decode(this->GetPackedFields());
2287 }
Dave Allison20dfc792014-06-16 20:44:29 -07002288
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002289 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002290 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2291 static constexpr size_t kFieldTypeSize =
2292 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2293 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2294 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2295 "Too many packed fields.");
2296 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002297};
2298
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002299// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2300// instruction that branches to the exit block.
2301class HReturnVoid : public HTemplateInstruction<0> {
2302 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002303 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2304 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002305
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002306 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002307
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002308 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002309
2310 private:
2311 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2312};
2313
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002314// Represents dex's RETURN opcodes. A HReturn is a control flow
2315// instruction that branches to the exit block.
2316class HReturn : public HTemplateInstruction<1> {
2317 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002318 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2319 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002320 SetRawInputAt(0, value);
2321 }
2322
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002323 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002324
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002325 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002326
2327 private:
2328 DISALLOW_COPY_AND_ASSIGN(HReturn);
2329};
2330
David Brazdildee58d62016-04-07 09:54:26 +00002331class HPhi : public HInstruction {
2332 public:
2333 HPhi(ArenaAllocator* arena,
2334 uint32_t reg_number,
2335 size_t number_of_inputs,
2336 Primitive::Type type,
2337 uint32_t dex_pc = kNoDexPc)
2338 : HInstruction(SideEffects::None(), dex_pc),
2339 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2340 reg_number_(reg_number) {
2341 SetPackedField<TypeField>(ToPhiType(type));
2342 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2343 // Phis are constructed live and marked dead if conflicting or unused.
2344 // Individual steps of SsaBuilder should assume that if a phi has been
2345 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2346 SetPackedFlag<kFlagIsLive>(true);
2347 SetPackedFlag<kFlagCanBeNull>(true);
2348 }
2349
2350 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2351 static Primitive::Type ToPhiType(Primitive::Type type) {
2352 return Primitive::PrimitiveKind(type);
2353 }
2354
2355 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2356
2357 size_t InputCount() const OVERRIDE { return inputs_.size(); }
2358
2359 void AddInput(HInstruction* input);
2360 void RemoveInputAt(size_t index);
2361
2362 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2363 void SetType(Primitive::Type new_type) {
2364 // Make sure that only valid type changes occur. The following are allowed:
2365 // (1) int -> float/ref (primitive type propagation),
2366 // (2) long -> double (primitive type propagation).
2367 DCHECK(GetType() == new_type ||
2368 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2369 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2370 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2371 SetPackedField<TypeField>(new_type);
2372 }
2373
2374 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2375 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2376
2377 uint32_t GetRegNumber() const { return reg_number_; }
2378
2379 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2380 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2381 bool IsDead() const { return !IsLive(); }
2382 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2383
2384 bool IsVRegEquivalentOf(HInstruction* other) const {
2385 return other != nullptr
2386 && other->IsPhi()
2387 && other->AsPhi()->GetBlock() == GetBlock()
2388 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2389 }
2390
2391 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2392 // An equivalent phi is a phi having the same dex register and type.
2393 // It assumes that phis with the same dex register are adjacent.
2394 HPhi* GetNextEquivalentPhiWithSameType() {
2395 HInstruction* next = GetNext();
2396 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2397 if (next->GetType() == GetType()) {
2398 return next->AsPhi();
2399 }
2400 next = next->GetNext();
2401 }
2402 return nullptr;
2403 }
2404
2405 DECLARE_INSTRUCTION(Phi);
2406
2407 protected:
2408 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
2409 return inputs_[index];
2410 }
2411
2412 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2413 inputs_[index] = input;
2414 }
2415
2416 private:
2417 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2418 static constexpr size_t kFieldTypeSize =
2419 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2420 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2421 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2422 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2423 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2424 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2425
2426 ArenaVector<HUserRecord<HInstruction*> > inputs_;
2427 const uint32_t reg_number_;
2428
2429 DISALLOW_COPY_AND_ASSIGN(HPhi);
2430};
2431
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002432// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002433// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002434// exit block.
2435class HExit : public HTemplateInstruction<0> {
2436 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002437 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002438
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002439 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002440
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002441 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002442
2443 private:
2444 DISALLOW_COPY_AND_ASSIGN(HExit);
2445};
2446
2447// Jumps from one block to another.
2448class HGoto : public HTemplateInstruction<0> {
2449 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002450 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002451
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002452 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002453
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002454 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002455 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002456 }
2457
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002458 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002459
2460 private:
2461 DISALLOW_COPY_AND_ASSIGN(HGoto);
2462};
2463
Roland Levillain9867bc72015-08-05 10:21:34 +01002464class HConstant : public HExpression<0> {
2465 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002466 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2467 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002468
2469 bool CanBeMoved() const OVERRIDE { return true; }
2470
Roland Levillain1a653882016-03-18 18:05:57 +00002471 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002472 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002473 // Is this constant 0 in the arithmetic sense?
2474 virtual bool IsArithmeticZero() const { return false; }
2475 // Is this constant a 0-bit pattern?
2476 virtual bool IsZeroBitPattern() const { return false; }
2477 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002478 virtual bool IsOne() const { return false; }
2479
David Brazdil77a48ae2015-09-15 12:34:04 +00002480 virtual uint64_t GetValueAsUint64() const = 0;
2481
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002482 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002483
2484 private:
2485 DISALLOW_COPY_AND_ASSIGN(HConstant);
2486};
2487
2488class HNullConstant : public HConstant {
2489 public:
2490 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2491 return true;
2492 }
2493
David Brazdil77a48ae2015-09-15 12:34:04 +00002494 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2495
Roland Levillain9867bc72015-08-05 10:21:34 +01002496 size_t ComputeHashCode() const OVERRIDE { return 0; }
2497
Roland Levillain1a653882016-03-18 18:05:57 +00002498 // The null constant representation is a 0-bit pattern.
2499 virtual bool IsZeroBitPattern() const { return true; }
2500
Roland Levillain9867bc72015-08-05 10:21:34 +01002501 DECLARE_INSTRUCTION(NullConstant);
2502
2503 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002504 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002505
2506 friend class HGraph;
2507 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2508};
2509
2510// Constants of the type int. Those can be from Dex instructions, or
2511// synthesized (for example with the if-eqz instruction).
2512class HIntConstant : public HConstant {
2513 public:
2514 int32_t GetValue() const { return value_; }
2515
David Brazdil9f389d42015-10-01 14:32:56 +01002516 uint64_t GetValueAsUint64() const OVERRIDE {
2517 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2518 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002519
Roland Levillain9867bc72015-08-05 10:21:34 +01002520 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002521 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002522 return other->AsIntConstant()->value_ == value_;
2523 }
2524
2525 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2526
2527 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002528 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2529 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002530 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2531
Roland Levillain1a653882016-03-18 18:05:57 +00002532 // Integer constants are used to encode Boolean values as well,
2533 // where 1 means true and 0 means false.
2534 bool IsTrue() const { return GetValue() == 1; }
2535 bool IsFalse() const { return GetValue() == 0; }
2536
Roland Levillain9867bc72015-08-05 10:21:34 +01002537 DECLARE_INSTRUCTION(IntConstant);
2538
2539 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002540 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2541 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2542 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2543 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002544
2545 const int32_t value_;
2546
2547 friend class HGraph;
2548 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2549 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2550 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2551};
2552
2553class HLongConstant : public HConstant {
2554 public:
2555 int64_t GetValue() const { return value_; }
2556
David Brazdil77a48ae2015-09-15 12:34:04 +00002557 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2558
Roland Levillain9867bc72015-08-05 10:21:34 +01002559 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002560 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002561 return other->AsLongConstant()->value_ == value_;
2562 }
2563
2564 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2565
2566 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002567 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2568 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002569 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2570
2571 DECLARE_INSTRUCTION(LongConstant);
2572
2573 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002574 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2575 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002576
2577 const int64_t value_;
2578
2579 friend class HGraph;
2580 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2581};
Dave Allison20dfc792014-06-16 20:44:29 -07002582
Roland Levillain31dd3d62016-02-16 12:21:02 +00002583class HFloatConstant : public HConstant {
2584 public:
2585 float GetValue() const { return value_; }
2586
2587 uint64_t GetValueAsUint64() const OVERRIDE {
2588 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2589 }
2590
2591 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2592 DCHECK(other->IsFloatConstant()) << other->DebugName();
2593 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2594 }
2595
2596 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2597
2598 bool IsMinusOne() const OVERRIDE {
2599 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2600 }
Roland Levillain1a653882016-03-18 18:05:57 +00002601 bool IsArithmeticZero() const OVERRIDE {
2602 return std::fpclassify(value_) == FP_ZERO;
2603 }
2604 bool IsArithmeticPositiveZero() const {
2605 return IsArithmeticZero() && !std::signbit(value_);
2606 }
2607 bool IsArithmeticNegativeZero() const {
2608 return IsArithmeticZero() && std::signbit(value_);
2609 }
2610 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002611 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002612 }
2613 bool IsOne() const OVERRIDE {
2614 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2615 }
2616 bool IsNaN() const {
2617 return std::isnan(value_);
2618 }
2619
2620 DECLARE_INSTRUCTION(FloatConstant);
2621
2622 private:
2623 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2624 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2625 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2626 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2627
2628 const float value_;
2629
2630 // Only the SsaBuilder and HGraph can create floating-point constants.
2631 friend class SsaBuilder;
2632 friend class HGraph;
2633 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2634};
2635
2636class HDoubleConstant : public HConstant {
2637 public:
2638 double GetValue() const { return value_; }
2639
2640 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2641
2642 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2643 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2644 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2645 }
2646
2647 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2648
2649 bool IsMinusOne() const OVERRIDE {
2650 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2651 }
Roland Levillain1a653882016-03-18 18:05:57 +00002652 bool IsArithmeticZero() const OVERRIDE {
2653 return std::fpclassify(value_) == FP_ZERO;
2654 }
2655 bool IsArithmeticPositiveZero() const {
2656 return IsArithmeticZero() && !std::signbit(value_);
2657 }
2658 bool IsArithmeticNegativeZero() const {
2659 return IsArithmeticZero() && std::signbit(value_);
2660 }
2661 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002662 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002663 }
2664 bool IsOne() const OVERRIDE {
2665 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2666 }
2667 bool IsNaN() const {
2668 return std::isnan(value_);
2669 }
2670
2671 DECLARE_INSTRUCTION(DoubleConstant);
2672
2673 private:
2674 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2675 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2676 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2677 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2678
2679 const double value_;
2680
2681 // Only the SsaBuilder and HGraph can create floating-point constants.
2682 friend class SsaBuilder;
2683 friend class HGraph;
2684 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2685};
2686
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002687// Conditional branch. A block ending with an HIf instruction must have
2688// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002689class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002690 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002691 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2692 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002693 SetRawInputAt(0, input);
2694 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002695
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002696 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002697
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002698 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002699 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002700 }
2701
2702 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002703 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002704 }
2705
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002706 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002707
2708 private:
2709 DISALLOW_COPY_AND_ASSIGN(HIf);
2710};
2711
David Brazdilfc6a86a2015-06-26 10:33:45 +00002712
2713// Abstract instruction which marks the beginning and/or end of a try block and
2714// links it to the respective exception handlers. Behaves the same as a Goto in
2715// non-exceptional control flow.
2716// Normal-flow successor is stored at index zero, exception handlers under
2717// higher indices in no particular order.
2718class HTryBoundary : public HTemplateInstruction<0> {
2719 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002720 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002721 kEntry,
2722 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002723 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002724 };
2725
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002726 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002727 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2728 SetPackedField<BoundaryKindField>(kind);
2729 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002730
2731 bool IsControlFlow() const OVERRIDE { return true; }
2732
2733 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002734 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002735
David Brazdild26a4112015-11-10 11:07:31 +00002736 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2737 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2738 }
2739
David Brazdilfc6a86a2015-06-26 10:33:45 +00002740 // Returns whether `handler` is among its exception handlers (non-zero index
2741 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002742 bool HasExceptionHandler(const HBasicBlock& handler) const {
2743 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002744 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002745 }
2746
2747 // If not present already, adds `handler` to its block's list of exception
2748 // handlers.
2749 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002750 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002751 GetBlock()->AddSuccessor(handler);
2752 }
2753 }
2754
Vladimir Markoa1de9182016-02-25 11:37:38 +00002755 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2756 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002757
David Brazdilffee3d32015-07-06 11:48:53 +01002758 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2759
David Brazdilfc6a86a2015-06-26 10:33:45 +00002760 DECLARE_INSTRUCTION(TryBoundary);
2761
2762 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002763 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2764 static constexpr size_t kFieldBoundaryKindSize =
2765 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2766 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2767 kFieldBoundaryKind + kFieldBoundaryKindSize;
2768 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2769 "Too many packed fields.");
2770 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002771
2772 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2773};
2774
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002775// Deoptimize to interpreter, upon checking a condition.
2776class HDeoptimize : public HTemplateInstruction<1> {
2777 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002778 // We set CanTriggerGC to prevent any intermediate address to be live
2779 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002780 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002781 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002782 SetRawInputAt(0, cond);
2783 }
2784
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002785 bool CanBeMoved() const OVERRIDE { return true; }
2786 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2787 return true;
2788 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002789 bool NeedsEnvironment() const OVERRIDE { return true; }
2790 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002791
2792 DECLARE_INSTRUCTION(Deoptimize);
2793
2794 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002795 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2796};
2797
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002798// Represents the ArtMethod that was passed as a first argument to
2799// the method. It is used by instructions that depend on it, like
2800// instructions that work with the dex cache.
2801class HCurrentMethod : public HExpression<0> {
2802 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002803 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2804 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002805
2806 DECLARE_INSTRUCTION(CurrentMethod);
2807
2808 private:
2809 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2810};
2811
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002812// Fetches an ArtMethod from the virtual table or the interface method table
2813// of a class.
2814class HClassTableGet : public HExpression<1> {
2815 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002816 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002817 kVTable,
2818 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002819 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002820 };
2821 HClassTableGet(HInstruction* cls,
2822 Primitive::Type type,
2823 TableKind kind,
2824 size_t index,
2825 uint32_t dex_pc)
2826 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002827 index_(index) {
2828 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002829 SetRawInputAt(0, cls);
2830 }
2831
2832 bool CanBeMoved() const OVERRIDE { return true; }
2833 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2834 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002835 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002836 }
2837
Vladimir Markoa1de9182016-02-25 11:37:38 +00002838 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002839 size_t GetIndex() const { return index_; }
2840
2841 DECLARE_INSTRUCTION(ClassTableGet);
2842
2843 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002844 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2845 static constexpr size_t kFieldTableKindSize =
2846 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2847 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2848 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2849 "Too many packed fields.");
2850 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2851
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002852 // The index of the ArtMethod in the table.
2853 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002854
2855 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2856};
2857
Mark Mendellfe57faa2015-09-18 09:26:15 -04002858// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2859// have one successor for each entry in the switch table, and the final successor
2860// will be the block containing the next Dex opcode.
2861class HPackedSwitch : public HTemplateInstruction<1> {
2862 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002863 HPackedSwitch(int32_t start_value,
2864 uint32_t num_entries,
2865 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002866 uint32_t dex_pc = kNoDexPc)
2867 : HTemplateInstruction(SideEffects::None(), dex_pc),
2868 start_value_(start_value),
2869 num_entries_(num_entries) {
2870 SetRawInputAt(0, input);
2871 }
2872
2873 bool IsControlFlow() const OVERRIDE { return true; }
2874
2875 int32_t GetStartValue() const { return start_value_; }
2876
Vladimir Marko211c2112015-09-24 16:52:33 +01002877 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002878
2879 HBasicBlock* GetDefaultBlock() const {
2880 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002881 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002882 }
2883 DECLARE_INSTRUCTION(PackedSwitch);
2884
2885 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002886 const int32_t start_value_;
2887 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002888
2889 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2890};
2891
Roland Levillain88cb1752014-10-20 16:36:47 +01002892class HUnaryOperation : public HExpression<1> {
2893 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002894 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2895 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002896 SetRawInputAt(0, input);
2897 }
2898
2899 HInstruction* GetInput() const { return InputAt(0); }
2900 Primitive::Type GetResultType() const { return GetType(); }
2901
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002902 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002903 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002904 return true;
2905 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002906
Roland Levillain31dd3d62016-02-16 12:21:02 +00002907 // Try to statically evaluate `this` and return a HConstant
2908 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002909 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002910 HConstant* TryStaticEvaluation() const;
2911
2912 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002913 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2914 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002915 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2916 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002917
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002918 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002919
2920 private:
2921 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2922};
2923
Dave Allison20dfc792014-06-16 20:44:29 -07002924class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002925 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002926 HBinaryOperation(Primitive::Type result_type,
2927 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002928 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002929 SideEffects side_effects = SideEffects::None(),
2930 uint32_t dex_pc = kNoDexPc)
2931 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002932 SetRawInputAt(0, left);
2933 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002934 }
2935
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002936 HInstruction* GetLeft() const { return InputAt(0); }
2937 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002938 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002939
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002940 virtual bool IsCommutative() const { return false; }
2941
2942 // Put constant on the right.
2943 // Returns whether order is changed.
2944 bool OrderInputsWithConstantOnTheRight() {
2945 HInstruction* left = InputAt(0);
2946 HInstruction* right = InputAt(1);
2947 if (left->IsConstant() && !right->IsConstant()) {
2948 ReplaceInput(right, 0);
2949 ReplaceInput(left, 1);
2950 return true;
2951 }
2952 return false;
2953 }
2954
2955 // Order inputs by instruction id, but favor constant on the right side.
2956 // This helps GVN for commutative ops.
2957 void OrderInputs() {
2958 DCHECK(IsCommutative());
2959 HInstruction* left = InputAt(0);
2960 HInstruction* right = InputAt(1);
2961 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2962 return;
2963 }
2964 if (OrderInputsWithConstantOnTheRight()) {
2965 return;
2966 }
2967 // Order according to instruction id.
2968 if (left->GetId() > right->GetId()) {
2969 ReplaceInput(right, 0);
2970 ReplaceInput(left, 1);
2971 }
2972 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002973
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002974 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002975 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002976 return true;
2977 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002978
Roland Levillain31dd3d62016-02-16 12:21:02 +00002979 // Try to statically evaluate `this` and return a HConstant
2980 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002981 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002982 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002983
2984 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002985 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2986 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002987 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2988 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002989 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002990 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2991 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002992 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2993 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002994 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2995 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01002996 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002997 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2998 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002999
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003000 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003001 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003002 HConstant* GetConstantRight() const;
3003
3004 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003005 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003006 HInstruction* GetLeastConstantLeft() const;
3007
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003008 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003009
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003010 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003011 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3012};
3013
Mark Mendellc4701932015-04-10 13:18:51 -04003014// The comparison bias applies for floating point operations and indicates how NaN
3015// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003016enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003017 kNoBias, // bias is not applicable (i.e. for long operation)
3018 kGtBias, // return 1 for NaN comparisons
3019 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003020 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003021};
3022
Roland Levillain31dd3d62016-02-16 12:21:02 +00003023std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3024
Dave Allison20dfc792014-06-16 20:44:29 -07003025class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003026 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003027 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003028 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3029 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3030 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003031
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003032 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003033 // `instruction`, and disregard moves in between.
3034 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003035
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003036 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003037
3038 virtual IfCondition GetCondition() const = 0;
3039
Mark Mendellc4701932015-04-10 13:18:51 -04003040 virtual IfCondition GetOppositeCondition() const = 0;
3041
Vladimir Markoa1de9182016-02-25 11:37:38 +00003042 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003043 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3044
Vladimir Markoa1de9182016-02-25 11:37:38 +00003045 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3046 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003047
3048 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003049 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003050 }
3051
Roland Levillain4fa13f62015-07-06 18:11:54 +01003052 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003053 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003054 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003055 if (if_cond == kCondNE) {
3056 return true;
3057 } else if (if_cond == kCondEQ) {
3058 return false;
3059 }
3060 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003061 }
3062
3063 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003064 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003065 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003066 if (if_cond == kCondEQ) {
3067 return true;
3068 } else if (if_cond == kCondNE) {
3069 return false;
3070 }
3071 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003072 }
3073
Roland Levillain31dd3d62016-02-16 12:21:02 +00003074 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003075 // Needed if we merge a HCompare into a HCondition.
3076 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3077 static constexpr size_t kFieldComparisonBiasSize =
3078 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3079 static constexpr size_t kNumberOfConditionPackedBits =
3080 kFieldComparisonBias + kFieldComparisonBiasSize;
3081 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3082 using ComparisonBiasField =
3083 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3084
Roland Levillain31dd3d62016-02-16 12:21:02 +00003085 template <typename T>
3086 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3087
3088 template <typename T>
3089 int32_t CompareFP(T x, T y) const {
3090 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3091 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3092 // Handle the bias.
3093 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3094 }
3095
3096 // Return an integer constant containing the result of a condition evaluated at compile time.
3097 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3098 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3099 }
3100
Dave Allison20dfc792014-06-16 20:44:29 -07003101 private:
3102 DISALLOW_COPY_AND_ASSIGN(HCondition);
3103};
3104
3105// Instruction to check if two inputs are equal to each other.
3106class HEqual : public HCondition {
3107 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003108 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3109 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003110
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003111 bool IsCommutative() const OVERRIDE { return true; }
3112
Vladimir Marko9e23df52015-11-10 17:14:35 +00003113 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3114 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003115 return MakeConstantCondition(true, GetDexPc());
3116 }
3117 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3118 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3119 }
3120 // In the following Evaluate methods, a HCompare instruction has
3121 // been merged into this HEqual instruction; evaluate it as
3122 // `Compare(x, y) == 0`.
3123 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3124 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3125 GetDexPc());
3126 }
3127 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3128 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3129 }
3130 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3131 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003132 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003133
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003134 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003135
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003136 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003137 return kCondEQ;
3138 }
3139
Mark Mendellc4701932015-04-10 13:18:51 -04003140 IfCondition GetOppositeCondition() const OVERRIDE {
3141 return kCondNE;
3142 }
3143
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003144 private:
Aart Bike9f37602015-10-09 11:15:55 -07003145 template <typename T> bool Compute(T x, T y) const { return x == y; }
3146
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003147 DISALLOW_COPY_AND_ASSIGN(HEqual);
3148};
3149
Dave Allison20dfc792014-06-16 20:44:29 -07003150class HNotEqual : public HCondition {
3151 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003152 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3153 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003154
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003155 bool IsCommutative() const OVERRIDE { return true; }
3156
Vladimir Marko9e23df52015-11-10 17:14:35 +00003157 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3158 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003159 return MakeConstantCondition(false, GetDexPc());
3160 }
3161 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3162 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3163 }
3164 // In the following Evaluate methods, a HCompare instruction has
3165 // been merged into this HNotEqual instruction; evaluate it as
3166 // `Compare(x, y) != 0`.
3167 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3168 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3169 }
3170 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3171 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3172 }
3173 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3174 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003175 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003176
Dave Allison20dfc792014-06-16 20:44:29 -07003177 DECLARE_INSTRUCTION(NotEqual);
3178
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003179 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003180 return kCondNE;
3181 }
3182
Mark Mendellc4701932015-04-10 13:18:51 -04003183 IfCondition GetOppositeCondition() const OVERRIDE {
3184 return kCondEQ;
3185 }
3186
Dave Allison20dfc792014-06-16 20:44:29 -07003187 private:
Aart Bike9f37602015-10-09 11:15:55 -07003188 template <typename T> bool Compute(T x, T y) const { return x != y; }
3189
Dave Allison20dfc792014-06-16 20:44:29 -07003190 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3191};
3192
3193class HLessThan : public HCondition {
3194 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003195 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3196 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003197
Roland Levillain9867bc72015-08-05 10:21:34 +01003198 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003199 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003200 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003201 // In the following Evaluate methods, a HCompare instruction has
3202 // been merged into this HLessThan instruction; evaluate it as
3203 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003204 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003205 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3206 }
3207 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3208 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3209 }
3210 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3211 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003212 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003213
Dave Allison20dfc792014-06-16 20:44:29 -07003214 DECLARE_INSTRUCTION(LessThan);
3215
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003216 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003217 return kCondLT;
3218 }
3219
Mark Mendellc4701932015-04-10 13:18:51 -04003220 IfCondition GetOppositeCondition() const OVERRIDE {
3221 return kCondGE;
3222 }
3223
Dave Allison20dfc792014-06-16 20:44:29 -07003224 private:
Aart Bike9f37602015-10-09 11:15:55 -07003225 template <typename T> bool Compute(T x, T y) const { return x < y; }
3226
Dave Allison20dfc792014-06-16 20:44:29 -07003227 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3228};
3229
3230class HLessThanOrEqual : public HCondition {
3231 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003232 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3233 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003234
Roland Levillain9867bc72015-08-05 10:21:34 +01003235 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003236 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003237 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003238 // In the following Evaluate methods, a HCompare instruction has
3239 // been merged into this HLessThanOrEqual instruction; evaluate it as
3240 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003241 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003242 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3243 }
3244 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3245 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3246 }
3247 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3248 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003249 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003250
Dave Allison20dfc792014-06-16 20:44:29 -07003251 DECLARE_INSTRUCTION(LessThanOrEqual);
3252
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003253 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003254 return kCondLE;
3255 }
3256
Mark Mendellc4701932015-04-10 13:18:51 -04003257 IfCondition GetOppositeCondition() const OVERRIDE {
3258 return kCondGT;
3259 }
3260
Dave Allison20dfc792014-06-16 20:44:29 -07003261 private:
Aart Bike9f37602015-10-09 11:15:55 -07003262 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3263
Dave Allison20dfc792014-06-16 20:44:29 -07003264 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3265};
3266
3267class HGreaterThan : public HCondition {
3268 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003269 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3270 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003271
Roland Levillain9867bc72015-08-05 10:21:34 +01003272 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003273 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003274 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003275 // In the following Evaluate methods, a HCompare instruction has
3276 // been merged into this HGreaterThan instruction; evaluate it as
3277 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003278 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003279 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3280 }
3281 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3282 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3283 }
3284 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3285 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003286 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003287
Dave Allison20dfc792014-06-16 20:44:29 -07003288 DECLARE_INSTRUCTION(GreaterThan);
3289
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003290 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003291 return kCondGT;
3292 }
3293
Mark Mendellc4701932015-04-10 13:18:51 -04003294 IfCondition GetOppositeCondition() const OVERRIDE {
3295 return kCondLE;
3296 }
3297
Dave Allison20dfc792014-06-16 20:44:29 -07003298 private:
Aart Bike9f37602015-10-09 11:15:55 -07003299 template <typename T> bool Compute(T x, T y) const { return x > y; }
3300
Dave Allison20dfc792014-06-16 20:44:29 -07003301 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3302};
3303
3304class HGreaterThanOrEqual : public HCondition {
3305 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003306 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3307 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003308
Roland Levillain9867bc72015-08-05 10:21:34 +01003309 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003310 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003311 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003312 // In the following Evaluate methods, a HCompare instruction has
3313 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3314 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003315 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003316 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3317 }
3318 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3319 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3320 }
3321 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3322 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003323 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003324
Dave Allison20dfc792014-06-16 20:44:29 -07003325 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3326
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003327 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003328 return kCondGE;
3329 }
3330
Mark Mendellc4701932015-04-10 13:18:51 -04003331 IfCondition GetOppositeCondition() const OVERRIDE {
3332 return kCondLT;
3333 }
3334
Dave Allison20dfc792014-06-16 20:44:29 -07003335 private:
Aart Bike9f37602015-10-09 11:15:55 -07003336 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3337
Dave Allison20dfc792014-06-16 20:44:29 -07003338 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3339};
3340
Aart Bike9f37602015-10-09 11:15:55 -07003341class HBelow : public HCondition {
3342 public:
3343 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3344 : HCondition(first, second, dex_pc) {}
3345
3346 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003347 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003348 }
3349 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003350 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3351 }
3352 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3353 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3354 LOG(FATAL) << DebugName() << " is not defined for float values";
3355 UNREACHABLE();
3356 }
3357 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3358 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3359 LOG(FATAL) << DebugName() << " is not defined for double values";
3360 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003361 }
3362
3363 DECLARE_INSTRUCTION(Below);
3364
3365 IfCondition GetCondition() const OVERRIDE {
3366 return kCondB;
3367 }
3368
3369 IfCondition GetOppositeCondition() const OVERRIDE {
3370 return kCondAE;
3371 }
3372
3373 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003374 template <typename T> bool Compute(T x, T y) const {
3375 return MakeUnsigned(x) < MakeUnsigned(y);
3376 }
Aart Bike9f37602015-10-09 11:15:55 -07003377
3378 DISALLOW_COPY_AND_ASSIGN(HBelow);
3379};
3380
3381class HBelowOrEqual : public HCondition {
3382 public:
3383 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3384 : HCondition(first, second, dex_pc) {}
3385
3386 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003387 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003388 }
3389 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003390 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3391 }
3392 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3393 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3394 LOG(FATAL) << DebugName() << " is not defined for float values";
3395 UNREACHABLE();
3396 }
3397 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3398 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3399 LOG(FATAL) << DebugName() << " is not defined for double values";
3400 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003401 }
3402
3403 DECLARE_INSTRUCTION(BelowOrEqual);
3404
3405 IfCondition GetCondition() const OVERRIDE {
3406 return kCondBE;
3407 }
3408
3409 IfCondition GetOppositeCondition() const OVERRIDE {
3410 return kCondA;
3411 }
3412
3413 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003414 template <typename T> bool Compute(T x, T y) const {
3415 return MakeUnsigned(x) <= MakeUnsigned(y);
3416 }
Aart Bike9f37602015-10-09 11:15:55 -07003417
3418 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3419};
3420
3421class HAbove : public HCondition {
3422 public:
3423 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3424 : HCondition(first, second, dex_pc) {}
3425
3426 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003427 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003428 }
3429 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003430 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3431 }
3432 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3433 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3434 LOG(FATAL) << DebugName() << " is not defined for float values";
3435 UNREACHABLE();
3436 }
3437 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3438 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3439 LOG(FATAL) << DebugName() << " is not defined for double values";
3440 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003441 }
3442
3443 DECLARE_INSTRUCTION(Above);
3444
3445 IfCondition GetCondition() const OVERRIDE {
3446 return kCondA;
3447 }
3448
3449 IfCondition GetOppositeCondition() const OVERRIDE {
3450 return kCondBE;
3451 }
3452
3453 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003454 template <typename T> bool Compute(T x, T y) const {
3455 return MakeUnsigned(x) > MakeUnsigned(y);
3456 }
Aart Bike9f37602015-10-09 11:15:55 -07003457
3458 DISALLOW_COPY_AND_ASSIGN(HAbove);
3459};
3460
3461class HAboveOrEqual : public HCondition {
3462 public:
3463 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3464 : HCondition(first, second, dex_pc) {}
3465
3466 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003467 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003468 }
3469 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003470 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3471 }
3472 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3473 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3474 LOG(FATAL) << DebugName() << " is not defined for float values";
3475 UNREACHABLE();
3476 }
3477 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3478 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3479 LOG(FATAL) << DebugName() << " is not defined for double values";
3480 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003481 }
3482
3483 DECLARE_INSTRUCTION(AboveOrEqual);
3484
3485 IfCondition GetCondition() const OVERRIDE {
3486 return kCondAE;
3487 }
3488
3489 IfCondition GetOppositeCondition() const OVERRIDE {
3490 return kCondB;
3491 }
3492
3493 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003494 template <typename T> bool Compute(T x, T y) const {
3495 return MakeUnsigned(x) >= MakeUnsigned(y);
3496 }
Aart Bike9f37602015-10-09 11:15:55 -07003497
3498 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3499};
Dave Allison20dfc792014-06-16 20:44:29 -07003500
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003501// Instruction to check how two inputs compare to each other.
3502// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3503class HCompare : public HBinaryOperation {
3504 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003505 // Note that `comparison_type` is the type of comparison performed
3506 // between the comparison's inputs, not the type of the instantiated
3507 // HCompare instruction (which is always Primitive::kPrimInt).
3508 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003509 HInstruction* first,
3510 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003511 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003512 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003513 : HBinaryOperation(Primitive::kPrimInt,
3514 first,
3515 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003516 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003517 dex_pc) {
3518 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003519 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3520 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003521 }
3522
Roland Levillain9867bc72015-08-05 10:21:34 +01003523 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003524 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3525
3526 template <typename T>
3527 int32_t ComputeFP(T x, T y) const {
3528 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3529 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3530 // Handle the bias.
3531 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3532 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003533
Roland Levillain9867bc72015-08-05 10:21:34 +01003534 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003535 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3536 // reach this code path when processing a freshly built HIR
3537 // graph. However HCompare integer instructions can be synthesized
3538 // by the instruction simplifier to implement IntegerCompare and
3539 // IntegerSignum intrinsics, so we have to handle this case.
3540 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003541 }
3542 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003543 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3544 }
3545 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3546 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3547 }
3548 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3549 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003550 }
3551
Calin Juravleddb7df22014-11-25 20:56:51 +00003552 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003553 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003554 }
3555
Vladimir Markoa1de9182016-02-25 11:37:38 +00003556 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003557
Roland Levillain31dd3d62016-02-16 12:21:02 +00003558 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003559 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003560 bool IsGtBias() const {
3561 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003562 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003563 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003564
Roland Levillain1693a1f2016-03-15 14:57:31 +00003565 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3566 // Comparisons do not require a runtime call in any back end.
3567 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003568 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003569
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003570 DECLARE_INSTRUCTION(Compare);
3571
Roland Levillain31dd3d62016-02-16 12:21:02 +00003572 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003573 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3574 static constexpr size_t kFieldComparisonBiasSize =
3575 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3576 static constexpr size_t kNumberOfComparePackedBits =
3577 kFieldComparisonBias + kFieldComparisonBiasSize;
3578 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3579 using ComparisonBiasField =
3580 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3581
Roland Levillain31dd3d62016-02-16 12:21:02 +00003582 // Return an integer constant containing the result of a comparison evaluated at compile time.
3583 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3584 DCHECK(value == -1 || value == 0 || value == 1) << value;
3585 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3586 }
3587
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003588 private:
3589 DISALLOW_COPY_AND_ASSIGN(HCompare);
3590};
3591
David Brazdil6de19382016-01-08 17:37:10 +00003592class HNewInstance : public HExpression<2> {
3593 public:
3594 HNewInstance(HInstruction* cls,
3595 HCurrentMethod* current_method,
3596 uint32_t dex_pc,
3597 uint16_t type_index,
3598 const DexFile& dex_file,
3599 bool can_throw,
3600 bool finalizable,
3601 QuickEntrypointEnum entrypoint)
3602 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3603 type_index_(type_index),
3604 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003605 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003606 SetPackedFlag<kFlagCanThrow>(can_throw);
3607 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003608 SetRawInputAt(0, cls);
3609 SetRawInputAt(1, current_method);
3610 }
3611
3612 uint16_t GetTypeIndex() const { return type_index_; }
3613 const DexFile& GetDexFile() const { return dex_file_; }
3614
3615 // Calls runtime so needs an environment.
3616 bool NeedsEnvironment() const OVERRIDE { return true; }
3617
3618 // It may throw when called on type that's not instantiable/accessible.
3619 // It can throw OOME.
3620 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003621 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>() || true; }
David Brazdil6de19382016-01-08 17:37:10 +00003622
Vladimir Markoa1de9182016-02-25 11:37:38 +00003623 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003624
3625 bool CanBeNull() const OVERRIDE { return false; }
3626
3627 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3628
3629 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3630 entrypoint_ = entrypoint;
3631 }
3632
3633 bool IsStringAlloc() const;
3634
3635 DECLARE_INSTRUCTION(NewInstance);
3636
3637 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003638 static constexpr size_t kFlagCanThrow = kNumberOfExpressionPackedBits;
3639 static constexpr size_t kFlagFinalizable = kFlagCanThrow + 1;
3640 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3641 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3642 "Too many packed fields.");
3643
David Brazdil6de19382016-01-08 17:37:10 +00003644 const uint16_t type_index_;
3645 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003646 QuickEntrypointEnum entrypoint_;
3647
3648 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3649};
3650
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003651enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003652#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3653 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003654#include "intrinsics_list.h"
3655 kNone,
3656 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3657#undef INTRINSICS_LIST
3658#undef OPTIMIZING_INTRINSICS
3659};
3660std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3661
Agi Csaki05f20562015-08-19 14:58:14 -07003662enum IntrinsicNeedsEnvironmentOrCache {
3663 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3664 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003665};
3666
Aart Bik5d75afe2015-12-14 11:57:01 -08003667enum IntrinsicSideEffects {
3668 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3669 kReadSideEffects, // Intrinsic may read heap memory.
3670 kWriteSideEffects, // Intrinsic may write heap memory.
3671 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3672};
3673
3674enum IntrinsicExceptions {
3675 kNoThrow, // Intrinsic does not throw any exceptions.
3676 kCanThrow // Intrinsic may throw exceptions.
3677};
3678
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003679class HInvoke : public HInstruction {
3680 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003681 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003682
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003683 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003684
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003685 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003686 SetRawInputAt(index, argument);
3687 }
3688
Roland Levillain3e3d7332015-04-28 11:00:54 +01003689 // Return the number of arguments. This number can be lower than
3690 // the number of inputs returned by InputCount(), as some invoke
3691 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3692 // inputs at the end of their list of inputs.
3693 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3694
Vladimir Markoa1de9182016-02-25 11:37:38 +00003695 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003696
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003697 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003698 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003699
Vladimir Markoa1de9182016-02-25 11:37:38 +00003700 InvokeType GetOriginalInvokeType() const {
3701 return GetPackedField<OriginalInvokeTypeField>();
3702 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003703
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003704 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003705 return intrinsic_;
3706 }
3707
Aart Bik5d75afe2015-12-14 11:57:01 -08003708 void SetIntrinsic(Intrinsics intrinsic,
3709 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3710 IntrinsicSideEffects side_effects,
3711 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003712
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003713 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003714 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003715 }
3716
Vladimir Markoa1de9182016-02-25 11:37:38 +00003717 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003718
3719 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3720
3721 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3722 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3723 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003724
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003725 uint32_t* GetIntrinsicOptimizations() {
3726 return &intrinsic_optimizations_;
3727 }
3728
3729 const uint32_t* GetIntrinsicOptimizations() const {
3730 return &intrinsic_optimizations_;
3731 }
3732
3733 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3734
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003735 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003736
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003737 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003738 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3739 static constexpr size_t kFieldOriginalInvokeTypeSize =
3740 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3741 static constexpr size_t kFieldReturnType =
3742 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3743 static constexpr size_t kFieldReturnTypeSize =
3744 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3745 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3746 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3747 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3748 using OriginalInvokeTypeField =
3749 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3750 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3751
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003752 HInvoke(ArenaAllocator* arena,
3753 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003754 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003755 Primitive::Type return_type,
3756 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003757 uint32_t dex_method_index,
3758 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003759 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003760 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003761 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003762 inputs_(number_of_arguments + number_of_other_inputs,
3763 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003764 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003765 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003766 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003767 SetPackedField<ReturnTypeField>(return_type);
3768 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3769 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003770 }
3771
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003772 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003773 return inputs_[index];
3774 }
3775
David Brazdil1abb4192015-02-17 18:33:36 +00003776 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003777 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003778 }
3779
Vladimir Markoa1de9182016-02-25 11:37:38 +00003780 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003781
Roland Levillain3e3d7332015-04-28 11:00:54 +01003782 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003783 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003784 const uint32_t dex_method_index_;
3785 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003786
3787 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3788 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003789
3790 private:
3791 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3792};
3793
Calin Juravle175dc732015-08-25 15:42:32 +01003794class HInvokeUnresolved : public HInvoke {
3795 public:
3796 HInvokeUnresolved(ArenaAllocator* arena,
3797 uint32_t number_of_arguments,
3798 Primitive::Type return_type,
3799 uint32_t dex_pc,
3800 uint32_t dex_method_index,
3801 InvokeType invoke_type)
3802 : HInvoke(arena,
3803 number_of_arguments,
3804 0u /* number_of_other_inputs */,
3805 return_type,
3806 dex_pc,
3807 dex_method_index,
3808 invoke_type) {
3809 }
3810
3811 DECLARE_INSTRUCTION(InvokeUnresolved);
3812
3813 private:
3814 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3815};
3816
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003817class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003818 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003819 // Requirements of this method call regarding the class
3820 // initialization (clinit) check of its declaring class.
3821 enum class ClinitCheckRequirement {
3822 kNone, // Class already initialized.
3823 kExplicit, // Static call having explicit clinit check as last input.
3824 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003825 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003826 };
3827
Vladimir Marko58155012015-08-19 12:49:41 +00003828 // Determines how to load the target ArtMethod*.
3829 enum class MethodLoadKind {
3830 // Use a String init ArtMethod* loaded from Thread entrypoints.
3831 kStringInit,
3832
3833 // Use the method's own ArtMethod* loaded by the register allocator.
3834 kRecursive,
3835
3836 // Use ArtMethod* at a known address, embed the direct address in the code.
3837 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3838 kDirectAddress,
3839
3840 // Use ArtMethod* at an address that will be known at link time, embed the direct
3841 // address in the code. If the image is relocatable, emit .patch_oat entry.
3842 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3843 // the image relocatable or not.
3844 kDirectAddressWithFixup,
3845
Vladimir Markoa1de9182016-02-25 11:37:38 +00003846 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003847 // Used when we need to use the dex cache, for example for invoke-static that
3848 // may cause class initialization (the entry may point to a resolution method),
3849 // and we know that we can access the dex cache arrays using a PC-relative load.
3850 kDexCachePcRelative,
3851
3852 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3853 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3854 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3855 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3856 kDexCacheViaMethod,
3857 };
3858
3859 // Determines the location of the code pointer.
3860 enum class CodePtrLocation {
3861 // Recursive call, use local PC-relative call instruction.
3862 kCallSelf,
3863
3864 // Use PC-relative call instruction patched at link time.
3865 // Used for calls within an oat file, boot->boot or app->app.
3866 kCallPCRelative,
3867
3868 // Call to a known target address, embed the direct address in code.
3869 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3870 kCallDirect,
3871
3872 // Call to a target address that will be known at link time, embed the direct
3873 // address in code. If the image is relocatable, emit .patch_oat entry.
3874 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3875 // the image relocatable or not.
3876 kCallDirectWithFixup,
3877
3878 // Use code pointer from the ArtMethod*.
3879 // Used when we don't know the target code. This is also the last-resort-kind used when
3880 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3881 kCallArtMethod,
3882 };
3883
3884 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003885 MethodLoadKind method_load_kind;
3886 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003887 // The method load data holds
3888 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3889 // Note that there are multiple string init methods, each having its own offset.
3890 // - the method address for kDirectAddress
3891 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003892 uint64_t method_load_data;
3893 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003894 };
3895
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003896 HInvokeStaticOrDirect(ArenaAllocator* arena,
3897 uint32_t number_of_arguments,
3898 Primitive::Type return_type,
3899 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003900 uint32_t method_index,
3901 MethodReference target_method,
3902 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003903 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003904 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003905 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003906 : HInvoke(arena,
3907 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003908 // There is potentially one extra argument for the HCurrentMethod node, and
3909 // potentially one other if the clinit check is explicit, and potentially
3910 // one other if the method is a string factory.
3911 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003912 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003913 return_type,
3914 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003915 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003916 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003917 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003918 dispatch_info_(dispatch_info) {
3919 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3920 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3921 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003922
Vladimir Markodc151b22015-10-15 18:02:30 +01003923 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003924 bool had_current_method_input = HasCurrentMethodInput();
3925 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3926
3927 // Using the current method is the default and once we find a better
3928 // method load kind, we should not go back to using the current method.
3929 DCHECK(had_current_method_input || !needs_current_method_input);
3930
3931 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003932 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3933 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003934 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003935 dispatch_info_ = dispatch_info;
3936 }
3937
Vladimir Markoc53c0792015-11-19 15:48:33 +00003938 void AddSpecialInput(HInstruction* input) {
3939 // We allow only one special input.
3940 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3941 DCHECK(InputCount() == GetSpecialInputIndex() ||
3942 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3943 InsertInputAt(GetSpecialInputIndex(), input);
3944 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003945
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003946 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003947 // We access the method via the dex cache so we can't do an implicit null check.
3948 // TODO: for intrinsics we can generate implicit null checks.
3949 return false;
3950 }
3951
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003952 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003953 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003954 }
3955
Vladimir Markoc53c0792015-11-19 15:48:33 +00003956 // Get the index of the special input, if any.
3957 //
David Brazdil6de19382016-01-08 17:37:10 +00003958 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3959 // method pointer; otherwise there may be one platform-specific special input,
3960 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003961 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003962 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003963
Vladimir Markoa1de9182016-02-25 11:37:38 +00003964 InvokeType GetOptimizedInvokeType() const {
3965 return GetPackedField<OptimizedInvokeTypeField>();
3966 }
3967
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003968 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003969 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003970 }
3971
Vladimir Marko58155012015-08-19 12:49:41 +00003972 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3973 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3974 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003975 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003976 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003977 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003978 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003979 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3980 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003981 bool HasCurrentMethodInput() const {
3982 // This function can be called only after the invoke has been fully initialized by the builder.
3983 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003984 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003985 return true;
3986 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003987 DCHECK(InputCount() == GetSpecialInputIndex() ||
3988 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003989 return false;
3990 }
3991 }
Vladimir Marko58155012015-08-19 12:49:41 +00003992 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3993 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003994 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003995
3996 int32_t GetStringInitOffset() const {
3997 DCHECK(IsStringInit());
3998 return dispatch_info_.method_load_data;
3999 }
4000
4001 uint64_t GetMethodAddress() const {
4002 DCHECK(HasMethodAddress());
4003 return dispatch_info_.method_load_data;
4004 }
4005
4006 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004007 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004008 return dispatch_info_.method_load_data;
4009 }
4010
4011 uint64_t GetDirectCodePtr() const {
4012 DCHECK(HasDirectCodePtr());
4013 return dispatch_info_.direct_code_ptr;
4014 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004015
Vladimir Markoa1de9182016-02-25 11:37:38 +00004016 ClinitCheckRequirement GetClinitCheckRequirement() const {
4017 return GetPackedField<ClinitCheckRequirementField>();
4018 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004019
Roland Levillain4c0eb422015-04-24 16:43:49 +01004020 // Is this instruction a call to a static method?
4021 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004022 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004023 }
4024
Vladimir Markofbb184a2015-11-13 14:47:00 +00004025 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4026 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4027 // instruction; only relevant for static calls with explicit clinit check.
4028 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004029 DCHECK(IsStaticWithExplicitClinitCheck());
4030 size_t last_input_index = InputCount() - 1;
4031 HInstruction* last_input = InputAt(last_input_index);
4032 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004033 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004034 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004035 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004036 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004037 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004038 }
4039
4040 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004041 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004042 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004043 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004044 }
4045
4046 // Is this a call to a static method whose declaring class has an
4047 // implicit intialization check requirement?
4048 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004049 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004050 }
4051
Vladimir Markob554b5a2015-11-06 12:57:55 +00004052 // Does this method load kind need the current method as an input?
4053 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4054 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4055 }
4056
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004057 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004058
Roland Levillain4c0eb422015-04-24 16:43:49 +01004059 protected:
4060 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
4061 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
4062 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
4063 HInstruction* input = input_record.GetInstruction();
4064 // `input` is the last input of a static invoke marked as having
4065 // an explicit clinit check. It must either be:
4066 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4067 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4068 DCHECK(input != nullptr);
4069 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
4070 }
4071 return input_record;
4072 }
4073
Vladimir Markoc53c0792015-11-19 15:48:33 +00004074 void InsertInputAt(size_t index, HInstruction* input);
4075 void RemoveInputAt(size_t index);
4076
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004077 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004078 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4079 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4080 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4081 static constexpr size_t kFieldClinitCheckRequirement =
4082 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4083 static constexpr size_t kFieldClinitCheckRequirementSize =
4084 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4085 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4086 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4087 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4088 "Too many packed fields.");
4089 using OptimizedInvokeTypeField =
4090 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4091 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4092 kFieldClinitCheckRequirement,
4093 kFieldClinitCheckRequirementSize>;
4094
Vladimir Marko58155012015-08-19 12:49:41 +00004095 // The target method may refer to different dex file or method index than the original
4096 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4097 // in derived class to increase visibility.
4098 MethodReference target_method_;
4099 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004100
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004101 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004102};
Vladimir Markof64242a2015-12-01 14:58:23 +00004103std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004104std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004105
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004106class HInvokeVirtual : public HInvoke {
4107 public:
4108 HInvokeVirtual(ArenaAllocator* arena,
4109 uint32_t number_of_arguments,
4110 Primitive::Type return_type,
4111 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004112 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004113 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004114 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004115 vtable_index_(vtable_index) {}
4116
Calin Juravle641547a2015-04-21 22:08:51 +01004117 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004118 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004119 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004120 }
4121
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004122 uint32_t GetVTableIndex() const { return vtable_index_; }
4123
4124 DECLARE_INSTRUCTION(InvokeVirtual);
4125
4126 private:
4127 const uint32_t vtable_index_;
4128
4129 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4130};
4131
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004132class HInvokeInterface : public HInvoke {
4133 public:
4134 HInvokeInterface(ArenaAllocator* arena,
4135 uint32_t number_of_arguments,
4136 Primitive::Type return_type,
4137 uint32_t dex_pc,
4138 uint32_t dex_method_index,
4139 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004140 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004141 imt_index_(imt_index) {}
4142
Calin Juravle641547a2015-04-21 22:08:51 +01004143 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004144 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004145 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004146 }
4147
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004148 uint32_t GetImtIndex() const { return imt_index_; }
4149 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4150
4151 DECLARE_INSTRUCTION(InvokeInterface);
4152
4153 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004154 const uint32_t imt_index_;
4155
4156 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4157};
4158
Roland Levillain88cb1752014-10-20 16:36:47 +01004159class HNeg : public HUnaryOperation {
4160 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004161 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004162 : HUnaryOperation(result_type, input, dex_pc) {
4163 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4164 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004165
Roland Levillain9867bc72015-08-05 10:21:34 +01004166 template <typename T> T Compute(T x) const { return -x; }
4167
4168 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004169 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004170 }
4171 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004172 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004173 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004174 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4175 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4176 }
4177 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4178 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4179 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004180
Roland Levillain88cb1752014-10-20 16:36:47 +01004181 DECLARE_INSTRUCTION(Neg);
4182
4183 private:
4184 DISALLOW_COPY_AND_ASSIGN(HNeg);
4185};
4186
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004187class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004188 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004189 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004190 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004191 uint32_t dex_pc,
4192 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004193 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004194 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004195 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004196 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004197 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004198 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004199 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004200 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004201 }
4202
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004203 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004204 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004205
4206 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004207 bool NeedsEnvironment() const OVERRIDE { return true; }
4208
Mingyao Yang0c365e62015-03-31 15:09:29 -07004209 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4210 bool CanThrow() const OVERRIDE { return true; }
4211
Calin Juravle10e244f2015-01-26 18:54:32 +00004212 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004213
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004214 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4215
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004216 DECLARE_INSTRUCTION(NewArray);
4217
4218 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004219 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004220 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004221 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004222
4223 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4224};
4225
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004226class HAdd : public HBinaryOperation {
4227 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004228 HAdd(Primitive::Type result_type,
4229 HInstruction* left,
4230 HInstruction* right,
4231 uint32_t dex_pc = kNoDexPc)
4232 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004233
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004234 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004235
Roland Levillain9867bc72015-08-05 10:21:34 +01004236 template <typename T> T Compute(T x, T y) const { return x + y; }
4237
4238 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004239 return GetBlock()->GetGraph()->GetIntConstant(
4240 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004241 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004242 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004243 return GetBlock()->GetGraph()->GetLongConstant(
4244 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004245 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004246 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4247 return GetBlock()->GetGraph()->GetFloatConstant(
4248 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4249 }
4250 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4251 return GetBlock()->GetGraph()->GetDoubleConstant(
4252 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4253 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004254
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004255 DECLARE_INSTRUCTION(Add);
4256
4257 private:
4258 DISALLOW_COPY_AND_ASSIGN(HAdd);
4259};
4260
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004261class HSub : public HBinaryOperation {
4262 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004263 HSub(Primitive::Type result_type,
4264 HInstruction* left,
4265 HInstruction* right,
4266 uint32_t dex_pc = kNoDexPc)
4267 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004268
Roland Levillain9867bc72015-08-05 10:21:34 +01004269 template <typename T> T Compute(T x, T y) const { return x - y; }
4270
4271 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004272 return GetBlock()->GetGraph()->GetIntConstant(
4273 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004274 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004275 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004276 return GetBlock()->GetGraph()->GetLongConstant(
4277 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004278 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004279 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4280 return GetBlock()->GetGraph()->GetFloatConstant(
4281 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4282 }
4283 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4284 return GetBlock()->GetGraph()->GetDoubleConstant(
4285 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4286 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004287
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004288 DECLARE_INSTRUCTION(Sub);
4289
4290 private:
4291 DISALLOW_COPY_AND_ASSIGN(HSub);
4292};
4293
Calin Juravle34bacdf2014-10-07 20:23:36 +01004294class HMul : public HBinaryOperation {
4295 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004296 HMul(Primitive::Type result_type,
4297 HInstruction* left,
4298 HInstruction* right,
4299 uint32_t dex_pc = kNoDexPc)
4300 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004301
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004302 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004303
Roland Levillain9867bc72015-08-05 10:21:34 +01004304 template <typename T> T Compute(T x, T y) const { return x * y; }
4305
4306 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004307 return GetBlock()->GetGraph()->GetIntConstant(
4308 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004309 }
4310 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004311 return GetBlock()->GetGraph()->GetLongConstant(
4312 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004313 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004314 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4315 return GetBlock()->GetGraph()->GetFloatConstant(
4316 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4317 }
4318 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4319 return GetBlock()->GetGraph()->GetDoubleConstant(
4320 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4321 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004322
4323 DECLARE_INSTRUCTION(Mul);
4324
4325 private:
4326 DISALLOW_COPY_AND_ASSIGN(HMul);
4327};
4328
Calin Juravle7c4954d2014-10-28 16:57:40 +00004329class HDiv : public HBinaryOperation {
4330 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004331 HDiv(Primitive::Type result_type,
4332 HInstruction* left,
4333 HInstruction* right,
4334 uint32_t dex_pc)
4335 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004336
Roland Levillain9867bc72015-08-05 10:21:34 +01004337 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004338 T ComputeIntegral(T x, T y) const {
4339 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004340 // Our graph structure ensures we never have 0 for `y` during
4341 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004342 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004343 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004344 return (y == -1) ? -x : x / y;
4345 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004346
Roland Levillain31dd3d62016-02-16 12:21:02 +00004347 template <typename T>
4348 T ComputeFP(T x, T y) const {
4349 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4350 return x / y;
4351 }
4352
Roland Levillain9867bc72015-08-05 10:21:34 +01004353 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004354 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004355 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004356 }
4357 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004358 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004359 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4360 }
4361 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4362 return GetBlock()->GetGraph()->GetFloatConstant(
4363 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4364 }
4365 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4366 return GetBlock()->GetGraph()->GetDoubleConstant(
4367 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004368 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004369
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004370 static SideEffects SideEffectsForArchRuntimeCalls() {
4371 // The generated code can use a runtime call.
4372 return SideEffects::CanTriggerGC();
4373 }
4374
Calin Juravle7c4954d2014-10-28 16:57:40 +00004375 DECLARE_INSTRUCTION(Div);
4376
4377 private:
4378 DISALLOW_COPY_AND_ASSIGN(HDiv);
4379};
4380
Calin Juravlebacfec32014-11-14 15:54:36 +00004381class HRem : public HBinaryOperation {
4382 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004383 HRem(Primitive::Type result_type,
4384 HInstruction* left,
4385 HInstruction* right,
4386 uint32_t dex_pc)
4387 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004388
Roland Levillain9867bc72015-08-05 10:21:34 +01004389 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004390 T ComputeIntegral(T x, T y) const {
4391 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004392 // Our graph structure ensures we never have 0 for `y` during
4393 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004394 DCHECK_NE(y, 0);
4395 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4396 return (y == -1) ? 0 : x % y;
4397 }
4398
Roland Levillain31dd3d62016-02-16 12:21:02 +00004399 template <typename T>
4400 T ComputeFP(T x, T y) const {
4401 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4402 return std::fmod(x, y);
4403 }
4404
Roland Levillain9867bc72015-08-05 10:21:34 +01004405 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004406 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004407 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004408 }
4409 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004410 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004411 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004412 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004413 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4414 return GetBlock()->GetGraph()->GetFloatConstant(
4415 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4416 }
4417 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4418 return GetBlock()->GetGraph()->GetDoubleConstant(
4419 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4420 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004421
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004422 static SideEffects SideEffectsForArchRuntimeCalls() {
4423 return SideEffects::CanTriggerGC();
4424 }
4425
Calin Juravlebacfec32014-11-14 15:54:36 +00004426 DECLARE_INSTRUCTION(Rem);
4427
4428 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004429 DISALLOW_COPY_AND_ASSIGN(HRem);
4430};
4431
Calin Juravled0d48522014-11-04 16:40:20 +00004432class HDivZeroCheck : public HExpression<1> {
4433 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004434 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4435 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004436 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004437 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004438 SetRawInputAt(0, value);
4439 }
4440
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004441 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4442
Calin Juravled0d48522014-11-04 16:40:20 +00004443 bool CanBeMoved() const OVERRIDE { return true; }
4444
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004445 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004446 return true;
4447 }
4448
4449 bool NeedsEnvironment() const OVERRIDE { return true; }
4450 bool CanThrow() const OVERRIDE { return true; }
4451
Calin Juravled0d48522014-11-04 16:40:20 +00004452 DECLARE_INSTRUCTION(DivZeroCheck);
4453
4454 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004455 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4456};
4457
Calin Juravle9aec02f2014-11-18 23:06:35 +00004458class HShl : public HBinaryOperation {
4459 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004460 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004461 HInstruction* value,
4462 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004463 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004464 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4465 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4466 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004467 }
4468
Roland Levillain5b5b9312016-03-22 14:57:31 +00004469 template <typename T>
4470 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4471 return value << (distance & max_shift_distance);
4472 }
4473
4474 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004475 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004476 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004477 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004478 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004479 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004480 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004481 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004482 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4483 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4484 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4485 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004486 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004487 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4488 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004489 LOG(FATAL) << DebugName() << " is not defined for float values";
4490 UNREACHABLE();
4491 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004492 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4493 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004494 LOG(FATAL) << DebugName() << " is not defined for double values";
4495 UNREACHABLE();
4496 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004497
4498 DECLARE_INSTRUCTION(Shl);
4499
4500 private:
4501 DISALLOW_COPY_AND_ASSIGN(HShl);
4502};
4503
4504class HShr : public HBinaryOperation {
4505 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004506 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004507 HInstruction* value,
4508 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004509 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004510 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4511 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4512 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004513 }
4514
Roland Levillain5b5b9312016-03-22 14:57:31 +00004515 template <typename T>
4516 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4517 return value >> (distance & max_shift_distance);
4518 }
4519
4520 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004521 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004522 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004523 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004524 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004525 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004526 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004527 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004528 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4529 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4530 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4531 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004532 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004533 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4534 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004535 LOG(FATAL) << DebugName() << " is not defined for float values";
4536 UNREACHABLE();
4537 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004538 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4539 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004540 LOG(FATAL) << DebugName() << " is not defined for double values";
4541 UNREACHABLE();
4542 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004543
4544 DECLARE_INSTRUCTION(Shr);
4545
4546 private:
4547 DISALLOW_COPY_AND_ASSIGN(HShr);
4548};
4549
4550class HUShr : public HBinaryOperation {
4551 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004552 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004553 HInstruction* value,
4554 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004555 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004556 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4557 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4558 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004559 }
4560
Roland Levillain5b5b9312016-03-22 14:57:31 +00004561 template <typename T>
4562 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4563 typedef typename std::make_unsigned<T>::type V;
4564 V ux = static_cast<V>(value);
4565 return static_cast<T>(ux >> (distance & max_shift_distance));
4566 }
4567
4568 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004569 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004570 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004571 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004572 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004573 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004574 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004575 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004576 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4577 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4578 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4579 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004580 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004581 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4582 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004583 LOG(FATAL) << DebugName() << " is not defined for float values";
4584 UNREACHABLE();
4585 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004586 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4587 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004588 LOG(FATAL) << DebugName() << " is not defined for double values";
4589 UNREACHABLE();
4590 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004591
4592 DECLARE_INSTRUCTION(UShr);
4593
4594 private:
4595 DISALLOW_COPY_AND_ASSIGN(HUShr);
4596};
4597
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004598class HAnd : public HBinaryOperation {
4599 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004600 HAnd(Primitive::Type result_type,
4601 HInstruction* left,
4602 HInstruction* right,
4603 uint32_t dex_pc = kNoDexPc)
4604 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004605
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004606 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004607
Roland Levillaine53bd812016-02-24 14:54:18 +00004608 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004609
4610 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004611 return GetBlock()->GetGraph()->GetIntConstant(
4612 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004613 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004614 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004615 return GetBlock()->GetGraph()->GetLongConstant(
4616 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004617 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004618 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4619 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4620 LOG(FATAL) << DebugName() << " is not defined for float values";
4621 UNREACHABLE();
4622 }
4623 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4624 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4625 LOG(FATAL) << DebugName() << " is not defined for double values";
4626 UNREACHABLE();
4627 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004628
4629 DECLARE_INSTRUCTION(And);
4630
4631 private:
4632 DISALLOW_COPY_AND_ASSIGN(HAnd);
4633};
4634
4635class HOr : public HBinaryOperation {
4636 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004637 HOr(Primitive::Type result_type,
4638 HInstruction* left,
4639 HInstruction* right,
4640 uint32_t dex_pc = kNoDexPc)
4641 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004642
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004643 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004644
Roland Levillaine53bd812016-02-24 14:54:18 +00004645 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004646
4647 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004648 return GetBlock()->GetGraph()->GetIntConstant(
4649 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004650 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004651 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004652 return GetBlock()->GetGraph()->GetLongConstant(
4653 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004654 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004655 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4656 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4657 LOG(FATAL) << DebugName() << " is not defined for float values";
4658 UNREACHABLE();
4659 }
4660 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4661 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4662 LOG(FATAL) << DebugName() << " is not defined for double values";
4663 UNREACHABLE();
4664 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004665
4666 DECLARE_INSTRUCTION(Or);
4667
4668 private:
4669 DISALLOW_COPY_AND_ASSIGN(HOr);
4670};
4671
4672class HXor : public HBinaryOperation {
4673 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004674 HXor(Primitive::Type result_type,
4675 HInstruction* left,
4676 HInstruction* right,
4677 uint32_t dex_pc = kNoDexPc)
4678 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004679
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004680 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004681
Roland Levillaine53bd812016-02-24 14:54:18 +00004682 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004683
4684 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004685 return GetBlock()->GetGraph()->GetIntConstant(
4686 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004687 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004688 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004689 return GetBlock()->GetGraph()->GetLongConstant(
4690 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004691 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004692 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4693 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4694 LOG(FATAL) << DebugName() << " is not defined for float values";
4695 UNREACHABLE();
4696 }
4697 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4698 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4699 LOG(FATAL) << DebugName() << " is not defined for double values";
4700 UNREACHABLE();
4701 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004702
4703 DECLARE_INSTRUCTION(Xor);
4704
4705 private:
4706 DISALLOW_COPY_AND_ASSIGN(HXor);
4707};
4708
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004709class HRor : public HBinaryOperation {
4710 public:
4711 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004712 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004713 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4714 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004715 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004716
Roland Levillain5b5b9312016-03-22 14:57:31 +00004717 template <typename T>
4718 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4719 typedef typename std::make_unsigned<T>::type V;
4720 V ux = static_cast<V>(value);
4721 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004722 return static_cast<T>(ux);
4723 } else {
4724 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004725 return static_cast<T>(ux >> (distance & max_shift_value)) |
4726 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004727 }
4728 }
4729
Roland Levillain5b5b9312016-03-22 14:57:31 +00004730 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004731 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004732 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004733 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004734 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004735 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004736 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004737 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004738 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4739 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4740 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4741 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004742 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004743 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4744 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004745 LOG(FATAL) << DebugName() << " is not defined for float values";
4746 UNREACHABLE();
4747 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004748 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4749 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004750 LOG(FATAL) << DebugName() << " is not defined for double values";
4751 UNREACHABLE();
4752 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004753
4754 DECLARE_INSTRUCTION(Ror);
4755
4756 private:
4757 DISALLOW_COPY_AND_ASSIGN(HRor);
4758};
4759
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004760// The value of a parameter in this method. Its location depends on
4761// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004762class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004763 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004764 HParameterValue(const DexFile& dex_file,
4765 uint16_t type_index,
4766 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004767 Primitive::Type parameter_type,
4768 bool is_this = false)
4769 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004770 dex_file_(dex_file),
4771 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004772 index_(index) {
4773 SetPackedFlag<kFlagIsThis>(is_this);
4774 SetPackedFlag<kFlagCanBeNull>(!is_this);
4775 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004776
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004777 const DexFile& GetDexFile() const { return dex_file_; }
4778 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004779 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004780 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004781
Vladimir Markoa1de9182016-02-25 11:37:38 +00004782 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4783 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004784
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004785 DECLARE_INSTRUCTION(ParameterValue);
4786
4787 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004788 // Whether or not the parameter value corresponds to 'this' argument.
4789 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4790 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4791 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4792 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4793 "Too many packed fields.");
4794
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004795 const DexFile& dex_file_;
4796 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004797 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004798 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004799 const uint8_t index_;
4800
4801 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4802};
4803
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004804class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004805 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004806 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4807 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004808
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004809 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004810 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004811 return true;
4812 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004813
Roland Levillain9867bc72015-08-05 10:21:34 +01004814 template <typename T> T Compute(T x) const { return ~x; }
4815
4816 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004817 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004818 }
4819 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004820 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004821 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004822 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4823 LOG(FATAL) << DebugName() << " is not defined for float values";
4824 UNREACHABLE();
4825 }
4826 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4827 LOG(FATAL) << DebugName() << " is not defined for double values";
4828 UNREACHABLE();
4829 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004830
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004831 DECLARE_INSTRUCTION(Not);
4832
4833 private:
4834 DISALLOW_COPY_AND_ASSIGN(HNot);
4835};
4836
David Brazdil66d126e2015-04-03 16:02:44 +01004837class HBooleanNot : public HUnaryOperation {
4838 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004839 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4840 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004841
4842 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004843 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004844 return true;
4845 }
4846
Roland Levillain9867bc72015-08-05 10:21:34 +01004847 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004848 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004849 return !x;
4850 }
4851
Roland Levillain9867bc72015-08-05 10:21:34 +01004852 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004853 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004854 }
4855 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4856 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004857 UNREACHABLE();
4858 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004859 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4860 LOG(FATAL) << DebugName() << " is not defined for float values";
4861 UNREACHABLE();
4862 }
4863 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4864 LOG(FATAL) << DebugName() << " is not defined for double values";
4865 UNREACHABLE();
4866 }
David Brazdil66d126e2015-04-03 16:02:44 +01004867
4868 DECLARE_INSTRUCTION(BooleanNot);
4869
4870 private:
4871 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4872};
4873
Roland Levillaindff1f282014-11-05 14:15:05 +00004874class HTypeConversion : public HExpression<1> {
4875 public:
4876 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004877 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004878 : HExpression(result_type,
4879 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4880 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004881 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004882 // Invariant: We should never generate a conversion to a Boolean value.
4883 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004884 }
4885
4886 HInstruction* GetInput() const { return InputAt(0); }
4887 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4888 Primitive::Type GetResultType() const { return GetType(); }
4889
4890 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004891 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004892
Mark Mendelle82549b2015-05-06 10:55:34 -04004893 // Try to statically evaluate the conversion and return a HConstant
4894 // containing the result. If the input cannot be converted, return nullptr.
4895 HConstant* TryStaticEvaluation() const;
4896
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004897 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4898 Primitive::Type result_type) {
4899 // Some architectures may not require the 'GC' side effects, but at this point
4900 // in the compilation process we do not know what architecture we will
4901 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004902 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4903 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004904 return SideEffects::CanTriggerGC();
4905 }
4906 return SideEffects::None();
4907 }
4908
Roland Levillaindff1f282014-11-05 14:15:05 +00004909 DECLARE_INSTRUCTION(TypeConversion);
4910
4911 private:
4912 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4913};
4914
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004915static constexpr uint32_t kNoRegNumber = -1;
4916
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004917class HNullCheck : public HExpression<1> {
4918 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004919 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4920 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004921 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004922 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004923 SetRawInputAt(0, value);
4924 }
4925
Calin Juravle10e244f2015-01-26 18:54:32 +00004926 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004927 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004928 return true;
4929 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004930
Calin Juravle10e244f2015-01-26 18:54:32 +00004931 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004932
Calin Juravle10e244f2015-01-26 18:54:32 +00004933 bool CanThrow() const OVERRIDE { return true; }
4934
4935 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004936
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004937
4938 DECLARE_INSTRUCTION(NullCheck);
4939
4940 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004941 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4942};
4943
4944class FieldInfo : public ValueObject {
4945 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004946 FieldInfo(MemberOffset field_offset,
4947 Primitive::Type field_type,
4948 bool is_volatile,
4949 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004950 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004951 const DexFile& dex_file,
4952 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004953 : field_offset_(field_offset),
4954 field_type_(field_type),
4955 is_volatile_(is_volatile),
4956 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004957 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004958 dex_file_(dex_file),
4959 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004960
4961 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004962 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004963 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004964 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004965 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004966 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004967 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004968
4969 private:
4970 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004971 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004972 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004973 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004974 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004975 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004976 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004977};
4978
4979class HInstanceFieldGet : public HExpression<1> {
4980 public:
4981 HInstanceFieldGet(HInstruction* value,
4982 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004983 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004984 bool is_volatile,
4985 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004986 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004987 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004988 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004989 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004990 : HExpression(field_type,
4991 SideEffects::FieldReadOfType(field_type, is_volatile),
4992 dex_pc),
4993 field_info_(field_offset,
4994 field_type,
4995 is_volatile,
4996 field_idx,
4997 declaring_class_def_index,
4998 dex_file,
4999 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005000 SetRawInputAt(0, value);
5001 }
5002
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005003 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005004
5005 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5006 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
5007 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005008 }
5009
Calin Juravle641547a2015-04-21 22:08:51 +01005010 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5011 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005012 }
5013
5014 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005015 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5016 }
5017
Calin Juravle52c48962014-12-16 17:02:57 +00005018 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005019 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005020 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005021 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005022
5023 DECLARE_INSTRUCTION(InstanceFieldGet);
5024
5025 private:
5026 const FieldInfo field_info_;
5027
5028 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5029};
5030
5031class HInstanceFieldSet : public HTemplateInstruction<2> {
5032 public:
5033 HInstanceFieldSet(HInstruction* object,
5034 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005035 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005036 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005037 bool is_volatile,
5038 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005039 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005040 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005041 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005042 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005043 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5044 dex_pc),
5045 field_info_(field_offset,
5046 field_type,
5047 is_volatile,
5048 field_idx,
5049 declaring_class_def_index,
5050 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005051 dex_cache) {
5052 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005053 SetRawInputAt(0, object);
5054 SetRawInputAt(1, value);
5055 }
5056
Calin Juravle641547a2015-04-21 22:08:51 +01005057 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5058 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005059 }
5060
Calin Juravle52c48962014-12-16 17:02:57 +00005061 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005062 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005063 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005064 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005065 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005066 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5067 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005068
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005069 DECLARE_INSTRUCTION(InstanceFieldSet);
5070
5071 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005072 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5073 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5074 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5075 "Too many packed fields.");
5076
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005077 const FieldInfo field_info_;
5078
5079 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5080};
5081
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005082class HArrayGet : public HExpression<2> {
5083 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005084 HArrayGet(HInstruction* array,
5085 HInstruction* index,
5086 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005087 uint32_t dex_pc,
5088 SideEffects additional_side_effects = SideEffects::None())
5089 : HExpression(type,
5090 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00005091 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005092 SetRawInputAt(0, array);
5093 SetRawInputAt(1, index);
5094 }
5095
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005096 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005097 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005098 return true;
5099 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005100 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005101 // TODO: We can be smarter here.
5102 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5103 // which generates the implicit null check. There are cases when these can be removed
5104 // to produce better code. If we ever add optimizations to do so we should allow an
5105 // implicit check here (as long as the address falls in the first page).
5106 return false;
5107 }
5108
David Brazdil4833f5a2015-12-16 10:37:39 +00005109 bool IsEquivalentOf(HArrayGet* other) const {
5110 bool result = (GetDexPc() == other->GetDexPc());
5111 if (kIsDebugBuild && result) {
5112 DCHECK_EQ(GetBlock(), other->GetBlock());
5113 DCHECK_EQ(GetArray(), other->GetArray());
5114 DCHECK_EQ(GetIndex(), other->GetIndex());
5115 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005116 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005117 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005118 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5119 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005120 }
5121 }
5122 return result;
5123 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005124
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005125 HInstruction* GetArray() const { return InputAt(0); }
5126 HInstruction* GetIndex() const { return InputAt(1); }
5127
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005128 DECLARE_INSTRUCTION(ArrayGet);
5129
5130 private:
5131 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5132};
5133
5134class HArraySet : public HTemplateInstruction<3> {
5135 public:
5136 HArraySet(HInstruction* array,
5137 HInstruction* index,
5138 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005139 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005140 uint32_t dex_pc,
5141 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005142 : HTemplateInstruction(
5143 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005144 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
5145 additional_side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005146 dex_pc) {
5147 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5148 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5149 SetPackedFlag<kFlagValueCanBeNull>(true);
5150 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005151 SetRawInputAt(0, array);
5152 SetRawInputAt(1, index);
5153 SetRawInputAt(2, value);
5154 }
5155
Calin Juravle77520bc2015-01-12 18:45:46 +00005156 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005157 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005158 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005159 }
5160
Mingyao Yang81014cb2015-06-02 03:16:27 -07005161 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005162 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005163
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005164 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005165 // TODO: Same as for ArrayGet.
5166 return false;
5167 }
5168
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005169 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005170 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005171 }
5172
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005173 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005174 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005175 }
5176
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005177 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005178 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005179 }
5180
Vladimir Markoa1de9182016-02-25 11:37:38 +00005181 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5182 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5183 bool StaticTypeOfArrayIsObjectArray() const {
5184 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5185 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005186
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005187 HInstruction* GetArray() const { return InputAt(0); }
5188 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005189 HInstruction* GetValue() const { return InputAt(2); }
5190
5191 Primitive::Type GetComponentType() const {
5192 // The Dex format does not type floating point index operations. Since the
5193 // `expected_component_type_` is set during building and can therefore not
5194 // be correct, we also check what is the value type. If it is a floating
5195 // point type, we must use that type.
5196 Primitive::Type value_type = GetValue()->GetType();
5197 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5198 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005199 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005200 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005201
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005202 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005203 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005204 }
5205
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005206 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5207 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5208 }
5209
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005210 DECLARE_INSTRUCTION(ArraySet);
5211
5212 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005213 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5214 static constexpr size_t kFieldExpectedComponentTypeSize =
5215 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5216 static constexpr size_t kFlagNeedsTypeCheck =
5217 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5218 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005219 // Cached information for the reference_type_info_ so that codegen
5220 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005221 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5222 static constexpr size_t kNumberOfArraySetPackedBits =
5223 kFlagStaticTypeOfArrayIsObjectArray + 1;
5224 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5225 using ExpectedComponentTypeField =
5226 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005227
5228 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5229};
5230
5231class HArrayLength : public HExpression<1> {
5232 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005233 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005234 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005235 // Note that arrays do not change length, so the instruction does not
5236 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005237 SetRawInputAt(0, array);
5238 }
5239
Calin Juravle77520bc2015-01-12 18:45:46 +00005240 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005241 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005242 return true;
5243 }
Calin Juravle641547a2015-04-21 22:08:51 +01005244 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5245 return obj == InputAt(0);
5246 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005247
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005248 DECLARE_INSTRUCTION(ArrayLength);
5249
5250 private:
5251 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5252};
5253
5254class HBoundsCheck : public HExpression<2> {
5255 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005256 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5257 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005258 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005259 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005260 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005261 SetRawInputAt(0, index);
5262 SetRawInputAt(1, length);
5263 }
5264
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005265 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005266 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005267 return true;
5268 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005269
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005270 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005271
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005272 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005273
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005274 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005275
5276 DECLARE_INSTRUCTION(BoundsCheck);
5277
5278 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005279 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5280};
5281
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005282class HSuspendCheck : public HTemplateInstruction<0> {
5283 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005284 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005285 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005286
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005287 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005288 return true;
5289 }
5290
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005291 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5292 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005293
5294 DECLARE_INSTRUCTION(SuspendCheck);
5295
5296 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005297 // Only used for code generation, in order to share the same slow path between back edges
5298 // of a same loop.
5299 SlowPathCode* slow_path_;
5300
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005301 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5302};
5303
David Srbecky0cf44932015-12-09 14:09:59 +00005304// Pseudo-instruction which provides the native debugger with mapping information.
5305// It ensures that we can generate line number and local variables at this point.
5306class HNativeDebugInfo : public HTemplateInstruction<0> {
5307 public:
5308 explicit HNativeDebugInfo(uint32_t dex_pc)
5309 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5310
5311 bool NeedsEnvironment() const OVERRIDE {
5312 return true;
5313 }
5314
5315 DECLARE_INSTRUCTION(NativeDebugInfo);
5316
5317 private:
5318 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5319};
5320
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005321/**
5322 * Instruction to load a Class object.
5323 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005324class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005325 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005326 HLoadClass(HCurrentMethod* current_method,
5327 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005328 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005329 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005330 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005331 bool needs_access_check,
5332 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005333 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005334 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005335 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005336 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005337 // Referrers class should not need access check. We never inline unverified
5338 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005339 DCHECK(!is_referrers_class || !needs_access_check);
5340
5341 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5342 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5343 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5344 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005345 SetRawInputAt(0, current_method);
5346 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005347
5348 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005349
5350 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005351 // Note that we don't need to test for generate_clinit_check_.
5352 // Whether or not we need to generate the clinit check is processed in
5353 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005354 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005355 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005356 }
5357
5358 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5359
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005360 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005361 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005362
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005363 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005364 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005365 }
5366
Calin Juravle0ba218d2015-05-19 18:46:01 +01005367 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005368 // The entrypoint the code generator is going to call does not do
5369 // clinit of the class.
5370 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005371 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005372 }
5373
5374 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005375 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005376 (!IsReferrersClass() && !IsInDexCache()) ||
5377 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005378 }
5379
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005380
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005381 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005382 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005383 }
5384
Calin Juravleacf735c2015-02-12 15:25:22 +00005385 ReferenceTypeInfo GetLoadedClassRTI() {
5386 return loaded_class_rti_;
5387 }
5388
5389 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5390 // Make sure we only set exact types (the loaded class should never be merged).
5391 DCHECK(rti.IsExact());
5392 loaded_class_rti_ = rti;
5393 }
5394
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005395 const DexFile& GetDexFile() { return dex_file_; }
5396
Vladimir Markoa1de9182016-02-25 11:37:38 +00005397 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005398
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005399 static SideEffects SideEffectsForArchRuntimeCalls() {
5400 return SideEffects::CanTriggerGC();
5401 }
5402
Vladimir Markoa1de9182016-02-25 11:37:38 +00005403 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5404 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5405 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5406 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005407
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005408 DECLARE_INSTRUCTION(LoadClass);
5409
5410 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005411 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5412 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5413 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005414 // Whether this instruction must generate the initialization check.
5415 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005416 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5417 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5418 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5419
5420 const uint16_t type_index_;
5421 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005422
Calin Juravleacf735c2015-02-12 15:25:22 +00005423 ReferenceTypeInfo loaded_class_rti_;
5424
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005425 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5426};
5427
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005428class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005429 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005430 // Determines how to load the String.
5431 enum class LoadKind {
5432 // Use boot image String* address that will be known at link time.
5433 // Used for boot image strings referenced by boot image code in non-PIC mode.
5434 kBootImageLinkTimeAddress,
5435
5436 // Use PC-relative boot image String* address that will be known at link time.
5437 // Used for boot image strings referenced by boot image code in PIC mode.
5438 kBootImageLinkTimePcRelative,
5439
5440 // Use a known boot image String* address, embedded in the code by the codegen.
5441 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5442 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5443 // GetIncludePatchInformation().
5444 kBootImageAddress,
5445
5446 // Load from the resolved strings array at an absolute address.
5447 // Used for strings outside the boot image referenced by JIT-compiled code.
5448 kDexCacheAddress,
5449
5450 // Load from resolved strings array in the dex cache using a PC-relative load.
5451 // Used for strings outside boot image when we know that we can access
5452 // the dex cache arrays using a PC-relative load.
5453 kDexCachePcRelative,
5454
5455 // Load from resolved strings array accessed through the class loaded from
5456 // the compiled method's own ArtMethod*. This is the default access type when
5457 // all other types are unavailable.
5458 kDexCacheViaMethod,
5459
5460 kLast = kDexCacheViaMethod
5461 };
5462
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005463 HLoadString(HCurrentMethod* current_method,
5464 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005465 const DexFile& dex_file,
5466 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005467 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005468 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005469 SetPackedFlag<kFlagIsInDexCache>(false);
5470 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5471 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005472 SetRawInputAt(0, current_method);
5473 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005474
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005475 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5476 DCHECK(HasAddress(load_kind));
5477 load_data_.address = address;
5478 SetLoadKindInternal(load_kind);
5479 }
5480
5481 void SetLoadKindWithStringReference(LoadKind load_kind,
5482 const DexFile& dex_file,
5483 uint32_t string_index) {
5484 DCHECK(HasStringReference(load_kind));
5485 load_data_.ref.dex_file = &dex_file;
5486 string_index_ = string_index;
5487 SetLoadKindInternal(load_kind);
5488 }
5489
5490 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5491 const DexFile& dex_file,
5492 uint32_t element_index) {
5493 DCHECK(HasDexCacheReference(load_kind));
5494 load_data_.ref.dex_file = &dex_file;
5495 load_data_.ref.dex_cache_element_index = element_index;
5496 SetLoadKindInternal(load_kind);
5497 }
5498
5499 LoadKind GetLoadKind() const {
5500 return GetPackedField<LoadKindField>();
5501 }
5502
5503 const DexFile& GetDexFile() const;
5504
5505 uint32_t GetStringIndex() const {
5506 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5507 return string_index_;
5508 }
5509
5510 uint32_t GetDexCacheElementOffset() const;
5511
5512 uint64_t GetAddress() const {
5513 DCHECK(HasAddress(GetLoadKind()));
5514 return load_data_.address;
5515 }
5516
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005517 bool CanBeMoved() const OVERRIDE { return true; }
5518
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005519 bool InstructionDataEquals(HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005520
5521 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5522
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005523 // Will call the runtime if we need to load the string through
5524 // the dex cache and the string is not guaranteed to be there yet.
5525 bool NeedsEnvironment() const OVERRIDE {
5526 LoadKind load_kind = GetLoadKind();
5527 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5528 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5529 load_kind == LoadKind::kBootImageAddress) {
5530 return false;
5531 }
5532 return !IsInDexCache();
5533 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005534
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005535 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5536 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5537 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005538
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005539 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005540 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005541
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005542 static SideEffects SideEffectsForArchRuntimeCalls() {
5543 return SideEffects::CanTriggerGC();
5544 }
5545
Vladimir Markoa1de9182016-02-25 11:37:38 +00005546 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5547
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005548 void MarkInDexCache() {
5549 SetPackedFlag<kFlagIsInDexCache>(true);
5550 DCHECK(!NeedsEnvironment());
5551 RemoveEnvironment();
Vladimir Markobf12e4d2016-04-05 11:18:49 +01005552 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005553 }
5554
5555 size_t InputCount() const OVERRIDE {
5556 return (InputAt(0) != nullptr) ? 1u : 0u;
5557 }
5558
5559 void AddSpecialInput(HInstruction* special_input);
5560
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005561 DECLARE_INSTRUCTION(LoadString);
5562
5563 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005564 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005565 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5566 static constexpr size_t kFieldLoadKindSize =
5567 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5568 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005569 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005570 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005571
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005572 static bool HasStringReference(LoadKind load_kind) {
5573 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5574 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5575 load_kind == LoadKind::kDexCacheViaMethod;
5576 }
5577
5578 static bool HasAddress(LoadKind load_kind) {
5579 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5580 }
5581
5582 static bool HasDexCacheReference(LoadKind load_kind) {
5583 return load_kind == LoadKind::kDexCachePcRelative;
5584 }
5585
5586 void SetLoadKindInternal(LoadKind load_kind);
5587
5588 // String index serves also as the hash code and it's also needed for slow-paths,
5589 // so it must not be overwritten with other load data.
5590 uint32_t string_index_;
5591
5592 union {
5593 struct {
5594 const DexFile* dex_file; // For string reference and dex cache reference.
5595 uint32_t dex_cache_element_index; // Only for dex cache reference.
5596 } ref;
5597 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5598 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005599
5600 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5601};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005602std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5603
5604// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5605inline const DexFile& HLoadString::GetDexFile() const {
5606 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5607 << GetLoadKind();
5608 return *load_data_.ref.dex_file;
5609}
5610
5611// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5612inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5613 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5614 return load_data_.ref.dex_cache_element_index;
5615}
5616
5617// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5618inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5619 // The special input is used for PC-relative loads on some architectures.
5620 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5621 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
5622 DCHECK(InputAt(0) == nullptr);
5623 SetRawInputAt(0u, special_input);
5624 special_input->AddUseAt(this, 0);
5625}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005626
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005627/**
5628 * Performs an initialization check on its Class object input.
5629 */
5630class HClinitCheck : public HExpression<1> {
5631 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005632 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005633 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005634 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005635 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5636 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005637 SetRawInputAt(0, constant);
5638 }
5639
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005640 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005641 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005642 return true;
5643 }
5644
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005645 bool NeedsEnvironment() const OVERRIDE {
5646 // May call runtime to initialize the class.
5647 return true;
5648 }
5649
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005650 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005651
5652 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5653
5654 DECLARE_INSTRUCTION(ClinitCheck);
5655
5656 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005657 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5658};
5659
5660class HStaticFieldGet : public HExpression<1> {
5661 public:
5662 HStaticFieldGet(HInstruction* cls,
5663 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005664 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005665 bool is_volatile,
5666 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005667 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005668 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005669 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005670 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005671 : HExpression(field_type,
5672 SideEffects::FieldReadOfType(field_type, is_volatile),
5673 dex_pc),
5674 field_info_(field_offset,
5675 field_type,
5676 is_volatile,
5677 field_idx,
5678 declaring_class_def_index,
5679 dex_file,
5680 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005681 SetRawInputAt(0, cls);
5682 }
5683
Calin Juravle52c48962014-12-16 17:02:57 +00005684
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005685 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005686
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005687 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005688 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5689 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005690 }
5691
5692 size_t ComputeHashCode() const OVERRIDE {
5693 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5694 }
5695
Calin Juravle52c48962014-12-16 17:02:57 +00005696 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005697 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5698 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005699 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005700
5701 DECLARE_INSTRUCTION(StaticFieldGet);
5702
5703 private:
5704 const FieldInfo field_info_;
5705
5706 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5707};
5708
5709class HStaticFieldSet : public HTemplateInstruction<2> {
5710 public:
5711 HStaticFieldSet(HInstruction* cls,
5712 HInstruction* value,
5713 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005714 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005715 bool is_volatile,
5716 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005717 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005718 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005719 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005720 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005721 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5722 dex_pc),
5723 field_info_(field_offset,
5724 field_type,
5725 is_volatile,
5726 field_idx,
5727 declaring_class_def_index,
5728 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005729 dex_cache) {
5730 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005731 SetRawInputAt(0, cls);
5732 SetRawInputAt(1, value);
5733 }
5734
Calin Juravle52c48962014-12-16 17:02:57 +00005735 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005736 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5737 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005738 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005739
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005740 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005741 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5742 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005743
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005744 DECLARE_INSTRUCTION(StaticFieldSet);
5745
5746 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005747 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5748 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5749 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5750 "Too many packed fields.");
5751
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005752 const FieldInfo field_info_;
5753
5754 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5755};
5756
Calin Juravlee460d1d2015-09-29 04:52:17 +01005757class HUnresolvedInstanceFieldGet : public HExpression<1> {
5758 public:
5759 HUnresolvedInstanceFieldGet(HInstruction* obj,
5760 Primitive::Type field_type,
5761 uint32_t field_index,
5762 uint32_t dex_pc)
5763 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5764 field_index_(field_index) {
5765 SetRawInputAt(0, obj);
5766 }
5767
5768 bool NeedsEnvironment() const OVERRIDE { return true; }
5769 bool CanThrow() const OVERRIDE { return true; }
5770
5771 Primitive::Type GetFieldType() const { return GetType(); }
5772 uint32_t GetFieldIndex() const { return field_index_; }
5773
5774 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5775
5776 private:
5777 const uint32_t field_index_;
5778
5779 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5780};
5781
5782class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5783 public:
5784 HUnresolvedInstanceFieldSet(HInstruction* obj,
5785 HInstruction* value,
5786 Primitive::Type field_type,
5787 uint32_t field_index,
5788 uint32_t dex_pc)
5789 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005790 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005791 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005792 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005793 SetRawInputAt(0, obj);
5794 SetRawInputAt(1, value);
5795 }
5796
5797 bool NeedsEnvironment() const OVERRIDE { return true; }
5798 bool CanThrow() const OVERRIDE { return true; }
5799
Vladimir Markoa1de9182016-02-25 11:37:38 +00005800 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005801 uint32_t GetFieldIndex() const { return field_index_; }
5802
5803 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5804
5805 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005806 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5807 static constexpr size_t kFieldFieldTypeSize =
5808 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5809 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5810 kFieldFieldType + kFieldFieldTypeSize;
5811 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5812 "Too many packed fields.");
5813 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5814
Calin Juravlee460d1d2015-09-29 04:52:17 +01005815 const uint32_t field_index_;
5816
5817 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5818};
5819
5820class HUnresolvedStaticFieldGet : public HExpression<0> {
5821 public:
5822 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5823 uint32_t field_index,
5824 uint32_t dex_pc)
5825 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5826 field_index_(field_index) {
5827 }
5828
5829 bool NeedsEnvironment() const OVERRIDE { return true; }
5830 bool CanThrow() const OVERRIDE { return true; }
5831
5832 Primitive::Type GetFieldType() const { return GetType(); }
5833 uint32_t GetFieldIndex() const { return field_index_; }
5834
5835 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5836
5837 private:
5838 const uint32_t field_index_;
5839
5840 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5841};
5842
5843class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5844 public:
5845 HUnresolvedStaticFieldSet(HInstruction* value,
5846 Primitive::Type field_type,
5847 uint32_t field_index,
5848 uint32_t dex_pc)
5849 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005850 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005851 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005852 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005853 SetRawInputAt(0, value);
5854 }
5855
5856 bool NeedsEnvironment() const OVERRIDE { return true; }
5857 bool CanThrow() const OVERRIDE { return true; }
5858
Vladimir Markoa1de9182016-02-25 11:37:38 +00005859 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005860 uint32_t GetFieldIndex() const { return field_index_; }
5861
5862 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5863
5864 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005865 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5866 static constexpr size_t kFieldFieldTypeSize =
5867 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5868 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5869 kFieldFieldType + kFieldFieldTypeSize;
5870 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5871 "Too many packed fields.");
5872 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5873
Calin Juravlee460d1d2015-09-29 04:52:17 +01005874 const uint32_t field_index_;
5875
5876 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5877};
5878
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005879// Implement the move-exception DEX instruction.
5880class HLoadException : public HExpression<0> {
5881 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005882 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5883 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005884
David Brazdilbbd733e2015-08-18 17:48:17 +01005885 bool CanBeNull() const OVERRIDE { return false; }
5886
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005887 DECLARE_INSTRUCTION(LoadException);
5888
5889 private:
5890 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5891};
5892
David Brazdilcb1c0552015-08-04 16:22:25 +01005893// Implicit part of move-exception which clears thread-local exception storage.
5894// Must not be removed because the runtime expects the TLS to get cleared.
5895class HClearException : public HTemplateInstruction<0> {
5896 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005897 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5898 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005899
5900 DECLARE_INSTRUCTION(ClearException);
5901
5902 private:
5903 DISALLOW_COPY_AND_ASSIGN(HClearException);
5904};
5905
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005906class HThrow : public HTemplateInstruction<1> {
5907 public:
5908 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005909 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005910 SetRawInputAt(0, exception);
5911 }
5912
5913 bool IsControlFlow() const OVERRIDE { return true; }
5914
5915 bool NeedsEnvironment() const OVERRIDE { return true; }
5916
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005917 bool CanThrow() const OVERRIDE { return true; }
5918
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005919
5920 DECLARE_INSTRUCTION(Throw);
5921
5922 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005923 DISALLOW_COPY_AND_ASSIGN(HThrow);
5924};
5925
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005926/**
5927 * Implementation strategies for the code generator of a HInstanceOf
5928 * or `HCheckCast`.
5929 */
5930enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005931 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005932 kExactCheck, // Can do a single class compare.
5933 kClassHierarchyCheck, // Can just walk the super class chain.
5934 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5935 kInterfaceCheck, // No optimization yet when checking against an interface.
5936 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005937 kArrayCheck, // No optimization yet when checking against a generic array.
5938 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005939};
5940
Roland Levillain86503782016-02-11 19:07:30 +00005941std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5942
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005943class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005944 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005945 HInstanceOf(HInstruction* object,
5946 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005947 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005948 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005949 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005950 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005951 dex_pc) {
5952 SetPackedField<TypeCheckKindField>(check_kind);
5953 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005954 SetRawInputAt(0, object);
5955 SetRawInputAt(1, constant);
5956 }
5957
5958 bool CanBeMoved() const OVERRIDE { return true; }
5959
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005960 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005961 return true;
5962 }
5963
5964 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005965 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005966 }
5967
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005968 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005969 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5970 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5971 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5972 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005973
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005974 static bool CanCallRuntime(TypeCheckKind check_kind) {
5975 // Mips currently does runtime calls for any other checks.
5976 return check_kind != TypeCheckKind::kExactCheck;
5977 }
5978
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005979 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005980 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005981 }
5982
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005983 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005984
5985 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005986 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
5987 static constexpr size_t kFieldTypeCheckKindSize =
5988 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
5989 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
5990 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
5991 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5992 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005993
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005994 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5995};
5996
Calin Juravleb1498f62015-02-16 13:13:29 +00005997class HBoundType : public HExpression<1> {
5998 public:
David Brazdilf5552582015-12-27 13:36:12 +00005999 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006000 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006001 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6002 SetPackedFlag<kFlagUpperCanBeNull>(true);
6003 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006004 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006005 SetRawInputAt(0, input);
6006 }
6007
David Brazdilf5552582015-12-27 13:36:12 +00006008 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006009 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006010 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006011 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006012
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006013 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006014 DCHECK(GetUpperCanBeNull() || !can_be_null);
6015 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006016 }
6017
Vladimir Markoa1de9182016-02-25 11:37:38 +00006018 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006019
Calin Juravleb1498f62015-02-16 13:13:29 +00006020 DECLARE_INSTRUCTION(BoundType);
6021
6022 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006023 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6024 // is false then CanBeNull() cannot be true).
6025 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6026 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6027 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6028 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6029
Calin Juravleb1498f62015-02-16 13:13:29 +00006030 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006031 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6032 // It is used to bound the type in cases like:
6033 // if (x instanceof ClassX) {
6034 // // uper_bound_ will be ClassX
6035 // }
David Brazdilf5552582015-12-27 13:36:12 +00006036 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006037
6038 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6039};
6040
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006041class HCheckCast : public HTemplateInstruction<2> {
6042 public:
6043 HCheckCast(HInstruction* object,
6044 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006045 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006046 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006047 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6048 SetPackedField<TypeCheckKindField>(check_kind);
6049 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006050 SetRawInputAt(0, object);
6051 SetRawInputAt(1, constant);
6052 }
6053
6054 bool CanBeMoved() const OVERRIDE { return true; }
6055
6056 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6057 return true;
6058 }
6059
6060 bool NeedsEnvironment() const OVERRIDE {
6061 // Instruction may throw a CheckCastError.
6062 return true;
6063 }
6064
6065 bool CanThrow() const OVERRIDE { return true; }
6066
Vladimir Markoa1de9182016-02-25 11:37:38 +00006067 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6068 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6069 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6070 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006071
6072 DECLARE_INSTRUCTION(CheckCast);
6073
6074 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006075 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6076 static constexpr size_t kFieldTypeCheckKindSize =
6077 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6078 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6079 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6080 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6081 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006082
6083 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006084};
6085
Calin Juravle27df7582015-04-17 19:12:31 +01006086class HMemoryBarrier : public HTemplateInstruction<0> {
6087 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006088 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006089 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006090 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6091 SetPackedField<BarrierKindField>(barrier_kind);
6092 }
Calin Juravle27df7582015-04-17 19:12:31 +01006093
Vladimir Markoa1de9182016-02-25 11:37:38 +00006094 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006095
6096 DECLARE_INSTRUCTION(MemoryBarrier);
6097
6098 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006099 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6100 static constexpr size_t kFieldBarrierKindSize =
6101 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6102 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6103 kFieldBarrierKind + kFieldBarrierKindSize;
6104 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6105 "Too many packed fields.");
6106 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006107
6108 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6109};
6110
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006111class HMonitorOperation : public HTemplateInstruction<1> {
6112 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006113 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006114 kEnter,
6115 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006116 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006117 };
6118
6119 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006120 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006121 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6122 dex_pc) {
6123 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006124 SetRawInputAt(0, object);
6125 }
6126
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006127 // Instruction may go into runtime, so we need an environment.
6128 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006129
6130 bool CanThrow() const OVERRIDE {
6131 // Verifier guarantees that monitor-exit cannot throw.
6132 // This is important because it allows the HGraphBuilder to remove
6133 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6134 return IsEnter();
6135 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006136
Vladimir Markoa1de9182016-02-25 11:37:38 +00006137 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6138 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006139
6140 DECLARE_INSTRUCTION(MonitorOperation);
6141
Calin Juravle52c48962014-12-16 17:02:57 +00006142 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006143 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6144 static constexpr size_t kFieldOperationKindSize =
6145 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6146 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6147 kFieldOperationKind + kFieldOperationKindSize;
6148 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6149 "Too many packed fields.");
6150 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006151
6152 private:
6153 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6154};
6155
David Brazdil74eb1b22015-12-14 11:44:01 +00006156class HSelect : public HExpression<3> {
6157 public:
6158 HSelect(HInstruction* condition,
6159 HInstruction* true_value,
6160 HInstruction* false_value,
6161 uint32_t dex_pc)
6162 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6163 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6164
6165 // First input must be `true_value` or `false_value` to allow codegens to
6166 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6167 // that architectures which implement HSelect as a conditional move also
6168 // will not need to invert the condition.
6169 SetRawInputAt(0, false_value);
6170 SetRawInputAt(1, true_value);
6171 SetRawInputAt(2, condition);
6172 }
6173
6174 HInstruction* GetFalseValue() const { return InputAt(0); }
6175 HInstruction* GetTrueValue() const { return InputAt(1); }
6176 HInstruction* GetCondition() const { return InputAt(2); }
6177
6178 bool CanBeMoved() const OVERRIDE { return true; }
6179 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6180
6181 bool CanBeNull() const OVERRIDE {
6182 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6183 }
6184
6185 DECLARE_INSTRUCTION(Select);
6186
6187 private:
6188 DISALLOW_COPY_AND_ASSIGN(HSelect);
6189};
6190
Vladimir Markof9f64412015-09-02 14:05:49 +01006191class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006192 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006193 MoveOperands(Location source,
6194 Location destination,
6195 Primitive::Type type,
6196 HInstruction* instruction)
6197 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006198
6199 Location GetSource() const { return source_; }
6200 Location GetDestination() const { return destination_; }
6201
6202 void SetSource(Location value) { source_ = value; }
6203 void SetDestination(Location value) { destination_ = value; }
6204
6205 // The parallel move resolver marks moves as "in-progress" by clearing the
6206 // destination (but not the source).
6207 Location MarkPending() {
6208 DCHECK(!IsPending());
6209 Location dest = destination_;
6210 destination_ = Location::NoLocation();
6211 return dest;
6212 }
6213
6214 void ClearPending(Location dest) {
6215 DCHECK(IsPending());
6216 destination_ = dest;
6217 }
6218
6219 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006220 DCHECK(source_.IsValid() || destination_.IsInvalid());
6221 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006222 }
6223
6224 // True if this blocks a move from the given location.
6225 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006226 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006227 }
6228
6229 // A move is redundant if it's been eliminated, if its source and
6230 // destination are the same, or if its destination is unneeded.
6231 bool IsRedundant() const {
6232 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6233 }
6234
6235 // We clear both operands to indicate move that's been eliminated.
6236 void Eliminate() {
6237 source_ = destination_ = Location::NoLocation();
6238 }
6239
6240 bool IsEliminated() const {
6241 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6242 return source_.IsInvalid();
6243 }
6244
Alexey Frunze4dda3372015-06-01 18:31:49 -07006245 Primitive::Type GetType() const { return type_; }
6246
Nicolas Geoffray90218252015-04-15 11:56:51 +01006247 bool Is64BitMove() const {
6248 return Primitive::Is64BitType(type_);
6249 }
6250
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006251 HInstruction* GetInstruction() const { return instruction_; }
6252
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006253 private:
6254 Location source_;
6255 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006256 // The type this move is for.
6257 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006258 // The instruction this move is assocatied with. Null when this move is
6259 // for moving an input in the expected locations of user (including a phi user).
6260 // This is only used in debug mode, to ensure we do not connect interval siblings
6261 // in the same parallel move.
6262 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006263};
6264
Roland Levillainc9285912015-12-18 10:38:42 +00006265std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6266
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006267static constexpr size_t kDefaultNumberOfMoves = 4;
6268
6269class HParallelMove : public HTemplateInstruction<0> {
6270 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006271 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006272 : HTemplateInstruction(SideEffects::None(), dex_pc),
6273 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6274 moves_.reserve(kDefaultNumberOfMoves);
6275 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006276
Nicolas Geoffray90218252015-04-15 11:56:51 +01006277 void AddMove(Location source,
6278 Location destination,
6279 Primitive::Type type,
6280 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006281 DCHECK(source.IsValid());
6282 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006283 if (kIsDebugBuild) {
6284 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006285 for (const MoveOperands& move : moves_) {
6286 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006287 // Special case the situation where the move is for the spill slot
6288 // of the instruction.
6289 if ((GetPrevious() == instruction)
6290 || ((GetPrevious() == nullptr)
6291 && instruction->IsPhi()
6292 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006293 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006294 << "Doing parallel moves for the same instruction.";
6295 } else {
6296 DCHECK(false) << "Doing parallel moves for the same instruction.";
6297 }
6298 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006299 }
6300 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006301 for (const MoveOperands& move : moves_) {
6302 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006303 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006304 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006305 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006306 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006307 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006308 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006309 }
6310
Vladimir Marko225b6462015-09-28 12:17:40 +01006311 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006312 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006313 }
6314
Vladimir Marko225b6462015-09-28 12:17:40 +01006315 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006316
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006317 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006318
6319 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006320 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006321
6322 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6323};
6324
Mark Mendell0616ae02015-04-17 12:49:27 -04006325} // namespace art
6326
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006327#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6328#include "nodes_shared.h"
6329#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006330#ifdef ART_ENABLE_CODEGEN_arm
6331#include "nodes_arm.h"
6332#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006333#ifdef ART_ENABLE_CODEGEN_arm64
6334#include "nodes_arm64.h"
6335#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006336#ifdef ART_ENABLE_CODEGEN_x86
6337#include "nodes_x86.h"
6338#endif
6339
6340namespace art {
6341
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006342class HGraphVisitor : public ValueObject {
6343 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006344 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6345 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006346
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006347 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006348 virtual void VisitBasicBlock(HBasicBlock* block);
6349
Roland Levillain633021e2014-10-01 14:12:25 +01006350 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006351 void VisitInsertionOrder();
6352
Roland Levillain633021e2014-10-01 14:12:25 +01006353 // Visit the graph following dominator tree reverse post-order.
6354 void VisitReversePostOrder();
6355
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006356 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006357
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006358 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006359#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006360 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6361
6362 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6363
6364#undef DECLARE_VISIT_INSTRUCTION
6365
6366 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006367 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006368
6369 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6370};
6371
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006372class HGraphDelegateVisitor : public HGraphVisitor {
6373 public:
6374 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6375 virtual ~HGraphDelegateVisitor() {}
6376
6377 // Visit functions that delegate to to super class.
6378#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006379 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006380
6381 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6382
6383#undef DECLARE_VISIT_INSTRUCTION
6384
6385 private:
6386 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6387};
6388
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006389class HInsertionOrderIterator : public ValueObject {
6390 public:
6391 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6392
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006393 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006394 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006395 void Advance() { ++index_; }
6396
6397 private:
6398 const HGraph& graph_;
6399 size_t index_;
6400
6401 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6402};
6403
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006404class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006405 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006406 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6407 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006408 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006409 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006410
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006411 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6412 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006413 void Advance() { ++index_; }
6414
6415 private:
6416 const HGraph& graph_;
6417 size_t index_;
6418
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006419 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006420};
6421
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006422class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006423 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006424 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006425 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006426 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006427 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006428 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006429
6430 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006431 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006432 void Advance() { --index_; }
6433
6434 private:
6435 const HGraph& graph_;
6436 size_t index_;
6437
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006438 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006439};
6440
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006441class HLinearPostOrderIterator : public ValueObject {
6442 public:
6443 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006444 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006445
6446 bool Done() const { return index_ == 0; }
6447
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006448 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006449
6450 void Advance() {
6451 --index_;
6452 DCHECK_GE(index_, 0U);
6453 }
6454
6455 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006456 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006457 size_t index_;
6458
6459 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6460};
6461
6462class HLinearOrderIterator : public ValueObject {
6463 public:
6464 explicit HLinearOrderIterator(const HGraph& graph)
6465 : order_(graph.GetLinearOrder()), index_(0) {}
6466
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006467 bool Done() const { return index_ == order_.size(); }
6468 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006469 void Advance() { ++index_; }
6470
6471 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006472 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006473 size_t index_;
6474
6475 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6476};
6477
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006478// Iterator over the blocks that art part of the loop. Includes blocks part
6479// of an inner loop. The order in which the blocks are iterated is on their
6480// block id.
6481class HBlocksInLoopIterator : public ValueObject {
6482 public:
6483 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6484 : blocks_in_loop_(info.GetBlocks()),
6485 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6486 index_(0) {
6487 if (!blocks_in_loop_.IsBitSet(index_)) {
6488 Advance();
6489 }
6490 }
6491
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006492 bool Done() const { return index_ == blocks_.size(); }
6493 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006494 void Advance() {
6495 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006496 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006497 if (blocks_in_loop_.IsBitSet(index_)) {
6498 break;
6499 }
6500 }
6501 }
6502
6503 private:
6504 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006505 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006506 size_t index_;
6507
6508 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6509};
6510
Mingyao Yang3584bce2015-05-19 16:01:59 -07006511// Iterator over the blocks that art part of the loop. Includes blocks part
6512// of an inner loop. The order in which the blocks are iterated is reverse
6513// post order.
6514class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6515 public:
6516 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6517 : blocks_in_loop_(info.GetBlocks()),
6518 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6519 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006520 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006521 Advance();
6522 }
6523 }
6524
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006525 bool Done() const { return index_ == blocks_.size(); }
6526 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006527 void Advance() {
6528 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006529 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6530 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006531 break;
6532 }
6533 }
6534 }
6535
6536 private:
6537 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006538 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006539 size_t index_;
6540
6541 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6542};
6543
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006544inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006545 if (constant->IsIntConstant()) {
6546 return constant->AsIntConstant()->GetValue();
6547 } else if (constant->IsLongConstant()) {
6548 return constant->AsLongConstant()->GetValue();
6549 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006550 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006551 return 0;
6552 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006553}
6554
Vladimir Marko58155012015-08-19 12:49:41 +00006555inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6556 // For the purposes of the compiler, the dex files must actually be the same object
6557 // if we want to safely treat them as the same. This is especially important for JIT
6558 // as custom class loaders can open the same underlying file (or memory) multiple
6559 // times and provide different class resolution but no two class loaders should ever
6560 // use the same DexFile object - doing so is an unsupported hack that can lead to
6561 // all sorts of weird failures.
6562 return &lhs == &rhs;
6563}
6564
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006565#define INSTRUCTION_TYPE_CHECK(type, super) \
6566 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6567 inline const H##type* HInstruction::As##type() const { \
6568 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6569 } \
6570 inline H##type* HInstruction::As##type() { \
6571 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6572 }
6573
6574 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6575#undef INSTRUCTION_TYPE_CHECK
6576
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006577// Create space in `blocks` for adding `number_of_new_blocks` entries
6578// starting at location `at`. Blocks after `at` are moved accordingly.
6579inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6580 size_t number_of_new_blocks,
6581 size_t after) {
6582 DCHECK_LT(after, blocks->size());
6583 size_t old_size = blocks->size();
6584 size_t new_size = old_size + number_of_new_blocks;
6585 blocks->resize(new_size);
6586 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6587}
6588
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006589} // namespace art
6590
6591#endif // ART_COMPILER_OPTIMIZING_NODES_H_