blob: 12ea059d3f39ba9ea4d2cfade896a640a70524b4 [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 Marko46817b82016-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 Marko456307a2016-04-19 14:12:13 +0000173 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000174 return IsValidHandle(type_handle_);
175 }
176
177 bool IsExact() const { return is_exact_; }
178
179 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
180 DCHECK(IsValid());
181 return GetTypeHandle()->IsObjectClass();
182 }
183
184 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
185 DCHECK(IsValid());
186 return GetTypeHandle()->IsStringClass();
187 }
188
189 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
190 DCHECK(IsValid());
191 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
192 }
193
194 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
195 DCHECK(IsValid());
196 return GetTypeHandle()->IsInterface();
197 }
198
199 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
200 DCHECK(IsValid());
201 return GetTypeHandle()->IsArrayClass();
202 }
203
204 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
205 DCHECK(IsValid());
206 return GetTypeHandle()->IsPrimitiveArray();
207 }
208
209 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
210 DCHECK(IsValid());
211 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
212 }
213
214 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
215 DCHECK(IsValid());
216 if (!IsExact()) return false;
217 if (!IsArrayClass()) return false;
218 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
219 }
220
221 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
222 DCHECK(IsValid());
223 if (!IsExact()) return false;
224 if (!IsArrayClass()) return false;
225 if (!rti.IsArrayClass()) return false;
226 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
227 rti.GetTypeHandle()->GetComponentType());
228 }
229
230 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
231
232 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
233 DCHECK(IsValid());
234 DCHECK(rti.IsValid());
235 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
236 }
237
238 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
239 DCHECK(IsValid());
240 DCHECK(rti.IsValid());
241 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
242 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
243 }
244
245 // Returns true if the type information provide the same amount of details.
246 // Note that it does not mean that the instructions have the same actual type
247 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000248 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000249 if (!IsValid() && !rti.IsValid()) {
250 // Invalid types are equal.
251 return true;
252 }
253 if (!IsValid() || !rti.IsValid()) {
254 // One is valid, the other not.
255 return false;
256 }
257 return IsExact() == rti.IsExact()
258 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
259 }
260
261 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000262 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
263 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
264 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000265
266 // The class of the object.
267 TypeHandle type_handle_;
268 // Whether or not the type is exact or a superclass of the actual type.
269 // Whether or not we have any information about this type.
270 bool is_exact_;
271};
272
273std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
274
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000275// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100276class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000277 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100278 HGraph(ArenaAllocator* arena,
279 const DexFile& dex_file,
280 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100281 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700282 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100283 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100284 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000285 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100286 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000287 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100288 blocks_(arena->Adapter(kArenaAllocBlockList)),
289 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
290 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700291 entry_block_(nullptr),
292 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100293 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100294 number_of_vregs_(0),
295 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000296 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400297 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000298 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000299 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000300 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000301 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100302 dex_file_(dex_file),
303 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100304 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100305 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100306 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700307 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000308 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100309 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
310 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
311 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
312 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000313 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000314 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
315 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100316 blocks_.reserve(kDefaultNumberOfBlocks);
317 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000318
David Brazdilbadd8262016-02-02 16:28:56 +0000319 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
320 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
321
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000322 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100323 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
324
David Brazdil69ba7b72015-06-23 18:27:30 +0100325 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000326 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100327
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000328 HBasicBlock* GetEntryBlock() const { return entry_block_; }
329 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100330 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000331
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000332 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
333 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000334
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000335 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100336
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100337 void ComputeDominanceInformation();
338 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000339 void ClearLoopInformation();
340 void FindBackEdges(ArenaBitVector* visited);
341 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100342 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100343 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000344
David Brazdil4833f5a2015-12-16 10:37:39 +0000345 // Analyze all natural loops in this graph. Returns a code specifying that it
346 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000347 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000348 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100349
David Brazdilffee3d32015-07-06 11:48:53 +0100350 // Iterate over blocks to compute try block membership. Needs reverse post
351 // order and loop information.
352 void ComputeTryBlockInformation();
353
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000354 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000355 // Returns the instruction to replace the invoke expression or null if the
356 // invoke is for a void method. Note that the caller is responsible for replacing
357 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000358 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000359
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000360 // Update the loop and try membership of `block`, which was spawned from `reference`.
361 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
362 // should be the new back edge.
363 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
364 HBasicBlock* reference,
365 bool replace_if_back_edge);
366
Mingyao Yang3584bce2015-05-19 16:01:59 -0700367 // Need to add a couple of blocks to test if the loop body is entered and
368 // put deoptimization instructions, etc.
369 void TransformLoopHeaderForBCE(HBasicBlock* header);
370
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000371 // Removes `block` from the graph. Assumes `block` has been disconnected from
372 // other blocks and has no instructions or phis.
373 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000374
David Brazdilfc6a86a2015-06-26 10:33:45 +0000375 // Splits the edge between `block` and `successor` while preserving the
376 // indices in the predecessor/successor lists. If there are multiple edges
377 // between the blocks, the lowest indices are used.
378 // Returns the new block which is empty and has the same dex pc as `successor`.
379 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
380
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100381 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
382 void SimplifyLoop(HBasicBlock* header);
383
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000384 int32_t GetNextInstructionId() {
385 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000386 return current_instruction_id_++;
387 }
388
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000389 int32_t GetCurrentInstructionId() const {
390 return current_instruction_id_;
391 }
392
393 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000394 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000395 current_instruction_id_ = id;
396 }
397
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100398 uint16_t GetMaximumNumberOfOutVRegs() const {
399 return maximum_number_of_out_vregs_;
400 }
401
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000402 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
403 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100404 }
405
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100406 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
407 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
408 }
409
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000410 void UpdateTemporariesVRegSlots(size_t slots) {
411 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 }
413
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000414 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100415 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000416 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100417 }
418
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100419 void SetNumberOfVRegs(uint16_t number_of_vregs) {
420 number_of_vregs_ = number_of_vregs;
421 }
422
423 uint16_t GetNumberOfVRegs() const {
424 return number_of_vregs_;
425 }
426
427 void SetNumberOfInVRegs(uint16_t value) {
428 number_of_in_vregs_ = value;
429 }
430
David Brazdildee58d62016-04-07 09:54:26 +0000431 uint16_t GetNumberOfInVRegs() const {
432 return number_of_in_vregs_;
433 }
434
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100435 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100436 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100437 return number_of_vregs_ - number_of_in_vregs_;
438 }
439
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100440 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100441 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100442 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100443
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100444 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100445 return linear_order_;
446 }
447
Mark Mendell1152c922015-04-24 17:06:35 -0400448 bool HasBoundsChecks() const {
449 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800450 }
451
Mark Mendell1152c922015-04-24 17:06:35 -0400452 void SetHasBoundsChecks(bool value) {
453 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800454 }
455
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100456 bool ShouldGenerateConstructorBarrier() const {
457 return should_generate_constructor_barrier_;
458 }
459
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000460 bool IsDebuggable() const { return debuggable_; }
461
David Brazdil8d5b8b22015-03-24 10:51:52 +0000462 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000463 // already, it is created and inserted into the graph. This method is only for
464 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600465 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000466
467 // TODO: This is problematic for the consistency of reference type propagation
468 // because it can be created anytime after the pass and thus it will be left
469 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600470 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000471
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600472 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
473 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000474 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600475 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
476 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000477 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600478 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
479 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000480 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600481 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
482 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000483 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000484
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100485 HCurrentMethod* GetCurrentMethod();
486
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100487 const DexFile& GetDexFile() const {
488 return dex_file_;
489 }
490
491 uint32_t GetMethodIdx() const {
492 return method_idx_;
493 }
494
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100495 InvokeType GetInvokeType() const {
496 return invoke_type_;
497 }
498
Mark Mendellc4701932015-04-10 13:18:51 -0400499 InstructionSet GetInstructionSet() const {
500 return instruction_set_;
501 }
502
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000503 bool IsCompilingOsr() const { return osr_; }
504
David Brazdil77a48ae2015-09-15 12:34:04 +0000505 bool HasTryCatch() const { return has_try_catch_; }
506 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100507
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000508 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
509 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
510
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100511 ArtMethod* GetArtMethod() const { return art_method_; }
512 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
513
Mark Mendellf6529172015-11-17 11:16:56 -0500514 // Returns an instruction with the opposite boolean value from 'cond'.
515 // The instruction has been inserted into the graph, either as a constant, or
516 // before cursor.
517 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
518
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000519 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
520
David Brazdil2d7352b2015-04-20 14:52:42 +0100521 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000522 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100523 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000524
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000525 template <class InstructionType, typename ValueType>
526 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600527 ArenaSafeMap<ValueType, InstructionType*>* cache,
528 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000529 // Try to find an existing constant of the given value.
530 InstructionType* constant = nullptr;
531 auto cached_constant = cache->find(value);
532 if (cached_constant != cache->end()) {
533 constant = cached_constant->second;
534 }
535
536 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100537 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000538 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600539 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000540 cache->Overwrite(value, constant);
541 InsertConstant(constant);
542 }
543 return constant;
544 }
545
David Brazdil8d5b8b22015-03-24 10:51:52 +0000546 void InsertConstant(HConstant* instruction);
547
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000548 // Cache a float constant into the graph. This method should only be
549 // called by the SsaBuilder when creating "equivalent" instructions.
550 void CacheFloatConstant(HFloatConstant* constant);
551
552 // See CacheFloatConstant comment.
553 void CacheDoubleConstant(HDoubleConstant* constant);
554
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000555 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000556
557 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100558 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000559
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100560 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100561 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000562
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100563 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100564 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100565
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000566 HBasicBlock* entry_block_;
567 HBasicBlock* exit_block_;
568
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100569 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100570 uint16_t maximum_number_of_out_vregs_;
571
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100572 // The number of virtual registers in this method. Contains the parameters.
573 uint16_t number_of_vregs_;
574
575 // The number of virtual registers used by parameters of this method.
576 uint16_t number_of_in_vregs_;
577
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000578 // Number of vreg size slots that the temporaries use (used in baseline compiler).
579 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100580
Mark Mendell1152c922015-04-24 17:06:35 -0400581 // Has bounds checks. We can totally skip BCE if it's false.
582 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800583
David Brazdil77a48ae2015-09-15 12:34:04 +0000584 // Flag whether there are any try/catch blocks in the graph. We will skip
585 // try/catch-related passes if false.
586 bool has_try_catch_;
587
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000588 // Flag whether there are any irreducible loops in the graph.
589 bool has_irreducible_loops_;
590
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000591 // Indicates whether the graph should be compiled in a way that
592 // ensures full debuggability. If false, we can apply more
593 // aggressive optimizations that may limit the level of debugging.
594 const bool debuggable_;
595
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000596 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000597 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000598
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100599 // The dex file from which the method is from.
600 const DexFile& dex_file_;
601
602 // The method index in the dex file.
603 const uint32_t method_idx_;
604
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100605 // If inlined, this encodes how the callee is being invoked.
606 const InvokeType invoke_type_;
607
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100608 // Whether the graph has been transformed to SSA form. Only used
609 // in debug mode to ensure we are not using properties only valid
610 // for non-SSA form (like the number of temporaries).
611 bool in_ssa_form_;
612
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100613 const bool should_generate_constructor_barrier_;
614
Mathieu Chartiere401d142015-04-22 13:56:20 -0700615 const InstructionSet instruction_set_;
616
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000617 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000618 HNullConstant* cached_null_constant_;
619 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000620 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000621 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000622 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000623
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100624 HCurrentMethod* cached_current_method_;
625
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100626 // The ArtMethod this graph is for. Note that for AOT, it may be null,
627 // for example for methods whose declaring class could not be resolved
628 // (such as when the superclass could not be found).
629 ArtMethod* art_method_;
630
David Brazdil4833f5a2015-12-16 10:37:39 +0000631 // Keep the RTI of inexact Object to avoid having to pass stack handle
632 // collection pointer to passes which may create NullConstant.
633 ReferenceTypeInfo inexact_object_rti_;
634
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000635 // Whether we are compiling this graph for on stack replacement: this will
636 // make all loops seen as irreducible and emit special stack maps to mark
637 // compiled code entries which the interpreter can directly jump to.
638 const bool osr_;
639
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000640 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100641 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000642 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000643 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000644 DISALLOW_COPY_AND_ASSIGN(HGraph);
645};
646
Vladimir Markof9f64412015-09-02 14:05:49 +0100647class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000648 public:
649 HLoopInformation(HBasicBlock* header, HGraph* graph)
650 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100651 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000652 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100653 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100654 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100655 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000656 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100657 back_edges_.reserve(kDefaultNumberOfBackEdges);
658 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100659
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000660 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100661 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000662
663 void Dump(std::ostream& os);
664
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100665 HBasicBlock* GetHeader() const {
666 return header_;
667 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000668
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000669 void SetHeader(HBasicBlock* block) {
670 header_ = block;
671 }
672
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100673 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
674 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
675 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
676
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000677 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100678 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000679 }
680
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100681 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100682 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100683 }
684
David Brazdil46e2a392015-03-16 17:31:52 +0000685 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100686 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100687 }
688
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000689 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100690 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000691 }
692
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100693 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100694
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100695 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100696 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100697 }
698
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100699 // Returns the lifetime position of the back edge that has the
700 // greatest lifetime position.
701 size_t GetLifetimeEnd() const;
702
703 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100704 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100705 }
706
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000707 // Finds blocks that are part of this loop.
708 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100709
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100710 // Returns whether this loop information contains `block`.
711 // Note that this loop information *must* be populated before entering this function.
712 bool Contains(const HBasicBlock& block) const;
713
714 // Returns whether this loop information is an inner loop of `other`.
715 // Note that `other` *must* be populated before entering this function.
716 bool IsIn(const HLoopInformation& other) const;
717
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800718 // Returns true if instruction is not defined within this loop.
719 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700720
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100721 const ArenaBitVector& GetBlocks() const { return blocks_; }
722
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000723 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000724 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000725
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000726 void ClearAllBlocks() {
727 blocks_.ClearAllBits();
728 }
729
David Brazdil3f4a5222016-05-06 12:46:21 +0100730 bool HasBackEdgeNotDominatedByHeader() const;
731
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100732 bool IsPopulated() const {
733 return blocks_.GetHighestBitSet() != -1;
734 }
735
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000736 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100737 // Internal recursive implementation of `Populate`.
738 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100739 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100740
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000741 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100742 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000743 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100744 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100745 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100746 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000747
748 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
749};
750
David Brazdilec16f792015-08-19 15:04:01 +0100751// Stores try/catch information for basic blocks.
752// Note that HGraph is constructed so that catch blocks cannot simultaneously
753// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100754class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100755 public:
756 // Try block information constructor.
757 explicit TryCatchInformation(const HTryBoundary& try_entry)
758 : try_entry_(&try_entry),
759 catch_dex_file_(nullptr),
760 catch_type_index_(DexFile::kDexNoIndex16) {
761 DCHECK(try_entry_ != nullptr);
762 }
763
764 // Catch block information constructor.
765 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
766 : try_entry_(nullptr),
767 catch_dex_file_(&dex_file),
768 catch_type_index_(catch_type_index) {}
769
770 bool IsTryBlock() const { return try_entry_ != nullptr; }
771
772 const HTryBoundary& GetTryEntry() const {
773 DCHECK(IsTryBlock());
774 return *try_entry_;
775 }
776
777 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
778
779 bool IsCatchAllTypeIndex() const {
780 DCHECK(IsCatchBlock());
781 return catch_type_index_ == DexFile::kDexNoIndex16;
782 }
783
784 uint16_t GetCatchTypeIndex() const {
785 DCHECK(IsCatchBlock());
786 return catch_type_index_;
787 }
788
789 const DexFile& GetCatchDexFile() const {
790 DCHECK(IsCatchBlock());
791 return *catch_dex_file_;
792 }
793
794 private:
795 // One of possibly several TryBoundary instructions entering the block's try.
796 // Only set for try blocks.
797 const HTryBoundary* try_entry_;
798
799 // Exception type information. Only set for catch blocks.
800 const DexFile* catch_dex_file_;
801 const uint16_t catch_type_index_;
802};
803
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100804static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100805static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100806
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000807// A block in a method. Contains the list of instructions represented
808// as a double linked list. Each block knows its predecessors and
809// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100810
Vladimir Markof9f64412015-09-02 14:05:49 +0100811class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000812 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600813 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000814 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000815 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
816 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000817 loop_information_(nullptr),
818 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000819 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100820 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100821 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100822 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000823 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000824 try_catch_information_(nullptr) {
825 predecessors_.reserve(kDefaultNumberOfPredecessors);
826 successors_.reserve(kDefaultNumberOfSuccessors);
827 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
828 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000829
Vladimir Marko60584552015-09-03 13:35:12 +0000830 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100831 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000832 }
833
Vladimir Marko60584552015-09-03 13:35:12 +0000834 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100835 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000836 }
837
David Brazdild26a4112015-11-10 11:07:31 +0000838 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
839 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
840
Vladimir Marko60584552015-09-03 13:35:12 +0000841 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
842 return ContainsElement(successors_, block, start_from);
843 }
844
845 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100846 return dominated_blocks_;
847 }
848
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100849 bool IsEntryBlock() const {
850 return graph_->GetEntryBlock() == this;
851 }
852
853 bool IsExitBlock() const {
854 return graph_->GetExitBlock() == this;
855 }
856
David Brazdil46e2a392015-03-16 17:31:52 +0000857 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000858 bool IsSingleTryBoundary() const;
859
860 // Returns true if this block emits nothing but a jump.
861 bool IsSingleJump() const {
862 HLoopInformation* loop_info = GetLoopInformation();
863 return (IsSingleGoto() || IsSingleTryBoundary())
864 // Back edges generate a suspend check.
865 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
866 }
David Brazdil46e2a392015-03-16 17:31:52 +0000867
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000868 void AddBackEdge(HBasicBlock* back_edge) {
869 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000870 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000871 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100872 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000873 loop_information_->AddBackEdge(back_edge);
874 }
875
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000876 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000877 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000878
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100879 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000880 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600881 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000882
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000883 HBasicBlock* GetDominator() const { return dominator_; }
884 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000885 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
886
887 void RemoveDominatedBlock(HBasicBlock* block) {
888 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100889 }
Vladimir Marko60584552015-09-03 13:35:12 +0000890
891 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
892 ReplaceElement(dominated_blocks_, existing, new_block);
893 }
894
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100895 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000896
897 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100898 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000899 }
900
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100901 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
902 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100903 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100904 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100905 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
906 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000907
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000908 HInstruction* GetFirstInstructionDisregardMoves() const;
909
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000910 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000911 successors_.push_back(block);
912 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000913 }
914
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100915 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
916 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100917 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000918 new_block->predecessors_.push_back(this);
919 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000920 }
921
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000922 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
923 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000924 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000925 new_block->successors_.push_back(this);
926 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000927 }
928
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100929 // Insert `this` between `predecessor` and `successor. This method
930 // preserves the indicies, and will update the first edge found between
931 // `predecessor` and `successor`.
932 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
933 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100934 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000935 successor->predecessors_[predecessor_index] = this;
936 predecessor->successors_[successor_index] = this;
937 successors_.push_back(successor);
938 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100939 }
940
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100941 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000942 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100943 }
944
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000945 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000946 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000947 }
948
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100949 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000950 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100951 }
952
953 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000954 predecessors_.push_back(block);
955 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100956 }
957
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100958 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000959 DCHECK_EQ(predecessors_.size(), 2u);
960 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100961 }
962
David Brazdil769c9e52015-04-27 13:54:09 +0100963 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000964 DCHECK_EQ(successors_.size(), 2u);
965 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100966 }
967
David Brazdilfc6a86a2015-06-26 10:33:45 +0000968 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000969 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100970 }
971
David Brazdilfc6a86a2015-06-26 10:33:45 +0000972 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000973 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100974 }
975
David Brazdilfc6a86a2015-06-26 10:33:45 +0000976 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000977 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100978 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000979 }
980
981 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000982 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100983 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000984 }
985
986 // Returns whether the first occurrence of `predecessor` in the list of
987 // predecessors is at index `idx`.
988 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100989 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000990 return GetPredecessorIndexOf(predecessor) == idx;
991 }
992
David Brazdild7558da2015-09-22 13:04:14 +0100993 // Create a new block between this block and its predecessors. The new block
994 // is added to the graph, all predecessor edges are relinked to it and an edge
995 // is created to `this`. Returns the new empty block. Reverse post order or
996 // loop and try/catch information are not updated.
997 HBasicBlock* CreateImmediateDominator();
998
David Brazdilfc6a86a2015-06-26 10:33:45 +0000999 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001000 // created, latter block. Note that this method will add the block to the
1001 // graph, create a Goto at the end of the former block and will create an edge
1002 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001003 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001004 HBasicBlock* SplitBefore(HInstruction* cursor);
1005
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001006 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001007 // created block. Note that this method just updates raw block information,
1008 // like predecessors, successors, dominators, and instruction list. It does not
1009 // update the graph, reverse post order, loop information, nor make sure the
1010 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001011 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1012
1013 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1014 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001015
1016 // Merge `other` at the end of `this`. Successors and dominated blocks of
1017 // `other` are changed to be successors and dominated blocks of `this`. Note
1018 // that this method does not update the graph, reverse post order, loop
1019 // information, nor make sure the blocks are consistent (for example ending
1020 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001021 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001022
1023 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1024 // of `this` are moved to `other`.
1025 // Note that this method does not update the graph, reverse post order, loop
1026 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001027 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001028 void ReplaceWith(HBasicBlock* other);
1029
David Brazdil2d7352b2015-04-20 14:52:42 +01001030 // Merge `other` at the end of `this`. This method updates loops, reverse post
1031 // order, links to predecessors, successors, dominators and deletes the block
1032 // from the graph. The two blocks must be successive, i.e. `this` the only
1033 // predecessor of `other` and vice versa.
1034 void MergeWith(HBasicBlock* other);
1035
1036 // Disconnects `this` from all its predecessors, successors and dominator,
1037 // removes it from all loops it is included in and eventually from the graph.
1038 // The block must not dominate any other block. Predecessors and successors
1039 // are safely updated.
1040 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001041
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001042 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001043 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001044 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001045 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001046 // Replace instruction `initial` with `replacement` within this block.
1047 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1048 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001049 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001050 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001051 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001052 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1053 // instruction list. With 'ensure_safety' set to true, it verifies that the
1054 // instruction is not in use and removes it from the use lists of its inputs.
1055 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1056 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001057 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001058
1059 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001060 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001061 }
1062
Roland Levillain6b879dd2014-09-22 17:13:44 +01001063 bool IsLoopPreHeaderFirstPredecessor() const {
1064 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001065 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001066 }
1067
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001068 bool IsFirstPredecessorBackEdge() const {
1069 DCHECK(IsLoopHeader());
1070 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1071 }
1072
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001073 HLoopInformation* GetLoopInformation() const {
1074 return loop_information_;
1075 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001076
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001077 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001078 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001079 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001080 void SetInLoop(HLoopInformation* info) {
1081 if (IsLoopHeader()) {
1082 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001083 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001084 loop_information_ = info;
1085 } else if (loop_information_->Contains(*info->GetHeader())) {
1086 // Block is currently part of an outer loop. Make it part of this inner loop.
1087 // Note that a non loop header having a loop information means this loop information
1088 // has already been populated
1089 loop_information_ = info;
1090 } else {
1091 // Block is part of an inner loop. Do not update the loop information.
1092 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1093 // at this point, because this method is being called while populating `info`.
1094 }
1095 }
1096
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001097 // Raw update of the loop information.
1098 void SetLoopInformation(HLoopInformation* info) {
1099 loop_information_ = info;
1100 }
1101
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001102 bool IsInLoop() const { return loop_information_ != nullptr; }
1103
David Brazdilec16f792015-08-19 15:04:01 +01001104 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1105
1106 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1107 try_catch_information_ = try_catch_information;
1108 }
1109
1110 bool IsTryBlock() const {
1111 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1112 }
1113
1114 bool IsCatchBlock() const {
1115 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1116 }
David Brazdilffee3d32015-07-06 11:48:53 +01001117
1118 // Returns the try entry that this block's successors should have. They will
1119 // be in the same try, unless the block ends in a try boundary. In that case,
1120 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001121 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001122
David Brazdild7558da2015-09-22 13:04:14 +01001123 bool HasThrowingInstructions() const;
1124
David Brazdila4b8c212015-05-07 09:59:30 +01001125 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001126 bool Dominates(HBasicBlock* block) const;
1127
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001128 size_t GetLifetimeStart() const { return lifetime_start_; }
1129 size_t GetLifetimeEnd() const { return lifetime_end_; }
1130
1131 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1132 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1133
David Brazdil8d5b8b22015-03-24 10:51:52 +00001134 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001135 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001136 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001137 bool HasSinglePhi() const;
1138
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001139 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001140 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001141 ArenaVector<HBasicBlock*> predecessors_;
1142 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001143 HInstructionList instructions_;
1144 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001145 HLoopInformation* loop_information_;
1146 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001147 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001148 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001149 // The dex program counter of the first instruction of this block.
1150 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001151 size_t lifetime_start_;
1152 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001153 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001154
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001155 friend class HGraph;
1156 friend class HInstruction;
1157
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001158 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1159};
1160
David Brazdilb2bd1c52015-03-25 11:17:37 +00001161// Iterates over the LoopInformation of all loops which contain 'block'
1162// from the innermost to the outermost.
1163class HLoopInformationOutwardIterator : public ValueObject {
1164 public:
1165 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1166 : current_(block.GetLoopInformation()) {}
1167
1168 bool Done() const { return current_ == nullptr; }
1169
1170 void Advance() {
1171 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001172 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001173 }
1174
1175 HLoopInformation* Current() const {
1176 DCHECK(!Done());
1177 return current_;
1178 }
1179
1180 private:
1181 HLoopInformation* current_;
1182
1183 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1184};
1185
Alexandre Ramesef20f712015-06-09 10:29:30 +01001186#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001187 M(Above, Condition) \
1188 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001189 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001190 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001191 M(ArrayGet, Instruction) \
1192 M(ArrayLength, Instruction) \
1193 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001194 M(Below, Condition) \
1195 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001196 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001197 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001198 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001199 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001200 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001201 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001202 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001203 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001204 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001205 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001206 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001207 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001208 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001209 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001210 M(Exit, Instruction) \
1211 M(FloatConstant, Constant) \
1212 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001213 M(GreaterThan, Condition) \
1214 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001215 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001216 M(InstanceFieldGet, Instruction) \
1217 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001218 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001219 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001220 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001221 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001222 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001223 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001224 M(LessThan, Condition) \
1225 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001226 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001227 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001228 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001229 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001230 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001231 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001232 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001233 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001234 M(Neg, UnaryOperation) \
1235 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001236 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001237 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001238 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001239 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001240 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001241 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001242 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001243 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001244 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001245 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001246 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001247 M(Return, Instruction) \
1248 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001249 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001250 M(Shl, BinaryOperation) \
1251 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001252 M(StaticFieldGet, Instruction) \
1253 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001254 M(UnresolvedInstanceFieldGet, Instruction) \
1255 M(UnresolvedInstanceFieldSet, Instruction) \
1256 M(UnresolvedStaticFieldGet, Instruction) \
1257 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001258 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001259 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001260 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001261 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001262 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001263 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001264 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001265 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001266
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001267/*
1268 * Instructions, shared across several (not all) architectures.
1269 */
1270#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1271#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1272#else
1273#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001274 M(BitwiseNegatedRight, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001275 M(MultiplyAccumulate, Instruction)
1276#endif
1277
Vladimir Markob4536b72015-11-24 13:45:23 +00001278#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001279#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001280#else
1281#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1282 M(ArmDexCacheArraysBase, Instruction)
1283#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001284
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001285#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001286#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001287#else
1288#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001289 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001290 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001291#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001292
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001293#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1294
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001295#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1296
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001297#ifndef ART_ENABLE_CODEGEN_x86
1298#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1299#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001300#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1301 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001302 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001303 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001304 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001305#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001306
1307#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1308
1309#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1310 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001311 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001312 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1313 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001314 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001315 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001316 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1317 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1318
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001319#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1320 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001321 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001322 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001323 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001324 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001325
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001326#define FOR_EACH_INSTRUCTION(M) \
1327 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1328 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1329
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001330#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001331FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1332#undef FORWARD_DECLARATION
1333
Roland Levillainccc07a92014-09-16 14:48:16 +01001334#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001335 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001336 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001337 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001338 return other->Is##type(); \
1339 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001340 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001341
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001342#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1343 bool Is##type() const { return As##type() != nullptr; } \
1344 const H##type* As##type() const { return this; } \
1345 H##type* As##type() { return this; }
1346
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001347template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001348class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001349 public:
David Brazdiled596192015-01-23 10:39:45 +00001350 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001351 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001352 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001353
Vladimir Marko46817b82016-03-29 12:21:58 +01001354 // Hook for the IntrusiveForwardList<>.
1355 // TODO: Hide this better.
1356 IntrusiveForwardListHook hook;
1357
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001358 private:
David Brazdiled596192015-01-23 10:39:45 +00001359 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001360 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001361
1362 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001363 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001364
Vladimir Marko46817b82016-03-29 12:21:58 +01001365 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001366
1367 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1368};
1369
David Brazdiled596192015-01-23 10:39:45 +00001370template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001371using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001372
David Brazdil1abb4192015-02-17 18:33:36 +00001373// This class is used by HEnvironment and HInstruction classes to record the
1374// instructions they use and pointers to the corresponding HUseListNodes kept
1375// by the used instructions.
1376template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001377class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001378 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001379 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1380 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001381
Vladimir Marko46817b82016-03-29 12:21:58 +01001382 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1383 : HUserRecord(old_record.instruction_, before_use_node) {}
1384 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1385 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001386 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001387 }
1388
1389 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001390 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1391 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001392
1393 private:
1394 // Instruction used by the user.
1395 HInstruction* instruction_;
1396
Vladimir Marko46817b82016-03-29 12:21:58 +01001397 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1398 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001399};
1400
Aart Bik854a02b2015-07-14 16:07:00 -07001401/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001402 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001403 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001404 * For write/read dependences on fields/arrays, the dependence analysis uses
1405 * type disambiguation (e.g. a float field write cannot modify the value of an
1406 * integer field read) and the access type (e.g. a reference array write cannot
1407 * modify the value of a reference field read [although it may modify the
1408 * reference fetch prior to reading the field, which is represented by its own
1409 * write/read dependence]). The analysis makes conservative points-to
1410 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1411 * the same, and any reference read depends on any reference read without
1412 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001413 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001414 * The internal representation uses 38-bit and is described in the table below.
1415 * The first line indicates the side effect, and for field/array accesses the
1416 * second line indicates the type of the access (in the order of the
1417 * Primitive::Type enum).
1418 * The two numbered lines below indicate the bit position in the bitfield (read
1419 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001420 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001421 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1422 * +-------------+---------+---------+--------------+---------+---------+
1423 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1424 * | 3 |333333322|222222221| 1 |111111110|000000000|
1425 * | 7 |654321098|765432109| 8 |765432109|876543210|
1426 *
1427 * Note that, to ease the implementation, 'changes' bits are least significant
1428 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001429 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001430class SideEffects : public ValueObject {
1431 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001432 SideEffects() : flags_(0) {}
1433
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001434 static SideEffects None() {
1435 return SideEffects(0);
1436 }
1437
1438 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001439 return SideEffects(kAllChangeBits | kAllDependOnBits);
1440 }
1441
1442 static SideEffects AllChanges() {
1443 return SideEffects(kAllChangeBits);
1444 }
1445
1446 static SideEffects AllDependencies() {
1447 return SideEffects(kAllDependOnBits);
1448 }
1449
1450 static SideEffects AllExceptGCDependency() {
1451 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1452 }
1453
1454 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001455 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001456 }
1457
Aart Bik34c3ba92015-07-20 14:08:59 -07001458 static SideEffects AllWrites() {
1459 return SideEffects(kAllWrites);
1460 }
1461
1462 static SideEffects AllReads() {
1463 return SideEffects(kAllReads);
1464 }
1465
1466 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1467 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001468 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001469 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001470 }
1471
Aart Bik854a02b2015-07-14 16:07:00 -07001472 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001473 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001474 }
1475
Aart Bik34c3ba92015-07-20 14:08:59 -07001476 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1477 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001478 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001479 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001480 }
1481
1482 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001483 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001484 }
1485
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001486 static SideEffects CanTriggerGC() {
1487 return SideEffects(1ULL << kCanTriggerGCBit);
1488 }
1489
1490 static SideEffects DependsOnGC() {
1491 return SideEffects(1ULL << kDependsOnGCBit);
1492 }
1493
Aart Bik854a02b2015-07-14 16:07:00 -07001494 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001495 SideEffects Union(SideEffects other) const {
1496 return SideEffects(flags_ | other.flags_);
1497 }
1498
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001499 SideEffects Exclusion(SideEffects other) const {
1500 return SideEffects(flags_ & ~other.flags_);
1501 }
1502
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001503 void Add(SideEffects other) {
1504 flags_ |= other.flags_;
1505 }
1506
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001507 bool Includes(SideEffects other) const {
1508 return (other.flags_ & flags_) == other.flags_;
1509 }
1510
1511 bool HasSideEffects() const {
1512 return (flags_ & kAllChangeBits);
1513 }
1514
1515 bool HasDependencies() const {
1516 return (flags_ & kAllDependOnBits);
1517 }
1518
1519 // Returns true if there are no side effects or dependencies.
1520 bool DoesNothing() const {
1521 return flags_ == 0;
1522 }
1523
Aart Bik854a02b2015-07-14 16:07:00 -07001524 // Returns true if something is written.
1525 bool DoesAnyWrite() const {
1526 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001527 }
1528
Aart Bik854a02b2015-07-14 16:07:00 -07001529 // Returns true if something is read.
1530 bool DoesAnyRead() const {
1531 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001532 }
1533
Aart Bik854a02b2015-07-14 16:07:00 -07001534 // Returns true if potentially everything is written and read
1535 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001536 bool DoesAllReadWrite() const {
1537 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1538 }
1539
Aart Bik854a02b2015-07-14 16:07:00 -07001540 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001541 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001542 }
1543
Roland Levillain0d5a2812015-11-13 10:07:31 +00001544 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001545 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001546 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1547 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001548 }
1549
1550 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001551 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001552 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001553 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001554 for (int s = kLastBit; s >= 0; s--) {
1555 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1556 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1557 // This is a bit for the GC side effect.
1558 if (current_bit_is_set) {
1559 flags += "GC";
1560 }
Aart Bik854a02b2015-07-14 16:07:00 -07001561 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001562 } else {
1563 // This is a bit for the array/field analysis.
1564 // The underscore character stands for the 'can trigger GC' bit.
1565 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1566 if (current_bit_is_set) {
1567 flags += kDebug[s];
1568 }
1569 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1570 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1571 flags += "|";
1572 }
1573 }
Aart Bik854a02b2015-07-14 16:07:00 -07001574 }
1575 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001576 }
1577
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001578 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001579
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001580 private:
1581 static constexpr int kFieldArrayAnalysisBits = 9;
1582
1583 static constexpr int kFieldWriteOffset = 0;
1584 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1585 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1586 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1587
1588 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1589
1590 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1591 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1592 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1593 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1594
1595 static constexpr int kLastBit = kDependsOnGCBit;
1596 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1597
1598 // Aliases.
1599
1600 static_assert(kChangeBits == kDependOnBits,
1601 "the 'change' bits should match the 'depend on' bits.");
1602
1603 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1604 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1605 static constexpr uint64_t kAllWrites =
1606 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1607 static constexpr uint64_t kAllReads =
1608 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001609
Aart Bik854a02b2015-07-14 16:07:00 -07001610 // Translates type to bit flag.
1611 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1612 CHECK_NE(type, Primitive::kPrimVoid);
1613 const uint64_t one = 1;
1614 const int shift = type; // 0-based consecutive enum
1615 DCHECK_LE(kFieldWriteOffset, shift);
1616 DCHECK_LT(shift, kArrayWriteOffset);
1617 return one << (type + offset);
1618 }
1619
1620 // Private constructor on direct flags value.
1621 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1622
1623 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001624};
1625
David Brazdiled596192015-01-23 10:39:45 +00001626// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001627class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001628 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001629 HEnvironment(ArenaAllocator* arena,
1630 size_t number_of_vregs,
1631 const DexFile& dex_file,
1632 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001633 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001634 InvokeType invoke_type,
1635 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001636 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1637 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001638 parent_(nullptr),
1639 dex_file_(dex_file),
1640 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001641 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001642 invoke_type_(invoke_type),
1643 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001644 }
1645
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001646 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001647 : HEnvironment(arena,
1648 to_copy.Size(),
1649 to_copy.GetDexFile(),
1650 to_copy.GetMethodIdx(),
1651 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001652 to_copy.GetInvokeType(),
1653 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001654
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001655 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001656 if (parent_ != nullptr) {
1657 parent_->SetAndCopyParentChain(allocator, parent);
1658 } else {
1659 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1660 parent_->CopyFrom(parent);
1661 if (parent->GetParent() != nullptr) {
1662 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1663 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001664 }
David Brazdiled596192015-01-23 10:39:45 +00001665 }
1666
Vladimir Marko71bf8092015-09-15 15:33:14 +01001667 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001668 void CopyFrom(HEnvironment* environment);
1669
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001670 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1671 // input to the loop phi instead. This is for inserting instructions that
1672 // require an environment (like HDeoptimization) in the loop pre-header.
1673 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001674
1675 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001676 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001677 }
1678
1679 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001680 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001681 }
1682
David Brazdil1abb4192015-02-17 18:33:36 +00001683 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001684
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001685 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001686
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001687 HEnvironment* GetParent() const { return parent_; }
1688
1689 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001690 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001691 }
1692
1693 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001694 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001695 }
1696
1697 uint32_t GetDexPc() const {
1698 return dex_pc_;
1699 }
1700
1701 uint32_t GetMethodIdx() const {
1702 return method_idx_;
1703 }
1704
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001705 InvokeType GetInvokeType() const {
1706 return invoke_type_;
1707 }
1708
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001709 const DexFile& GetDexFile() const {
1710 return dex_file_;
1711 }
1712
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001713 HInstruction* GetHolder() const {
1714 return holder_;
1715 }
1716
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001717
1718 bool IsFromInlinedInvoke() const {
1719 return GetParent() != nullptr;
1720 }
1721
David Brazdiled596192015-01-23 10:39:45 +00001722 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001723 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1724 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001725 HEnvironment* parent_;
1726 const DexFile& dex_file_;
1727 const uint32_t method_idx_;
1728 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001729 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001730
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001731 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001732 HInstruction* const holder_;
1733
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001734 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001735
1736 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1737};
1738
Vladimir Markof9f64412015-09-02 14:05:49 +01001739class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001740 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001741 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001742 : previous_(nullptr),
1743 next_(nullptr),
1744 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001745 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001746 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001747 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001748 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001749 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001750 locations_(nullptr),
1751 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001752 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001753 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001754 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1755 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1756 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001757
Dave Allison20dfc792014-06-16 20:44:29 -07001758 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001759
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001760#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001761 enum InstructionKind {
1762 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1763 };
1764#undef DECLARE_KIND
1765
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001766 HInstruction* GetNext() const { return next_; }
1767 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001768
Calin Juravle77520bc2015-01-12 18:45:46 +00001769 HInstruction* GetNextDisregardingMoves() const;
1770 HInstruction* GetPreviousDisregardingMoves() const;
1771
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001772 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001773 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001774 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001775 bool IsInBlock() const { return block_ != nullptr; }
1776 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001777 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1778 bool IsIrreducibleLoopHeaderPhi() const {
1779 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1780 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001781
Roland Levillain6b879dd2014-09-22 17:13:44 +01001782 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001783 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001784
1785 virtual void Accept(HGraphVisitor* visitor) = 0;
1786 virtual const char* DebugName() const = 0;
1787
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001788 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001789 void SetRawInputAt(size_t index, HInstruction* input) {
1790 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1791 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001792
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001793 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001794
1795 uint32_t GetDexPc() const { return dex_pc_; }
1796
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001797 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001798
Roland Levillaine161a2a2014-10-03 12:45:18 +01001799 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001800 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001801
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001802 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001803 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001804
Calin Juravle10e244f2015-01-26 18:54:32 +00001805 // Does not apply for all instructions, but having this at top level greatly
1806 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001807 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001808 virtual bool CanBeNull() const {
1809 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1810 return true;
1811 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001812
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001813 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001814 return false;
1815 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001816
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001817 virtual bool IsActualObject() const {
1818 return GetType() == Primitive::kPrimNot;
1819 }
1820
Calin Juravle2e768302015-07-28 14:41:11 +00001821 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001822
Calin Juravle61d544b2015-02-23 16:46:57 +00001823 ReferenceTypeInfo GetReferenceTypeInfo() const {
1824 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001825 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001826 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001827 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001828
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001829 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001830 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001831 // Note: fixup_end remains valid across push_front().
1832 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1833 HUseListNode<HInstruction*>* new_node =
1834 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1835 uses_.push_front(*new_node);
1836 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001837 }
1838
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001839 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001840 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001841 // Note: env_fixup_end remains valid across push_front().
1842 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1843 HUseListNode<HEnvironment*>* new_node =
1844 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1845 env_uses_.push_front(*new_node);
1846 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001847 }
1848
David Brazdil1abb4192015-02-17 18:33:36 +00001849 void RemoveAsUserOfInput(size_t input) {
1850 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001851 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1852 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1853 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001854 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001855
David Brazdil1abb4192015-02-17 18:33:36 +00001856 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1857 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001858
Vladimir Marko46817b82016-03-29 12:21:58 +01001859 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1860 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1861 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001862 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001863 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001864 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001865
Roland Levillain6c82d402014-10-13 16:10:27 +01001866 // Does this instruction strictly dominate `other_instruction`?
1867 // Returns false if this instruction and `other_instruction` are the same.
1868 // Aborts if this instruction and `other_instruction` are both phis.
1869 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001870
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001871 int GetId() const { return id_; }
1872 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001873
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001874 int GetSsaIndex() const { return ssa_index_; }
1875 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1876 bool HasSsaIndex() const { return ssa_index_ != -1; }
1877
1878 bool HasEnvironment() const { return environment_ != nullptr; }
1879 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001880 // Set the `environment_` field. Raw because this method does not
1881 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001882 void SetRawEnvironment(HEnvironment* environment) {
1883 DCHECK(environment_ == nullptr);
1884 DCHECK_EQ(environment->GetHolder(), this);
1885 environment_ = environment;
1886 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001887
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001888 void RemoveEnvironment();
1889
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001890 // Set the environment of this instruction, copying it from `environment`. While
1891 // copying, the uses lists are being updated.
1892 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001893 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001894 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001895 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001896 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001897 if (environment->GetParent() != nullptr) {
1898 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1899 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001900 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001901
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001902 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1903 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001904 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001905 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001906 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001907 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001908 if (environment->GetParent() != nullptr) {
1909 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1910 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001911 }
1912
Nicolas Geoffray39468442014-09-02 15:17:15 +01001913 // Returns the number of entries in the environment. Typically, that is the
1914 // number of dex registers in a method. It could be more in case of inlining.
1915 size_t EnvironmentSize() const;
1916
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001917 LocationSummary* GetLocations() const { return locations_; }
1918 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001919
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001920 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001921 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001922
Alexandre Rames188d4312015-04-09 18:30:21 +01001923 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1924 // uses of this instruction by `other` are *not* updated.
1925 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1926 ReplaceWith(other);
1927 other->ReplaceInput(this, use_index);
1928 }
1929
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001930 // Move `this` instruction before `cursor`.
1931 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001932
Vladimir Markofb337ea2015-11-25 15:25:10 +00001933 // Move `this` before its first user and out of any loops. If there is no
1934 // out-of-loop user that dominates all other users, move the instruction
1935 // to the end of the out-of-loop common dominator of the user's blocks.
1936 //
1937 // This can be used only on non-throwing instructions with no side effects that
1938 // have at least one use but no environment uses.
1939 void MoveBeforeFirstUserAndOutOfLoops();
1940
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001941#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001942 bool Is##type() const; \
1943 const H##type* As##type() const; \
1944 H##type* As##type();
1945
1946 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1947#undef INSTRUCTION_TYPE_CHECK
1948
1949#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001950 bool Is##type() const { return (As##type() != nullptr); } \
1951 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001952 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001953 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001954#undef INSTRUCTION_TYPE_CHECK
1955
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001956 // Returns whether the instruction can be moved within the graph.
1957 virtual bool CanBeMoved() const { return false; }
1958
1959 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001960 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001961 return false;
1962 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001963
1964 // Returns whether any data encoded in the two instructions is equal.
1965 // This method does not look at the inputs. Both instructions must be
1966 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001967 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001968 return false;
1969 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001970
1971 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001972 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001973 // 2) Their inputs are identical.
1974 bool Equals(HInstruction* other) const;
1975
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001976 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
1977 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
1978 // the virtual function because the __attribute__((__pure__)) doesn't really
1979 // apply the strong requirement for virtual functions, preventing optimizations.
1980 InstructionKind GetKind() const PURE;
1981 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001982
1983 virtual size_t ComputeHashCode() const {
1984 size_t result = GetKind();
1985 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1986 result = (result * 31) + InputAt(i)->GetId();
1987 }
1988 return result;
1989 }
1990
1991 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001992 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001993 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001994
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001995 size_t GetLifetimePosition() const { return lifetime_position_; }
1996 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1997 LiveInterval* GetLiveInterval() const { return live_interval_; }
1998 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1999 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2000
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002001 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2002
2003 // Returns whether the code generation of the instruction will require to have access
2004 // to the current method. Such instructions are:
2005 // (1): Instructions that require an environment, as calling the runtime requires
2006 // to walk the stack and have the current method stored at a specific stack address.
2007 // (2): Object literals like classes and strings, that are loaded from the dex cache
2008 // fields of the current method.
2009 bool NeedsCurrentMethod() const {
2010 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2011 }
2012
Vladimir Markodc151b22015-10-15 18:02:30 +01002013 // Returns whether the code generation of the instruction will require to have access
2014 // to the dex cache of the current method's declaring class via the current method.
2015 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002016
Mark Mendellc4701932015-04-10 13:18:51 -04002017 // Does this instruction have any use in an environment before
2018 // control flow hits 'other'?
2019 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2020
2021 // Remove all references to environment uses of this instruction.
2022 // The caller must ensure that this is safe to do.
2023 void RemoveEnvironmentUsers();
2024
Vladimir Markoa1de9182016-02-25 11:37:38 +00002025 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2026 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002027
David Brazdil1abb4192015-02-17 18:33:36 +00002028 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002029 // If set, the machine code for this instruction is assumed to be generated by
2030 // its users. Used by liveness analysis to compute use positions accordingly.
2031 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2032 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2033 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2034 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2035
David Brazdil1abb4192015-02-17 18:33:36 +00002036 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2037 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2038
Vladimir Markoa1de9182016-02-25 11:37:38 +00002039 uint32_t GetPackedFields() const {
2040 return packed_fields_;
2041 }
2042
2043 template <size_t flag>
2044 bool GetPackedFlag() const {
2045 return (packed_fields_ & (1u << flag)) != 0u;
2046 }
2047
2048 template <size_t flag>
2049 void SetPackedFlag(bool value = true) {
2050 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2051 }
2052
2053 template <typename BitFieldType>
2054 typename BitFieldType::value_type GetPackedField() const {
2055 return BitFieldType::Decode(packed_fields_);
2056 }
2057
2058 template <typename BitFieldType>
2059 void SetPackedField(typename BitFieldType::value_type value) {
2060 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2061 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2062 }
2063
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002064 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002065 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2066 auto before_use_node = uses_.before_begin();
2067 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2068 HInstruction* user = use_node->GetUser();
2069 size_t input_index = use_node->GetIndex();
2070 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2071 before_use_node = use_node;
2072 }
2073 }
2074
2075 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2076 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2077 if (next != uses_.end()) {
2078 HInstruction* next_user = next->GetUser();
2079 size_t next_index = next->GetIndex();
2080 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2081 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2082 }
2083 }
2084
2085 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2086 auto before_env_use_node = env_uses_.before_begin();
2087 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2088 HEnvironment* user = env_use_node->GetUser();
2089 size_t input_index = env_use_node->GetIndex();
2090 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2091 before_env_use_node = env_use_node;
2092 }
2093 }
2094
2095 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2096 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2097 if (next != env_uses_.end()) {
2098 HEnvironment* next_user = next->GetUser();
2099 size_t next_index = next->GetIndex();
2100 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2101 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2102 }
2103 }
David Brazdil1abb4192015-02-17 18:33:36 +00002104
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002105 HInstruction* previous_;
2106 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002107 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002108 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002109
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002110 // An instruction gets an id when it is added to the graph.
2111 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002112 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002113 int id_;
2114
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002115 // When doing liveness analysis, instructions that have uses get an SSA index.
2116 int ssa_index_;
2117
Vladimir Markoa1de9182016-02-25 11:37:38 +00002118 // Packed fields.
2119 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002120
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002121 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002122 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002123
2124 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002125 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002126
Nicolas Geoffray39468442014-09-02 15:17:15 +01002127 // The environment associated with this instruction. Not null if the instruction
2128 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002129 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002130
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002131 // Set by the code generator.
2132 LocationSummary* locations_;
2133
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002134 // Set by the liveness analysis.
2135 LiveInterval* live_interval_;
2136
2137 // Set by the liveness analysis, this is the position in a linear
2138 // order of blocks where this instruction's live interval start.
2139 size_t lifetime_position_;
2140
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002141 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002142
Vladimir Markoa1de9182016-02-25 11:37:38 +00002143 // The reference handle part of the reference type info.
2144 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002145 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002146 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002147
David Brazdil1abb4192015-02-17 18:33:36 +00002148 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002149 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002150 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002151 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002152 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002153
2154 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2155};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002156std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002157
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002158class HInputIterator : public ValueObject {
2159 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002160 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002161
2162 bool Done() const { return index_ == instruction_->InputCount(); }
2163 HInstruction* Current() const { return instruction_->InputAt(index_); }
2164 void Advance() { index_++; }
2165
2166 private:
2167 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002168 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002169
2170 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2171};
2172
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002173class HInstructionIterator : public ValueObject {
2174 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002175 explicit HInstructionIterator(const HInstructionList& instructions)
2176 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002177 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002178 }
2179
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002180 bool Done() const { return instruction_ == nullptr; }
2181 HInstruction* Current() const { return instruction_; }
2182 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002183 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002184 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002185 }
2186
2187 private:
2188 HInstruction* instruction_;
2189 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002190
2191 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002192};
2193
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002194class HBackwardInstructionIterator : public ValueObject {
2195 public:
2196 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2197 : instruction_(instructions.last_instruction_) {
2198 next_ = Done() ? nullptr : instruction_->GetPrevious();
2199 }
2200
2201 bool Done() const { return instruction_ == nullptr; }
2202 HInstruction* Current() const { return instruction_; }
2203 void Advance() {
2204 instruction_ = next_;
2205 next_ = Done() ? nullptr : instruction_->GetPrevious();
2206 }
2207
2208 private:
2209 HInstruction* instruction_;
2210 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002211
2212 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002213};
2214
Vladimir Markof9f64412015-09-02 14:05:49 +01002215template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002216class HTemplateInstruction: public HInstruction {
2217 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002218 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002219 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002220 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002221
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002222 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002223
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002224 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002225 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2226 DCHECK_LT(i, N);
2227 return inputs_[i];
2228 }
David Brazdil1abb4192015-02-17 18:33:36 +00002229
2230 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002231 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002232 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002233 }
2234
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002235 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002236 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002237
2238 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002239};
2240
Vladimir Markof9f64412015-09-02 14:05:49 +01002241// HTemplateInstruction specialization for N=0.
2242template<>
2243class HTemplateInstruction<0>: public HInstruction {
2244 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002245 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002246 : HInstruction(side_effects, dex_pc) {}
2247
Vladimir Markof9f64412015-09-02 14:05:49 +01002248 virtual ~HTemplateInstruction() {}
2249
2250 size_t InputCount() const OVERRIDE { return 0; }
2251
2252 protected:
2253 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2254 LOG(FATAL) << "Unreachable";
2255 UNREACHABLE();
2256 }
2257
2258 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2259 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2260 LOG(FATAL) << "Unreachable";
2261 UNREACHABLE();
2262 }
2263
2264 private:
2265 friend class SsaBuilder;
2266};
2267
Dave Allison20dfc792014-06-16 20:44:29 -07002268template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002269class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002270 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002271 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002272 : HTemplateInstruction<N>(side_effects, dex_pc) {
2273 this->template SetPackedField<TypeField>(type);
2274 }
Dave Allison20dfc792014-06-16 20:44:29 -07002275 virtual ~HExpression() {}
2276
Vladimir Markoa1de9182016-02-25 11:37:38 +00002277 Primitive::Type GetType() const OVERRIDE {
2278 return TypeField::Decode(this->GetPackedFields());
2279 }
Dave Allison20dfc792014-06-16 20:44:29 -07002280
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002281 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002282 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2283 static constexpr size_t kFieldTypeSize =
2284 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2285 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2286 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2287 "Too many packed fields.");
2288 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002289};
2290
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002291// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2292// instruction that branches to the exit block.
2293class HReturnVoid : public HTemplateInstruction<0> {
2294 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002295 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2296 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002297
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002298 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002299
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002300 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002301
2302 private:
2303 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2304};
2305
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002306// Represents dex's RETURN opcodes. A HReturn is a control flow
2307// instruction that branches to the exit block.
2308class HReturn : public HTemplateInstruction<1> {
2309 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002310 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2311 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002312 SetRawInputAt(0, value);
2313 }
2314
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002315 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002316
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002317 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002318
2319 private:
2320 DISALLOW_COPY_AND_ASSIGN(HReturn);
2321};
2322
David Brazdildee58d62016-04-07 09:54:26 +00002323class HPhi : public HInstruction {
2324 public:
2325 HPhi(ArenaAllocator* arena,
2326 uint32_t reg_number,
2327 size_t number_of_inputs,
2328 Primitive::Type type,
2329 uint32_t dex_pc = kNoDexPc)
2330 : HInstruction(SideEffects::None(), dex_pc),
2331 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2332 reg_number_(reg_number) {
2333 SetPackedField<TypeField>(ToPhiType(type));
2334 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2335 // Phis are constructed live and marked dead if conflicting or unused.
2336 // Individual steps of SsaBuilder should assume that if a phi has been
2337 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2338 SetPackedFlag<kFlagIsLive>(true);
2339 SetPackedFlag<kFlagCanBeNull>(true);
2340 }
2341
2342 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2343 static Primitive::Type ToPhiType(Primitive::Type type) {
2344 return Primitive::PrimitiveKind(type);
2345 }
2346
2347 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2348
2349 size_t InputCount() const OVERRIDE { return inputs_.size(); }
2350
2351 void AddInput(HInstruction* input);
2352 void RemoveInputAt(size_t index);
2353
2354 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2355 void SetType(Primitive::Type new_type) {
2356 // Make sure that only valid type changes occur. The following are allowed:
2357 // (1) int -> float/ref (primitive type propagation),
2358 // (2) long -> double (primitive type propagation).
2359 DCHECK(GetType() == new_type ||
2360 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2361 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2362 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2363 SetPackedField<TypeField>(new_type);
2364 }
2365
2366 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2367 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2368
2369 uint32_t GetRegNumber() const { return reg_number_; }
2370
2371 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2372 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2373 bool IsDead() const { return !IsLive(); }
2374 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2375
2376 bool IsVRegEquivalentOf(HInstruction* other) const {
2377 return other != nullptr
2378 && other->IsPhi()
2379 && other->AsPhi()->GetBlock() == GetBlock()
2380 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2381 }
2382
2383 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2384 // An equivalent phi is a phi having the same dex register and type.
2385 // It assumes that phis with the same dex register are adjacent.
2386 HPhi* GetNextEquivalentPhiWithSameType() {
2387 HInstruction* next = GetNext();
2388 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2389 if (next->GetType() == GetType()) {
2390 return next->AsPhi();
2391 }
2392 next = next->GetNext();
2393 }
2394 return nullptr;
2395 }
2396
2397 DECLARE_INSTRUCTION(Phi);
2398
2399 protected:
2400 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
2401 return inputs_[index];
2402 }
2403
2404 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2405 inputs_[index] = input;
2406 }
2407
2408 private:
2409 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2410 static constexpr size_t kFieldTypeSize =
2411 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2412 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2413 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2414 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2415 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2416 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2417
2418 ArenaVector<HUserRecord<HInstruction*> > inputs_;
2419 const uint32_t reg_number_;
2420
2421 DISALLOW_COPY_AND_ASSIGN(HPhi);
2422};
2423
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002424// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002425// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002426// exit block.
2427class HExit : public HTemplateInstruction<0> {
2428 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002429 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002430
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002431 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002432
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002433 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002434
2435 private:
2436 DISALLOW_COPY_AND_ASSIGN(HExit);
2437};
2438
2439// Jumps from one block to another.
2440class HGoto : public HTemplateInstruction<0> {
2441 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002442 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002443
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002444 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002445
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002446 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002447 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002448 }
2449
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002450 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002451
2452 private:
2453 DISALLOW_COPY_AND_ASSIGN(HGoto);
2454};
2455
Roland Levillain9867bc72015-08-05 10:21:34 +01002456class HConstant : public HExpression<0> {
2457 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002458 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2459 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002460
2461 bool CanBeMoved() const OVERRIDE { return true; }
2462
Roland Levillain1a653882016-03-18 18:05:57 +00002463 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002464 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002465 // Is this constant 0 in the arithmetic sense?
2466 virtual bool IsArithmeticZero() const { return false; }
2467 // Is this constant a 0-bit pattern?
2468 virtual bool IsZeroBitPattern() const { return false; }
2469 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002470 virtual bool IsOne() const { return false; }
2471
David Brazdil77a48ae2015-09-15 12:34:04 +00002472 virtual uint64_t GetValueAsUint64() const = 0;
2473
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002474 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002475
2476 private:
2477 DISALLOW_COPY_AND_ASSIGN(HConstant);
2478};
2479
2480class HNullConstant : public HConstant {
2481 public:
2482 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2483 return true;
2484 }
2485
David Brazdil77a48ae2015-09-15 12:34:04 +00002486 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2487
Roland Levillain9867bc72015-08-05 10:21:34 +01002488 size_t ComputeHashCode() const OVERRIDE { return 0; }
2489
Roland Levillain1a653882016-03-18 18:05:57 +00002490 // The null constant representation is a 0-bit pattern.
2491 virtual bool IsZeroBitPattern() const { return true; }
2492
Roland Levillain9867bc72015-08-05 10:21:34 +01002493 DECLARE_INSTRUCTION(NullConstant);
2494
2495 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002496 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002497
2498 friend class HGraph;
2499 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2500};
2501
2502// Constants of the type int. Those can be from Dex instructions, or
2503// synthesized (for example with the if-eqz instruction).
2504class HIntConstant : public HConstant {
2505 public:
2506 int32_t GetValue() const { return value_; }
2507
David Brazdil9f389d42015-10-01 14:32:56 +01002508 uint64_t GetValueAsUint64() const OVERRIDE {
2509 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2510 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002511
Roland Levillain9867bc72015-08-05 10:21:34 +01002512 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002513 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002514 return other->AsIntConstant()->value_ == value_;
2515 }
2516
2517 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2518
2519 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002520 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2521 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002522 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2523
Roland Levillain1a653882016-03-18 18:05:57 +00002524 // Integer constants are used to encode Boolean values as well,
2525 // where 1 means true and 0 means false.
2526 bool IsTrue() const { return GetValue() == 1; }
2527 bool IsFalse() const { return GetValue() == 0; }
2528
Roland Levillain9867bc72015-08-05 10:21:34 +01002529 DECLARE_INSTRUCTION(IntConstant);
2530
2531 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002532 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2533 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2534 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2535 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002536
2537 const int32_t value_;
2538
2539 friend class HGraph;
2540 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2541 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2542 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2543};
2544
2545class HLongConstant : public HConstant {
2546 public:
2547 int64_t GetValue() const { return value_; }
2548
David Brazdil77a48ae2015-09-15 12:34:04 +00002549 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2550
Roland Levillain9867bc72015-08-05 10:21:34 +01002551 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002552 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002553 return other->AsLongConstant()->value_ == value_;
2554 }
2555
2556 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2557
2558 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002559 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2560 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002561 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2562
2563 DECLARE_INSTRUCTION(LongConstant);
2564
2565 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002566 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2567 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002568
2569 const int64_t value_;
2570
2571 friend class HGraph;
2572 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2573};
Dave Allison20dfc792014-06-16 20:44:29 -07002574
Roland Levillain31dd3d62016-02-16 12:21:02 +00002575class HFloatConstant : public HConstant {
2576 public:
2577 float GetValue() const { return value_; }
2578
2579 uint64_t GetValueAsUint64() const OVERRIDE {
2580 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2581 }
2582
2583 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2584 DCHECK(other->IsFloatConstant()) << other->DebugName();
2585 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2586 }
2587
2588 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2589
2590 bool IsMinusOne() const OVERRIDE {
2591 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2592 }
Roland Levillain1a653882016-03-18 18:05:57 +00002593 bool IsArithmeticZero() const OVERRIDE {
2594 return std::fpclassify(value_) == FP_ZERO;
2595 }
2596 bool IsArithmeticPositiveZero() const {
2597 return IsArithmeticZero() && !std::signbit(value_);
2598 }
2599 bool IsArithmeticNegativeZero() const {
2600 return IsArithmeticZero() && std::signbit(value_);
2601 }
2602 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002603 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002604 }
2605 bool IsOne() const OVERRIDE {
2606 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2607 }
2608 bool IsNaN() const {
2609 return std::isnan(value_);
2610 }
2611
2612 DECLARE_INSTRUCTION(FloatConstant);
2613
2614 private:
2615 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2616 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2617 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2618 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2619
2620 const float value_;
2621
2622 // Only the SsaBuilder and HGraph can create floating-point constants.
2623 friend class SsaBuilder;
2624 friend class HGraph;
2625 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2626};
2627
2628class HDoubleConstant : public HConstant {
2629 public:
2630 double GetValue() const { return value_; }
2631
2632 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2633
2634 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2635 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2636 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2637 }
2638
2639 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2640
2641 bool IsMinusOne() const OVERRIDE {
2642 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2643 }
Roland Levillain1a653882016-03-18 18:05:57 +00002644 bool IsArithmeticZero() const OVERRIDE {
2645 return std::fpclassify(value_) == FP_ZERO;
2646 }
2647 bool IsArithmeticPositiveZero() const {
2648 return IsArithmeticZero() && !std::signbit(value_);
2649 }
2650 bool IsArithmeticNegativeZero() const {
2651 return IsArithmeticZero() && std::signbit(value_);
2652 }
2653 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002654 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002655 }
2656 bool IsOne() const OVERRIDE {
2657 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2658 }
2659 bool IsNaN() const {
2660 return std::isnan(value_);
2661 }
2662
2663 DECLARE_INSTRUCTION(DoubleConstant);
2664
2665 private:
2666 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2667 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2668 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2669 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2670
2671 const double value_;
2672
2673 // Only the SsaBuilder and HGraph can create floating-point constants.
2674 friend class SsaBuilder;
2675 friend class HGraph;
2676 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2677};
2678
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002679// Conditional branch. A block ending with an HIf instruction must have
2680// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002681class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002682 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002683 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2684 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002685 SetRawInputAt(0, input);
2686 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002687
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002688 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002689
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002690 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002691 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002692 }
2693
2694 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002695 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002696 }
2697
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002698 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002699
2700 private:
2701 DISALLOW_COPY_AND_ASSIGN(HIf);
2702};
2703
David Brazdilfc6a86a2015-06-26 10:33:45 +00002704
2705// Abstract instruction which marks the beginning and/or end of a try block and
2706// links it to the respective exception handlers. Behaves the same as a Goto in
2707// non-exceptional control flow.
2708// Normal-flow successor is stored at index zero, exception handlers under
2709// higher indices in no particular order.
2710class HTryBoundary : public HTemplateInstruction<0> {
2711 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002712 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002713 kEntry,
2714 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002715 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002716 };
2717
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002718 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002719 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2720 SetPackedField<BoundaryKindField>(kind);
2721 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002722
2723 bool IsControlFlow() const OVERRIDE { return true; }
2724
2725 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002726 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002727
David Brazdild26a4112015-11-10 11:07:31 +00002728 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2729 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2730 }
2731
David Brazdilfc6a86a2015-06-26 10:33:45 +00002732 // Returns whether `handler` is among its exception handlers (non-zero index
2733 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002734 bool HasExceptionHandler(const HBasicBlock& handler) const {
2735 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002736 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002737 }
2738
2739 // If not present already, adds `handler` to its block's list of exception
2740 // handlers.
2741 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002742 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002743 GetBlock()->AddSuccessor(handler);
2744 }
2745 }
2746
Vladimir Markoa1de9182016-02-25 11:37:38 +00002747 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2748 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002749
David Brazdilffee3d32015-07-06 11:48:53 +01002750 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2751
David Brazdilfc6a86a2015-06-26 10:33:45 +00002752 DECLARE_INSTRUCTION(TryBoundary);
2753
2754 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002755 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2756 static constexpr size_t kFieldBoundaryKindSize =
2757 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2758 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2759 kFieldBoundaryKind + kFieldBoundaryKindSize;
2760 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2761 "Too many packed fields.");
2762 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002763
2764 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2765};
2766
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002767// Deoptimize to interpreter, upon checking a condition.
2768class HDeoptimize : public HTemplateInstruction<1> {
2769 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002770 // We set CanTriggerGC to prevent any intermediate address to be live
2771 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002772 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002773 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002774 SetRawInputAt(0, cond);
2775 }
2776
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002777 bool CanBeMoved() const OVERRIDE { return true; }
2778 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2779 return true;
2780 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002781 bool NeedsEnvironment() const OVERRIDE { return true; }
2782 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002783
2784 DECLARE_INSTRUCTION(Deoptimize);
2785
2786 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002787 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2788};
2789
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002790// Represents the ArtMethod that was passed as a first argument to
2791// the method. It is used by instructions that depend on it, like
2792// instructions that work with the dex cache.
2793class HCurrentMethod : public HExpression<0> {
2794 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002795 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2796 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002797
2798 DECLARE_INSTRUCTION(CurrentMethod);
2799
2800 private:
2801 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2802};
2803
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002804// Fetches an ArtMethod from the virtual table or the interface method table
2805// of a class.
2806class HClassTableGet : public HExpression<1> {
2807 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002808 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002809 kVTable,
2810 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002811 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002812 };
2813 HClassTableGet(HInstruction* cls,
2814 Primitive::Type type,
2815 TableKind kind,
2816 size_t index,
2817 uint32_t dex_pc)
2818 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002819 index_(index) {
2820 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002821 SetRawInputAt(0, cls);
2822 }
2823
2824 bool CanBeMoved() const OVERRIDE { return true; }
2825 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2826 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002827 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002828 }
2829
Vladimir Markoa1de9182016-02-25 11:37:38 +00002830 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002831 size_t GetIndex() const { return index_; }
2832
2833 DECLARE_INSTRUCTION(ClassTableGet);
2834
2835 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002836 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2837 static constexpr size_t kFieldTableKindSize =
2838 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2839 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2840 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2841 "Too many packed fields.");
2842 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2843
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002844 // The index of the ArtMethod in the table.
2845 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002846
2847 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2848};
2849
Mark Mendellfe57faa2015-09-18 09:26:15 -04002850// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2851// have one successor for each entry in the switch table, and the final successor
2852// will be the block containing the next Dex opcode.
2853class HPackedSwitch : public HTemplateInstruction<1> {
2854 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002855 HPackedSwitch(int32_t start_value,
2856 uint32_t num_entries,
2857 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002858 uint32_t dex_pc = kNoDexPc)
2859 : HTemplateInstruction(SideEffects::None(), dex_pc),
2860 start_value_(start_value),
2861 num_entries_(num_entries) {
2862 SetRawInputAt(0, input);
2863 }
2864
2865 bool IsControlFlow() const OVERRIDE { return true; }
2866
2867 int32_t GetStartValue() const { return start_value_; }
2868
Vladimir Marko211c2112015-09-24 16:52:33 +01002869 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002870
2871 HBasicBlock* GetDefaultBlock() const {
2872 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002873 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002874 }
2875 DECLARE_INSTRUCTION(PackedSwitch);
2876
2877 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002878 const int32_t start_value_;
2879 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002880
2881 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2882};
2883
Roland Levillain88cb1752014-10-20 16:36:47 +01002884class HUnaryOperation : public HExpression<1> {
2885 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002886 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2887 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002888 SetRawInputAt(0, input);
2889 }
2890
2891 HInstruction* GetInput() const { return InputAt(0); }
2892 Primitive::Type GetResultType() const { return GetType(); }
2893
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002894 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002895 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002896 return true;
2897 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002898
Roland Levillain31dd3d62016-02-16 12:21:02 +00002899 // Try to statically evaluate `this` and return a HConstant
2900 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002901 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002902 HConstant* TryStaticEvaluation() const;
2903
2904 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002905 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2906 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002907 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2908 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002909
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002910 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002911
2912 private:
2913 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2914};
2915
Dave Allison20dfc792014-06-16 20:44:29 -07002916class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002917 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002918 HBinaryOperation(Primitive::Type result_type,
2919 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002920 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002921 SideEffects side_effects = SideEffects::None(),
2922 uint32_t dex_pc = kNoDexPc)
2923 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002924 SetRawInputAt(0, left);
2925 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002926 }
2927
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002928 HInstruction* GetLeft() const { return InputAt(0); }
2929 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002930 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002931
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002932 virtual bool IsCommutative() const { return false; }
2933
2934 // Put constant on the right.
2935 // Returns whether order is changed.
2936 bool OrderInputsWithConstantOnTheRight() {
2937 HInstruction* left = InputAt(0);
2938 HInstruction* right = InputAt(1);
2939 if (left->IsConstant() && !right->IsConstant()) {
2940 ReplaceInput(right, 0);
2941 ReplaceInput(left, 1);
2942 return true;
2943 }
2944 return false;
2945 }
2946
2947 // Order inputs by instruction id, but favor constant on the right side.
2948 // This helps GVN for commutative ops.
2949 void OrderInputs() {
2950 DCHECK(IsCommutative());
2951 HInstruction* left = InputAt(0);
2952 HInstruction* right = InputAt(1);
2953 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2954 return;
2955 }
2956 if (OrderInputsWithConstantOnTheRight()) {
2957 return;
2958 }
2959 // Order according to instruction id.
2960 if (left->GetId() > right->GetId()) {
2961 ReplaceInput(right, 0);
2962 ReplaceInput(left, 1);
2963 }
2964 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002965
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002966 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002967 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002968 return true;
2969 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002970
Roland Levillain31dd3d62016-02-16 12:21:02 +00002971 // Try to statically evaluate `this` and return a HConstant
2972 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002973 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002974 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002975
2976 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002977 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2978 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002979 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2980 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002981 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002982 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2983 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002984 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2985 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002986 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2987 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01002988 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002989 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2990 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002991
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002992 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002993 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002994 HConstant* GetConstantRight() const;
2995
2996 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002997 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002998 HInstruction* GetLeastConstantLeft() const;
2999
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003000 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003001
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003002 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003003 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3004};
3005
Mark Mendellc4701932015-04-10 13:18:51 -04003006// The comparison bias applies for floating point operations and indicates how NaN
3007// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003008enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003009 kNoBias, // bias is not applicable (i.e. for long operation)
3010 kGtBias, // return 1 for NaN comparisons
3011 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003012 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003013};
3014
Roland Levillain31dd3d62016-02-16 12:21:02 +00003015std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3016
Dave Allison20dfc792014-06-16 20:44:29 -07003017class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003018 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003019 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003020 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3021 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3022 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003023
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003024 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003025 // `instruction`, and disregard moves in between.
3026 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003027
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003028 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003029
3030 virtual IfCondition GetCondition() const = 0;
3031
Mark Mendellc4701932015-04-10 13:18:51 -04003032 virtual IfCondition GetOppositeCondition() const = 0;
3033
Vladimir Markoa1de9182016-02-25 11:37:38 +00003034 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003035 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3036
Vladimir Markoa1de9182016-02-25 11:37:38 +00003037 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3038 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003039
3040 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003041 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003042 }
3043
Roland Levillain4fa13f62015-07-06 18:11:54 +01003044 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003045 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003046 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003047 if (if_cond == kCondNE) {
3048 return true;
3049 } else if (if_cond == kCondEQ) {
3050 return false;
3051 }
3052 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003053 }
3054
3055 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003056 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003057 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003058 if (if_cond == kCondEQ) {
3059 return true;
3060 } else if (if_cond == kCondNE) {
3061 return false;
3062 }
3063 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003064 }
3065
Roland Levillain31dd3d62016-02-16 12:21:02 +00003066 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003067 // Needed if we merge a HCompare into a HCondition.
3068 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3069 static constexpr size_t kFieldComparisonBiasSize =
3070 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3071 static constexpr size_t kNumberOfConditionPackedBits =
3072 kFieldComparisonBias + kFieldComparisonBiasSize;
3073 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3074 using ComparisonBiasField =
3075 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3076
Roland Levillain31dd3d62016-02-16 12:21:02 +00003077 template <typename T>
3078 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3079
3080 template <typename T>
3081 int32_t CompareFP(T x, T y) const {
3082 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3083 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3084 // Handle the bias.
3085 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3086 }
3087
3088 // Return an integer constant containing the result of a condition evaluated at compile time.
3089 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3090 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3091 }
3092
Dave Allison20dfc792014-06-16 20:44:29 -07003093 private:
3094 DISALLOW_COPY_AND_ASSIGN(HCondition);
3095};
3096
3097// Instruction to check if two inputs are equal to each other.
3098class HEqual : public HCondition {
3099 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003100 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3101 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003102
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003103 bool IsCommutative() const OVERRIDE { return true; }
3104
Vladimir Marko9e23df52015-11-10 17:14:35 +00003105 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3106 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003107 return MakeConstantCondition(true, GetDexPc());
3108 }
3109 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3110 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3111 }
3112 // In the following Evaluate methods, a HCompare instruction has
3113 // been merged into this HEqual instruction; evaluate it as
3114 // `Compare(x, y) == 0`.
3115 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3116 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3117 GetDexPc());
3118 }
3119 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3120 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3121 }
3122 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3123 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003124 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003125
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003126 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003127
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003128 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003129 return kCondEQ;
3130 }
3131
Mark Mendellc4701932015-04-10 13:18:51 -04003132 IfCondition GetOppositeCondition() const OVERRIDE {
3133 return kCondNE;
3134 }
3135
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003136 private:
Aart Bike9f37602015-10-09 11:15:55 -07003137 template <typename T> bool Compute(T x, T y) const { return x == y; }
3138
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003139 DISALLOW_COPY_AND_ASSIGN(HEqual);
3140};
3141
Dave Allison20dfc792014-06-16 20:44:29 -07003142class HNotEqual : public HCondition {
3143 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003144 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3145 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003146
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003147 bool IsCommutative() const OVERRIDE { return true; }
3148
Vladimir Marko9e23df52015-11-10 17:14:35 +00003149 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3150 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003151 return MakeConstantCondition(false, GetDexPc());
3152 }
3153 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3154 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3155 }
3156 // In the following Evaluate methods, a HCompare instruction has
3157 // been merged into this HNotEqual instruction; evaluate it as
3158 // `Compare(x, y) != 0`.
3159 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3160 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3161 }
3162 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3163 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3164 }
3165 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3166 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003167 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003168
Dave Allison20dfc792014-06-16 20:44:29 -07003169 DECLARE_INSTRUCTION(NotEqual);
3170
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003171 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003172 return kCondNE;
3173 }
3174
Mark Mendellc4701932015-04-10 13:18:51 -04003175 IfCondition GetOppositeCondition() const OVERRIDE {
3176 return kCondEQ;
3177 }
3178
Dave Allison20dfc792014-06-16 20:44:29 -07003179 private:
Aart Bike9f37602015-10-09 11:15:55 -07003180 template <typename T> bool Compute(T x, T y) const { return x != y; }
3181
Dave Allison20dfc792014-06-16 20:44:29 -07003182 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3183};
3184
3185class HLessThan : public HCondition {
3186 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003187 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3188 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003189
Roland Levillain9867bc72015-08-05 10:21:34 +01003190 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003191 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003192 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003193 // In the following Evaluate methods, a HCompare instruction has
3194 // been merged into this HLessThan instruction; evaluate it as
3195 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003196 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003197 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3198 }
3199 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3200 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3201 }
3202 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3203 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003204 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003205
Dave Allison20dfc792014-06-16 20:44:29 -07003206 DECLARE_INSTRUCTION(LessThan);
3207
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003208 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003209 return kCondLT;
3210 }
3211
Mark Mendellc4701932015-04-10 13:18:51 -04003212 IfCondition GetOppositeCondition() const OVERRIDE {
3213 return kCondGE;
3214 }
3215
Dave Allison20dfc792014-06-16 20:44:29 -07003216 private:
Aart Bike9f37602015-10-09 11:15:55 -07003217 template <typename T> bool Compute(T x, T y) const { return x < y; }
3218
Dave Allison20dfc792014-06-16 20:44:29 -07003219 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3220};
3221
3222class HLessThanOrEqual : public HCondition {
3223 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003224 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3225 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003226
Roland Levillain9867bc72015-08-05 10:21:34 +01003227 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003228 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003229 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003230 // In the following Evaluate methods, a HCompare instruction has
3231 // been merged into this HLessThanOrEqual instruction; evaluate it as
3232 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003233 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003234 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3235 }
3236 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3237 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3238 }
3239 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3240 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003241 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003242
Dave Allison20dfc792014-06-16 20:44:29 -07003243 DECLARE_INSTRUCTION(LessThanOrEqual);
3244
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003245 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003246 return kCondLE;
3247 }
3248
Mark Mendellc4701932015-04-10 13:18:51 -04003249 IfCondition GetOppositeCondition() const OVERRIDE {
3250 return kCondGT;
3251 }
3252
Dave Allison20dfc792014-06-16 20:44:29 -07003253 private:
Aart Bike9f37602015-10-09 11:15:55 -07003254 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3255
Dave Allison20dfc792014-06-16 20:44:29 -07003256 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3257};
3258
3259class HGreaterThan : public HCondition {
3260 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003261 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3262 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003263
Roland Levillain9867bc72015-08-05 10:21:34 +01003264 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003265 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003266 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003267 // In the following Evaluate methods, a HCompare instruction has
3268 // been merged into this HGreaterThan instruction; evaluate it as
3269 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003270 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003271 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3272 }
3273 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3274 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3275 }
3276 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3277 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003278 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003279
Dave Allison20dfc792014-06-16 20:44:29 -07003280 DECLARE_INSTRUCTION(GreaterThan);
3281
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003282 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003283 return kCondGT;
3284 }
3285
Mark Mendellc4701932015-04-10 13:18:51 -04003286 IfCondition GetOppositeCondition() const OVERRIDE {
3287 return kCondLE;
3288 }
3289
Dave Allison20dfc792014-06-16 20:44:29 -07003290 private:
Aart Bike9f37602015-10-09 11:15:55 -07003291 template <typename T> bool Compute(T x, T y) const { return x > y; }
3292
Dave Allison20dfc792014-06-16 20:44:29 -07003293 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3294};
3295
3296class HGreaterThanOrEqual : public HCondition {
3297 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003298 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3299 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003300
Roland Levillain9867bc72015-08-05 10:21:34 +01003301 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003302 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003303 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003304 // In the following Evaluate methods, a HCompare instruction has
3305 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3306 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003307 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003308 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3309 }
3310 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3311 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3312 }
3313 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3314 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003315 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003316
Dave Allison20dfc792014-06-16 20:44:29 -07003317 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3318
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003319 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003320 return kCondGE;
3321 }
3322
Mark Mendellc4701932015-04-10 13:18:51 -04003323 IfCondition GetOppositeCondition() const OVERRIDE {
3324 return kCondLT;
3325 }
3326
Dave Allison20dfc792014-06-16 20:44:29 -07003327 private:
Aart Bike9f37602015-10-09 11:15:55 -07003328 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3329
Dave Allison20dfc792014-06-16 20:44:29 -07003330 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3331};
3332
Aart Bike9f37602015-10-09 11:15:55 -07003333class HBelow : public HCondition {
3334 public:
3335 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3336 : HCondition(first, second, dex_pc) {}
3337
3338 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003339 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003340 }
3341 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003342 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3343 }
3344 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3345 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3346 LOG(FATAL) << DebugName() << " is not defined for float values";
3347 UNREACHABLE();
3348 }
3349 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3350 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3351 LOG(FATAL) << DebugName() << " is not defined for double values";
3352 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003353 }
3354
3355 DECLARE_INSTRUCTION(Below);
3356
3357 IfCondition GetCondition() const OVERRIDE {
3358 return kCondB;
3359 }
3360
3361 IfCondition GetOppositeCondition() const OVERRIDE {
3362 return kCondAE;
3363 }
3364
3365 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003366 template <typename T> bool Compute(T x, T y) const {
3367 return MakeUnsigned(x) < MakeUnsigned(y);
3368 }
Aart Bike9f37602015-10-09 11:15:55 -07003369
3370 DISALLOW_COPY_AND_ASSIGN(HBelow);
3371};
3372
3373class HBelowOrEqual : public HCondition {
3374 public:
3375 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3376 : HCondition(first, second, dex_pc) {}
3377
3378 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003379 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003380 }
3381 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003382 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3383 }
3384 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3385 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3386 LOG(FATAL) << DebugName() << " is not defined for float values";
3387 UNREACHABLE();
3388 }
3389 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3390 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3391 LOG(FATAL) << DebugName() << " is not defined for double values";
3392 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003393 }
3394
3395 DECLARE_INSTRUCTION(BelowOrEqual);
3396
3397 IfCondition GetCondition() const OVERRIDE {
3398 return kCondBE;
3399 }
3400
3401 IfCondition GetOppositeCondition() const OVERRIDE {
3402 return kCondA;
3403 }
3404
3405 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003406 template <typename T> bool Compute(T x, T y) const {
3407 return MakeUnsigned(x) <= MakeUnsigned(y);
3408 }
Aart Bike9f37602015-10-09 11:15:55 -07003409
3410 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3411};
3412
3413class HAbove : public HCondition {
3414 public:
3415 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3416 : HCondition(first, second, dex_pc) {}
3417
3418 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003419 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003420 }
3421 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003422 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3423 }
3424 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3425 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3426 LOG(FATAL) << DebugName() << " is not defined for float values";
3427 UNREACHABLE();
3428 }
3429 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3430 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3431 LOG(FATAL) << DebugName() << " is not defined for double values";
3432 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003433 }
3434
3435 DECLARE_INSTRUCTION(Above);
3436
3437 IfCondition GetCondition() const OVERRIDE {
3438 return kCondA;
3439 }
3440
3441 IfCondition GetOppositeCondition() const OVERRIDE {
3442 return kCondBE;
3443 }
3444
3445 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003446 template <typename T> bool Compute(T x, T y) const {
3447 return MakeUnsigned(x) > MakeUnsigned(y);
3448 }
Aart Bike9f37602015-10-09 11:15:55 -07003449
3450 DISALLOW_COPY_AND_ASSIGN(HAbove);
3451};
3452
3453class HAboveOrEqual : public HCondition {
3454 public:
3455 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3456 : HCondition(first, second, dex_pc) {}
3457
3458 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003459 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003460 }
3461 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003462 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3463 }
3464 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3465 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3466 LOG(FATAL) << DebugName() << " is not defined for float values";
3467 UNREACHABLE();
3468 }
3469 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3470 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3471 LOG(FATAL) << DebugName() << " is not defined for double values";
3472 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003473 }
3474
3475 DECLARE_INSTRUCTION(AboveOrEqual);
3476
3477 IfCondition GetCondition() const OVERRIDE {
3478 return kCondAE;
3479 }
3480
3481 IfCondition GetOppositeCondition() const OVERRIDE {
3482 return kCondB;
3483 }
3484
3485 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003486 template <typename T> bool Compute(T x, T y) const {
3487 return MakeUnsigned(x) >= MakeUnsigned(y);
3488 }
Aart Bike9f37602015-10-09 11:15:55 -07003489
3490 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3491};
Dave Allison20dfc792014-06-16 20:44:29 -07003492
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003493// Instruction to check how two inputs compare to each other.
3494// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3495class HCompare : public HBinaryOperation {
3496 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003497 // Note that `comparison_type` is the type of comparison performed
3498 // between the comparison's inputs, not the type of the instantiated
3499 // HCompare instruction (which is always Primitive::kPrimInt).
3500 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003501 HInstruction* first,
3502 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003503 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003504 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003505 : HBinaryOperation(Primitive::kPrimInt,
3506 first,
3507 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003508 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003509 dex_pc) {
3510 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003511 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3512 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003513 }
3514
Roland Levillain9867bc72015-08-05 10:21:34 +01003515 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003516 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3517
3518 template <typename T>
3519 int32_t ComputeFP(T x, T y) const {
3520 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3521 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3522 // Handle the bias.
3523 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3524 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003525
Roland Levillain9867bc72015-08-05 10:21:34 +01003526 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003527 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3528 // reach this code path when processing a freshly built HIR
3529 // graph. However HCompare integer instructions can be synthesized
3530 // by the instruction simplifier to implement IntegerCompare and
3531 // IntegerSignum intrinsics, so we have to handle this case.
3532 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003533 }
3534 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003535 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3536 }
3537 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3538 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3539 }
3540 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3541 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003542 }
3543
Calin Juravleddb7df22014-11-25 20:56:51 +00003544 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003545 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003546 }
3547
Vladimir Markoa1de9182016-02-25 11:37:38 +00003548 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003549
Roland Levillain31dd3d62016-02-16 12:21:02 +00003550 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003551 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003552 bool IsGtBias() const {
3553 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003554 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003555 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003556
Roland Levillain1693a1f2016-03-15 14:57:31 +00003557 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3558 // Comparisons do not require a runtime call in any back end.
3559 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003560 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003561
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003562 DECLARE_INSTRUCTION(Compare);
3563
Roland Levillain31dd3d62016-02-16 12:21:02 +00003564 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003565 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3566 static constexpr size_t kFieldComparisonBiasSize =
3567 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3568 static constexpr size_t kNumberOfComparePackedBits =
3569 kFieldComparisonBias + kFieldComparisonBiasSize;
3570 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3571 using ComparisonBiasField =
3572 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3573
Roland Levillain31dd3d62016-02-16 12:21:02 +00003574 // Return an integer constant containing the result of a comparison evaluated at compile time.
3575 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3576 DCHECK(value == -1 || value == 0 || value == 1) << value;
3577 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3578 }
3579
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003580 private:
3581 DISALLOW_COPY_AND_ASSIGN(HCompare);
3582};
3583
David Brazdil6de19382016-01-08 17:37:10 +00003584class HNewInstance : public HExpression<2> {
3585 public:
3586 HNewInstance(HInstruction* cls,
3587 HCurrentMethod* current_method,
3588 uint32_t dex_pc,
3589 uint16_t type_index,
3590 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003591 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003592 bool finalizable,
3593 QuickEntrypointEnum entrypoint)
3594 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3595 type_index_(type_index),
3596 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003597 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003598 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003599 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003600 SetRawInputAt(0, cls);
3601 SetRawInputAt(1, current_method);
3602 }
3603
3604 uint16_t GetTypeIndex() const { return type_index_; }
3605 const DexFile& GetDexFile() const { return dex_file_; }
3606
3607 // Calls runtime so needs an environment.
3608 bool NeedsEnvironment() const OVERRIDE { return true; }
3609
Mingyao Yang062157f2016-03-02 10:15:36 -08003610 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3611 bool CanThrow() const OVERRIDE { return true; }
3612
3613 // Needs to call into runtime to make sure it's instantiable/accessible.
3614 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003615
Vladimir Markoa1de9182016-02-25 11:37:38 +00003616 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003617
3618 bool CanBeNull() const OVERRIDE { return false; }
3619
3620 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3621
3622 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3623 entrypoint_ = entrypoint;
3624 }
3625
3626 bool IsStringAlloc() const;
3627
3628 DECLARE_INSTRUCTION(NewInstance);
3629
3630 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003631 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3632 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003633 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3634 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3635 "Too many packed fields.");
3636
David Brazdil6de19382016-01-08 17:37:10 +00003637 const uint16_t type_index_;
3638 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003639 QuickEntrypointEnum entrypoint_;
3640
3641 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3642};
3643
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003644enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003645#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3646 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003647#include "intrinsics_list.h"
3648 kNone,
3649 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3650#undef INTRINSICS_LIST
3651#undef OPTIMIZING_INTRINSICS
3652};
3653std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3654
Agi Csaki05f20562015-08-19 14:58:14 -07003655enum IntrinsicNeedsEnvironmentOrCache {
3656 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3657 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003658};
3659
Aart Bik5d75afe2015-12-14 11:57:01 -08003660enum IntrinsicSideEffects {
3661 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3662 kReadSideEffects, // Intrinsic may read heap memory.
3663 kWriteSideEffects, // Intrinsic may write heap memory.
3664 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3665};
3666
3667enum IntrinsicExceptions {
3668 kNoThrow, // Intrinsic does not throw any exceptions.
3669 kCanThrow // Intrinsic may throw exceptions.
3670};
3671
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003672class HInvoke : public HInstruction {
3673 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003674 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003675
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003676 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003677
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003678 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003679 SetRawInputAt(index, argument);
3680 }
3681
Roland Levillain3e3d7332015-04-28 11:00:54 +01003682 // Return the number of arguments. This number can be lower than
3683 // the number of inputs returned by InputCount(), as some invoke
3684 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3685 // inputs at the end of their list of inputs.
3686 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3687
Vladimir Markoa1de9182016-02-25 11:37:38 +00003688 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003689
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003690 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003691 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003692
Vladimir Markoa1de9182016-02-25 11:37:38 +00003693 InvokeType GetOriginalInvokeType() const {
3694 return GetPackedField<OriginalInvokeTypeField>();
3695 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003696
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003697 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003698 return intrinsic_;
3699 }
3700
Aart Bik5d75afe2015-12-14 11:57:01 -08003701 void SetIntrinsic(Intrinsics intrinsic,
3702 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3703 IntrinsicSideEffects side_effects,
3704 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003705
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003706 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003707 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003708 }
3709
Vladimir Markoa1de9182016-02-25 11:37:38 +00003710 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003711
3712 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3713
3714 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3715 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3716 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003717
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003718 uint32_t* GetIntrinsicOptimizations() {
3719 return &intrinsic_optimizations_;
3720 }
3721
3722 const uint32_t* GetIntrinsicOptimizations() const {
3723 return &intrinsic_optimizations_;
3724 }
3725
3726 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3727
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003728 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003729
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003730 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003731 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3732 static constexpr size_t kFieldOriginalInvokeTypeSize =
3733 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3734 static constexpr size_t kFieldReturnType =
3735 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3736 static constexpr size_t kFieldReturnTypeSize =
3737 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3738 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3739 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3740 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3741 using OriginalInvokeTypeField =
3742 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3743 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3744
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003745 HInvoke(ArenaAllocator* arena,
3746 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003747 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003748 Primitive::Type return_type,
3749 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003750 uint32_t dex_method_index,
3751 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003752 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003753 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003754 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003755 inputs_(number_of_arguments + number_of_other_inputs,
3756 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003757 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003758 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003759 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003760 SetPackedField<ReturnTypeField>(return_type);
3761 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3762 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003763 }
3764
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003765 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003766 return inputs_[index];
3767 }
3768
David Brazdil1abb4192015-02-17 18:33:36 +00003769 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003770 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003771 }
3772
Vladimir Markoa1de9182016-02-25 11:37:38 +00003773 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003774
Roland Levillain3e3d7332015-04-28 11:00:54 +01003775 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003776 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003777 const uint32_t dex_method_index_;
3778 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003779
3780 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3781 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003782
3783 private:
3784 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3785};
3786
Calin Juravle175dc732015-08-25 15:42:32 +01003787class HInvokeUnresolved : public HInvoke {
3788 public:
3789 HInvokeUnresolved(ArenaAllocator* arena,
3790 uint32_t number_of_arguments,
3791 Primitive::Type return_type,
3792 uint32_t dex_pc,
3793 uint32_t dex_method_index,
3794 InvokeType invoke_type)
3795 : HInvoke(arena,
3796 number_of_arguments,
3797 0u /* number_of_other_inputs */,
3798 return_type,
3799 dex_pc,
3800 dex_method_index,
3801 invoke_type) {
3802 }
3803
3804 DECLARE_INSTRUCTION(InvokeUnresolved);
3805
3806 private:
3807 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3808};
3809
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003810class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003811 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003812 // Requirements of this method call regarding the class
3813 // initialization (clinit) check of its declaring class.
3814 enum class ClinitCheckRequirement {
3815 kNone, // Class already initialized.
3816 kExplicit, // Static call having explicit clinit check as last input.
3817 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003818 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003819 };
3820
Vladimir Marko58155012015-08-19 12:49:41 +00003821 // Determines how to load the target ArtMethod*.
3822 enum class MethodLoadKind {
3823 // Use a String init ArtMethod* loaded from Thread entrypoints.
3824 kStringInit,
3825
3826 // Use the method's own ArtMethod* loaded by the register allocator.
3827 kRecursive,
3828
3829 // Use ArtMethod* at a known address, embed the direct address in the code.
3830 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3831 kDirectAddress,
3832
3833 // Use ArtMethod* at an address that will be known at link time, embed the direct
3834 // address in the code. If the image is relocatable, emit .patch_oat entry.
3835 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3836 // the image relocatable or not.
3837 kDirectAddressWithFixup,
3838
Vladimir Markoa1de9182016-02-25 11:37:38 +00003839 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003840 // Used when we need to use the dex cache, for example for invoke-static that
3841 // may cause class initialization (the entry may point to a resolution method),
3842 // and we know that we can access the dex cache arrays using a PC-relative load.
3843 kDexCachePcRelative,
3844
3845 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3846 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3847 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3848 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3849 kDexCacheViaMethod,
3850 };
3851
3852 // Determines the location of the code pointer.
3853 enum class CodePtrLocation {
3854 // Recursive call, use local PC-relative call instruction.
3855 kCallSelf,
3856
3857 // Use PC-relative call instruction patched at link time.
3858 // Used for calls within an oat file, boot->boot or app->app.
3859 kCallPCRelative,
3860
3861 // Call to a known target address, embed the direct address in code.
3862 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3863 kCallDirect,
3864
3865 // Call to a target address that will be known at link time, embed the direct
3866 // address in code. If the image is relocatable, emit .patch_oat entry.
3867 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3868 // the image relocatable or not.
3869 kCallDirectWithFixup,
3870
3871 // Use code pointer from the ArtMethod*.
3872 // Used when we don't know the target code. This is also the last-resort-kind used when
3873 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3874 kCallArtMethod,
3875 };
3876
3877 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003878 MethodLoadKind method_load_kind;
3879 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003880 // The method load data holds
3881 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3882 // Note that there are multiple string init methods, each having its own offset.
3883 // - the method address for kDirectAddress
3884 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003885 uint64_t method_load_data;
3886 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003887 };
3888
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003889 HInvokeStaticOrDirect(ArenaAllocator* arena,
3890 uint32_t number_of_arguments,
3891 Primitive::Type return_type,
3892 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003893 uint32_t method_index,
3894 MethodReference target_method,
3895 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003896 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003897 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003898 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003899 : HInvoke(arena,
3900 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003901 // There is potentially one extra argument for the HCurrentMethod node, and
3902 // potentially one other if the clinit check is explicit, and potentially
3903 // one other if the method is a string factory.
3904 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003905 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003906 return_type,
3907 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003908 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003909 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003910 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003911 dispatch_info_(dispatch_info) {
3912 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3913 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3914 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003915
Vladimir Markodc151b22015-10-15 18:02:30 +01003916 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003917 bool had_current_method_input = HasCurrentMethodInput();
3918 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3919
3920 // Using the current method is the default and once we find a better
3921 // method load kind, we should not go back to using the current method.
3922 DCHECK(had_current_method_input || !needs_current_method_input);
3923
3924 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003925 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3926 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003927 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003928 dispatch_info_ = dispatch_info;
3929 }
3930
Vladimir Markoc53c0792015-11-19 15:48:33 +00003931 void AddSpecialInput(HInstruction* input) {
3932 // We allow only one special input.
3933 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3934 DCHECK(InputCount() == GetSpecialInputIndex() ||
3935 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3936 InsertInputAt(GetSpecialInputIndex(), input);
3937 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003938
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003939 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003940 // We access the method via the dex cache so we can't do an implicit null check.
3941 // TODO: for intrinsics we can generate implicit null checks.
3942 return false;
3943 }
3944
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003945 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003946 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003947 }
3948
Vladimir Markoc53c0792015-11-19 15:48:33 +00003949 // Get the index of the special input, if any.
3950 //
David Brazdil6de19382016-01-08 17:37:10 +00003951 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3952 // method pointer; otherwise there may be one platform-specific special input,
3953 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003954 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003955 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003956
Vladimir Markoa1de9182016-02-25 11:37:38 +00003957 InvokeType GetOptimizedInvokeType() const {
3958 return GetPackedField<OptimizedInvokeTypeField>();
3959 }
3960
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003961 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003962 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003963 }
3964
Vladimir Marko58155012015-08-19 12:49:41 +00003965 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3966 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3967 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003968 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003969 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003970 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003971 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003972 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3973 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003974 bool HasCurrentMethodInput() const {
3975 // This function can be called only after the invoke has been fully initialized by the builder.
3976 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003977 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003978 return true;
3979 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003980 DCHECK(InputCount() == GetSpecialInputIndex() ||
3981 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003982 return false;
3983 }
3984 }
Vladimir Marko58155012015-08-19 12:49:41 +00003985 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3986 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003987 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003988
3989 int32_t GetStringInitOffset() const {
3990 DCHECK(IsStringInit());
3991 return dispatch_info_.method_load_data;
3992 }
3993
3994 uint64_t GetMethodAddress() const {
3995 DCHECK(HasMethodAddress());
3996 return dispatch_info_.method_load_data;
3997 }
3998
3999 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004000 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004001 return dispatch_info_.method_load_data;
4002 }
4003
4004 uint64_t GetDirectCodePtr() const {
4005 DCHECK(HasDirectCodePtr());
4006 return dispatch_info_.direct_code_ptr;
4007 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004008
Vladimir Markoa1de9182016-02-25 11:37:38 +00004009 ClinitCheckRequirement GetClinitCheckRequirement() const {
4010 return GetPackedField<ClinitCheckRequirementField>();
4011 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004012
Roland Levillain4c0eb422015-04-24 16:43:49 +01004013 // Is this instruction a call to a static method?
4014 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004015 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004016 }
4017
Vladimir Markofbb184a2015-11-13 14:47:00 +00004018 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4019 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4020 // instruction; only relevant for static calls with explicit clinit check.
4021 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004022 DCHECK(IsStaticWithExplicitClinitCheck());
4023 size_t last_input_index = InputCount() - 1;
4024 HInstruction* last_input = InputAt(last_input_index);
4025 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004026 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004027 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004028 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004029 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004030 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004031 }
4032
4033 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004034 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004035 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004036 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004037 }
4038
4039 // Is this a call to a static method whose declaring class has an
4040 // implicit intialization check requirement?
4041 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004042 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004043 }
4044
Vladimir Markob554b5a2015-11-06 12:57:55 +00004045 // Does this method load kind need the current method as an input?
4046 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4047 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4048 }
4049
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004050 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004051
Roland Levillain4c0eb422015-04-24 16:43:49 +01004052 protected:
4053 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
4054 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
4055 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
4056 HInstruction* input = input_record.GetInstruction();
4057 // `input` is the last input of a static invoke marked as having
4058 // an explicit clinit check. It must either be:
4059 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4060 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4061 DCHECK(input != nullptr);
4062 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
4063 }
4064 return input_record;
4065 }
4066
Vladimir Markoc53c0792015-11-19 15:48:33 +00004067 void InsertInputAt(size_t index, HInstruction* input);
4068 void RemoveInputAt(size_t index);
4069
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004070 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004071 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4072 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4073 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4074 static constexpr size_t kFieldClinitCheckRequirement =
4075 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4076 static constexpr size_t kFieldClinitCheckRequirementSize =
4077 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4078 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4079 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4080 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4081 "Too many packed fields.");
4082 using OptimizedInvokeTypeField =
4083 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4084 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4085 kFieldClinitCheckRequirement,
4086 kFieldClinitCheckRequirementSize>;
4087
Vladimir Marko58155012015-08-19 12:49:41 +00004088 // The target method may refer to different dex file or method index than the original
4089 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4090 // in derived class to increase visibility.
4091 MethodReference target_method_;
4092 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004093
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004094 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004095};
Vladimir Markof64242a2015-12-01 14:58:23 +00004096std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004097std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004098
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004099class HInvokeVirtual : public HInvoke {
4100 public:
4101 HInvokeVirtual(ArenaAllocator* arena,
4102 uint32_t number_of_arguments,
4103 Primitive::Type return_type,
4104 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004105 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004106 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004107 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004108 vtable_index_(vtable_index) {}
4109
Calin Juravle641547a2015-04-21 22:08:51 +01004110 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004111 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004112 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004113 }
4114
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004115 uint32_t GetVTableIndex() const { return vtable_index_; }
4116
4117 DECLARE_INSTRUCTION(InvokeVirtual);
4118
4119 private:
4120 const uint32_t vtable_index_;
4121
4122 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4123};
4124
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004125class HInvokeInterface : public HInvoke {
4126 public:
4127 HInvokeInterface(ArenaAllocator* arena,
4128 uint32_t number_of_arguments,
4129 Primitive::Type return_type,
4130 uint32_t dex_pc,
4131 uint32_t dex_method_index,
4132 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004133 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004134 imt_index_(imt_index) {}
4135
Calin Juravle641547a2015-04-21 22:08:51 +01004136 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004137 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004138 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004139 }
4140
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004141 uint32_t GetImtIndex() const { return imt_index_; }
4142 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4143
4144 DECLARE_INSTRUCTION(InvokeInterface);
4145
4146 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004147 const uint32_t imt_index_;
4148
4149 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4150};
4151
Roland Levillain88cb1752014-10-20 16:36:47 +01004152class HNeg : public HUnaryOperation {
4153 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004154 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004155 : HUnaryOperation(result_type, input, dex_pc) {
4156 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4157 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004158
Roland Levillain9867bc72015-08-05 10:21:34 +01004159 template <typename T> T Compute(T x) const { return -x; }
4160
4161 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004162 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004163 }
4164 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004165 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004166 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004167 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4168 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4169 }
4170 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4171 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4172 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004173
Roland Levillain88cb1752014-10-20 16:36:47 +01004174 DECLARE_INSTRUCTION(Neg);
4175
4176 private:
4177 DISALLOW_COPY_AND_ASSIGN(HNeg);
4178};
4179
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004180class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004181 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004182 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004183 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004184 uint32_t dex_pc,
4185 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004186 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004187 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004188 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004189 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004190 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004191 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004192 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004193 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004194 }
4195
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004196 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004197 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004198
4199 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004200 bool NeedsEnvironment() const OVERRIDE { return true; }
4201
Mingyao Yang0c365e62015-03-31 15:09:29 -07004202 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4203 bool CanThrow() const OVERRIDE { return true; }
4204
Calin Juravle10e244f2015-01-26 18:54:32 +00004205 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004206
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004207 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4208
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004209 DECLARE_INSTRUCTION(NewArray);
4210
4211 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004212 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004213 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004214 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004215
4216 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4217};
4218
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004219class HAdd : public HBinaryOperation {
4220 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004221 HAdd(Primitive::Type result_type,
4222 HInstruction* left,
4223 HInstruction* right,
4224 uint32_t dex_pc = kNoDexPc)
4225 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004226
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004227 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004228
Roland Levillain9867bc72015-08-05 10:21:34 +01004229 template <typename T> T Compute(T x, T y) const { return x + y; }
4230
4231 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004232 return GetBlock()->GetGraph()->GetIntConstant(
4233 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004234 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004235 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004236 return GetBlock()->GetGraph()->GetLongConstant(
4237 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004238 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004239 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4240 return GetBlock()->GetGraph()->GetFloatConstant(
4241 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4242 }
4243 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4244 return GetBlock()->GetGraph()->GetDoubleConstant(
4245 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4246 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004247
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004248 DECLARE_INSTRUCTION(Add);
4249
4250 private:
4251 DISALLOW_COPY_AND_ASSIGN(HAdd);
4252};
4253
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004254class HSub : public HBinaryOperation {
4255 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004256 HSub(Primitive::Type result_type,
4257 HInstruction* left,
4258 HInstruction* right,
4259 uint32_t dex_pc = kNoDexPc)
4260 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004261
Roland Levillain9867bc72015-08-05 10:21:34 +01004262 template <typename T> T Compute(T x, T y) const { return x - y; }
4263
4264 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004265 return GetBlock()->GetGraph()->GetIntConstant(
4266 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004267 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004268 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004269 return GetBlock()->GetGraph()->GetLongConstant(
4270 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004271 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004272 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4273 return GetBlock()->GetGraph()->GetFloatConstant(
4274 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4275 }
4276 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4277 return GetBlock()->GetGraph()->GetDoubleConstant(
4278 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4279 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004280
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004281 DECLARE_INSTRUCTION(Sub);
4282
4283 private:
4284 DISALLOW_COPY_AND_ASSIGN(HSub);
4285};
4286
Calin Juravle34bacdf2014-10-07 20:23:36 +01004287class HMul : public HBinaryOperation {
4288 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004289 HMul(Primitive::Type result_type,
4290 HInstruction* left,
4291 HInstruction* right,
4292 uint32_t dex_pc = kNoDexPc)
4293 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004294
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004295 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004296
Roland Levillain9867bc72015-08-05 10:21:34 +01004297 template <typename T> T Compute(T x, T y) const { return x * y; }
4298
4299 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004300 return GetBlock()->GetGraph()->GetIntConstant(
4301 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004302 }
4303 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004304 return GetBlock()->GetGraph()->GetLongConstant(
4305 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004306 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004307 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4308 return GetBlock()->GetGraph()->GetFloatConstant(
4309 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4310 }
4311 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4312 return GetBlock()->GetGraph()->GetDoubleConstant(
4313 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4314 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004315
4316 DECLARE_INSTRUCTION(Mul);
4317
4318 private:
4319 DISALLOW_COPY_AND_ASSIGN(HMul);
4320};
4321
Calin Juravle7c4954d2014-10-28 16:57:40 +00004322class HDiv : public HBinaryOperation {
4323 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004324 HDiv(Primitive::Type result_type,
4325 HInstruction* left,
4326 HInstruction* right,
4327 uint32_t dex_pc)
4328 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004329
Roland Levillain9867bc72015-08-05 10:21:34 +01004330 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004331 T ComputeIntegral(T x, T y) const {
4332 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004333 // Our graph structure ensures we never have 0 for `y` during
4334 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004335 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004336 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004337 return (y == -1) ? -x : x / y;
4338 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004339
Roland Levillain31dd3d62016-02-16 12:21:02 +00004340 template <typename T>
4341 T ComputeFP(T x, T y) const {
4342 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4343 return x / y;
4344 }
4345
Roland Levillain9867bc72015-08-05 10:21:34 +01004346 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004347 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004348 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004349 }
4350 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004351 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004352 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4353 }
4354 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4355 return GetBlock()->GetGraph()->GetFloatConstant(
4356 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4357 }
4358 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4359 return GetBlock()->GetGraph()->GetDoubleConstant(
4360 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004361 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004362
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004363 static SideEffects SideEffectsForArchRuntimeCalls() {
4364 // The generated code can use a runtime call.
4365 return SideEffects::CanTriggerGC();
4366 }
4367
Calin Juravle7c4954d2014-10-28 16:57:40 +00004368 DECLARE_INSTRUCTION(Div);
4369
4370 private:
4371 DISALLOW_COPY_AND_ASSIGN(HDiv);
4372};
4373
Calin Juravlebacfec32014-11-14 15:54:36 +00004374class HRem : public HBinaryOperation {
4375 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004376 HRem(Primitive::Type result_type,
4377 HInstruction* left,
4378 HInstruction* right,
4379 uint32_t dex_pc)
4380 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004381
Roland Levillain9867bc72015-08-05 10:21:34 +01004382 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004383 T ComputeIntegral(T x, T y) const {
4384 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004385 // Our graph structure ensures we never have 0 for `y` during
4386 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004387 DCHECK_NE(y, 0);
4388 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4389 return (y == -1) ? 0 : x % y;
4390 }
4391
Roland Levillain31dd3d62016-02-16 12:21:02 +00004392 template <typename T>
4393 T ComputeFP(T x, T y) const {
4394 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4395 return std::fmod(x, y);
4396 }
4397
Roland Levillain9867bc72015-08-05 10:21:34 +01004398 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004399 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004400 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004401 }
4402 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004403 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004404 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004405 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004406 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4407 return GetBlock()->GetGraph()->GetFloatConstant(
4408 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4409 }
4410 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4411 return GetBlock()->GetGraph()->GetDoubleConstant(
4412 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4413 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004414
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004415 static SideEffects SideEffectsForArchRuntimeCalls() {
4416 return SideEffects::CanTriggerGC();
4417 }
4418
Calin Juravlebacfec32014-11-14 15:54:36 +00004419 DECLARE_INSTRUCTION(Rem);
4420
4421 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004422 DISALLOW_COPY_AND_ASSIGN(HRem);
4423};
4424
Calin Juravled0d48522014-11-04 16:40:20 +00004425class HDivZeroCheck : public HExpression<1> {
4426 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004427 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4428 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004429 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004430 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004431 SetRawInputAt(0, value);
4432 }
4433
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004434 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4435
Calin Juravled0d48522014-11-04 16:40:20 +00004436 bool CanBeMoved() const OVERRIDE { return true; }
4437
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004438 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004439 return true;
4440 }
4441
4442 bool NeedsEnvironment() const OVERRIDE { return true; }
4443 bool CanThrow() const OVERRIDE { return true; }
4444
Calin Juravled0d48522014-11-04 16:40:20 +00004445 DECLARE_INSTRUCTION(DivZeroCheck);
4446
4447 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004448 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4449};
4450
Calin Juravle9aec02f2014-11-18 23:06:35 +00004451class HShl : public HBinaryOperation {
4452 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004453 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004454 HInstruction* value,
4455 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004456 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004457 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4458 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4459 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004460 }
4461
Roland Levillain5b5b9312016-03-22 14:57:31 +00004462 template <typename T>
4463 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4464 return value << (distance & max_shift_distance);
4465 }
4466
4467 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004468 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004469 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004470 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004471 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004472 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004473 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004474 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004475 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4476 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4477 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4478 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004479 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004480 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4481 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004482 LOG(FATAL) << DebugName() << " is not defined for float values";
4483 UNREACHABLE();
4484 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004485 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4486 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004487 LOG(FATAL) << DebugName() << " is not defined for double values";
4488 UNREACHABLE();
4489 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004490
4491 DECLARE_INSTRUCTION(Shl);
4492
4493 private:
4494 DISALLOW_COPY_AND_ASSIGN(HShl);
4495};
4496
4497class HShr : public HBinaryOperation {
4498 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004499 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004500 HInstruction* value,
4501 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004502 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004503 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4504 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4505 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004506 }
4507
Roland Levillain5b5b9312016-03-22 14:57:31 +00004508 template <typename T>
4509 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4510 return value >> (distance & max_shift_distance);
4511 }
4512
4513 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004514 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004515 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004516 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004517 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004518 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004519 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004520 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004521 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4522 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4523 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4524 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004525 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004526 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4527 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004528 LOG(FATAL) << DebugName() << " is not defined for float values";
4529 UNREACHABLE();
4530 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004531 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4532 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004533 LOG(FATAL) << DebugName() << " is not defined for double values";
4534 UNREACHABLE();
4535 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004536
4537 DECLARE_INSTRUCTION(Shr);
4538
4539 private:
4540 DISALLOW_COPY_AND_ASSIGN(HShr);
4541};
4542
4543class HUShr : public HBinaryOperation {
4544 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004545 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004546 HInstruction* value,
4547 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004548 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004549 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4550 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4551 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004552 }
4553
Roland Levillain5b5b9312016-03-22 14:57:31 +00004554 template <typename T>
4555 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4556 typedef typename std::make_unsigned<T>::type V;
4557 V ux = static_cast<V>(value);
4558 return static_cast<T>(ux >> (distance & max_shift_distance));
4559 }
4560
4561 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004562 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004563 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004564 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004565 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004566 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004567 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004568 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004569 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4570 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4571 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4572 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004573 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004574 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4575 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004576 LOG(FATAL) << DebugName() << " is not defined for float values";
4577 UNREACHABLE();
4578 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004579 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4580 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004581 LOG(FATAL) << DebugName() << " is not defined for double values";
4582 UNREACHABLE();
4583 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004584
4585 DECLARE_INSTRUCTION(UShr);
4586
4587 private:
4588 DISALLOW_COPY_AND_ASSIGN(HUShr);
4589};
4590
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004591class HAnd : public HBinaryOperation {
4592 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004593 HAnd(Primitive::Type result_type,
4594 HInstruction* left,
4595 HInstruction* right,
4596 uint32_t dex_pc = kNoDexPc)
4597 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004598
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004599 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004600
Roland Levillaine53bd812016-02-24 14:54:18 +00004601 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004602
4603 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004604 return GetBlock()->GetGraph()->GetIntConstant(
4605 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004606 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004607 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004608 return GetBlock()->GetGraph()->GetLongConstant(
4609 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004610 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004611 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4612 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4613 LOG(FATAL) << DebugName() << " is not defined for float values";
4614 UNREACHABLE();
4615 }
4616 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4617 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4618 LOG(FATAL) << DebugName() << " is not defined for double values";
4619 UNREACHABLE();
4620 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004621
4622 DECLARE_INSTRUCTION(And);
4623
4624 private:
4625 DISALLOW_COPY_AND_ASSIGN(HAnd);
4626};
4627
4628class HOr : public HBinaryOperation {
4629 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004630 HOr(Primitive::Type result_type,
4631 HInstruction* left,
4632 HInstruction* right,
4633 uint32_t dex_pc = kNoDexPc)
4634 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004635
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004636 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004637
Roland Levillaine53bd812016-02-24 14:54:18 +00004638 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004639
4640 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004641 return GetBlock()->GetGraph()->GetIntConstant(
4642 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004643 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004644 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004645 return GetBlock()->GetGraph()->GetLongConstant(
4646 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004647 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004648 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4649 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4650 LOG(FATAL) << DebugName() << " is not defined for float values";
4651 UNREACHABLE();
4652 }
4653 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4654 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4655 LOG(FATAL) << DebugName() << " is not defined for double values";
4656 UNREACHABLE();
4657 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004658
4659 DECLARE_INSTRUCTION(Or);
4660
4661 private:
4662 DISALLOW_COPY_AND_ASSIGN(HOr);
4663};
4664
4665class HXor : public HBinaryOperation {
4666 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004667 HXor(Primitive::Type result_type,
4668 HInstruction* left,
4669 HInstruction* right,
4670 uint32_t dex_pc = kNoDexPc)
4671 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004672
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004673 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004674
Roland Levillaine53bd812016-02-24 14:54:18 +00004675 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004676
4677 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004678 return GetBlock()->GetGraph()->GetIntConstant(
4679 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004680 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004681 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004682 return GetBlock()->GetGraph()->GetLongConstant(
4683 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004684 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004685 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4686 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4687 LOG(FATAL) << DebugName() << " is not defined for float values";
4688 UNREACHABLE();
4689 }
4690 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4691 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4692 LOG(FATAL) << DebugName() << " is not defined for double values";
4693 UNREACHABLE();
4694 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004695
4696 DECLARE_INSTRUCTION(Xor);
4697
4698 private:
4699 DISALLOW_COPY_AND_ASSIGN(HXor);
4700};
4701
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004702class HRor : public HBinaryOperation {
4703 public:
4704 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004705 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004706 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4707 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004708 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004709
Roland Levillain5b5b9312016-03-22 14:57:31 +00004710 template <typename T>
4711 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4712 typedef typename std::make_unsigned<T>::type V;
4713 V ux = static_cast<V>(value);
4714 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004715 return static_cast<T>(ux);
4716 } else {
4717 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004718 return static_cast<T>(ux >> (distance & max_shift_value)) |
4719 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004720 }
4721 }
4722
Roland Levillain5b5b9312016-03-22 14:57:31 +00004723 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004724 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004725 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004726 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004727 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004728 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004729 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004730 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004731 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4732 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4733 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4734 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004735 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004736 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4737 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004738 LOG(FATAL) << DebugName() << " is not defined for float values";
4739 UNREACHABLE();
4740 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004741 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4742 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004743 LOG(FATAL) << DebugName() << " is not defined for double values";
4744 UNREACHABLE();
4745 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004746
4747 DECLARE_INSTRUCTION(Ror);
4748
4749 private:
4750 DISALLOW_COPY_AND_ASSIGN(HRor);
4751};
4752
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004753// The value of a parameter in this method. Its location depends on
4754// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004755class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004756 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004757 HParameterValue(const DexFile& dex_file,
4758 uint16_t type_index,
4759 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004760 Primitive::Type parameter_type,
4761 bool is_this = false)
4762 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004763 dex_file_(dex_file),
4764 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004765 index_(index) {
4766 SetPackedFlag<kFlagIsThis>(is_this);
4767 SetPackedFlag<kFlagCanBeNull>(!is_this);
4768 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004769
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004770 const DexFile& GetDexFile() const { return dex_file_; }
4771 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004772 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004773 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004774
Vladimir Markoa1de9182016-02-25 11:37:38 +00004775 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4776 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004777
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004778 DECLARE_INSTRUCTION(ParameterValue);
4779
4780 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004781 // Whether or not the parameter value corresponds to 'this' argument.
4782 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4783 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4784 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4785 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4786 "Too many packed fields.");
4787
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004788 const DexFile& dex_file_;
4789 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004790 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004791 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004792 const uint8_t index_;
4793
4794 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4795};
4796
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004797class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004798 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004799 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4800 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004801
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004802 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004803 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004804 return true;
4805 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004806
Roland Levillain9867bc72015-08-05 10:21:34 +01004807 template <typename T> T Compute(T x) const { return ~x; }
4808
4809 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004810 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004811 }
4812 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004813 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004814 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004815 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4816 LOG(FATAL) << DebugName() << " is not defined for float values";
4817 UNREACHABLE();
4818 }
4819 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4820 LOG(FATAL) << DebugName() << " is not defined for double values";
4821 UNREACHABLE();
4822 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004823
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004824 DECLARE_INSTRUCTION(Not);
4825
4826 private:
4827 DISALLOW_COPY_AND_ASSIGN(HNot);
4828};
4829
David Brazdil66d126e2015-04-03 16:02:44 +01004830class HBooleanNot : public HUnaryOperation {
4831 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004832 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4833 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004834
4835 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004836 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004837 return true;
4838 }
4839
Roland Levillain9867bc72015-08-05 10:21:34 +01004840 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004841 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004842 return !x;
4843 }
4844
Roland Levillain9867bc72015-08-05 10:21:34 +01004845 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004846 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004847 }
4848 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4849 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004850 UNREACHABLE();
4851 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004852 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4853 LOG(FATAL) << DebugName() << " is not defined for float values";
4854 UNREACHABLE();
4855 }
4856 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4857 LOG(FATAL) << DebugName() << " is not defined for double values";
4858 UNREACHABLE();
4859 }
David Brazdil66d126e2015-04-03 16:02:44 +01004860
4861 DECLARE_INSTRUCTION(BooleanNot);
4862
4863 private:
4864 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4865};
4866
Roland Levillaindff1f282014-11-05 14:15:05 +00004867class HTypeConversion : public HExpression<1> {
4868 public:
4869 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004870 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004871 : HExpression(result_type,
4872 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4873 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004874 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004875 // Invariant: We should never generate a conversion to a Boolean value.
4876 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004877 }
4878
4879 HInstruction* GetInput() const { return InputAt(0); }
4880 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4881 Primitive::Type GetResultType() const { return GetType(); }
4882
4883 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004884 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004885
Mark Mendelle82549b2015-05-06 10:55:34 -04004886 // Try to statically evaluate the conversion and return a HConstant
4887 // containing the result. If the input cannot be converted, return nullptr.
4888 HConstant* TryStaticEvaluation() const;
4889
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004890 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4891 Primitive::Type result_type) {
4892 // Some architectures may not require the 'GC' side effects, but at this point
4893 // in the compilation process we do not know what architecture we will
4894 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004895 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4896 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004897 return SideEffects::CanTriggerGC();
4898 }
4899 return SideEffects::None();
4900 }
4901
Roland Levillaindff1f282014-11-05 14:15:05 +00004902 DECLARE_INSTRUCTION(TypeConversion);
4903
4904 private:
4905 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4906};
4907
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004908static constexpr uint32_t kNoRegNumber = -1;
4909
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004910class HNullCheck : public HExpression<1> {
4911 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004912 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4913 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004914 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004915 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004916 SetRawInputAt(0, value);
4917 }
4918
Calin Juravle10e244f2015-01-26 18:54:32 +00004919 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004920 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004921 return true;
4922 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004923
Calin Juravle10e244f2015-01-26 18:54:32 +00004924 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004925
Calin Juravle10e244f2015-01-26 18:54:32 +00004926 bool CanThrow() const OVERRIDE { return true; }
4927
4928 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004929
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004930
4931 DECLARE_INSTRUCTION(NullCheck);
4932
4933 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004934 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4935};
4936
4937class FieldInfo : public ValueObject {
4938 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004939 FieldInfo(MemberOffset field_offset,
4940 Primitive::Type field_type,
4941 bool is_volatile,
4942 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004943 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004944 const DexFile& dex_file,
4945 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004946 : field_offset_(field_offset),
4947 field_type_(field_type),
4948 is_volatile_(is_volatile),
4949 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004950 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004951 dex_file_(dex_file),
4952 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004953
4954 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004955 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004956 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004957 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004958 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004959 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004960 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004961
4962 private:
4963 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004964 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004965 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004966 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004967 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004968 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004969 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004970};
4971
4972class HInstanceFieldGet : public HExpression<1> {
4973 public:
4974 HInstanceFieldGet(HInstruction* value,
4975 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004976 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004977 bool is_volatile,
4978 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004979 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004980 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004981 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004982 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004983 : HExpression(field_type,
4984 SideEffects::FieldReadOfType(field_type, is_volatile),
4985 dex_pc),
4986 field_info_(field_offset,
4987 field_type,
4988 is_volatile,
4989 field_idx,
4990 declaring_class_def_index,
4991 dex_file,
4992 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004993 SetRawInputAt(0, value);
4994 }
4995
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004996 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004997
4998 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4999 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
5000 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005001 }
5002
Calin Juravle641547a2015-04-21 22:08:51 +01005003 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5004 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005005 }
5006
5007 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005008 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5009 }
5010
Calin Juravle52c48962014-12-16 17:02:57 +00005011 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005012 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005013 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005014 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005015
5016 DECLARE_INSTRUCTION(InstanceFieldGet);
5017
5018 private:
5019 const FieldInfo field_info_;
5020
5021 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5022};
5023
5024class HInstanceFieldSet : public HTemplateInstruction<2> {
5025 public:
5026 HInstanceFieldSet(HInstruction* object,
5027 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005028 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005029 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005030 bool is_volatile,
5031 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005032 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005033 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005034 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005035 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005036 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5037 dex_pc),
5038 field_info_(field_offset,
5039 field_type,
5040 is_volatile,
5041 field_idx,
5042 declaring_class_def_index,
5043 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005044 dex_cache) {
5045 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005046 SetRawInputAt(0, object);
5047 SetRawInputAt(1, value);
5048 }
5049
Calin Juravle641547a2015-04-21 22:08:51 +01005050 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5051 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005052 }
5053
Calin Juravle52c48962014-12-16 17:02:57 +00005054 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005055 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005056 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005057 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005058 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005059 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5060 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005061
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005062 DECLARE_INSTRUCTION(InstanceFieldSet);
5063
5064 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005065 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5066 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5067 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5068 "Too many packed fields.");
5069
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005070 const FieldInfo field_info_;
5071
5072 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5073};
5074
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005075class HArrayGet : public HExpression<2> {
5076 public:
Aart Bik18b36ab2016-04-13 16:41:35 -07005077 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type, uint32_t dex_pc)
5078 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005079 SetRawInputAt(0, array);
5080 SetRawInputAt(1, index);
5081 }
5082
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005083 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005084 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005085 return true;
5086 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005087 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005088 // TODO: We can be smarter here.
5089 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5090 // which generates the implicit null check. There are cases when these can be removed
5091 // to produce better code. If we ever add optimizations to do so we should allow an
5092 // implicit check here (as long as the address falls in the first page).
5093 return false;
5094 }
5095
David Brazdil4833f5a2015-12-16 10:37:39 +00005096 bool IsEquivalentOf(HArrayGet* other) const {
5097 bool result = (GetDexPc() == other->GetDexPc());
5098 if (kIsDebugBuild && result) {
5099 DCHECK_EQ(GetBlock(), other->GetBlock());
5100 DCHECK_EQ(GetArray(), other->GetArray());
5101 DCHECK_EQ(GetIndex(), other->GetIndex());
5102 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005103 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005104 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005105 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5106 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005107 }
5108 }
5109 return result;
5110 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005111
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005112 HInstruction* GetArray() const { return InputAt(0); }
5113 HInstruction* GetIndex() const { return InputAt(1); }
5114
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005115 DECLARE_INSTRUCTION(ArrayGet);
5116
5117 private:
5118 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5119};
5120
5121class HArraySet : public HTemplateInstruction<3> {
5122 public:
5123 HArraySet(HInstruction* array,
5124 HInstruction* index,
5125 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005126 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005127 uint32_t dex_pc)
5128 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005129 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5130 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5131 SetPackedFlag<kFlagValueCanBeNull>(true);
5132 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005133 SetRawInputAt(0, array);
5134 SetRawInputAt(1, index);
5135 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005136 // Make a best guess now, may be refined during SSA building.
5137 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005138 }
5139
Calin Juravle77520bc2015-01-12 18:45:46 +00005140 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005141 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005142 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005143 }
5144
Mingyao Yang81014cb2015-06-02 03:16:27 -07005145 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005146 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005147
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005148 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005149 // TODO: Same as for ArrayGet.
5150 return false;
5151 }
5152
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005153 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005154 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005155 }
5156
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005157 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005158 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005159 }
5160
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005161 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005162 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005163 }
5164
Vladimir Markoa1de9182016-02-25 11:37:38 +00005165 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5166 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5167 bool StaticTypeOfArrayIsObjectArray() const {
5168 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5169 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005170
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005171 HInstruction* GetArray() const { return InputAt(0); }
5172 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005173 HInstruction* GetValue() const { return InputAt(2); }
5174
5175 Primitive::Type GetComponentType() const {
5176 // The Dex format does not type floating point index operations. Since the
5177 // `expected_component_type_` is set during building and can therefore not
5178 // be correct, we also check what is the value type. If it is a floating
5179 // point type, we must use that type.
5180 Primitive::Type value_type = GetValue()->GetType();
5181 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5182 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005183 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005184 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005185
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005186 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005187 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005188 }
5189
Aart Bik18b36ab2016-04-13 16:41:35 -07005190 void ComputeSideEffects() {
5191 Primitive::Type type = GetComponentType();
5192 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5193 SideEffectsForArchRuntimeCalls(type)));
5194 }
5195
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005196 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5197 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5198 }
5199
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005200 DECLARE_INSTRUCTION(ArraySet);
5201
5202 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005203 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5204 static constexpr size_t kFieldExpectedComponentTypeSize =
5205 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5206 static constexpr size_t kFlagNeedsTypeCheck =
5207 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5208 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005209 // Cached information for the reference_type_info_ so that codegen
5210 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005211 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5212 static constexpr size_t kNumberOfArraySetPackedBits =
5213 kFlagStaticTypeOfArrayIsObjectArray + 1;
5214 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5215 using ExpectedComponentTypeField =
5216 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005217
5218 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5219};
5220
5221class HArrayLength : public HExpression<1> {
5222 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005223 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005224 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005225 // Note that arrays do not change length, so the instruction does not
5226 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005227 SetRawInputAt(0, array);
5228 }
5229
Calin Juravle77520bc2015-01-12 18:45:46 +00005230 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005231 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005232 return true;
5233 }
Calin Juravle641547a2015-04-21 22:08:51 +01005234 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5235 return obj == InputAt(0);
5236 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005237
Vladimir Markodce016e2016-04-28 13:10:02 +01005238 void MarkAsStringLength() { SetPackedFlag<kFlagIsStringLength>(); }
5239 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5240
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005241 DECLARE_INSTRUCTION(ArrayLength);
5242
5243 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005244 // We treat a String as an array, creating the HArrayLength from String.length()
5245 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5246 // determine whether a particular HArrayLength is actually a String.length() by
5247 // looking at the type of the input but that requires holding the mutator lock, so
5248 // we prefer to use a flag, so that code generators don't need to do the locking.
5249 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5250 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5251 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5252 "Too many packed fields.");
5253
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005254 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5255};
5256
5257class HBoundsCheck : public HExpression<2> {
5258 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005259 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5260 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005261 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005262 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005263 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005264 SetRawInputAt(0, index);
5265 SetRawInputAt(1, length);
5266 }
5267
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005268 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005269 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005270 return true;
5271 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005272
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005273 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005274
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005275 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005276
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005277 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005278
5279 DECLARE_INSTRUCTION(BoundsCheck);
5280
5281 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005282 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5283};
5284
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005285class HSuspendCheck : public HTemplateInstruction<0> {
5286 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005287 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005288 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005289
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005290 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005291 return true;
5292 }
5293
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005294 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5295 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005296
5297 DECLARE_INSTRUCTION(SuspendCheck);
5298
5299 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005300 // Only used for code generation, in order to share the same slow path between back edges
5301 // of a same loop.
5302 SlowPathCode* slow_path_;
5303
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005304 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5305};
5306
David Srbecky0cf44932015-12-09 14:09:59 +00005307// Pseudo-instruction which provides the native debugger with mapping information.
5308// It ensures that we can generate line number and local variables at this point.
5309class HNativeDebugInfo : public HTemplateInstruction<0> {
5310 public:
5311 explicit HNativeDebugInfo(uint32_t dex_pc)
5312 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5313
5314 bool NeedsEnvironment() const OVERRIDE {
5315 return true;
5316 }
5317
5318 DECLARE_INSTRUCTION(NativeDebugInfo);
5319
5320 private:
5321 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5322};
5323
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005324/**
5325 * Instruction to load a Class object.
5326 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005327class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005328 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005329 HLoadClass(HCurrentMethod* current_method,
5330 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005331 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005332 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005333 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005334 bool needs_access_check,
5335 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005336 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005337 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005338 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005339 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005340 // Referrers class should not need access check. We never inline unverified
5341 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005342 DCHECK(!is_referrers_class || !needs_access_check);
5343
5344 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5345 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5346 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5347 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005348 SetRawInputAt(0, current_method);
5349 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005350
5351 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005352
5353 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005354 // Note that we don't need to test for generate_clinit_check_.
5355 // Whether or not we need to generate the clinit check is processed in
5356 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005357 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005358 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005359 }
5360
5361 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5362
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005363 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005364 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005365
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005366 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005367 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005368 }
5369
Calin Juravle0ba218d2015-05-19 18:46:01 +01005370 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005371 // The entrypoint the code generator is going to call does not do
5372 // clinit of the class.
5373 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005374 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005375 }
5376
5377 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005378 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005379 (!IsReferrersClass() && !IsInDexCache()) ||
5380 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005381 }
5382
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005383
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005384 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005385 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005386 }
5387
Calin Juravleacf735c2015-02-12 15:25:22 +00005388 ReferenceTypeInfo GetLoadedClassRTI() {
5389 return loaded_class_rti_;
5390 }
5391
5392 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5393 // Make sure we only set exact types (the loaded class should never be merged).
5394 DCHECK(rti.IsExact());
5395 loaded_class_rti_ = rti;
5396 }
5397
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005398 const DexFile& GetDexFile() { return dex_file_; }
5399
Vladimir Markoa1de9182016-02-25 11:37:38 +00005400 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005401
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005402 static SideEffects SideEffectsForArchRuntimeCalls() {
5403 return SideEffects::CanTriggerGC();
5404 }
5405
Vladimir Markoa1de9182016-02-25 11:37:38 +00005406 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5407 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5408 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5409 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005410
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005411 DECLARE_INSTRUCTION(LoadClass);
5412
5413 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005414 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5415 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5416 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005417 // Whether this instruction must generate the initialization check.
5418 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005419 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5420 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5421 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5422
5423 const uint16_t type_index_;
5424 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005425
Calin Juravleacf735c2015-02-12 15:25:22 +00005426 ReferenceTypeInfo loaded_class_rti_;
5427
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005428 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5429};
5430
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005431class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005432 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005433 // Determines how to load the String.
5434 enum class LoadKind {
5435 // Use boot image String* address that will be known at link time.
5436 // Used for boot image strings referenced by boot image code in non-PIC mode.
5437 kBootImageLinkTimeAddress,
5438
5439 // Use PC-relative boot image String* address that will be known at link time.
5440 // Used for boot image strings referenced by boot image code in PIC mode.
5441 kBootImageLinkTimePcRelative,
5442
5443 // Use a known boot image String* address, embedded in the code by the codegen.
5444 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5445 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5446 // GetIncludePatchInformation().
5447 kBootImageAddress,
5448
5449 // Load from the resolved strings array at an absolute address.
5450 // Used for strings outside the boot image referenced by JIT-compiled code.
5451 kDexCacheAddress,
5452
5453 // Load from resolved strings array in the dex cache using a PC-relative load.
5454 // Used for strings outside boot image when we know that we can access
5455 // the dex cache arrays using a PC-relative load.
5456 kDexCachePcRelative,
5457
5458 // Load from resolved strings array accessed through the class loaded from
5459 // the compiled method's own ArtMethod*. This is the default access type when
5460 // all other types are unavailable.
5461 kDexCacheViaMethod,
5462
5463 kLast = kDexCacheViaMethod
5464 };
5465
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005466 HLoadString(HCurrentMethod* current_method,
5467 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005468 const DexFile& dex_file,
5469 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005470 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005471 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005472 SetPackedFlag<kFlagIsInDexCache>(false);
5473 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5474 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005475 SetRawInputAt(0, current_method);
5476 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005477
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005478 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5479 DCHECK(HasAddress(load_kind));
5480 load_data_.address = address;
5481 SetLoadKindInternal(load_kind);
5482 }
5483
5484 void SetLoadKindWithStringReference(LoadKind load_kind,
5485 const DexFile& dex_file,
5486 uint32_t string_index) {
5487 DCHECK(HasStringReference(load_kind));
5488 load_data_.ref.dex_file = &dex_file;
5489 string_index_ = string_index;
5490 SetLoadKindInternal(load_kind);
5491 }
5492
5493 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5494 const DexFile& dex_file,
5495 uint32_t element_index) {
5496 DCHECK(HasDexCacheReference(load_kind));
5497 load_data_.ref.dex_file = &dex_file;
5498 load_data_.ref.dex_cache_element_index = element_index;
5499 SetLoadKindInternal(load_kind);
5500 }
5501
5502 LoadKind GetLoadKind() const {
5503 return GetPackedField<LoadKindField>();
5504 }
5505
5506 const DexFile& GetDexFile() const;
5507
5508 uint32_t GetStringIndex() const {
5509 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5510 return string_index_;
5511 }
5512
5513 uint32_t GetDexCacheElementOffset() const;
5514
5515 uint64_t GetAddress() const {
5516 DCHECK(HasAddress(GetLoadKind()));
5517 return load_data_.address;
5518 }
5519
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005520 bool CanBeMoved() const OVERRIDE { return true; }
5521
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005522 bool InstructionDataEquals(HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005523
5524 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5525
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005526 // Will call the runtime if we need to load the string through
5527 // the dex cache and the string is not guaranteed to be there yet.
5528 bool NeedsEnvironment() const OVERRIDE {
5529 LoadKind load_kind = GetLoadKind();
5530 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5531 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5532 load_kind == LoadKind::kBootImageAddress) {
5533 return false;
5534 }
5535 return !IsInDexCache();
5536 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005537
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005538 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5539 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5540 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005541
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005542 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005543 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005544
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005545 static SideEffects SideEffectsForArchRuntimeCalls() {
5546 return SideEffects::CanTriggerGC();
5547 }
5548
Vladimir Markoa1de9182016-02-25 11:37:38 +00005549 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5550
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005551 void MarkInDexCache() {
5552 SetPackedFlag<kFlagIsInDexCache>(true);
5553 DCHECK(!NeedsEnvironment());
5554 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005555 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005556 }
5557
5558 size_t InputCount() const OVERRIDE {
5559 return (InputAt(0) != nullptr) ? 1u : 0u;
5560 }
5561
5562 void AddSpecialInput(HInstruction* special_input);
5563
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005564 DECLARE_INSTRUCTION(LoadString);
5565
5566 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005567 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005568 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5569 static constexpr size_t kFieldLoadKindSize =
5570 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5571 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005572 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005573 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005574
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005575 static bool HasStringReference(LoadKind load_kind) {
5576 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5577 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5578 load_kind == LoadKind::kDexCacheViaMethod;
5579 }
5580
5581 static bool HasAddress(LoadKind load_kind) {
5582 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5583 }
5584
5585 static bool HasDexCacheReference(LoadKind load_kind) {
5586 return load_kind == LoadKind::kDexCachePcRelative;
5587 }
5588
5589 void SetLoadKindInternal(LoadKind load_kind);
5590
5591 // String index serves also as the hash code and it's also needed for slow-paths,
5592 // so it must not be overwritten with other load data.
5593 uint32_t string_index_;
5594
5595 union {
5596 struct {
5597 const DexFile* dex_file; // For string reference and dex cache reference.
5598 uint32_t dex_cache_element_index; // Only for dex cache reference.
5599 } ref;
5600 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5601 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005602
5603 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5604};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005605std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5606
5607// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5608inline const DexFile& HLoadString::GetDexFile() const {
5609 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5610 << GetLoadKind();
5611 return *load_data_.ref.dex_file;
5612}
5613
5614// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5615inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5616 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5617 return load_data_.ref.dex_cache_element_index;
5618}
5619
5620// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5621inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5622 // The special input is used for PC-relative loads on some architectures.
5623 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5624 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
5625 DCHECK(InputAt(0) == nullptr);
5626 SetRawInputAt(0u, special_input);
5627 special_input->AddUseAt(this, 0);
5628}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005629
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005630/**
5631 * Performs an initialization check on its Class object input.
5632 */
5633class HClinitCheck : public HExpression<1> {
5634 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005635 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005636 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005637 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005638 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5639 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005640 SetRawInputAt(0, constant);
5641 }
5642
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005643 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005644 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005645 return true;
5646 }
5647
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005648 bool NeedsEnvironment() const OVERRIDE {
5649 // May call runtime to initialize the class.
5650 return true;
5651 }
5652
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005653 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005654
5655 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5656
5657 DECLARE_INSTRUCTION(ClinitCheck);
5658
5659 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005660 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5661};
5662
5663class HStaticFieldGet : public HExpression<1> {
5664 public:
5665 HStaticFieldGet(HInstruction* cls,
5666 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005667 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005668 bool is_volatile,
5669 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005670 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005671 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005672 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005673 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005674 : HExpression(field_type,
5675 SideEffects::FieldReadOfType(field_type, is_volatile),
5676 dex_pc),
5677 field_info_(field_offset,
5678 field_type,
5679 is_volatile,
5680 field_idx,
5681 declaring_class_def_index,
5682 dex_file,
5683 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005684 SetRawInputAt(0, cls);
5685 }
5686
Calin Juravle52c48962014-12-16 17:02:57 +00005687
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005688 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005689
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005690 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005691 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5692 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005693 }
5694
5695 size_t ComputeHashCode() const OVERRIDE {
5696 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5697 }
5698
Calin Juravle52c48962014-12-16 17:02:57 +00005699 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005700 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5701 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005702 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005703
5704 DECLARE_INSTRUCTION(StaticFieldGet);
5705
5706 private:
5707 const FieldInfo field_info_;
5708
5709 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5710};
5711
5712class HStaticFieldSet : public HTemplateInstruction<2> {
5713 public:
5714 HStaticFieldSet(HInstruction* cls,
5715 HInstruction* value,
5716 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005717 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005718 bool is_volatile,
5719 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005720 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005721 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005722 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005723 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005724 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5725 dex_pc),
5726 field_info_(field_offset,
5727 field_type,
5728 is_volatile,
5729 field_idx,
5730 declaring_class_def_index,
5731 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005732 dex_cache) {
5733 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005734 SetRawInputAt(0, cls);
5735 SetRawInputAt(1, value);
5736 }
5737
Calin Juravle52c48962014-12-16 17:02:57 +00005738 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005739 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5740 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005741 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005742
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005743 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005744 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5745 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005746
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005747 DECLARE_INSTRUCTION(StaticFieldSet);
5748
5749 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005750 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5751 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5752 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5753 "Too many packed fields.");
5754
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005755 const FieldInfo field_info_;
5756
5757 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5758};
5759
Calin Juravlee460d1d2015-09-29 04:52:17 +01005760class HUnresolvedInstanceFieldGet : public HExpression<1> {
5761 public:
5762 HUnresolvedInstanceFieldGet(HInstruction* obj,
5763 Primitive::Type field_type,
5764 uint32_t field_index,
5765 uint32_t dex_pc)
5766 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5767 field_index_(field_index) {
5768 SetRawInputAt(0, obj);
5769 }
5770
5771 bool NeedsEnvironment() const OVERRIDE { return true; }
5772 bool CanThrow() const OVERRIDE { return true; }
5773
5774 Primitive::Type GetFieldType() const { return GetType(); }
5775 uint32_t GetFieldIndex() const { return field_index_; }
5776
5777 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5778
5779 private:
5780 const uint32_t field_index_;
5781
5782 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5783};
5784
5785class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5786 public:
5787 HUnresolvedInstanceFieldSet(HInstruction* obj,
5788 HInstruction* value,
5789 Primitive::Type field_type,
5790 uint32_t field_index,
5791 uint32_t dex_pc)
5792 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005793 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005794 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005795 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005796 SetRawInputAt(0, obj);
5797 SetRawInputAt(1, value);
5798 }
5799
5800 bool NeedsEnvironment() const OVERRIDE { return true; }
5801 bool CanThrow() const OVERRIDE { return true; }
5802
Vladimir Markoa1de9182016-02-25 11:37:38 +00005803 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005804 uint32_t GetFieldIndex() const { return field_index_; }
5805
5806 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5807
5808 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005809 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5810 static constexpr size_t kFieldFieldTypeSize =
5811 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5812 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5813 kFieldFieldType + kFieldFieldTypeSize;
5814 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5815 "Too many packed fields.");
5816 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5817
Calin Juravlee460d1d2015-09-29 04:52:17 +01005818 const uint32_t field_index_;
5819
5820 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5821};
5822
5823class HUnresolvedStaticFieldGet : public HExpression<0> {
5824 public:
5825 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5826 uint32_t field_index,
5827 uint32_t dex_pc)
5828 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5829 field_index_(field_index) {
5830 }
5831
5832 bool NeedsEnvironment() const OVERRIDE { return true; }
5833 bool CanThrow() const OVERRIDE { return true; }
5834
5835 Primitive::Type GetFieldType() const { return GetType(); }
5836 uint32_t GetFieldIndex() const { return field_index_; }
5837
5838 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5839
5840 private:
5841 const uint32_t field_index_;
5842
5843 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5844};
5845
5846class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5847 public:
5848 HUnresolvedStaticFieldSet(HInstruction* value,
5849 Primitive::Type field_type,
5850 uint32_t field_index,
5851 uint32_t dex_pc)
5852 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005853 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005854 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005855 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005856 SetRawInputAt(0, value);
5857 }
5858
5859 bool NeedsEnvironment() const OVERRIDE { return true; }
5860 bool CanThrow() const OVERRIDE { return true; }
5861
Vladimir Markoa1de9182016-02-25 11:37:38 +00005862 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005863 uint32_t GetFieldIndex() const { return field_index_; }
5864
5865 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5866
5867 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005868 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5869 static constexpr size_t kFieldFieldTypeSize =
5870 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5871 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5872 kFieldFieldType + kFieldFieldTypeSize;
5873 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5874 "Too many packed fields.");
5875 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5876
Calin Juravlee460d1d2015-09-29 04:52:17 +01005877 const uint32_t field_index_;
5878
5879 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5880};
5881
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005882// Implement the move-exception DEX instruction.
5883class HLoadException : public HExpression<0> {
5884 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005885 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5886 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005887
David Brazdilbbd733e2015-08-18 17:48:17 +01005888 bool CanBeNull() const OVERRIDE { return false; }
5889
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005890 DECLARE_INSTRUCTION(LoadException);
5891
5892 private:
5893 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5894};
5895
David Brazdilcb1c0552015-08-04 16:22:25 +01005896// Implicit part of move-exception which clears thread-local exception storage.
5897// Must not be removed because the runtime expects the TLS to get cleared.
5898class HClearException : public HTemplateInstruction<0> {
5899 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005900 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5901 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005902
5903 DECLARE_INSTRUCTION(ClearException);
5904
5905 private:
5906 DISALLOW_COPY_AND_ASSIGN(HClearException);
5907};
5908
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005909class HThrow : public HTemplateInstruction<1> {
5910 public:
5911 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005912 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005913 SetRawInputAt(0, exception);
5914 }
5915
5916 bool IsControlFlow() const OVERRIDE { return true; }
5917
5918 bool NeedsEnvironment() const OVERRIDE { return true; }
5919
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005920 bool CanThrow() const OVERRIDE { return true; }
5921
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005922
5923 DECLARE_INSTRUCTION(Throw);
5924
5925 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005926 DISALLOW_COPY_AND_ASSIGN(HThrow);
5927};
5928
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005929/**
5930 * Implementation strategies for the code generator of a HInstanceOf
5931 * or `HCheckCast`.
5932 */
5933enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005934 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005935 kExactCheck, // Can do a single class compare.
5936 kClassHierarchyCheck, // Can just walk the super class chain.
5937 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5938 kInterfaceCheck, // No optimization yet when checking against an interface.
5939 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005940 kArrayCheck, // No optimization yet when checking against a generic array.
5941 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005942};
5943
Roland Levillain86503782016-02-11 19:07:30 +00005944std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5945
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005946class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005947 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005948 HInstanceOf(HInstruction* object,
5949 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005950 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005951 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005952 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005953 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005954 dex_pc) {
5955 SetPackedField<TypeCheckKindField>(check_kind);
5956 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005957 SetRawInputAt(0, object);
5958 SetRawInputAt(1, constant);
5959 }
5960
5961 bool CanBeMoved() const OVERRIDE { return true; }
5962
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005963 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005964 return true;
5965 }
5966
5967 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005968 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005969 }
5970
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005971 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005972 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5973 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5974 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5975 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005976
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005977 static bool CanCallRuntime(TypeCheckKind check_kind) {
5978 // Mips currently does runtime calls for any other checks.
5979 return check_kind != TypeCheckKind::kExactCheck;
5980 }
5981
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005982 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005983 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005984 }
5985
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005986 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005987
5988 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005989 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
5990 static constexpr size_t kFieldTypeCheckKindSize =
5991 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
5992 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
5993 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
5994 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5995 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005996
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005997 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5998};
5999
Calin Juravleb1498f62015-02-16 13:13:29 +00006000class HBoundType : public HExpression<1> {
6001 public:
David Brazdilf5552582015-12-27 13:36:12 +00006002 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006003 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006004 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6005 SetPackedFlag<kFlagUpperCanBeNull>(true);
6006 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006007 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006008 SetRawInputAt(0, input);
6009 }
6010
David Brazdilf5552582015-12-27 13:36:12 +00006011 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006012 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006013 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006014 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006015
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006016 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006017 DCHECK(GetUpperCanBeNull() || !can_be_null);
6018 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006019 }
6020
Vladimir Markoa1de9182016-02-25 11:37:38 +00006021 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006022
Calin Juravleb1498f62015-02-16 13:13:29 +00006023 DECLARE_INSTRUCTION(BoundType);
6024
6025 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006026 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6027 // is false then CanBeNull() cannot be true).
6028 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6029 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6030 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6031 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6032
Calin Juravleb1498f62015-02-16 13:13:29 +00006033 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006034 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6035 // It is used to bound the type in cases like:
6036 // if (x instanceof ClassX) {
6037 // // uper_bound_ will be ClassX
6038 // }
David Brazdilf5552582015-12-27 13:36:12 +00006039 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006040
6041 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6042};
6043
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006044class HCheckCast : public HTemplateInstruction<2> {
6045 public:
6046 HCheckCast(HInstruction* object,
6047 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006048 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006049 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006050 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6051 SetPackedField<TypeCheckKindField>(check_kind);
6052 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006053 SetRawInputAt(0, object);
6054 SetRawInputAt(1, constant);
6055 }
6056
6057 bool CanBeMoved() const OVERRIDE { return true; }
6058
6059 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6060 return true;
6061 }
6062
6063 bool NeedsEnvironment() const OVERRIDE {
6064 // Instruction may throw a CheckCastError.
6065 return true;
6066 }
6067
6068 bool CanThrow() const OVERRIDE { return true; }
6069
Vladimir Markoa1de9182016-02-25 11:37:38 +00006070 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6071 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6072 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6073 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006074
6075 DECLARE_INSTRUCTION(CheckCast);
6076
6077 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006078 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6079 static constexpr size_t kFieldTypeCheckKindSize =
6080 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6081 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6082 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6083 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6084 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006085
6086 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006087};
6088
Calin Juravle27df7582015-04-17 19:12:31 +01006089class HMemoryBarrier : public HTemplateInstruction<0> {
6090 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006091 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006092 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006093 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6094 SetPackedField<BarrierKindField>(barrier_kind);
6095 }
Calin Juravle27df7582015-04-17 19:12:31 +01006096
Vladimir Markoa1de9182016-02-25 11:37:38 +00006097 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006098
6099 DECLARE_INSTRUCTION(MemoryBarrier);
6100
6101 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006102 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6103 static constexpr size_t kFieldBarrierKindSize =
6104 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6105 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6106 kFieldBarrierKind + kFieldBarrierKindSize;
6107 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6108 "Too many packed fields.");
6109 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006110
6111 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6112};
6113
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006114class HMonitorOperation : public HTemplateInstruction<1> {
6115 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006116 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006117 kEnter,
6118 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006119 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006120 };
6121
6122 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006123 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006124 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6125 dex_pc) {
6126 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006127 SetRawInputAt(0, object);
6128 }
6129
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006130 // Instruction may go into runtime, so we need an environment.
6131 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006132
6133 bool CanThrow() const OVERRIDE {
6134 // Verifier guarantees that monitor-exit cannot throw.
6135 // This is important because it allows the HGraphBuilder to remove
6136 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6137 return IsEnter();
6138 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006139
Vladimir Markoa1de9182016-02-25 11:37:38 +00006140 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6141 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006142
6143 DECLARE_INSTRUCTION(MonitorOperation);
6144
Calin Juravle52c48962014-12-16 17:02:57 +00006145 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006146 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6147 static constexpr size_t kFieldOperationKindSize =
6148 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6149 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6150 kFieldOperationKind + kFieldOperationKindSize;
6151 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6152 "Too many packed fields.");
6153 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006154
6155 private:
6156 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6157};
6158
David Brazdil74eb1b22015-12-14 11:44:01 +00006159class HSelect : public HExpression<3> {
6160 public:
6161 HSelect(HInstruction* condition,
6162 HInstruction* true_value,
6163 HInstruction* false_value,
6164 uint32_t dex_pc)
6165 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6166 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6167
6168 // First input must be `true_value` or `false_value` to allow codegens to
6169 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6170 // that architectures which implement HSelect as a conditional move also
6171 // will not need to invert the condition.
6172 SetRawInputAt(0, false_value);
6173 SetRawInputAt(1, true_value);
6174 SetRawInputAt(2, condition);
6175 }
6176
6177 HInstruction* GetFalseValue() const { return InputAt(0); }
6178 HInstruction* GetTrueValue() const { return InputAt(1); }
6179 HInstruction* GetCondition() const { return InputAt(2); }
6180
6181 bool CanBeMoved() const OVERRIDE { return true; }
6182 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6183
6184 bool CanBeNull() const OVERRIDE {
6185 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6186 }
6187
6188 DECLARE_INSTRUCTION(Select);
6189
6190 private:
6191 DISALLOW_COPY_AND_ASSIGN(HSelect);
6192};
6193
Vladimir Markof9f64412015-09-02 14:05:49 +01006194class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006195 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006196 MoveOperands(Location source,
6197 Location destination,
6198 Primitive::Type type,
6199 HInstruction* instruction)
6200 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006201
6202 Location GetSource() const { return source_; }
6203 Location GetDestination() const { return destination_; }
6204
6205 void SetSource(Location value) { source_ = value; }
6206 void SetDestination(Location value) { destination_ = value; }
6207
6208 // The parallel move resolver marks moves as "in-progress" by clearing the
6209 // destination (but not the source).
6210 Location MarkPending() {
6211 DCHECK(!IsPending());
6212 Location dest = destination_;
6213 destination_ = Location::NoLocation();
6214 return dest;
6215 }
6216
6217 void ClearPending(Location dest) {
6218 DCHECK(IsPending());
6219 destination_ = dest;
6220 }
6221
6222 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006223 DCHECK(source_.IsValid() || destination_.IsInvalid());
6224 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006225 }
6226
6227 // True if this blocks a move from the given location.
6228 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006229 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006230 }
6231
6232 // A move is redundant if it's been eliminated, if its source and
6233 // destination are the same, or if its destination is unneeded.
6234 bool IsRedundant() const {
6235 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6236 }
6237
6238 // We clear both operands to indicate move that's been eliminated.
6239 void Eliminate() {
6240 source_ = destination_ = Location::NoLocation();
6241 }
6242
6243 bool IsEliminated() const {
6244 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6245 return source_.IsInvalid();
6246 }
6247
Alexey Frunze4dda3372015-06-01 18:31:49 -07006248 Primitive::Type GetType() const { return type_; }
6249
Nicolas Geoffray90218252015-04-15 11:56:51 +01006250 bool Is64BitMove() const {
6251 return Primitive::Is64BitType(type_);
6252 }
6253
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006254 HInstruction* GetInstruction() const { return instruction_; }
6255
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006256 private:
6257 Location source_;
6258 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006259 // The type this move is for.
6260 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006261 // The instruction this move is assocatied with. Null when this move is
6262 // for moving an input in the expected locations of user (including a phi user).
6263 // This is only used in debug mode, to ensure we do not connect interval siblings
6264 // in the same parallel move.
6265 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006266};
6267
Roland Levillainc9285912015-12-18 10:38:42 +00006268std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6269
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006270static constexpr size_t kDefaultNumberOfMoves = 4;
6271
6272class HParallelMove : public HTemplateInstruction<0> {
6273 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006274 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006275 : HTemplateInstruction(SideEffects::None(), dex_pc),
6276 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6277 moves_.reserve(kDefaultNumberOfMoves);
6278 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006279
Nicolas Geoffray90218252015-04-15 11:56:51 +01006280 void AddMove(Location source,
6281 Location destination,
6282 Primitive::Type type,
6283 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006284 DCHECK(source.IsValid());
6285 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006286 if (kIsDebugBuild) {
6287 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006288 for (const MoveOperands& move : moves_) {
6289 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006290 // Special case the situation where the move is for the spill slot
6291 // of the instruction.
6292 if ((GetPrevious() == instruction)
6293 || ((GetPrevious() == nullptr)
6294 && instruction->IsPhi()
6295 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006296 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006297 << "Doing parallel moves for the same instruction.";
6298 } else {
6299 DCHECK(false) << "Doing parallel moves for the same instruction.";
6300 }
6301 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006302 }
6303 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006304 for (const MoveOperands& move : moves_) {
6305 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006306 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006307 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006308 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006309 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006310 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006311 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006312 }
6313
Vladimir Marko225b6462015-09-28 12:17:40 +01006314 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006315 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006316 }
6317
Vladimir Marko225b6462015-09-28 12:17:40 +01006318 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006319
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006320 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006321
6322 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006323 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006324
6325 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6326};
6327
Mark Mendell0616ae02015-04-17 12:49:27 -04006328} // namespace art
6329
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006330#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6331#include "nodes_shared.h"
6332#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006333#ifdef ART_ENABLE_CODEGEN_arm
6334#include "nodes_arm.h"
6335#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006336#ifdef ART_ENABLE_CODEGEN_arm64
6337#include "nodes_arm64.h"
6338#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006339#ifdef ART_ENABLE_CODEGEN_x86
6340#include "nodes_x86.h"
6341#endif
6342
6343namespace art {
6344
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006345class HGraphVisitor : public ValueObject {
6346 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006347 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6348 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006349
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006350 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006351 virtual void VisitBasicBlock(HBasicBlock* block);
6352
Roland Levillain633021e2014-10-01 14:12:25 +01006353 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006354 void VisitInsertionOrder();
6355
Roland Levillain633021e2014-10-01 14:12:25 +01006356 // Visit the graph following dominator tree reverse post-order.
6357 void VisitReversePostOrder();
6358
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006359 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006360
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006361 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006362#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006363 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6364
6365 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6366
6367#undef DECLARE_VISIT_INSTRUCTION
6368
6369 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006370 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006371
6372 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6373};
6374
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006375class HGraphDelegateVisitor : public HGraphVisitor {
6376 public:
6377 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6378 virtual ~HGraphDelegateVisitor() {}
6379
6380 // Visit functions that delegate to to super class.
6381#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006382 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006383
6384 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6385
6386#undef DECLARE_VISIT_INSTRUCTION
6387
6388 private:
6389 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6390};
6391
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006392class HInsertionOrderIterator : public ValueObject {
6393 public:
6394 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6395
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006396 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006397 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006398 void Advance() { ++index_; }
6399
6400 private:
6401 const HGraph& graph_;
6402 size_t index_;
6403
6404 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6405};
6406
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006407class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006408 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006409 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6410 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006411 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006412 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006413
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006414 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6415 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006416 void Advance() { ++index_; }
6417
6418 private:
6419 const HGraph& graph_;
6420 size_t index_;
6421
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006422 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006423};
6424
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006425class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006426 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006427 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006428 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006429 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006430 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006431 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006432
6433 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006434 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006435 void Advance() { --index_; }
6436
6437 private:
6438 const HGraph& graph_;
6439 size_t index_;
6440
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006441 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006442};
6443
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006444class HLinearPostOrderIterator : public ValueObject {
6445 public:
6446 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006447 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006448
6449 bool Done() const { return index_ == 0; }
6450
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006451 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006452
6453 void Advance() {
6454 --index_;
6455 DCHECK_GE(index_, 0U);
6456 }
6457
6458 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006459 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006460 size_t index_;
6461
6462 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6463};
6464
6465class HLinearOrderIterator : public ValueObject {
6466 public:
6467 explicit HLinearOrderIterator(const HGraph& graph)
6468 : order_(graph.GetLinearOrder()), index_(0) {}
6469
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006470 bool Done() const { return index_ == order_.size(); }
6471 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006472 void Advance() { ++index_; }
6473
6474 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006475 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006476 size_t index_;
6477
6478 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6479};
6480
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006481// Iterator over the blocks that art part of the loop. Includes blocks part
6482// of an inner loop. The order in which the blocks are iterated is on their
6483// block id.
6484class HBlocksInLoopIterator : public ValueObject {
6485 public:
6486 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6487 : blocks_in_loop_(info.GetBlocks()),
6488 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6489 index_(0) {
6490 if (!blocks_in_loop_.IsBitSet(index_)) {
6491 Advance();
6492 }
6493 }
6494
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006495 bool Done() const { return index_ == blocks_.size(); }
6496 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006497 void Advance() {
6498 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006499 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006500 if (blocks_in_loop_.IsBitSet(index_)) {
6501 break;
6502 }
6503 }
6504 }
6505
6506 private:
6507 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006508 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006509 size_t index_;
6510
6511 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6512};
6513
Mingyao Yang3584bce2015-05-19 16:01:59 -07006514// Iterator over the blocks that art part of the loop. Includes blocks part
6515// of an inner loop. The order in which the blocks are iterated is reverse
6516// post order.
6517class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6518 public:
6519 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6520 : blocks_in_loop_(info.GetBlocks()),
6521 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6522 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006523 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006524 Advance();
6525 }
6526 }
6527
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006528 bool Done() const { return index_ == blocks_.size(); }
6529 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006530 void Advance() {
6531 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006532 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6533 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006534 break;
6535 }
6536 }
6537 }
6538
6539 private:
6540 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006541 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006542 size_t index_;
6543
6544 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6545};
6546
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006547inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006548 if (constant->IsIntConstant()) {
6549 return constant->AsIntConstant()->GetValue();
6550 } else if (constant->IsLongConstant()) {
6551 return constant->AsLongConstant()->GetValue();
6552 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006553 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006554 return 0;
6555 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006556}
6557
Vladimir Marko58155012015-08-19 12:49:41 +00006558inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6559 // For the purposes of the compiler, the dex files must actually be the same object
6560 // if we want to safely treat them as the same. This is especially important for JIT
6561 // as custom class loaders can open the same underlying file (or memory) multiple
6562 // times and provide different class resolution but no two class loaders should ever
6563 // use the same DexFile object - doing so is an unsupported hack that can lead to
6564 // all sorts of weird failures.
6565 return &lhs == &rhs;
6566}
6567
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006568#define INSTRUCTION_TYPE_CHECK(type, super) \
6569 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6570 inline const H##type* HInstruction::As##type() const { \
6571 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6572 } \
6573 inline H##type* HInstruction::As##type() { \
6574 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6575 }
6576
6577 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6578#undef INSTRUCTION_TYPE_CHECK
6579
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006580// Create space in `blocks` for adding `number_of_new_blocks` entries
6581// starting at location `at`. Blocks after `at` are moved accordingly.
6582inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6583 size_t number_of_new_blocks,
6584 size_t after) {
6585 DCHECK_LT(after, blocks->size());
6586 size_t old_size = blocks->size();
6587 size_t new_size = old_size + number_of_new_blocks;
6588 blocks->resize(new_size);
6589 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6590}
6591
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006592} // namespace art
6593
6594#endif // ART_COMPILER_OPTIMIZING_NODES_H_