blob: afb995d43899da5b78289968b83dd3d270406be3 [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),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100653 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100654 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000655 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100656 back_edges_.reserve(kDefaultNumberOfBackEdges);
657 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100658
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000659 bool IsIrreducible() const { return irreducible_; }
660
661 void Dump(std::ostream& os);
662
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100663 HBasicBlock* GetHeader() const {
664 return header_;
665 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000666
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000667 void SetHeader(HBasicBlock* block) {
668 header_ = block;
669 }
670
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100671 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
672 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
673 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
674
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000675 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100676 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000677 }
678
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100679 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100680 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100681 }
682
David Brazdil46e2a392015-03-16 17:31:52 +0000683 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100684 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100685 }
686
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000687 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100688 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000689 }
690
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100691 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100692
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100693 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100694 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100695 }
696
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100697 // Returns the lifetime position of the back edge that has the
698 // greatest lifetime position.
699 size_t GetLifetimeEnd() const;
700
701 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100702 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100703 }
704
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000705 // Finds blocks that are part of this loop.
706 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100707
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100708 // Returns whether this loop information contains `block`.
709 // Note that this loop information *must* be populated before entering this function.
710 bool Contains(const HBasicBlock& block) const;
711
712 // Returns whether this loop information is an inner loop of `other`.
713 // Note that `other` *must* be populated before entering this function.
714 bool IsIn(const HLoopInformation& other) const;
715
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800716 // Returns true if instruction is not defined within this loop.
717 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700718
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100719 const ArenaBitVector& GetBlocks() const { return blocks_; }
720
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000721 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000722 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000723
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000724 void ClearAllBlocks() {
725 blocks_.ClearAllBits();
726 }
727
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000728 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100729 // Internal recursive implementation of `Populate`.
730 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100731 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100732
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000733 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100734 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000735 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100736 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100737 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000738
739 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
740};
741
David Brazdilec16f792015-08-19 15:04:01 +0100742// Stores try/catch information for basic blocks.
743// Note that HGraph is constructed so that catch blocks cannot simultaneously
744// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100745class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100746 public:
747 // Try block information constructor.
748 explicit TryCatchInformation(const HTryBoundary& try_entry)
749 : try_entry_(&try_entry),
750 catch_dex_file_(nullptr),
751 catch_type_index_(DexFile::kDexNoIndex16) {
752 DCHECK(try_entry_ != nullptr);
753 }
754
755 // Catch block information constructor.
756 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
757 : try_entry_(nullptr),
758 catch_dex_file_(&dex_file),
759 catch_type_index_(catch_type_index) {}
760
761 bool IsTryBlock() const { return try_entry_ != nullptr; }
762
763 const HTryBoundary& GetTryEntry() const {
764 DCHECK(IsTryBlock());
765 return *try_entry_;
766 }
767
768 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
769
770 bool IsCatchAllTypeIndex() const {
771 DCHECK(IsCatchBlock());
772 return catch_type_index_ == DexFile::kDexNoIndex16;
773 }
774
775 uint16_t GetCatchTypeIndex() const {
776 DCHECK(IsCatchBlock());
777 return catch_type_index_;
778 }
779
780 const DexFile& GetCatchDexFile() const {
781 DCHECK(IsCatchBlock());
782 return *catch_dex_file_;
783 }
784
785 private:
786 // One of possibly several TryBoundary instructions entering the block's try.
787 // Only set for try blocks.
788 const HTryBoundary* try_entry_;
789
790 // Exception type information. Only set for catch blocks.
791 const DexFile* catch_dex_file_;
792 const uint16_t catch_type_index_;
793};
794
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100795static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100796static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100797
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000798// A block in a method. Contains the list of instructions represented
799// as a double linked list. Each block knows its predecessors and
800// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100801
Vladimir Markof9f64412015-09-02 14:05:49 +0100802class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000803 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600804 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000805 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000806 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
807 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000808 loop_information_(nullptr),
809 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000810 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100811 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100812 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100813 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000814 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000815 try_catch_information_(nullptr) {
816 predecessors_.reserve(kDefaultNumberOfPredecessors);
817 successors_.reserve(kDefaultNumberOfSuccessors);
818 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
819 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000820
Vladimir Marko60584552015-09-03 13:35:12 +0000821 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100822 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000823 }
824
Vladimir Marko60584552015-09-03 13:35:12 +0000825 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100826 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000827 }
828
David Brazdild26a4112015-11-10 11:07:31 +0000829 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
830 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
831
Vladimir Marko60584552015-09-03 13:35:12 +0000832 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
833 return ContainsElement(successors_, block, start_from);
834 }
835
836 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100837 return dominated_blocks_;
838 }
839
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100840 bool IsEntryBlock() const {
841 return graph_->GetEntryBlock() == this;
842 }
843
844 bool IsExitBlock() const {
845 return graph_->GetExitBlock() == this;
846 }
847
David Brazdil46e2a392015-03-16 17:31:52 +0000848 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000849 bool IsSingleTryBoundary() const;
850
851 // Returns true if this block emits nothing but a jump.
852 bool IsSingleJump() const {
853 HLoopInformation* loop_info = GetLoopInformation();
854 return (IsSingleGoto() || IsSingleTryBoundary())
855 // Back edges generate a suspend check.
856 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
857 }
David Brazdil46e2a392015-03-16 17:31:52 +0000858
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000859 void AddBackEdge(HBasicBlock* back_edge) {
860 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000861 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000862 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100863 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000864 loop_information_->AddBackEdge(back_edge);
865 }
866
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000867 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000868 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000869
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100870 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000871 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600872 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000873
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000874 HBasicBlock* GetDominator() const { return dominator_; }
875 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000876 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
877
878 void RemoveDominatedBlock(HBasicBlock* block) {
879 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100880 }
Vladimir Marko60584552015-09-03 13:35:12 +0000881
882 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
883 ReplaceElement(dominated_blocks_, existing, new_block);
884 }
885
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100886 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000887
888 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100889 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000890 }
891
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100892 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
893 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100894 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100895 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100896 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
897 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000898
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000899 HInstruction* GetFirstInstructionDisregardMoves() const;
900
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000901 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000902 successors_.push_back(block);
903 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000904 }
905
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100906 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
907 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100908 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000909 new_block->predecessors_.push_back(this);
910 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000911 }
912
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000913 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
914 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000915 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000916 new_block->successors_.push_back(this);
917 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000918 }
919
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100920 // Insert `this` between `predecessor` and `successor. This method
921 // preserves the indicies, and will update the first edge found between
922 // `predecessor` and `successor`.
923 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
924 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100925 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000926 successor->predecessors_[predecessor_index] = this;
927 predecessor->successors_[successor_index] = this;
928 successors_.push_back(successor);
929 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100930 }
931
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100932 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000933 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100934 }
935
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000936 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000937 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000938 }
939
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100940 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000941 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100942 }
943
944 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000945 predecessors_.push_back(block);
946 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100947 }
948
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100949 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000950 DCHECK_EQ(predecessors_.size(), 2u);
951 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100952 }
953
David Brazdil769c9e52015-04-27 13:54:09 +0100954 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000955 DCHECK_EQ(successors_.size(), 2u);
956 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100957 }
958
David Brazdilfc6a86a2015-06-26 10:33:45 +0000959 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000960 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100961 }
962
David Brazdilfc6a86a2015-06-26 10:33:45 +0000963 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000964 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100965 }
966
David Brazdilfc6a86a2015-06-26 10:33:45 +0000967 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000968 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100969 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000970 }
971
972 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000973 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100974 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000975 }
976
977 // Returns whether the first occurrence of `predecessor` in the list of
978 // predecessors is at index `idx`.
979 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100980 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000981 return GetPredecessorIndexOf(predecessor) == idx;
982 }
983
David Brazdild7558da2015-09-22 13:04:14 +0100984 // Create a new block between this block and its predecessors. The new block
985 // is added to the graph, all predecessor edges are relinked to it and an edge
986 // is created to `this`. Returns the new empty block. Reverse post order or
987 // loop and try/catch information are not updated.
988 HBasicBlock* CreateImmediateDominator();
989
David Brazdilfc6a86a2015-06-26 10:33:45 +0000990 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100991 // created, latter block. Note that this method will add the block to the
992 // graph, create a Goto at the end of the former block and will create an edge
993 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100994 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000995 HBasicBlock* SplitBefore(HInstruction* cursor);
996
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000997 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000998 // created block. Note that this method just updates raw block information,
999 // like predecessors, successors, dominators, and instruction list. It does not
1000 // update the graph, reverse post order, loop information, nor make sure the
1001 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001002 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1003
1004 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1005 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001006
1007 // Merge `other` at the end of `this`. Successors and dominated blocks of
1008 // `other` are changed to be successors and dominated blocks of `this`. Note
1009 // that this method does not update the graph, reverse post order, loop
1010 // information, nor make sure the blocks are consistent (for example ending
1011 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001012 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001013
1014 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1015 // of `this` are moved to `other`.
1016 // Note that this method does not update the graph, reverse post order, loop
1017 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001018 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001019 void ReplaceWith(HBasicBlock* other);
1020
David Brazdil2d7352b2015-04-20 14:52:42 +01001021 // Merge `other` at the end of `this`. This method updates loops, reverse post
1022 // order, links to predecessors, successors, dominators and deletes the block
1023 // from the graph. The two blocks must be successive, i.e. `this` the only
1024 // predecessor of `other` and vice versa.
1025 void MergeWith(HBasicBlock* other);
1026
1027 // Disconnects `this` from all its predecessors, successors and dominator,
1028 // removes it from all loops it is included in and eventually from the graph.
1029 // The block must not dominate any other block. Predecessors and successors
1030 // are safely updated.
1031 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001032
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001033 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001034 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001035 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001036 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001037 // Replace instruction `initial` with `replacement` within this block.
1038 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1039 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001040 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001041 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001042 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001043 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1044 // instruction list. With 'ensure_safety' set to true, it verifies that the
1045 // instruction is not in use and removes it from the use lists of its inputs.
1046 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1047 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001048 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001049
1050 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001051 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001052 }
1053
Roland Levillain6b879dd2014-09-22 17:13:44 +01001054 bool IsLoopPreHeaderFirstPredecessor() const {
1055 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001056 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001057 }
1058
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001059 bool IsFirstPredecessorBackEdge() const {
1060 DCHECK(IsLoopHeader());
1061 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1062 }
1063
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001064 HLoopInformation* GetLoopInformation() const {
1065 return loop_information_;
1066 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001067
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001068 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001069 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001070 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001071 void SetInLoop(HLoopInformation* info) {
1072 if (IsLoopHeader()) {
1073 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001074 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001075 loop_information_ = info;
1076 } else if (loop_information_->Contains(*info->GetHeader())) {
1077 // Block is currently part of an outer loop. Make it part of this inner loop.
1078 // Note that a non loop header having a loop information means this loop information
1079 // has already been populated
1080 loop_information_ = info;
1081 } else {
1082 // Block is part of an inner loop. Do not update the loop information.
1083 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1084 // at this point, because this method is being called while populating `info`.
1085 }
1086 }
1087
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001088 // Raw update of the loop information.
1089 void SetLoopInformation(HLoopInformation* info) {
1090 loop_information_ = info;
1091 }
1092
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001093 bool IsInLoop() const { return loop_information_ != nullptr; }
1094
David Brazdilec16f792015-08-19 15:04:01 +01001095 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1096
1097 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1098 try_catch_information_ = try_catch_information;
1099 }
1100
1101 bool IsTryBlock() const {
1102 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1103 }
1104
1105 bool IsCatchBlock() const {
1106 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1107 }
David Brazdilffee3d32015-07-06 11:48:53 +01001108
1109 // Returns the try entry that this block's successors should have. They will
1110 // be in the same try, unless the block ends in a try boundary. In that case,
1111 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001112 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001113
David Brazdild7558da2015-09-22 13:04:14 +01001114 bool HasThrowingInstructions() const;
1115
David Brazdila4b8c212015-05-07 09:59:30 +01001116 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001117 bool Dominates(HBasicBlock* block) const;
1118
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001119 size_t GetLifetimeStart() const { return lifetime_start_; }
1120 size_t GetLifetimeEnd() const { return lifetime_end_; }
1121
1122 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1123 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1124
David Brazdil8d5b8b22015-03-24 10:51:52 +00001125 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001126 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001127 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001128 bool HasSinglePhi() const;
1129
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001130 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001131 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001132 ArenaVector<HBasicBlock*> predecessors_;
1133 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001134 HInstructionList instructions_;
1135 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001136 HLoopInformation* loop_information_;
1137 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001138 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001139 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001140 // The dex program counter of the first instruction of this block.
1141 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001142 size_t lifetime_start_;
1143 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001144 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001145
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001146 friend class HGraph;
1147 friend class HInstruction;
1148
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001149 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1150};
1151
David Brazdilb2bd1c52015-03-25 11:17:37 +00001152// Iterates over the LoopInformation of all loops which contain 'block'
1153// from the innermost to the outermost.
1154class HLoopInformationOutwardIterator : public ValueObject {
1155 public:
1156 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1157 : current_(block.GetLoopInformation()) {}
1158
1159 bool Done() const { return current_ == nullptr; }
1160
1161 void Advance() {
1162 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001163 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001164 }
1165
1166 HLoopInformation* Current() const {
1167 DCHECK(!Done());
1168 return current_;
1169 }
1170
1171 private:
1172 HLoopInformation* current_;
1173
1174 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1175};
1176
Alexandre Ramesef20f712015-06-09 10:29:30 +01001177#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001178 M(Above, Condition) \
1179 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001180 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001181 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001182 M(ArrayGet, Instruction) \
1183 M(ArrayLength, Instruction) \
1184 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001185 M(Below, Condition) \
1186 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001187 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001188 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001189 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001190 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001191 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001192 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001193 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001194 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001195 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001196 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001197 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001198 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001199 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001200 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001201 M(Exit, Instruction) \
1202 M(FloatConstant, Constant) \
1203 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001204 M(GreaterThan, Condition) \
1205 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001206 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001207 M(InstanceFieldGet, Instruction) \
1208 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001209 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001210 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001211 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001212 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001213 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001214 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001215 M(LessThan, Condition) \
1216 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001217 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001218 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001219 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001220 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001221 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001222 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001223 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001224 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001225 M(Neg, UnaryOperation) \
1226 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001227 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001228 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001229 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001230 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001231 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001232 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001233 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001234 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001235 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001236 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001237 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001238 M(Return, Instruction) \
1239 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001240 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001241 M(Shl, BinaryOperation) \
1242 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001243 M(StaticFieldGet, Instruction) \
1244 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001245 M(UnresolvedInstanceFieldGet, Instruction) \
1246 M(UnresolvedInstanceFieldSet, Instruction) \
1247 M(UnresolvedStaticFieldGet, Instruction) \
1248 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001249 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001250 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001251 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001252 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001253 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001254 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001255 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001256 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001257
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001258/*
1259 * Instructions, shared across several (not all) architectures.
1260 */
1261#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1262#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1263#else
1264#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001265 M(BitwiseNegatedRight, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001266 M(MultiplyAccumulate, Instruction)
1267#endif
1268
Vladimir Markob4536b72015-11-24 13:45:23 +00001269#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001270#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001271#else
1272#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1273 M(ArmDexCacheArraysBase, Instruction)
1274#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001275
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001276#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001277#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001278#else
1279#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001280 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001281 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001282#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001283
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001284#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1285
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001286#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1287
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001288#ifndef ART_ENABLE_CODEGEN_x86
1289#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1290#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001291#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1292 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001293 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001294 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001295 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001296#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001297
1298#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1299
1300#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1301 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001302 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001303 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1304 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001305 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001306 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001307 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1308 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1309
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001310#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1311 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001312 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001313 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001314 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001315 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001316
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001317#define FOR_EACH_INSTRUCTION(M) \
1318 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1319 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1320
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001321#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001322FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1323#undef FORWARD_DECLARATION
1324
Roland Levillainccc07a92014-09-16 14:48:16 +01001325#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001326 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001327 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001328 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001329 return other->Is##type(); \
1330 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001331 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001332
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001333#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1334 bool Is##type() const { return As##type() != nullptr; } \
1335 const H##type* As##type() const { return this; } \
1336 H##type* As##type() { return this; }
1337
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001338template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001339class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001340 public:
David Brazdiled596192015-01-23 10:39:45 +00001341 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001342 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001343 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001344
Vladimir Marko46817b82016-03-29 12:21:58 +01001345 // Hook for the IntrusiveForwardList<>.
1346 // TODO: Hide this better.
1347 IntrusiveForwardListHook hook;
1348
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001349 private:
David Brazdiled596192015-01-23 10:39:45 +00001350 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001351 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001352
1353 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001354 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001355
Vladimir Marko46817b82016-03-29 12:21:58 +01001356 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001357
1358 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1359};
1360
David Brazdiled596192015-01-23 10:39:45 +00001361template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001362using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001363
David Brazdil1abb4192015-02-17 18:33:36 +00001364// This class is used by HEnvironment and HInstruction classes to record the
1365// instructions they use and pointers to the corresponding HUseListNodes kept
1366// by the used instructions.
1367template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001368class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001369 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001370 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1371 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001372
Vladimir Marko46817b82016-03-29 12:21:58 +01001373 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1374 : HUserRecord(old_record.instruction_, before_use_node) {}
1375 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1376 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001377 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001378 }
1379
1380 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001381 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1382 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001383
1384 private:
1385 // Instruction used by the user.
1386 HInstruction* instruction_;
1387
Vladimir Marko46817b82016-03-29 12:21:58 +01001388 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1389 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001390};
1391
Aart Bik854a02b2015-07-14 16:07:00 -07001392/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001393 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001394 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001395 * For write/read dependences on fields/arrays, the dependence analysis uses
1396 * type disambiguation (e.g. a float field write cannot modify the value of an
1397 * integer field read) and the access type (e.g. a reference array write cannot
1398 * modify the value of a reference field read [although it may modify the
1399 * reference fetch prior to reading the field, which is represented by its own
1400 * write/read dependence]). The analysis makes conservative points-to
1401 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1402 * the same, and any reference read depends on any reference read without
1403 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001404 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001405 * The internal representation uses 38-bit and is described in the table below.
1406 * The first line indicates the side effect, and for field/array accesses the
1407 * second line indicates the type of the access (in the order of the
1408 * Primitive::Type enum).
1409 * The two numbered lines below indicate the bit position in the bitfield (read
1410 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001411 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001412 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1413 * +-------------+---------+---------+--------------+---------+---------+
1414 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1415 * | 3 |333333322|222222221| 1 |111111110|000000000|
1416 * | 7 |654321098|765432109| 8 |765432109|876543210|
1417 *
1418 * Note that, to ease the implementation, 'changes' bits are least significant
1419 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001420 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001421class SideEffects : public ValueObject {
1422 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001423 SideEffects() : flags_(0) {}
1424
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001425 static SideEffects None() {
1426 return SideEffects(0);
1427 }
1428
1429 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001430 return SideEffects(kAllChangeBits | kAllDependOnBits);
1431 }
1432
1433 static SideEffects AllChanges() {
1434 return SideEffects(kAllChangeBits);
1435 }
1436
1437 static SideEffects AllDependencies() {
1438 return SideEffects(kAllDependOnBits);
1439 }
1440
1441 static SideEffects AllExceptGCDependency() {
1442 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1443 }
1444
1445 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001446 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001447 }
1448
Aart Bik34c3ba92015-07-20 14:08:59 -07001449 static SideEffects AllWrites() {
1450 return SideEffects(kAllWrites);
1451 }
1452
1453 static SideEffects AllReads() {
1454 return SideEffects(kAllReads);
1455 }
1456
1457 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1458 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001459 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001460 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001461 }
1462
Aart Bik854a02b2015-07-14 16:07:00 -07001463 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001464 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001465 }
1466
Aart Bik34c3ba92015-07-20 14:08:59 -07001467 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1468 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001469 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001470 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001471 }
1472
1473 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001474 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001475 }
1476
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001477 static SideEffects CanTriggerGC() {
1478 return SideEffects(1ULL << kCanTriggerGCBit);
1479 }
1480
1481 static SideEffects DependsOnGC() {
1482 return SideEffects(1ULL << kDependsOnGCBit);
1483 }
1484
Aart Bik854a02b2015-07-14 16:07:00 -07001485 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001486 SideEffects Union(SideEffects other) const {
1487 return SideEffects(flags_ | other.flags_);
1488 }
1489
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001490 SideEffects Exclusion(SideEffects other) const {
1491 return SideEffects(flags_ & ~other.flags_);
1492 }
1493
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001494 void Add(SideEffects other) {
1495 flags_ |= other.flags_;
1496 }
1497
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001498 bool Includes(SideEffects other) const {
1499 return (other.flags_ & flags_) == other.flags_;
1500 }
1501
1502 bool HasSideEffects() const {
1503 return (flags_ & kAllChangeBits);
1504 }
1505
1506 bool HasDependencies() const {
1507 return (flags_ & kAllDependOnBits);
1508 }
1509
1510 // Returns true if there are no side effects or dependencies.
1511 bool DoesNothing() const {
1512 return flags_ == 0;
1513 }
1514
Aart Bik854a02b2015-07-14 16:07:00 -07001515 // Returns true if something is written.
1516 bool DoesAnyWrite() const {
1517 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001518 }
1519
Aart Bik854a02b2015-07-14 16:07:00 -07001520 // Returns true if something is read.
1521 bool DoesAnyRead() const {
1522 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001523 }
1524
Aart Bik854a02b2015-07-14 16:07:00 -07001525 // Returns true if potentially everything is written and read
1526 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001527 bool DoesAllReadWrite() const {
1528 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1529 }
1530
Aart Bik854a02b2015-07-14 16:07:00 -07001531 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001532 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001533 }
1534
Roland Levillain0d5a2812015-11-13 10:07:31 +00001535 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001536 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001537 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1538 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001539 }
1540
1541 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001542 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001543 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001544 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001545 for (int s = kLastBit; s >= 0; s--) {
1546 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1547 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1548 // This is a bit for the GC side effect.
1549 if (current_bit_is_set) {
1550 flags += "GC";
1551 }
Aart Bik854a02b2015-07-14 16:07:00 -07001552 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001553 } else {
1554 // This is a bit for the array/field analysis.
1555 // The underscore character stands for the 'can trigger GC' bit.
1556 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1557 if (current_bit_is_set) {
1558 flags += kDebug[s];
1559 }
1560 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1561 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1562 flags += "|";
1563 }
1564 }
Aart Bik854a02b2015-07-14 16:07:00 -07001565 }
1566 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001567 }
1568
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001569 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001570
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001571 private:
1572 static constexpr int kFieldArrayAnalysisBits = 9;
1573
1574 static constexpr int kFieldWriteOffset = 0;
1575 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1576 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1577 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1578
1579 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1580
1581 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1582 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1583 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1584 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1585
1586 static constexpr int kLastBit = kDependsOnGCBit;
1587 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1588
1589 // Aliases.
1590
1591 static_assert(kChangeBits == kDependOnBits,
1592 "the 'change' bits should match the 'depend on' bits.");
1593
1594 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1595 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1596 static constexpr uint64_t kAllWrites =
1597 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1598 static constexpr uint64_t kAllReads =
1599 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001600
Aart Bik854a02b2015-07-14 16:07:00 -07001601 // Translates type to bit flag.
1602 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1603 CHECK_NE(type, Primitive::kPrimVoid);
1604 const uint64_t one = 1;
1605 const int shift = type; // 0-based consecutive enum
1606 DCHECK_LE(kFieldWriteOffset, shift);
1607 DCHECK_LT(shift, kArrayWriteOffset);
1608 return one << (type + offset);
1609 }
1610
1611 // Private constructor on direct flags value.
1612 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1613
1614 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001615};
1616
David Brazdiled596192015-01-23 10:39:45 +00001617// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001618class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001619 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001620 HEnvironment(ArenaAllocator* arena,
1621 size_t number_of_vregs,
1622 const DexFile& dex_file,
1623 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001624 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001625 InvokeType invoke_type,
1626 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001627 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1628 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001629 parent_(nullptr),
1630 dex_file_(dex_file),
1631 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001632 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001633 invoke_type_(invoke_type),
1634 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001635 }
1636
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001637 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001638 : HEnvironment(arena,
1639 to_copy.Size(),
1640 to_copy.GetDexFile(),
1641 to_copy.GetMethodIdx(),
1642 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001643 to_copy.GetInvokeType(),
1644 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001645
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001646 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001647 if (parent_ != nullptr) {
1648 parent_->SetAndCopyParentChain(allocator, parent);
1649 } else {
1650 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1651 parent_->CopyFrom(parent);
1652 if (parent->GetParent() != nullptr) {
1653 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1654 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001655 }
David Brazdiled596192015-01-23 10:39:45 +00001656 }
1657
Vladimir Marko71bf8092015-09-15 15:33:14 +01001658 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001659 void CopyFrom(HEnvironment* environment);
1660
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001661 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1662 // input to the loop phi instead. This is for inserting instructions that
1663 // require an environment (like HDeoptimization) in the loop pre-header.
1664 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001665
1666 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001667 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001668 }
1669
1670 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001671 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001672 }
1673
David Brazdil1abb4192015-02-17 18:33:36 +00001674 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001675
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001676 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001677
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001678 HEnvironment* GetParent() const { return parent_; }
1679
1680 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001681 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001682 }
1683
1684 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001685 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001686 }
1687
1688 uint32_t GetDexPc() const {
1689 return dex_pc_;
1690 }
1691
1692 uint32_t GetMethodIdx() const {
1693 return method_idx_;
1694 }
1695
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001696 InvokeType GetInvokeType() const {
1697 return invoke_type_;
1698 }
1699
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001700 const DexFile& GetDexFile() const {
1701 return dex_file_;
1702 }
1703
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001704 HInstruction* GetHolder() const {
1705 return holder_;
1706 }
1707
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001708
1709 bool IsFromInlinedInvoke() const {
1710 return GetParent() != nullptr;
1711 }
1712
David Brazdiled596192015-01-23 10:39:45 +00001713 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001714 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1715 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001716 HEnvironment* parent_;
1717 const DexFile& dex_file_;
1718 const uint32_t method_idx_;
1719 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001720 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001721
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001722 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001723 HInstruction* const holder_;
1724
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001725 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001726
1727 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1728};
1729
Vladimir Markof9f64412015-09-02 14:05:49 +01001730class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001731 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001732 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001733 : previous_(nullptr),
1734 next_(nullptr),
1735 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001736 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001737 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001738 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001739 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001740 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001741 locations_(nullptr),
1742 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001743 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001744 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001745 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1746 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1747 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001748
Dave Allison20dfc792014-06-16 20:44:29 -07001749 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001750
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001751#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001752 enum InstructionKind {
1753 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1754 };
1755#undef DECLARE_KIND
1756
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001757 HInstruction* GetNext() const { return next_; }
1758 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001759
Calin Juravle77520bc2015-01-12 18:45:46 +00001760 HInstruction* GetNextDisregardingMoves() const;
1761 HInstruction* GetPreviousDisregardingMoves() const;
1762
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001763 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001764 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001765 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001766 bool IsInBlock() const { return block_ != nullptr; }
1767 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001768 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1769 bool IsIrreducibleLoopHeaderPhi() const {
1770 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1771 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001772
Roland Levillain6b879dd2014-09-22 17:13:44 +01001773 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001774 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001775
1776 virtual void Accept(HGraphVisitor* visitor) = 0;
1777 virtual const char* DebugName() const = 0;
1778
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001779 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001780 void SetRawInputAt(size_t index, HInstruction* input) {
1781 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1782 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001783
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001784 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001785
1786 uint32_t GetDexPc() const { return dex_pc_; }
1787
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001788 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001789
Roland Levillaine161a2a2014-10-03 12:45:18 +01001790 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001791 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001792
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001793 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001794 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001795
Calin Juravle10e244f2015-01-26 18:54:32 +00001796 // Does not apply for all instructions, but having this at top level greatly
1797 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001798 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001799 virtual bool CanBeNull() const {
1800 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1801 return true;
1802 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001803
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001804 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001805 return false;
1806 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001807
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001808 virtual bool IsActualObject() const {
1809 return GetType() == Primitive::kPrimNot;
1810 }
1811
Calin Juravle2e768302015-07-28 14:41:11 +00001812 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001813
Calin Juravle61d544b2015-02-23 16:46:57 +00001814 ReferenceTypeInfo GetReferenceTypeInfo() const {
1815 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001816 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001817 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001818 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001819
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001820 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001821 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001822 // Note: fixup_end remains valid across push_front().
1823 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1824 HUseListNode<HInstruction*>* new_node =
1825 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1826 uses_.push_front(*new_node);
1827 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001828 }
1829
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001830 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001831 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001832 // Note: env_fixup_end remains valid across push_front().
1833 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1834 HUseListNode<HEnvironment*>* new_node =
1835 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1836 env_uses_.push_front(*new_node);
1837 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001838 }
1839
David Brazdil1abb4192015-02-17 18:33:36 +00001840 void RemoveAsUserOfInput(size_t input) {
1841 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001842 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1843 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1844 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001845 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001846
David Brazdil1abb4192015-02-17 18:33:36 +00001847 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1848 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001849
Vladimir Marko46817b82016-03-29 12:21:58 +01001850 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1851 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1852 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001853 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001854 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001855 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001856
Roland Levillain6c82d402014-10-13 16:10:27 +01001857 // Does this instruction strictly dominate `other_instruction`?
1858 // Returns false if this instruction and `other_instruction` are the same.
1859 // Aborts if this instruction and `other_instruction` are both phis.
1860 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001861
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001862 int GetId() const { return id_; }
1863 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001864
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001865 int GetSsaIndex() const { return ssa_index_; }
1866 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1867 bool HasSsaIndex() const { return ssa_index_ != -1; }
1868
1869 bool HasEnvironment() const { return environment_ != nullptr; }
1870 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001871 // Set the `environment_` field. Raw because this method does not
1872 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001873 void SetRawEnvironment(HEnvironment* environment) {
1874 DCHECK(environment_ == nullptr);
1875 DCHECK_EQ(environment->GetHolder(), this);
1876 environment_ = environment;
1877 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001878
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001879 void RemoveEnvironment();
1880
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001881 // Set the environment of this instruction, copying it from `environment`. While
1882 // copying, the uses lists are being updated.
1883 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001884 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001885 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001886 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001887 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001888 if (environment->GetParent() != nullptr) {
1889 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1890 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001891 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001892
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001893 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1894 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001895 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001896 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001897 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001898 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001899 if (environment->GetParent() != nullptr) {
1900 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1901 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001902 }
1903
Nicolas Geoffray39468442014-09-02 15:17:15 +01001904 // Returns the number of entries in the environment. Typically, that is the
1905 // number of dex registers in a method. It could be more in case of inlining.
1906 size_t EnvironmentSize() const;
1907
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001908 LocationSummary* GetLocations() const { return locations_; }
1909 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001910
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001911 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001912 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001913
Alexandre Rames188d4312015-04-09 18:30:21 +01001914 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1915 // uses of this instruction by `other` are *not* updated.
1916 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1917 ReplaceWith(other);
1918 other->ReplaceInput(this, use_index);
1919 }
1920
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001921 // Move `this` instruction before `cursor`.
1922 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001923
Vladimir Markofb337ea2015-11-25 15:25:10 +00001924 // Move `this` before its first user and out of any loops. If there is no
1925 // out-of-loop user that dominates all other users, move the instruction
1926 // to the end of the out-of-loop common dominator of the user's blocks.
1927 //
1928 // This can be used only on non-throwing instructions with no side effects that
1929 // have at least one use but no environment uses.
1930 void MoveBeforeFirstUserAndOutOfLoops();
1931
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001932#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001933 bool Is##type() const; \
1934 const H##type* As##type() const; \
1935 H##type* As##type();
1936
1937 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1938#undef INSTRUCTION_TYPE_CHECK
1939
1940#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001941 bool Is##type() const { return (As##type() != nullptr); } \
1942 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001943 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001944 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001945#undef INSTRUCTION_TYPE_CHECK
1946
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001947 // Returns whether the instruction can be moved within the graph.
1948 virtual bool CanBeMoved() const { return false; }
1949
1950 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001951 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001952 return false;
1953 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001954
1955 // Returns whether any data encoded in the two instructions is equal.
1956 // This method does not look at the inputs. Both instructions must be
1957 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001958 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001959 return false;
1960 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001961
1962 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001963 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001964 // 2) Their inputs are identical.
1965 bool Equals(HInstruction* other) const;
1966
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001967 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
1968 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
1969 // the virtual function because the __attribute__((__pure__)) doesn't really
1970 // apply the strong requirement for virtual functions, preventing optimizations.
1971 InstructionKind GetKind() const PURE;
1972 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001973
1974 virtual size_t ComputeHashCode() const {
1975 size_t result = GetKind();
1976 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1977 result = (result * 31) + InputAt(i)->GetId();
1978 }
1979 return result;
1980 }
1981
1982 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001983 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001984 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001985
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001986 size_t GetLifetimePosition() const { return lifetime_position_; }
1987 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1988 LiveInterval* GetLiveInterval() const { return live_interval_; }
1989 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1990 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1991
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001992 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1993
1994 // Returns whether the code generation of the instruction will require to have access
1995 // to the current method. Such instructions are:
1996 // (1): Instructions that require an environment, as calling the runtime requires
1997 // to walk the stack and have the current method stored at a specific stack address.
1998 // (2): Object literals like classes and strings, that are loaded from the dex cache
1999 // fields of the current method.
2000 bool NeedsCurrentMethod() const {
2001 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2002 }
2003
Vladimir Markodc151b22015-10-15 18:02:30 +01002004 // Returns whether the code generation of the instruction will require to have access
2005 // to the dex cache of the current method's declaring class via the current method.
2006 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002007
Mark Mendellc4701932015-04-10 13:18:51 -04002008 // Does this instruction have any use in an environment before
2009 // control flow hits 'other'?
2010 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2011
2012 // Remove all references to environment uses of this instruction.
2013 // The caller must ensure that this is safe to do.
2014 void RemoveEnvironmentUsers();
2015
Vladimir Markoa1de9182016-02-25 11:37:38 +00002016 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2017 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002018
David Brazdil1abb4192015-02-17 18:33:36 +00002019 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002020 // If set, the machine code for this instruction is assumed to be generated by
2021 // its users. Used by liveness analysis to compute use positions accordingly.
2022 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2023 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2024 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2025 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2026
David Brazdil1abb4192015-02-17 18:33:36 +00002027 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2028 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2029
Vladimir Markoa1de9182016-02-25 11:37:38 +00002030 uint32_t GetPackedFields() const {
2031 return packed_fields_;
2032 }
2033
2034 template <size_t flag>
2035 bool GetPackedFlag() const {
2036 return (packed_fields_ & (1u << flag)) != 0u;
2037 }
2038
2039 template <size_t flag>
2040 void SetPackedFlag(bool value = true) {
2041 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2042 }
2043
2044 template <typename BitFieldType>
2045 typename BitFieldType::value_type GetPackedField() const {
2046 return BitFieldType::Decode(packed_fields_);
2047 }
2048
2049 template <typename BitFieldType>
2050 void SetPackedField(typename BitFieldType::value_type value) {
2051 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2052 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2053 }
2054
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002055 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002056 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2057 auto before_use_node = uses_.before_begin();
2058 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2059 HInstruction* user = use_node->GetUser();
2060 size_t input_index = use_node->GetIndex();
2061 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2062 before_use_node = use_node;
2063 }
2064 }
2065
2066 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2067 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2068 if (next != uses_.end()) {
2069 HInstruction* next_user = next->GetUser();
2070 size_t next_index = next->GetIndex();
2071 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2072 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2073 }
2074 }
2075
2076 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2077 auto before_env_use_node = env_uses_.before_begin();
2078 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2079 HEnvironment* user = env_use_node->GetUser();
2080 size_t input_index = env_use_node->GetIndex();
2081 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2082 before_env_use_node = env_use_node;
2083 }
2084 }
2085
2086 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2087 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2088 if (next != env_uses_.end()) {
2089 HEnvironment* next_user = next->GetUser();
2090 size_t next_index = next->GetIndex();
2091 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2092 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2093 }
2094 }
David Brazdil1abb4192015-02-17 18:33:36 +00002095
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002096 HInstruction* previous_;
2097 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002098 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002099 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002100
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002101 // An instruction gets an id when it is added to the graph.
2102 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002103 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002104 int id_;
2105
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002106 // When doing liveness analysis, instructions that have uses get an SSA index.
2107 int ssa_index_;
2108
Vladimir Markoa1de9182016-02-25 11:37:38 +00002109 // Packed fields.
2110 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002111
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002112 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002113 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002114
2115 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002116 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002117
Nicolas Geoffray39468442014-09-02 15:17:15 +01002118 // The environment associated with this instruction. Not null if the instruction
2119 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002120 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002121
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002122 // Set by the code generator.
2123 LocationSummary* locations_;
2124
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002125 // Set by the liveness analysis.
2126 LiveInterval* live_interval_;
2127
2128 // Set by the liveness analysis, this is the position in a linear
2129 // order of blocks where this instruction's live interval start.
2130 size_t lifetime_position_;
2131
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002132 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002133
Vladimir Markoa1de9182016-02-25 11:37:38 +00002134 // The reference handle part of the reference type info.
2135 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002136 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002137 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002138
David Brazdil1abb4192015-02-17 18:33:36 +00002139 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002140 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002141 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002142 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002143 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002144
2145 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2146};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002147std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002148
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002149class HInputIterator : public ValueObject {
2150 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002151 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002152
2153 bool Done() const { return index_ == instruction_->InputCount(); }
2154 HInstruction* Current() const { return instruction_->InputAt(index_); }
2155 void Advance() { index_++; }
2156
2157 private:
2158 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002159 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002160
2161 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2162};
2163
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002164class HInstructionIterator : public ValueObject {
2165 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002166 explicit HInstructionIterator(const HInstructionList& instructions)
2167 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002168 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002169 }
2170
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002171 bool Done() const { return instruction_ == nullptr; }
2172 HInstruction* Current() const { return instruction_; }
2173 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002174 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002175 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002176 }
2177
2178 private:
2179 HInstruction* instruction_;
2180 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002181
2182 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002183};
2184
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002185class HBackwardInstructionIterator : public ValueObject {
2186 public:
2187 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2188 : instruction_(instructions.last_instruction_) {
2189 next_ = Done() ? nullptr : instruction_->GetPrevious();
2190 }
2191
2192 bool Done() const { return instruction_ == nullptr; }
2193 HInstruction* Current() const { return instruction_; }
2194 void Advance() {
2195 instruction_ = next_;
2196 next_ = Done() ? nullptr : instruction_->GetPrevious();
2197 }
2198
2199 private:
2200 HInstruction* instruction_;
2201 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002202
2203 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002204};
2205
Vladimir Markof9f64412015-09-02 14:05:49 +01002206template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002207class HTemplateInstruction: public HInstruction {
2208 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002209 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002210 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002211 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002212
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002213 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002214
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002215 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002216 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2217 DCHECK_LT(i, N);
2218 return inputs_[i];
2219 }
David Brazdil1abb4192015-02-17 18:33:36 +00002220
2221 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002222 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002223 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002224 }
2225
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002226 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002227 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002228
2229 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002230};
2231
Vladimir Markof9f64412015-09-02 14:05:49 +01002232// HTemplateInstruction specialization for N=0.
2233template<>
2234class HTemplateInstruction<0>: public HInstruction {
2235 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002236 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002237 : HInstruction(side_effects, dex_pc) {}
2238
Vladimir Markof9f64412015-09-02 14:05:49 +01002239 virtual ~HTemplateInstruction() {}
2240
2241 size_t InputCount() const OVERRIDE { return 0; }
2242
2243 protected:
2244 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2245 LOG(FATAL) << "Unreachable";
2246 UNREACHABLE();
2247 }
2248
2249 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2250 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2251 LOG(FATAL) << "Unreachable";
2252 UNREACHABLE();
2253 }
2254
2255 private:
2256 friend class SsaBuilder;
2257};
2258
Dave Allison20dfc792014-06-16 20:44:29 -07002259template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002260class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002261 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002262 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002263 : HTemplateInstruction<N>(side_effects, dex_pc) {
2264 this->template SetPackedField<TypeField>(type);
2265 }
Dave Allison20dfc792014-06-16 20:44:29 -07002266 virtual ~HExpression() {}
2267
Vladimir Markoa1de9182016-02-25 11:37:38 +00002268 Primitive::Type GetType() const OVERRIDE {
2269 return TypeField::Decode(this->GetPackedFields());
2270 }
Dave Allison20dfc792014-06-16 20:44:29 -07002271
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002272 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002273 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2274 static constexpr size_t kFieldTypeSize =
2275 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2276 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2277 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2278 "Too many packed fields.");
2279 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002280};
2281
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002282// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2283// instruction that branches to the exit block.
2284class HReturnVoid : public HTemplateInstruction<0> {
2285 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002286 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2287 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002288
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002289 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002290
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002291 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002292
2293 private:
2294 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2295};
2296
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002297// Represents dex's RETURN opcodes. A HReturn is a control flow
2298// instruction that branches to the exit block.
2299class HReturn : public HTemplateInstruction<1> {
2300 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002301 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2302 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002303 SetRawInputAt(0, value);
2304 }
2305
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002306 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002307
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002308 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002309
2310 private:
2311 DISALLOW_COPY_AND_ASSIGN(HReturn);
2312};
2313
David Brazdildee58d62016-04-07 09:54:26 +00002314class HPhi : public HInstruction {
2315 public:
2316 HPhi(ArenaAllocator* arena,
2317 uint32_t reg_number,
2318 size_t number_of_inputs,
2319 Primitive::Type type,
2320 uint32_t dex_pc = kNoDexPc)
2321 : HInstruction(SideEffects::None(), dex_pc),
2322 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2323 reg_number_(reg_number) {
2324 SetPackedField<TypeField>(ToPhiType(type));
2325 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2326 // Phis are constructed live and marked dead if conflicting or unused.
2327 // Individual steps of SsaBuilder should assume that if a phi has been
2328 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2329 SetPackedFlag<kFlagIsLive>(true);
2330 SetPackedFlag<kFlagCanBeNull>(true);
2331 }
2332
2333 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2334 static Primitive::Type ToPhiType(Primitive::Type type) {
2335 return Primitive::PrimitiveKind(type);
2336 }
2337
2338 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2339
2340 size_t InputCount() const OVERRIDE { return inputs_.size(); }
2341
2342 void AddInput(HInstruction* input);
2343 void RemoveInputAt(size_t index);
2344
2345 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2346 void SetType(Primitive::Type new_type) {
2347 // Make sure that only valid type changes occur. The following are allowed:
2348 // (1) int -> float/ref (primitive type propagation),
2349 // (2) long -> double (primitive type propagation).
2350 DCHECK(GetType() == new_type ||
2351 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2352 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2353 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2354 SetPackedField<TypeField>(new_type);
2355 }
2356
2357 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2358 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2359
2360 uint32_t GetRegNumber() const { return reg_number_; }
2361
2362 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2363 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2364 bool IsDead() const { return !IsLive(); }
2365 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2366
2367 bool IsVRegEquivalentOf(HInstruction* other) const {
2368 return other != nullptr
2369 && other->IsPhi()
2370 && other->AsPhi()->GetBlock() == GetBlock()
2371 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2372 }
2373
2374 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2375 // An equivalent phi is a phi having the same dex register and type.
2376 // It assumes that phis with the same dex register are adjacent.
2377 HPhi* GetNextEquivalentPhiWithSameType() {
2378 HInstruction* next = GetNext();
2379 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2380 if (next->GetType() == GetType()) {
2381 return next->AsPhi();
2382 }
2383 next = next->GetNext();
2384 }
2385 return nullptr;
2386 }
2387
2388 DECLARE_INSTRUCTION(Phi);
2389
2390 protected:
2391 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
2392 return inputs_[index];
2393 }
2394
2395 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2396 inputs_[index] = input;
2397 }
2398
2399 private:
2400 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2401 static constexpr size_t kFieldTypeSize =
2402 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2403 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2404 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2405 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2406 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2407 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2408
2409 ArenaVector<HUserRecord<HInstruction*> > inputs_;
2410 const uint32_t reg_number_;
2411
2412 DISALLOW_COPY_AND_ASSIGN(HPhi);
2413};
2414
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002415// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002416// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002417// exit block.
2418class HExit : public HTemplateInstruction<0> {
2419 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002420 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002421
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002422 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002423
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002424 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002425
2426 private:
2427 DISALLOW_COPY_AND_ASSIGN(HExit);
2428};
2429
2430// Jumps from one block to another.
2431class HGoto : public HTemplateInstruction<0> {
2432 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002433 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002434
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002435 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002436
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002437 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002438 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002439 }
2440
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002441 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002442
2443 private:
2444 DISALLOW_COPY_AND_ASSIGN(HGoto);
2445};
2446
Roland Levillain9867bc72015-08-05 10:21:34 +01002447class HConstant : public HExpression<0> {
2448 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002449 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2450 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002451
2452 bool CanBeMoved() const OVERRIDE { return true; }
2453
Roland Levillain1a653882016-03-18 18:05:57 +00002454 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002455 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002456 // Is this constant 0 in the arithmetic sense?
2457 virtual bool IsArithmeticZero() const { return false; }
2458 // Is this constant a 0-bit pattern?
2459 virtual bool IsZeroBitPattern() const { return false; }
2460 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002461 virtual bool IsOne() const { return false; }
2462
David Brazdil77a48ae2015-09-15 12:34:04 +00002463 virtual uint64_t GetValueAsUint64() const = 0;
2464
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002465 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002466
2467 private:
2468 DISALLOW_COPY_AND_ASSIGN(HConstant);
2469};
2470
2471class HNullConstant : public HConstant {
2472 public:
2473 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2474 return true;
2475 }
2476
David Brazdil77a48ae2015-09-15 12:34:04 +00002477 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2478
Roland Levillain9867bc72015-08-05 10:21:34 +01002479 size_t ComputeHashCode() const OVERRIDE { return 0; }
2480
Roland Levillain1a653882016-03-18 18:05:57 +00002481 // The null constant representation is a 0-bit pattern.
2482 virtual bool IsZeroBitPattern() const { return true; }
2483
Roland Levillain9867bc72015-08-05 10:21:34 +01002484 DECLARE_INSTRUCTION(NullConstant);
2485
2486 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002487 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002488
2489 friend class HGraph;
2490 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2491};
2492
2493// Constants of the type int. Those can be from Dex instructions, or
2494// synthesized (for example with the if-eqz instruction).
2495class HIntConstant : public HConstant {
2496 public:
2497 int32_t GetValue() const { return value_; }
2498
David Brazdil9f389d42015-10-01 14:32:56 +01002499 uint64_t GetValueAsUint64() const OVERRIDE {
2500 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2501 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002502
Roland Levillain9867bc72015-08-05 10:21:34 +01002503 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002504 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002505 return other->AsIntConstant()->value_ == value_;
2506 }
2507
2508 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2509
2510 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002511 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2512 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002513 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2514
Roland Levillain1a653882016-03-18 18:05:57 +00002515 // Integer constants are used to encode Boolean values as well,
2516 // where 1 means true and 0 means false.
2517 bool IsTrue() const { return GetValue() == 1; }
2518 bool IsFalse() const { return GetValue() == 0; }
2519
Roland Levillain9867bc72015-08-05 10:21:34 +01002520 DECLARE_INSTRUCTION(IntConstant);
2521
2522 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002523 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2524 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2525 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2526 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002527
2528 const int32_t value_;
2529
2530 friend class HGraph;
2531 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2532 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2533 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2534};
2535
2536class HLongConstant : public HConstant {
2537 public:
2538 int64_t GetValue() const { return value_; }
2539
David Brazdil77a48ae2015-09-15 12:34:04 +00002540 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2541
Roland Levillain9867bc72015-08-05 10:21:34 +01002542 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002543 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002544 return other->AsLongConstant()->value_ == value_;
2545 }
2546
2547 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2548
2549 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002550 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2551 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002552 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2553
2554 DECLARE_INSTRUCTION(LongConstant);
2555
2556 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002557 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2558 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002559
2560 const int64_t value_;
2561
2562 friend class HGraph;
2563 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2564};
Dave Allison20dfc792014-06-16 20:44:29 -07002565
Roland Levillain31dd3d62016-02-16 12:21:02 +00002566class HFloatConstant : public HConstant {
2567 public:
2568 float GetValue() const { return value_; }
2569
2570 uint64_t GetValueAsUint64() const OVERRIDE {
2571 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2572 }
2573
2574 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2575 DCHECK(other->IsFloatConstant()) << other->DebugName();
2576 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2577 }
2578
2579 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2580
2581 bool IsMinusOne() const OVERRIDE {
2582 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2583 }
Roland Levillain1a653882016-03-18 18:05:57 +00002584 bool IsArithmeticZero() const OVERRIDE {
2585 return std::fpclassify(value_) == FP_ZERO;
2586 }
2587 bool IsArithmeticPositiveZero() const {
2588 return IsArithmeticZero() && !std::signbit(value_);
2589 }
2590 bool IsArithmeticNegativeZero() const {
2591 return IsArithmeticZero() && std::signbit(value_);
2592 }
2593 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002594 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002595 }
2596 bool IsOne() const OVERRIDE {
2597 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2598 }
2599 bool IsNaN() const {
2600 return std::isnan(value_);
2601 }
2602
2603 DECLARE_INSTRUCTION(FloatConstant);
2604
2605 private:
2606 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2607 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2608 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2609 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2610
2611 const float value_;
2612
2613 // Only the SsaBuilder and HGraph can create floating-point constants.
2614 friend class SsaBuilder;
2615 friend class HGraph;
2616 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2617};
2618
2619class HDoubleConstant : public HConstant {
2620 public:
2621 double GetValue() const { return value_; }
2622
2623 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2624
2625 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2626 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2627 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2628 }
2629
2630 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2631
2632 bool IsMinusOne() const OVERRIDE {
2633 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2634 }
Roland Levillain1a653882016-03-18 18:05:57 +00002635 bool IsArithmeticZero() const OVERRIDE {
2636 return std::fpclassify(value_) == FP_ZERO;
2637 }
2638 bool IsArithmeticPositiveZero() const {
2639 return IsArithmeticZero() && !std::signbit(value_);
2640 }
2641 bool IsArithmeticNegativeZero() const {
2642 return IsArithmeticZero() && std::signbit(value_);
2643 }
2644 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002645 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002646 }
2647 bool IsOne() const OVERRIDE {
2648 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2649 }
2650 bool IsNaN() const {
2651 return std::isnan(value_);
2652 }
2653
2654 DECLARE_INSTRUCTION(DoubleConstant);
2655
2656 private:
2657 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2658 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2659 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2660 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2661
2662 const double value_;
2663
2664 // Only the SsaBuilder and HGraph can create floating-point constants.
2665 friend class SsaBuilder;
2666 friend class HGraph;
2667 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2668};
2669
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002670// Conditional branch. A block ending with an HIf instruction must have
2671// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002672class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002673 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002674 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2675 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002676 SetRawInputAt(0, input);
2677 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002678
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002679 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002680
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002681 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002682 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002683 }
2684
2685 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002686 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002687 }
2688
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002689 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002690
2691 private:
2692 DISALLOW_COPY_AND_ASSIGN(HIf);
2693};
2694
David Brazdilfc6a86a2015-06-26 10:33:45 +00002695
2696// Abstract instruction which marks the beginning and/or end of a try block and
2697// links it to the respective exception handlers. Behaves the same as a Goto in
2698// non-exceptional control flow.
2699// Normal-flow successor is stored at index zero, exception handlers under
2700// higher indices in no particular order.
2701class HTryBoundary : public HTemplateInstruction<0> {
2702 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002703 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002704 kEntry,
2705 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002706 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002707 };
2708
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002709 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002710 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2711 SetPackedField<BoundaryKindField>(kind);
2712 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002713
2714 bool IsControlFlow() const OVERRIDE { return true; }
2715
2716 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002717 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002718
David Brazdild26a4112015-11-10 11:07:31 +00002719 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2720 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2721 }
2722
David Brazdilfc6a86a2015-06-26 10:33:45 +00002723 // Returns whether `handler` is among its exception handlers (non-zero index
2724 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002725 bool HasExceptionHandler(const HBasicBlock& handler) const {
2726 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002727 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002728 }
2729
2730 // If not present already, adds `handler` to its block's list of exception
2731 // handlers.
2732 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002733 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002734 GetBlock()->AddSuccessor(handler);
2735 }
2736 }
2737
Vladimir Markoa1de9182016-02-25 11:37:38 +00002738 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2739 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002740
David Brazdilffee3d32015-07-06 11:48:53 +01002741 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2742
David Brazdilfc6a86a2015-06-26 10:33:45 +00002743 DECLARE_INSTRUCTION(TryBoundary);
2744
2745 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002746 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2747 static constexpr size_t kFieldBoundaryKindSize =
2748 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2749 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2750 kFieldBoundaryKind + kFieldBoundaryKindSize;
2751 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2752 "Too many packed fields.");
2753 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002754
2755 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2756};
2757
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002758// Deoptimize to interpreter, upon checking a condition.
2759class HDeoptimize : public HTemplateInstruction<1> {
2760 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002761 // We set CanTriggerGC to prevent any intermediate address to be live
2762 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002763 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002764 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002765 SetRawInputAt(0, cond);
2766 }
2767
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002768 bool CanBeMoved() const OVERRIDE { return true; }
2769 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2770 return true;
2771 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002772 bool NeedsEnvironment() const OVERRIDE { return true; }
2773 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002774
2775 DECLARE_INSTRUCTION(Deoptimize);
2776
2777 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002778 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2779};
2780
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002781// Represents the ArtMethod that was passed as a first argument to
2782// the method. It is used by instructions that depend on it, like
2783// instructions that work with the dex cache.
2784class HCurrentMethod : public HExpression<0> {
2785 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002786 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2787 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002788
2789 DECLARE_INSTRUCTION(CurrentMethod);
2790
2791 private:
2792 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2793};
2794
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002795// Fetches an ArtMethod from the virtual table or the interface method table
2796// of a class.
2797class HClassTableGet : public HExpression<1> {
2798 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002799 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002800 kVTable,
2801 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002802 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002803 };
2804 HClassTableGet(HInstruction* cls,
2805 Primitive::Type type,
2806 TableKind kind,
2807 size_t index,
2808 uint32_t dex_pc)
2809 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002810 index_(index) {
2811 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002812 SetRawInputAt(0, cls);
2813 }
2814
2815 bool CanBeMoved() const OVERRIDE { return true; }
2816 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2817 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002818 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002819 }
2820
Vladimir Markoa1de9182016-02-25 11:37:38 +00002821 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002822 size_t GetIndex() const { return index_; }
2823
2824 DECLARE_INSTRUCTION(ClassTableGet);
2825
2826 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002827 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2828 static constexpr size_t kFieldTableKindSize =
2829 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2830 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2831 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2832 "Too many packed fields.");
2833 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2834
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002835 // The index of the ArtMethod in the table.
2836 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002837
2838 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2839};
2840
Mark Mendellfe57faa2015-09-18 09:26:15 -04002841// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2842// have one successor for each entry in the switch table, and the final successor
2843// will be the block containing the next Dex opcode.
2844class HPackedSwitch : public HTemplateInstruction<1> {
2845 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002846 HPackedSwitch(int32_t start_value,
2847 uint32_t num_entries,
2848 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002849 uint32_t dex_pc = kNoDexPc)
2850 : HTemplateInstruction(SideEffects::None(), dex_pc),
2851 start_value_(start_value),
2852 num_entries_(num_entries) {
2853 SetRawInputAt(0, input);
2854 }
2855
2856 bool IsControlFlow() const OVERRIDE { return true; }
2857
2858 int32_t GetStartValue() const { return start_value_; }
2859
Vladimir Marko211c2112015-09-24 16:52:33 +01002860 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002861
2862 HBasicBlock* GetDefaultBlock() const {
2863 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002864 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002865 }
2866 DECLARE_INSTRUCTION(PackedSwitch);
2867
2868 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002869 const int32_t start_value_;
2870 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002871
2872 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2873};
2874
Roland Levillain88cb1752014-10-20 16:36:47 +01002875class HUnaryOperation : public HExpression<1> {
2876 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002877 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2878 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002879 SetRawInputAt(0, input);
2880 }
2881
2882 HInstruction* GetInput() const { return InputAt(0); }
2883 Primitive::Type GetResultType() const { return GetType(); }
2884
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002885 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002886 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002887 return true;
2888 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002889
Roland Levillain31dd3d62016-02-16 12:21:02 +00002890 // Try to statically evaluate `this` and return a HConstant
2891 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002892 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002893 HConstant* TryStaticEvaluation() const;
2894
2895 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002896 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2897 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002898 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2899 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002900
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002901 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002902
2903 private:
2904 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2905};
2906
Dave Allison20dfc792014-06-16 20:44:29 -07002907class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002908 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002909 HBinaryOperation(Primitive::Type result_type,
2910 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002911 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002912 SideEffects side_effects = SideEffects::None(),
2913 uint32_t dex_pc = kNoDexPc)
2914 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002915 SetRawInputAt(0, left);
2916 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002917 }
2918
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002919 HInstruction* GetLeft() const { return InputAt(0); }
2920 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002921 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002922
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002923 virtual bool IsCommutative() const { return false; }
2924
2925 // Put constant on the right.
2926 // Returns whether order is changed.
2927 bool OrderInputsWithConstantOnTheRight() {
2928 HInstruction* left = InputAt(0);
2929 HInstruction* right = InputAt(1);
2930 if (left->IsConstant() && !right->IsConstant()) {
2931 ReplaceInput(right, 0);
2932 ReplaceInput(left, 1);
2933 return true;
2934 }
2935 return false;
2936 }
2937
2938 // Order inputs by instruction id, but favor constant on the right side.
2939 // This helps GVN for commutative ops.
2940 void OrderInputs() {
2941 DCHECK(IsCommutative());
2942 HInstruction* left = InputAt(0);
2943 HInstruction* right = InputAt(1);
2944 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2945 return;
2946 }
2947 if (OrderInputsWithConstantOnTheRight()) {
2948 return;
2949 }
2950 // Order according to instruction id.
2951 if (left->GetId() > right->GetId()) {
2952 ReplaceInput(right, 0);
2953 ReplaceInput(left, 1);
2954 }
2955 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002956
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002957 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002958 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002959 return true;
2960 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002961
Roland Levillain31dd3d62016-02-16 12:21:02 +00002962 // Try to statically evaluate `this` and return a HConstant
2963 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002964 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002965 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002966
2967 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002968 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2969 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002970 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2971 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002972 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002973 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2974 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002975 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2976 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002977 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2978 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01002979 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002980 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2981 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002982
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002983 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002984 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002985 HConstant* GetConstantRight() const;
2986
2987 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002988 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002989 HInstruction* GetLeastConstantLeft() const;
2990
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002991 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002992
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002993 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002994 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2995};
2996
Mark Mendellc4701932015-04-10 13:18:51 -04002997// The comparison bias applies for floating point operations and indicates how NaN
2998// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002999enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003000 kNoBias, // bias is not applicable (i.e. for long operation)
3001 kGtBias, // return 1 for NaN comparisons
3002 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003003 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003004};
3005
Roland Levillain31dd3d62016-02-16 12:21:02 +00003006std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3007
Dave Allison20dfc792014-06-16 20:44:29 -07003008class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003009 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003010 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003011 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3012 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3013 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003014
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003015 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003016 // `instruction`, and disregard moves in between.
3017 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003018
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003019 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003020
3021 virtual IfCondition GetCondition() const = 0;
3022
Mark Mendellc4701932015-04-10 13:18:51 -04003023 virtual IfCondition GetOppositeCondition() const = 0;
3024
Vladimir Markoa1de9182016-02-25 11:37:38 +00003025 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003026 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3027
Vladimir Markoa1de9182016-02-25 11:37:38 +00003028 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3029 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003030
3031 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003032 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003033 }
3034
Roland Levillain4fa13f62015-07-06 18:11:54 +01003035 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003036 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003037 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003038 if (if_cond == kCondNE) {
3039 return true;
3040 } else if (if_cond == kCondEQ) {
3041 return false;
3042 }
3043 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003044 }
3045
3046 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003047 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003048 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003049 if (if_cond == kCondEQ) {
3050 return true;
3051 } else if (if_cond == kCondNE) {
3052 return false;
3053 }
3054 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003055 }
3056
Roland Levillain31dd3d62016-02-16 12:21:02 +00003057 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003058 // Needed if we merge a HCompare into a HCondition.
3059 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3060 static constexpr size_t kFieldComparisonBiasSize =
3061 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3062 static constexpr size_t kNumberOfConditionPackedBits =
3063 kFieldComparisonBias + kFieldComparisonBiasSize;
3064 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3065 using ComparisonBiasField =
3066 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3067
Roland Levillain31dd3d62016-02-16 12:21:02 +00003068 template <typename T>
3069 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3070
3071 template <typename T>
3072 int32_t CompareFP(T x, T y) const {
3073 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3074 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3075 // Handle the bias.
3076 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3077 }
3078
3079 // Return an integer constant containing the result of a condition evaluated at compile time.
3080 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3081 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3082 }
3083
Dave Allison20dfc792014-06-16 20:44:29 -07003084 private:
3085 DISALLOW_COPY_AND_ASSIGN(HCondition);
3086};
3087
3088// Instruction to check if two inputs are equal to each other.
3089class HEqual : public HCondition {
3090 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003091 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3092 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003093
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003094 bool IsCommutative() const OVERRIDE { return true; }
3095
Vladimir Marko9e23df52015-11-10 17:14:35 +00003096 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3097 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003098 return MakeConstantCondition(true, GetDexPc());
3099 }
3100 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3101 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3102 }
3103 // In the following Evaluate methods, a HCompare instruction has
3104 // been merged into this HEqual instruction; evaluate it as
3105 // `Compare(x, y) == 0`.
3106 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3107 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3108 GetDexPc());
3109 }
3110 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3111 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3112 }
3113 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3114 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003115 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003116
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003117 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003118
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003119 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003120 return kCondEQ;
3121 }
3122
Mark Mendellc4701932015-04-10 13:18:51 -04003123 IfCondition GetOppositeCondition() const OVERRIDE {
3124 return kCondNE;
3125 }
3126
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003127 private:
Aart Bike9f37602015-10-09 11:15:55 -07003128 template <typename T> bool Compute(T x, T y) const { return x == y; }
3129
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003130 DISALLOW_COPY_AND_ASSIGN(HEqual);
3131};
3132
Dave Allison20dfc792014-06-16 20:44:29 -07003133class HNotEqual : public HCondition {
3134 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003135 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3136 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003137
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003138 bool IsCommutative() const OVERRIDE { return true; }
3139
Vladimir Marko9e23df52015-11-10 17:14:35 +00003140 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3141 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003142 return MakeConstantCondition(false, GetDexPc());
3143 }
3144 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3145 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3146 }
3147 // In the following Evaluate methods, a HCompare instruction has
3148 // been merged into this HNotEqual instruction; evaluate it as
3149 // `Compare(x, y) != 0`.
3150 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3151 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3152 }
3153 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3154 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3155 }
3156 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3157 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003158 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003159
Dave Allison20dfc792014-06-16 20:44:29 -07003160 DECLARE_INSTRUCTION(NotEqual);
3161
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003162 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003163 return kCondNE;
3164 }
3165
Mark Mendellc4701932015-04-10 13:18:51 -04003166 IfCondition GetOppositeCondition() const OVERRIDE {
3167 return kCondEQ;
3168 }
3169
Dave Allison20dfc792014-06-16 20:44:29 -07003170 private:
Aart Bike9f37602015-10-09 11:15:55 -07003171 template <typename T> bool Compute(T x, T y) const { return x != y; }
3172
Dave Allison20dfc792014-06-16 20:44:29 -07003173 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3174};
3175
3176class HLessThan : public HCondition {
3177 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003178 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3179 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003180
Roland Levillain9867bc72015-08-05 10:21:34 +01003181 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003182 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003183 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003184 // In the following Evaluate methods, a HCompare instruction has
3185 // been merged into this HLessThan instruction; evaluate it as
3186 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003187 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003188 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3189 }
3190 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3191 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3192 }
3193 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3194 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003195 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003196
Dave Allison20dfc792014-06-16 20:44:29 -07003197 DECLARE_INSTRUCTION(LessThan);
3198
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003199 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003200 return kCondLT;
3201 }
3202
Mark Mendellc4701932015-04-10 13:18:51 -04003203 IfCondition GetOppositeCondition() const OVERRIDE {
3204 return kCondGE;
3205 }
3206
Dave Allison20dfc792014-06-16 20:44:29 -07003207 private:
Aart Bike9f37602015-10-09 11:15:55 -07003208 template <typename T> bool Compute(T x, T y) const { return x < y; }
3209
Dave Allison20dfc792014-06-16 20:44:29 -07003210 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3211};
3212
3213class HLessThanOrEqual : public HCondition {
3214 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003215 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3216 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003217
Roland Levillain9867bc72015-08-05 10:21:34 +01003218 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003219 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003220 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003221 // In the following Evaluate methods, a HCompare instruction has
3222 // been merged into this HLessThanOrEqual instruction; evaluate it as
3223 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003224 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003225 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3226 }
3227 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3228 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3229 }
3230 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3231 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003232 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003233
Dave Allison20dfc792014-06-16 20:44:29 -07003234 DECLARE_INSTRUCTION(LessThanOrEqual);
3235
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003236 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003237 return kCondLE;
3238 }
3239
Mark Mendellc4701932015-04-10 13:18:51 -04003240 IfCondition GetOppositeCondition() const OVERRIDE {
3241 return kCondGT;
3242 }
3243
Dave Allison20dfc792014-06-16 20:44:29 -07003244 private:
Aart Bike9f37602015-10-09 11:15:55 -07003245 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3246
Dave Allison20dfc792014-06-16 20:44:29 -07003247 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3248};
3249
3250class HGreaterThan : public HCondition {
3251 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003252 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3253 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003254
Roland Levillain9867bc72015-08-05 10:21:34 +01003255 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003256 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003257 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003258 // In the following Evaluate methods, a HCompare instruction has
3259 // been merged into this HGreaterThan instruction; evaluate it as
3260 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003261 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003262 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3263 }
3264 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3265 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3266 }
3267 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3268 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003269 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003270
Dave Allison20dfc792014-06-16 20:44:29 -07003271 DECLARE_INSTRUCTION(GreaterThan);
3272
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003273 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003274 return kCondGT;
3275 }
3276
Mark Mendellc4701932015-04-10 13:18:51 -04003277 IfCondition GetOppositeCondition() const OVERRIDE {
3278 return kCondLE;
3279 }
3280
Dave Allison20dfc792014-06-16 20:44:29 -07003281 private:
Aart Bike9f37602015-10-09 11:15:55 -07003282 template <typename T> bool Compute(T x, T y) const { return x > y; }
3283
Dave Allison20dfc792014-06-16 20:44:29 -07003284 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3285};
3286
3287class HGreaterThanOrEqual : public HCondition {
3288 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003289 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3290 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003291
Roland Levillain9867bc72015-08-05 10:21:34 +01003292 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003293 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003294 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003295 // In the following Evaluate methods, a HCompare instruction has
3296 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3297 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003298 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003299 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3300 }
3301 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3302 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3303 }
3304 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3305 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003306 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003307
Dave Allison20dfc792014-06-16 20:44:29 -07003308 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3309
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003310 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003311 return kCondGE;
3312 }
3313
Mark Mendellc4701932015-04-10 13:18:51 -04003314 IfCondition GetOppositeCondition() const OVERRIDE {
3315 return kCondLT;
3316 }
3317
Dave Allison20dfc792014-06-16 20:44:29 -07003318 private:
Aart Bike9f37602015-10-09 11:15:55 -07003319 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3320
Dave Allison20dfc792014-06-16 20:44:29 -07003321 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3322};
3323
Aart Bike9f37602015-10-09 11:15:55 -07003324class HBelow : public HCondition {
3325 public:
3326 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3327 : HCondition(first, second, dex_pc) {}
3328
3329 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003330 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003331 }
3332 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003333 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3334 }
3335 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3336 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3337 LOG(FATAL) << DebugName() << " is not defined for float values";
3338 UNREACHABLE();
3339 }
3340 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3341 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3342 LOG(FATAL) << DebugName() << " is not defined for double values";
3343 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003344 }
3345
3346 DECLARE_INSTRUCTION(Below);
3347
3348 IfCondition GetCondition() const OVERRIDE {
3349 return kCondB;
3350 }
3351
3352 IfCondition GetOppositeCondition() const OVERRIDE {
3353 return kCondAE;
3354 }
3355
3356 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003357 template <typename T> bool Compute(T x, T y) const {
3358 return MakeUnsigned(x) < MakeUnsigned(y);
3359 }
Aart Bike9f37602015-10-09 11:15:55 -07003360
3361 DISALLOW_COPY_AND_ASSIGN(HBelow);
3362};
3363
3364class HBelowOrEqual : public HCondition {
3365 public:
3366 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3367 : HCondition(first, second, dex_pc) {}
3368
3369 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003370 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003371 }
3372 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003373 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3374 }
3375 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3376 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3377 LOG(FATAL) << DebugName() << " is not defined for float values";
3378 UNREACHABLE();
3379 }
3380 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3381 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3382 LOG(FATAL) << DebugName() << " is not defined for double values";
3383 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003384 }
3385
3386 DECLARE_INSTRUCTION(BelowOrEqual);
3387
3388 IfCondition GetCondition() const OVERRIDE {
3389 return kCondBE;
3390 }
3391
3392 IfCondition GetOppositeCondition() const OVERRIDE {
3393 return kCondA;
3394 }
3395
3396 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003397 template <typename T> bool Compute(T x, T y) const {
3398 return MakeUnsigned(x) <= MakeUnsigned(y);
3399 }
Aart Bike9f37602015-10-09 11:15:55 -07003400
3401 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3402};
3403
3404class HAbove : public HCondition {
3405 public:
3406 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3407 : HCondition(first, second, dex_pc) {}
3408
3409 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003410 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003411 }
3412 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003413 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3414 }
3415 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3416 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3417 LOG(FATAL) << DebugName() << " is not defined for float values";
3418 UNREACHABLE();
3419 }
3420 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3421 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3422 LOG(FATAL) << DebugName() << " is not defined for double values";
3423 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003424 }
3425
3426 DECLARE_INSTRUCTION(Above);
3427
3428 IfCondition GetCondition() const OVERRIDE {
3429 return kCondA;
3430 }
3431
3432 IfCondition GetOppositeCondition() const OVERRIDE {
3433 return kCondBE;
3434 }
3435
3436 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003437 template <typename T> bool Compute(T x, T y) const {
3438 return MakeUnsigned(x) > MakeUnsigned(y);
3439 }
Aart Bike9f37602015-10-09 11:15:55 -07003440
3441 DISALLOW_COPY_AND_ASSIGN(HAbove);
3442};
3443
3444class HAboveOrEqual : public HCondition {
3445 public:
3446 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3447 : HCondition(first, second, dex_pc) {}
3448
3449 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003450 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003451 }
3452 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003453 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3454 }
3455 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3456 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3457 LOG(FATAL) << DebugName() << " is not defined for float values";
3458 UNREACHABLE();
3459 }
3460 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3461 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3462 LOG(FATAL) << DebugName() << " is not defined for double values";
3463 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003464 }
3465
3466 DECLARE_INSTRUCTION(AboveOrEqual);
3467
3468 IfCondition GetCondition() const OVERRIDE {
3469 return kCondAE;
3470 }
3471
3472 IfCondition GetOppositeCondition() const OVERRIDE {
3473 return kCondB;
3474 }
3475
3476 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003477 template <typename T> bool Compute(T x, T y) const {
3478 return MakeUnsigned(x) >= MakeUnsigned(y);
3479 }
Aart Bike9f37602015-10-09 11:15:55 -07003480
3481 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3482};
Dave Allison20dfc792014-06-16 20:44:29 -07003483
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003484// Instruction to check how two inputs compare to each other.
3485// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3486class HCompare : public HBinaryOperation {
3487 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003488 // Note that `comparison_type` is the type of comparison performed
3489 // between the comparison's inputs, not the type of the instantiated
3490 // HCompare instruction (which is always Primitive::kPrimInt).
3491 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003492 HInstruction* first,
3493 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003494 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003495 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003496 : HBinaryOperation(Primitive::kPrimInt,
3497 first,
3498 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003499 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003500 dex_pc) {
3501 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003502 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3503 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003504 }
3505
Roland Levillain9867bc72015-08-05 10:21:34 +01003506 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003507 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3508
3509 template <typename T>
3510 int32_t ComputeFP(T x, T y) const {
3511 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3512 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3513 // Handle the bias.
3514 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3515 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003516
Roland Levillain9867bc72015-08-05 10:21:34 +01003517 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003518 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3519 // reach this code path when processing a freshly built HIR
3520 // graph. However HCompare integer instructions can be synthesized
3521 // by the instruction simplifier to implement IntegerCompare and
3522 // IntegerSignum intrinsics, so we have to handle this case.
3523 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003524 }
3525 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003526 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3527 }
3528 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3529 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3530 }
3531 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3532 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003533 }
3534
Calin Juravleddb7df22014-11-25 20:56:51 +00003535 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003536 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003537 }
3538
Vladimir Markoa1de9182016-02-25 11:37:38 +00003539 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003540
Roland Levillain31dd3d62016-02-16 12:21:02 +00003541 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003542 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003543 bool IsGtBias() const {
3544 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003545 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003546 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003547
Roland Levillain1693a1f2016-03-15 14:57:31 +00003548 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3549 // Comparisons do not require a runtime call in any back end.
3550 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003551 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003552
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003553 DECLARE_INSTRUCTION(Compare);
3554
Roland Levillain31dd3d62016-02-16 12:21:02 +00003555 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003556 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3557 static constexpr size_t kFieldComparisonBiasSize =
3558 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3559 static constexpr size_t kNumberOfComparePackedBits =
3560 kFieldComparisonBias + kFieldComparisonBiasSize;
3561 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3562 using ComparisonBiasField =
3563 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3564
Roland Levillain31dd3d62016-02-16 12:21:02 +00003565 // Return an integer constant containing the result of a comparison evaluated at compile time.
3566 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3567 DCHECK(value == -1 || value == 0 || value == 1) << value;
3568 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3569 }
3570
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003571 private:
3572 DISALLOW_COPY_AND_ASSIGN(HCompare);
3573};
3574
David Brazdil6de19382016-01-08 17:37:10 +00003575class HNewInstance : public HExpression<2> {
3576 public:
3577 HNewInstance(HInstruction* cls,
3578 HCurrentMethod* current_method,
3579 uint32_t dex_pc,
3580 uint16_t type_index,
3581 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003582 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003583 bool finalizable,
3584 QuickEntrypointEnum entrypoint)
3585 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3586 type_index_(type_index),
3587 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003588 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003589 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003590 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003591 SetRawInputAt(0, cls);
3592 SetRawInputAt(1, current_method);
3593 }
3594
3595 uint16_t GetTypeIndex() const { return type_index_; }
3596 const DexFile& GetDexFile() const { return dex_file_; }
3597
3598 // Calls runtime so needs an environment.
3599 bool NeedsEnvironment() const OVERRIDE { return true; }
3600
Mingyao Yang062157f2016-03-02 10:15:36 -08003601 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3602 bool CanThrow() const OVERRIDE { return true; }
3603
3604 // Needs to call into runtime to make sure it's instantiable/accessible.
3605 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003606
Vladimir Markoa1de9182016-02-25 11:37:38 +00003607 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003608
3609 bool CanBeNull() const OVERRIDE { return false; }
3610
3611 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3612
3613 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3614 entrypoint_ = entrypoint;
3615 }
3616
3617 bool IsStringAlloc() const;
3618
3619 DECLARE_INSTRUCTION(NewInstance);
3620
3621 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003622 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3623 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003624 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3625 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3626 "Too many packed fields.");
3627
David Brazdil6de19382016-01-08 17:37:10 +00003628 const uint16_t type_index_;
3629 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003630 QuickEntrypointEnum entrypoint_;
3631
3632 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3633};
3634
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003635enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003636#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3637 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003638#include "intrinsics_list.h"
3639 kNone,
3640 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3641#undef INTRINSICS_LIST
3642#undef OPTIMIZING_INTRINSICS
3643};
3644std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3645
Agi Csaki05f20562015-08-19 14:58:14 -07003646enum IntrinsicNeedsEnvironmentOrCache {
3647 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3648 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003649};
3650
Aart Bik5d75afe2015-12-14 11:57:01 -08003651enum IntrinsicSideEffects {
3652 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3653 kReadSideEffects, // Intrinsic may read heap memory.
3654 kWriteSideEffects, // Intrinsic may write heap memory.
3655 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3656};
3657
3658enum IntrinsicExceptions {
3659 kNoThrow, // Intrinsic does not throw any exceptions.
3660 kCanThrow // Intrinsic may throw exceptions.
3661};
3662
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003663class HInvoke : public HInstruction {
3664 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003665 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003666
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003667 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003668
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003669 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003670 SetRawInputAt(index, argument);
3671 }
3672
Roland Levillain3e3d7332015-04-28 11:00:54 +01003673 // Return the number of arguments. This number can be lower than
3674 // the number of inputs returned by InputCount(), as some invoke
3675 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3676 // inputs at the end of their list of inputs.
3677 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3678
Vladimir Markoa1de9182016-02-25 11:37:38 +00003679 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003680
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003681 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003682 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003683
Vladimir Markoa1de9182016-02-25 11:37:38 +00003684 InvokeType GetOriginalInvokeType() const {
3685 return GetPackedField<OriginalInvokeTypeField>();
3686 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003687
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003688 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003689 return intrinsic_;
3690 }
3691
Aart Bik5d75afe2015-12-14 11:57:01 -08003692 void SetIntrinsic(Intrinsics intrinsic,
3693 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3694 IntrinsicSideEffects side_effects,
3695 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003696
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003697 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003698 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003699 }
3700
Vladimir Markoa1de9182016-02-25 11:37:38 +00003701 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003702
3703 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3704
3705 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3706 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3707 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003708
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003709 uint32_t* GetIntrinsicOptimizations() {
3710 return &intrinsic_optimizations_;
3711 }
3712
3713 const uint32_t* GetIntrinsicOptimizations() const {
3714 return &intrinsic_optimizations_;
3715 }
3716
3717 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3718
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003719 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003720
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003721 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003722 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3723 static constexpr size_t kFieldOriginalInvokeTypeSize =
3724 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3725 static constexpr size_t kFieldReturnType =
3726 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3727 static constexpr size_t kFieldReturnTypeSize =
3728 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3729 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3730 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3731 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3732 using OriginalInvokeTypeField =
3733 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3734 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3735
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003736 HInvoke(ArenaAllocator* arena,
3737 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003738 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003739 Primitive::Type return_type,
3740 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003741 uint32_t dex_method_index,
3742 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003743 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003744 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003745 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003746 inputs_(number_of_arguments + number_of_other_inputs,
3747 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003748 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003749 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003750 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003751 SetPackedField<ReturnTypeField>(return_type);
3752 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3753 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003754 }
3755
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003756 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003757 return inputs_[index];
3758 }
3759
David Brazdil1abb4192015-02-17 18:33:36 +00003760 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003761 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003762 }
3763
Vladimir Markoa1de9182016-02-25 11:37:38 +00003764 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003765
Roland Levillain3e3d7332015-04-28 11:00:54 +01003766 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003767 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003768 const uint32_t dex_method_index_;
3769 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003770
3771 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3772 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003773
3774 private:
3775 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3776};
3777
Calin Juravle175dc732015-08-25 15:42:32 +01003778class HInvokeUnresolved : public HInvoke {
3779 public:
3780 HInvokeUnresolved(ArenaAllocator* arena,
3781 uint32_t number_of_arguments,
3782 Primitive::Type return_type,
3783 uint32_t dex_pc,
3784 uint32_t dex_method_index,
3785 InvokeType invoke_type)
3786 : HInvoke(arena,
3787 number_of_arguments,
3788 0u /* number_of_other_inputs */,
3789 return_type,
3790 dex_pc,
3791 dex_method_index,
3792 invoke_type) {
3793 }
3794
3795 DECLARE_INSTRUCTION(InvokeUnresolved);
3796
3797 private:
3798 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3799};
3800
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003801class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003802 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003803 // Requirements of this method call regarding the class
3804 // initialization (clinit) check of its declaring class.
3805 enum class ClinitCheckRequirement {
3806 kNone, // Class already initialized.
3807 kExplicit, // Static call having explicit clinit check as last input.
3808 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003809 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003810 };
3811
Vladimir Marko58155012015-08-19 12:49:41 +00003812 // Determines how to load the target ArtMethod*.
3813 enum class MethodLoadKind {
3814 // Use a String init ArtMethod* loaded from Thread entrypoints.
3815 kStringInit,
3816
3817 // Use the method's own ArtMethod* loaded by the register allocator.
3818 kRecursive,
3819
3820 // Use ArtMethod* at a known address, embed the direct address in the code.
3821 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3822 kDirectAddress,
3823
3824 // Use ArtMethod* at an address that will be known at link time, embed the direct
3825 // address in the code. If the image is relocatable, emit .patch_oat entry.
3826 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3827 // the image relocatable or not.
3828 kDirectAddressWithFixup,
3829
Vladimir Markoa1de9182016-02-25 11:37:38 +00003830 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003831 // Used when we need to use the dex cache, for example for invoke-static that
3832 // may cause class initialization (the entry may point to a resolution method),
3833 // and we know that we can access the dex cache arrays using a PC-relative load.
3834 kDexCachePcRelative,
3835
3836 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3837 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3838 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3839 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3840 kDexCacheViaMethod,
3841 };
3842
3843 // Determines the location of the code pointer.
3844 enum class CodePtrLocation {
3845 // Recursive call, use local PC-relative call instruction.
3846 kCallSelf,
3847
3848 // Use PC-relative call instruction patched at link time.
3849 // Used for calls within an oat file, boot->boot or app->app.
3850 kCallPCRelative,
3851
3852 // Call to a known target address, embed the direct address in code.
3853 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3854 kCallDirect,
3855
3856 // Call to a target address that will be known at link time, embed the direct
3857 // address in code. If the image is relocatable, emit .patch_oat entry.
3858 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3859 // the image relocatable or not.
3860 kCallDirectWithFixup,
3861
3862 // Use code pointer from the ArtMethod*.
3863 // Used when we don't know the target code. This is also the last-resort-kind used when
3864 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3865 kCallArtMethod,
3866 };
3867
3868 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003869 MethodLoadKind method_load_kind;
3870 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003871 // The method load data holds
3872 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3873 // Note that there are multiple string init methods, each having its own offset.
3874 // - the method address for kDirectAddress
3875 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003876 uint64_t method_load_data;
3877 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003878 };
3879
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003880 HInvokeStaticOrDirect(ArenaAllocator* arena,
3881 uint32_t number_of_arguments,
3882 Primitive::Type return_type,
3883 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003884 uint32_t method_index,
3885 MethodReference target_method,
3886 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003887 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003888 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003889 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003890 : HInvoke(arena,
3891 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003892 // There is potentially one extra argument for the HCurrentMethod node, and
3893 // potentially one other if the clinit check is explicit, and potentially
3894 // one other if the method is a string factory.
3895 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003896 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003897 return_type,
3898 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003899 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003900 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003901 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003902 dispatch_info_(dispatch_info) {
3903 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3904 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3905 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003906
Vladimir Markodc151b22015-10-15 18:02:30 +01003907 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003908 bool had_current_method_input = HasCurrentMethodInput();
3909 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3910
3911 // Using the current method is the default and once we find a better
3912 // method load kind, we should not go back to using the current method.
3913 DCHECK(had_current_method_input || !needs_current_method_input);
3914
3915 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003916 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3917 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003918 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003919 dispatch_info_ = dispatch_info;
3920 }
3921
Vladimir Markoc53c0792015-11-19 15:48:33 +00003922 void AddSpecialInput(HInstruction* input) {
3923 // We allow only one special input.
3924 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3925 DCHECK(InputCount() == GetSpecialInputIndex() ||
3926 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3927 InsertInputAt(GetSpecialInputIndex(), input);
3928 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003929
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003930 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003931 // We access the method via the dex cache so we can't do an implicit null check.
3932 // TODO: for intrinsics we can generate implicit null checks.
3933 return false;
3934 }
3935
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003936 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003937 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003938 }
3939
Vladimir Markoc53c0792015-11-19 15:48:33 +00003940 // Get the index of the special input, if any.
3941 //
David Brazdil6de19382016-01-08 17:37:10 +00003942 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3943 // method pointer; otherwise there may be one platform-specific special input,
3944 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003945 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003946 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003947
Vladimir Markoa1de9182016-02-25 11:37:38 +00003948 InvokeType GetOptimizedInvokeType() const {
3949 return GetPackedField<OptimizedInvokeTypeField>();
3950 }
3951
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003952 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003953 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003954 }
3955
Vladimir Marko58155012015-08-19 12:49:41 +00003956 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3957 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3958 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003959 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003960 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003961 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003962 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003963 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3964 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003965 bool HasCurrentMethodInput() const {
3966 // This function can be called only after the invoke has been fully initialized by the builder.
3967 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003968 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003969 return true;
3970 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003971 DCHECK(InputCount() == GetSpecialInputIndex() ||
3972 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003973 return false;
3974 }
3975 }
Vladimir Marko58155012015-08-19 12:49:41 +00003976 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3977 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003978 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003979
3980 int32_t GetStringInitOffset() const {
3981 DCHECK(IsStringInit());
3982 return dispatch_info_.method_load_data;
3983 }
3984
3985 uint64_t GetMethodAddress() const {
3986 DCHECK(HasMethodAddress());
3987 return dispatch_info_.method_load_data;
3988 }
3989
3990 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003991 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003992 return dispatch_info_.method_load_data;
3993 }
3994
3995 uint64_t GetDirectCodePtr() const {
3996 DCHECK(HasDirectCodePtr());
3997 return dispatch_info_.direct_code_ptr;
3998 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003999
Vladimir Markoa1de9182016-02-25 11:37:38 +00004000 ClinitCheckRequirement GetClinitCheckRequirement() const {
4001 return GetPackedField<ClinitCheckRequirementField>();
4002 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004003
Roland Levillain4c0eb422015-04-24 16:43:49 +01004004 // Is this instruction a call to a static method?
4005 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004006 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004007 }
4008
Vladimir Markofbb184a2015-11-13 14:47:00 +00004009 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4010 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4011 // instruction; only relevant for static calls with explicit clinit check.
4012 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004013 DCHECK(IsStaticWithExplicitClinitCheck());
4014 size_t last_input_index = InputCount() - 1;
4015 HInstruction* last_input = InputAt(last_input_index);
4016 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004017 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004018 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004019 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004020 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004021 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004022 }
4023
4024 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004025 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004026 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004027 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004028 }
4029
4030 // Is this a call to a static method whose declaring class has an
4031 // implicit intialization check requirement?
4032 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004033 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004034 }
4035
Vladimir Markob554b5a2015-11-06 12:57:55 +00004036 // Does this method load kind need the current method as an input?
4037 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4038 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4039 }
4040
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004041 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004042
Roland Levillain4c0eb422015-04-24 16:43:49 +01004043 protected:
4044 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
4045 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
4046 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
4047 HInstruction* input = input_record.GetInstruction();
4048 // `input` is the last input of a static invoke marked as having
4049 // an explicit clinit check. It must either be:
4050 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4051 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4052 DCHECK(input != nullptr);
4053 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
4054 }
4055 return input_record;
4056 }
4057
Vladimir Markoc53c0792015-11-19 15:48:33 +00004058 void InsertInputAt(size_t index, HInstruction* input);
4059 void RemoveInputAt(size_t index);
4060
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004061 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004062 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4063 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4064 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4065 static constexpr size_t kFieldClinitCheckRequirement =
4066 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4067 static constexpr size_t kFieldClinitCheckRequirementSize =
4068 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4069 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4070 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4071 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4072 "Too many packed fields.");
4073 using OptimizedInvokeTypeField =
4074 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4075 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4076 kFieldClinitCheckRequirement,
4077 kFieldClinitCheckRequirementSize>;
4078
Vladimir Marko58155012015-08-19 12:49:41 +00004079 // The target method may refer to different dex file or method index than the original
4080 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4081 // in derived class to increase visibility.
4082 MethodReference target_method_;
4083 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004084
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004085 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004086};
Vladimir Markof64242a2015-12-01 14:58:23 +00004087std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004088std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004089
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004090class HInvokeVirtual : public HInvoke {
4091 public:
4092 HInvokeVirtual(ArenaAllocator* arena,
4093 uint32_t number_of_arguments,
4094 Primitive::Type return_type,
4095 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004096 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004097 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004098 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004099 vtable_index_(vtable_index) {}
4100
Calin Juravle641547a2015-04-21 22:08:51 +01004101 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004102 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004103 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004104 }
4105
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004106 uint32_t GetVTableIndex() const { return vtable_index_; }
4107
4108 DECLARE_INSTRUCTION(InvokeVirtual);
4109
4110 private:
4111 const uint32_t vtable_index_;
4112
4113 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4114};
4115
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004116class HInvokeInterface : public HInvoke {
4117 public:
4118 HInvokeInterface(ArenaAllocator* arena,
4119 uint32_t number_of_arguments,
4120 Primitive::Type return_type,
4121 uint32_t dex_pc,
4122 uint32_t dex_method_index,
4123 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004124 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004125 imt_index_(imt_index) {}
4126
Calin Juravle641547a2015-04-21 22:08:51 +01004127 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004128 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004129 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004130 }
4131
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004132 uint32_t GetImtIndex() const { return imt_index_; }
4133 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4134
4135 DECLARE_INSTRUCTION(InvokeInterface);
4136
4137 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004138 const uint32_t imt_index_;
4139
4140 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4141};
4142
Roland Levillain88cb1752014-10-20 16:36:47 +01004143class HNeg : public HUnaryOperation {
4144 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004145 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004146 : HUnaryOperation(result_type, input, dex_pc) {
4147 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4148 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004149
Roland Levillain9867bc72015-08-05 10:21:34 +01004150 template <typename T> T Compute(T x) const { return -x; }
4151
4152 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004153 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004154 }
4155 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004156 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004157 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004158 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4159 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4160 }
4161 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4162 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4163 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004164
Roland Levillain88cb1752014-10-20 16:36:47 +01004165 DECLARE_INSTRUCTION(Neg);
4166
4167 private:
4168 DISALLOW_COPY_AND_ASSIGN(HNeg);
4169};
4170
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004171class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004172 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004173 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004174 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004175 uint32_t dex_pc,
4176 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004177 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004178 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004179 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004180 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004181 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004182 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004183 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004184 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004185 }
4186
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004187 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004188 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004189
4190 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004191 bool NeedsEnvironment() const OVERRIDE { return true; }
4192
Mingyao Yang0c365e62015-03-31 15:09:29 -07004193 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4194 bool CanThrow() const OVERRIDE { return true; }
4195
Calin Juravle10e244f2015-01-26 18:54:32 +00004196 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004197
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004198 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4199
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004200 DECLARE_INSTRUCTION(NewArray);
4201
4202 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004203 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004204 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004205 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004206
4207 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4208};
4209
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004210class HAdd : public HBinaryOperation {
4211 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004212 HAdd(Primitive::Type result_type,
4213 HInstruction* left,
4214 HInstruction* right,
4215 uint32_t dex_pc = kNoDexPc)
4216 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004217
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004218 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004219
Roland Levillain9867bc72015-08-05 10:21:34 +01004220 template <typename T> T Compute(T x, T y) const { return x + y; }
4221
4222 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004223 return GetBlock()->GetGraph()->GetIntConstant(
4224 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004225 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004226 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004227 return GetBlock()->GetGraph()->GetLongConstant(
4228 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004229 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004230 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4231 return GetBlock()->GetGraph()->GetFloatConstant(
4232 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4233 }
4234 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4235 return GetBlock()->GetGraph()->GetDoubleConstant(
4236 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4237 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004238
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004239 DECLARE_INSTRUCTION(Add);
4240
4241 private:
4242 DISALLOW_COPY_AND_ASSIGN(HAdd);
4243};
4244
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004245class HSub : public HBinaryOperation {
4246 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004247 HSub(Primitive::Type result_type,
4248 HInstruction* left,
4249 HInstruction* right,
4250 uint32_t dex_pc = kNoDexPc)
4251 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004252
Roland Levillain9867bc72015-08-05 10:21:34 +01004253 template <typename T> T Compute(T x, T y) const { return x - y; }
4254
4255 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004256 return GetBlock()->GetGraph()->GetIntConstant(
4257 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004258 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004259 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004260 return GetBlock()->GetGraph()->GetLongConstant(
4261 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004262 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004263 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4264 return GetBlock()->GetGraph()->GetFloatConstant(
4265 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4266 }
4267 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4268 return GetBlock()->GetGraph()->GetDoubleConstant(
4269 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4270 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004271
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004272 DECLARE_INSTRUCTION(Sub);
4273
4274 private:
4275 DISALLOW_COPY_AND_ASSIGN(HSub);
4276};
4277
Calin Juravle34bacdf2014-10-07 20:23:36 +01004278class HMul : public HBinaryOperation {
4279 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004280 HMul(Primitive::Type result_type,
4281 HInstruction* left,
4282 HInstruction* right,
4283 uint32_t dex_pc = kNoDexPc)
4284 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004285
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004286 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004287
Roland Levillain9867bc72015-08-05 10:21:34 +01004288 template <typename T> T Compute(T x, T y) const { return x * y; }
4289
4290 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004291 return GetBlock()->GetGraph()->GetIntConstant(
4292 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004293 }
4294 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004295 return GetBlock()->GetGraph()->GetLongConstant(
4296 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004297 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004298 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4299 return GetBlock()->GetGraph()->GetFloatConstant(
4300 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4301 }
4302 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4303 return GetBlock()->GetGraph()->GetDoubleConstant(
4304 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4305 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004306
4307 DECLARE_INSTRUCTION(Mul);
4308
4309 private:
4310 DISALLOW_COPY_AND_ASSIGN(HMul);
4311};
4312
Calin Juravle7c4954d2014-10-28 16:57:40 +00004313class HDiv : public HBinaryOperation {
4314 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004315 HDiv(Primitive::Type result_type,
4316 HInstruction* left,
4317 HInstruction* right,
4318 uint32_t dex_pc)
4319 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004320
Roland Levillain9867bc72015-08-05 10:21:34 +01004321 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004322 T ComputeIntegral(T x, T y) const {
4323 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004324 // Our graph structure ensures we never have 0 for `y` during
4325 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004326 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004327 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004328 return (y == -1) ? -x : x / y;
4329 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004330
Roland Levillain31dd3d62016-02-16 12:21:02 +00004331 template <typename T>
4332 T ComputeFP(T x, T y) const {
4333 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4334 return x / y;
4335 }
4336
Roland Levillain9867bc72015-08-05 10:21:34 +01004337 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004338 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004339 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004340 }
4341 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004342 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004343 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4344 }
4345 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4346 return GetBlock()->GetGraph()->GetFloatConstant(
4347 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4348 }
4349 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4350 return GetBlock()->GetGraph()->GetDoubleConstant(
4351 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004352 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004353
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004354 static SideEffects SideEffectsForArchRuntimeCalls() {
4355 // The generated code can use a runtime call.
4356 return SideEffects::CanTriggerGC();
4357 }
4358
Calin Juravle7c4954d2014-10-28 16:57:40 +00004359 DECLARE_INSTRUCTION(Div);
4360
4361 private:
4362 DISALLOW_COPY_AND_ASSIGN(HDiv);
4363};
4364
Calin Juravlebacfec32014-11-14 15:54:36 +00004365class HRem : public HBinaryOperation {
4366 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004367 HRem(Primitive::Type result_type,
4368 HInstruction* left,
4369 HInstruction* right,
4370 uint32_t dex_pc)
4371 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004372
Roland Levillain9867bc72015-08-05 10:21:34 +01004373 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004374 T ComputeIntegral(T x, T y) const {
4375 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004376 // Our graph structure ensures we never have 0 for `y` during
4377 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004378 DCHECK_NE(y, 0);
4379 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4380 return (y == -1) ? 0 : x % y;
4381 }
4382
Roland Levillain31dd3d62016-02-16 12:21:02 +00004383 template <typename T>
4384 T ComputeFP(T x, T y) const {
4385 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4386 return std::fmod(x, y);
4387 }
4388
Roland Levillain9867bc72015-08-05 10:21:34 +01004389 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004390 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004391 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004392 }
4393 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004394 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004395 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004396 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004397 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4398 return GetBlock()->GetGraph()->GetFloatConstant(
4399 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4400 }
4401 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4402 return GetBlock()->GetGraph()->GetDoubleConstant(
4403 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4404 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004405
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004406 static SideEffects SideEffectsForArchRuntimeCalls() {
4407 return SideEffects::CanTriggerGC();
4408 }
4409
Calin Juravlebacfec32014-11-14 15:54:36 +00004410 DECLARE_INSTRUCTION(Rem);
4411
4412 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004413 DISALLOW_COPY_AND_ASSIGN(HRem);
4414};
4415
Calin Juravled0d48522014-11-04 16:40:20 +00004416class HDivZeroCheck : public HExpression<1> {
4417 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004418 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4419 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004420 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004421 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004422 SetRawInputAt(0, value);
4423 }
4424
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004425 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4426
Calin Juravled0d48522014-11-04 16:40:20 +00004427 bool CanBeMoved() const OVERRIDE { return true; }
4428
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004429 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004430 return true;
4431 }
4432
4433 bool NeedsEnvironment() const OVERRIDE { return true; }
4434 bool CanThrow() const OVERRIDE { return true; }
4435
Calin Juravled0d48522014-11-04 16:40:20 +00004436 DECLARE_INSTRUCTION(DivZeroCheck);
4437
4438 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004439 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4440};
4441
Calin Juravle9aec02f2014-11-18 23:06:35 +00004442class HShl : public HBinaryOperation {
4443 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004444 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004445 HInstruction* value,
4446 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004447 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004448 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4449 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4450 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004451 }
4452
Roland Levillain5b5b9312016-03-22 14:57:31 +00004453 template <typename T>
4454 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4455 return value << (distance & max_shift_distance);
4456 }
4457
4458 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004459 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004460 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004461 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004462 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004463 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004464 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004465 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004466 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4467 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4468 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4469 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004470 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004471 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4472 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004473 LOG(FATAL) << DebugName() << " is not defined for float values";
4474 UNREACHABLE();
4475 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004476 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4477 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004478 LOG(FATAL) << DebugName() << " is not defined for double values";
4479 UNREACHABLE();
4480 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004481
4482 DECLARE_INSTRUCTION(Shl);
4483
4484 private:
4485 DISALLOW_COPY_AND_ASSIGN(HShl);
4486};
4487
4488class HShr : public HBinaryOperation {
4489 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004490 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004491 HInstruction* value,
4492 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004493 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004494 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4495 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4496 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004497 }
4498
Roland Levillain5b5b9312016-03-22 14:57:31 +00004499 template <typename T>
4500 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4501 return value >> (distance & max_shift_distance);
4502 }
4503
4504 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004505 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004506 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004507 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004508 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004509 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004510 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004511 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004512 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4513 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4514 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4515 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004516 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004517 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4518 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004519 LOG(FATAL) << DebugName() << " is not defined for float values";
4520 UNREACHABLE();
4521 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004522 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4523 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004524 LOG(FATAL) << DebugName() << " is not defined for double values";
4525 UNREACHABLE();
4526 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004527
4528 DECLARE_INSTRUCTION(Shr);
4529
4530 private:
4531 DISALLOW_COPY_AND_ASSIGN(HShr);
4532};
4533
4534class HUShr : public HBinaryOperation {
4535 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004536 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004537 HInstruction* value,
4538 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004539 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004540 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4541 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4542 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004543 }
4544
Roland Levillain5b5b9312016-03-22 14:57:31 +00004545 template <typename T>
4546 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4547 typedef typename std::make_unsigned<T>::type V;
4548 V ux = static_cast<V>(value);
4549 return static_cast<T>(ux >> (distance & max_shift_distance));
4550 }
4551
4552 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004553 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004554 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004555 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004556 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004557 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004558 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004559 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004560 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4561 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4562 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4563 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004564 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004565 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4566 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004567 LOG(FATAL) << DebugName() << " is not defined for float values";
4568 UNREACHABLE();
4569 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004570 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4571 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004572 LOG(FATAL) << DebugName() << " is not defined for double values";
4573 UNREACHABLE();
4574 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004575
4576 DECLARE_INSTRUCTION(UShr);
4577
4578 private:
4579 DISALLOW_COPY_AND_ASSIGN(HUShr);
4580};
4581
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004582class HAnd : public HBinaryOperation {
4583 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004584 HAnd(Primitive::Type result_type,
4585 HInstruction* left,
4586 HInstruction* right,
4587 uint32_t dex_pc = kNoDexPc)
4588 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004589
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004590 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004591
Roland Levillaine53bd812016-02-24 14:54:18 +00004592 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004593
4594 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004595 return GetBlock()->GetGraph()->GetIntConstant(
4596 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004597 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004598 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004599 return GetBlock()->GetGraph()->GetLongConstant(
4600 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004601 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004602 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4603 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4604 LOG(FATAL) << DebugName() << " is not defined for float values";
4605 UNREACHABLE();
4606 }
4607 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4608 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4609 LOG(FATAL) << DebugName() << " is not defined for double values";
4610 UNREACHABLE();
4611 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004612
4613 DECLARE_INSTRUCTION(And);
4614
4615 private:
4616 DISALLOW_COPY_AND_ASSIGN(HAnd);
4617};
4618
4619class HOr : public HBinaryOperation {
4620 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004621 HOr(Primitive::Type result_type,
4622 HInstruction* left,
4623 HInstruction* right,
4624 uint32_t dex_pc = kNoDexPc)
4625 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004626
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004627 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004628
Roland Levillaine53bd812016-02-24 14:54:18 +00004629 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004630
4631 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004632 return GetBlock()->GetGraph()->GetIntConstant(
4633 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004634 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004635 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004636 return GetBlock()->GetGraph()->GetLongConstant(
4637 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004638 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004639 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4640 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4641 LOG(FATAL) << DebugName() << " is not defined for float values";
4642 UNREACHABLE();
4643 }
4644 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4645 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4646 LOG(FATAL) << DebugName() << " is not defined for double values";
4647 UNREACHABLE();
4648 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004649
4650 DECLARE_INSTRUCTION(Or);
4651
4652 private:
4653 DISALLOW_COPY_AND_ASSIGN(HOr);
4654};
4655
4656class HXor : public HBinaryOperation {
4657 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004658 HXor(Primitive::Type result_type,
4659 HInstruction* left,
4660 HInstruction* right,
4661 uint32_t dex_pc = kNoDexPc)
4662 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004663
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004664 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004665
Roland Levillaine53bd812016-02-24 14:54:18 +00004666 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004667
4668 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004669 return GetBlock()->GetGraph()->GetIntConstant(
4670 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004671 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004672 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004673 return GetBlock()->GetGraph()->GetLongConstant(
4674 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004675 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004676 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4677 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4678 LOG(FATAL) << DebugName() << " is not defined for float values";
4679 UNREACHABLE();
4680 }
4681 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4682 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4683 LOG(FATAL) << DebugName() << " is not defined for double values";
4684 UNREACHABLE();
4685 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004686
4687 DECLARE_INSTRUCTION(Xor);
4688
4689 private:
4690 DISALLOW_COPY_AND_ASSIGN(HXor);
4691};
4692
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004693class HRor : public HBinaryOperation {
4694 public:
4695 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004696 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004697 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4698 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004699 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004700
Roland Levillain5b5b9312016-03-22 14:57:31 +00004701 template <typename T>
4702 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4703 typedef typename std::make_unsigned<T>::type V;
4704 V ux = static_cast<V>(value);
4705 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004706 return static_cast<T>(ux);
4707 } else {
4708 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004709 return static_cast<T>(ux >> (distance & max_shift_value)) |
4710 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004711 }
4712 }
4713
Roland Levillain5b5b9312016-03-22 14:57:31 +00004714 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004715 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004716 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004717 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004718 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004719 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004720 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004721 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004722 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4723 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4724 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4725 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004726 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004727 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4728 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004729 LOG(FATAL) << DebugName() << " is not defined for float values";
4730 UNREACHABLE();
4731 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004732 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4733 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004734 LOG(FATAL) << DebugName() << " is not defined for double values";
4735 UNREACHABLE();
4736 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004737
4738 DECLARE_INSTRUCTION(Ror);
4739
4740 private:
4741 DISALLOW_COPY_AND_ASSIGN(HRor);
4742};
4743
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004744// The value of a parameter in this method. Its location depends on
4745// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004746class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004747 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004748 HParameterValue(const DexFile& dex_file,
4749 uint16_t type_index,
4750 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004751 Primitive::Type parameter_type,
4752 bool is_this = false)
4753 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004754 dex_file_(dex_file),
4755 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004756 index_(index) {
4757 SetPackedFlag<kFlagIsThis>(is_this);
4758 SetPackedFlag<kFlagCanBeNull>(!is_this);
4759 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004760
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004761 const DexFile& GetDexFile() const { return dex_file_; }
4762 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004763 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004764 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004765
Vladimir Markoa1de9182016-02-25 11:37:38 +00004766 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4767 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004768
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004769 DECLARE_INSTRUCTION(ParameterValue);
4770
4771 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004772 // Whether or not the parameter value corresponds to 'this' argument.
4773 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4774 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4775 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4776 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4777 "Too many packed fields.");
4778
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004779 const DexFile& dex_file_;
4780 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004781 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004782 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004783 const uint8_t index_;
4784
4785 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4786};
4787
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004788class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004789 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004790 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4791 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004792
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004793 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004794 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004795 return true;
4796 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004797
Roland Levillain9867bc72015-08-05 10:21:34 +01004798 template <typename T> T Compute(T x) const { return ~x; }
4799
4800 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004801 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004802 }
4803 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004804 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004805 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004806 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4807 LOG(FATAL) << DebugName() << " is not defined for float values";
4808 UNREACHABLE();
4809 }
4810 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4811 LOG(FATAL) << DebugName() << " is not defined for double values";
4812 UNREACHABLE();
4813 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004814
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004815 DECLARE_INSTRUCTION(Not);
4816
4817 private:
4818 DISALLOW_COPY_AND_ASSIGN(HNot);
4819};
4820
David Brazdil66d126e2015-04-03 16:02:44 +01004821class HBooleanNot : public HUnaryOperation {
4822 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004823 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4824 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004825
4826 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004827 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004828 return true;
4829 }
4830
Roland Levillain9867bc72015-08-05 10:21:34 +01004831 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004832 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004833 return !x;
4834 }
4835
Roland Levillain9867bc72015-08-05 10:21:34 +01004836 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004837 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004838 }
4839 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4840 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004841 UNREACHABLE();
4842 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004843 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4844 LOG(FATAL) << DebugName() << " is not defined for float values";
4845 UNREACHABLE();
4846 }
4847 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4848 LOG(FATAL) << DebugName() << " is not defined for double values";
4849 UNREACHABLE();
4850 }
David Brazdil66d126e2015-04-03 16:02:44 +01004851
4852 DECLARE_INSTRUCTION(BooleanNot);
4853
4854 private:
4855 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4856};
4857
Roland Levillaindff1f282014-11-05 14:15:05 +00004858class HTypeConversion : public HExpression<1> {
4859 public:
4860 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004861 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004862 : HExpression(result_type,
4863 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4864 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004865 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004866 // Invariant: We should never generate a conversion to a Boolean value.
4867 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004868 }
4869
4870 HInstruction* GetInput() const { return InputAt(0); }
4871 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4872 Primitive::Type GetResultType() const { return GetType(); }
4873
4874 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004875 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004876
Mark Mendelle82549b2015-05-06 10:55:34 -04004877 // Try to statically evaluate the conversion and return a HConstant
4878 // containing the result. If the input cannot be converted, return nullptr.
4879 HConstant* TryStaticEvaluation() const;
4880
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004881 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4882 Primitive::Type result_type) {
4883 // Some architectures may not require the 'GC' side effects, but at this point
4884 // in the compilation process we do not know what architecture we will
4885 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004886 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4887 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004888 return SideEffects::CanTriggerGC();
4889 }
4890 return SideEffects::None();
4891 }
4892
Roland Levillaindff1f282014-11-05 14:15:05 +00004893 DECLARE_INSTRUCTION(TypeConversion);
4894
4895 private:
4896 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4897};
4898
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004899static constexpr uint32_t kNoRegNumber = -1;
4900
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004901class HNullCheck : public HExpression<1> {
4902 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004903 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4904 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004905 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004906 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004907 SetRawInputAt(0, value);
4908 }
4909
Calin Juravle10e244f2015-01-26 18:54:32 +00004910 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004911 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004912 return true;
4913 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004914
Calin Juravle10e244f2015-01-26 18:54:32 +00004915 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004916
Calin Juravle10e244f2015-01-26 18:54:32 +00004917 bool CanThrow() const OVERRIDE { return true; }
4918
4919 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004920
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004921
4922 DECLARE_INSTRUCTION(NullCheck);
4923
4924 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004925 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4926};
4927
4928class FieldInfo : public ValueObject {
4929 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004930 FieldInfo(MemberOffset field_offset,
4931 Primitive::Type field_type,
4932 bool is_volatile,
4933 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004934 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004935 const DexFile& dex_file,
4936 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004937 : field_offset_(field_offset),
4938 field_type_(field_type),
4939 is_volatile_(is_volatile),
4940 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004941 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004942 dex_file_(dex_file),
4943 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004944
4945 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004946 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004947 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004948 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004949 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004950 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004951 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004952
4953 private:
4954 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004955 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004956 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004957 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004958 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004959 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004960 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004961};
4962
4963class HInstanceFieldGet : public HExpression<1> {
4964 public:
4965 HInstanceFieldGet(HInstruction* value,
4966 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004967 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004968 bool is_volatile,
4969 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004970 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004971 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004972 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004973 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004974 : HExpression(field_type,
4975 SideEffects::FieldReadOfType(field_type, is_volatile),
4976 dex_pc),
4977 field_info_(field_offset,
4978 field_type,
4979 is_volatile,
4980 field_idx,
4981 declaring_class_def_index,
4982 dex_file,
4983 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004984 SetRawInputAt(0, value);
4985 }
4986
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004987 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004988
4989 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4990 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4991 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004992 }
4993
Calin Juravle641547a2015-04-21 22:08:51 +01004994 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4995 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004996 }
4997
4998 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004999 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5000 }
5001
Calin Juravle52c48962014-12-16 17:02:57 +00005002 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005003 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005004 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005005 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005006
5007 DECLARE_INSTRUCTION(InstanceFieldGet);
5008
5009 private:
5010 const FieldInfo field_info_;
5011
5012 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5013};
5014
5015class HInstanceFieldSet : public HTemplateInstruction<2> {
5016 public:
5017 HInstanceFieldSet(HInstruction* object,
5018 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005019 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005020 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005021 bool is_volatile,
5022 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005023 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005024 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005025 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005026 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005027 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5028 dex_pc),
5029 field_info_(field_offset,
5030 field_type,
5031 is_volatile,
5032 field_idx,
5033 declaring_class_def_index,
5034 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005035 dex_cache) {
5036 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005037 SetRawInputAt(0, object);
5038 SetRawInputAt(1, value);
5039 }
5040
Calin Juravle641547a2015-04-21 22:08:51 +01005041 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5042 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005043 }
5044
Calin Juravle52c48962014-12-16 17:02:57 +00005045 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005046 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005047 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005048 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005049 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005050 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5051 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005052
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005053 DECLARE_INSTRUCTION(InstanceFieldSet);
5054
5055 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005056 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5057 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5058 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5059 "Too many packed fields.");
5060
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005061 const FieldInfo field_info_;
5062
5063 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5064};
5065
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005066class HArrayGet : public HExpression<2> {
5067 public:
Aart Bik18b36ab2016-04-13 16:41:35 -07005068 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type, uint32_t dex_pc)
5069 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005070 SetRawInputAt(0, array);
5071 SetRawInputAt(1, index);
5072 }
5073
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005074 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005075 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005076 return true;
5077 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005078 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005079 // TODO: We can be smarter here.
5080 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5081 // which generates the implicit null check. There are cases when these can be removed
5082 // to produce better code. If we ever add optimizations to do so we should allow an
5083 // implicit check here (as long as the address falls in the first page).
5084 return false;
5085 }
5086
David Brazdil4833f5a2015-12-16 10:37:39 +00005087 bool IsEquivalentOf(HArrayGet* other) const {
5088 bool result = (GetDexPc() == other->GetDexPc());
5089 if (kIsDebugBuild && result) {
5090 DCHECK_EQ(GetBlock(), other->GetBlock());
5091 DCHECK_EQ(GetArray(), other->GetArray());
5092 DCHECK_EQ(GetIndex(), other->GetIndex());
5093 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005094 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005095 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005096 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5097 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005098 }
5099 }
5100 return result;
5101 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005102
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005103 HInstruction* GetArray() const { return InputAt(0); }
5104 HInstruction* GetIndex() const { return InputAt(1); }
5105
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005106 DECLARE_INSTRUCTION(ArrayGet);
5107
5108 private:
5109 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5110};
5111
5112class HArraySet : public HTemplateInstruction<3> {
5113 public:
5114 HArraySet(HInstruction* array,
5115 HInstruction* index,
5116 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005117 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005118 uint32_t dex_pc)
5119 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005120 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5121 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5122 SetPackedFlag<kFlagValueCanBeNull>(true);
5123 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005124 SetRawInputAt(0, array);
5125 SetRawInputAt(1, index);
5126 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005127 // Make a best guess now, may be refined during SSA building.
5128 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005129 }
5130
Calin Juravle77520bc2015-01-12 18:45:46 +00005131 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005132 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005133 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005134 }
5135
Mingyao Yang81014cb2015-06-02 03:16:27 -07005136 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005137 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005138
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005139 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005140 // TODO: Same as for ArrayGet.
5141 return false;
5142 }
5143
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005144 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005145 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005146 }
5147
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005148 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005149 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005150 }
5151
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005152 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005153 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005154 }
5155
Vladimir Markoa1de9182016-02-25 11:37:38 +00005156 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5157 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5158 bool StaticTypeOfArrayIsObjectArray() const {
5159 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5160 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005161
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005162 HInstruction* GetArray() const { return InputAt(0); }
5163 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005164 HInstruction* GetValue() const { return InputAt(2); }
5165
5166 Primitive::Type GetComponentType() const {
5167 // The Dex format does not type floating point index operations. Since the
5168 // `expected_component_type_` is set during building and can therefore not
5169 // be correct, we also check what is the value type. If it is a floating
5170 // point type, we must use that type.
5171 Primitive::Type value_type = GetValue()->GetType();
5172 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5173 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005174 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005175 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005176
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005177 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005178 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005179 }
5180
Aart Bik18b36ab2016-04-13 16:41:35 -07005181 void ComputeSideEffects() {
5182 Primitive::Type type = GetComponentType();
5183 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5184 SideEffectsForArchRuntimeCalls(type)));
5185 }
5186
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005187 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5188 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5189 }
5190
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005191 DECLARE_INSTRUCTION(ArraySet);
5192
5193 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005194 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5195 static constexpr size_t kFieldExpectedComponentTypeSize =
5196 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5197 static constexpr size_t kFlagNeedsTypeCheck =
5198 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5199 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005200 // Cached information for the reference_type_info_ so that codegen
5201 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005202 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5203 static constexpr size_t kNumberOfArraySetPackedBits =
5204 kFlagStaticTypeOfArrayIsObjectArray + 1;
5205 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5206 using ExpectedComponentTypeField =
5207 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005208
5209 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5210};
5211
5212class HArrayLength : public HExpression<1> {
5213 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005214 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005215 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005216 // Note that arrays do not change length, so the instruction does not
5217 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005218 SetRawInputAt(0, array);
5219 }
5220
Calin Juravle77520bc2015-01-12 18:45:46 +00005221 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005222 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005223 return true;
5224 }
Calin Juravle641547a2015-04-21 22:08:51 +01005225 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5226 return obj == InputAt(0);
5227 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005228
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005229 DECLARE_INSTRUCTION(ArrayLength);
5230
5231 private:
5232 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5233};
5234
5235class HBoundsCheck : public HExpression<2> {
5236 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005237 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5238 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005239 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005240 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005241 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005242 SetRawInputAt(0, index);
5243 SetRawInputAt(1, length);
5244 }
5245
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005246 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005247 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005248 return true;
5249 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005250
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005251 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005252
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005253 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005254
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005255 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005256
5257 DECLARE_INSTRUCTION(BoundsCheck);
5258
5259 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005260 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5261};
5262
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005263class HSuspendCheck : public HTemplateInstruction<0> {
5264 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005265 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005266 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005267
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005268 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005269 return true;
5270 }
5271
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005272 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5273 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005274
5275 DECLARE_INSTRUCTION(SuspendCheck);
5276
5277 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005278 // Only used for code generation, in order to share the same slow path between back edges
5279 // of a same loop.
5280 SlowPathCode* slow_path_;
5281
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005282 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5283};
5284
David Srbecky0cf44932015-12-09 14:09:59 +00005285// Pseudo-instruction which provides the native debugger with mapping information.
5286// It ensures that we can generate line number and local variables at this point.
5287class HNativeDebugInfo : public HTemplateInstruction<0> {
5288 public:
5289 explicit HNativeDebugInfo(uint32_t dex_pc)
5290 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5291
5292 bool NeedsEnvironment() const OVERRIDE {
5293 return true;
5294 }
5295
5296 DECLARE_INSTRUCTION(NativeDebugInfo);
5297
5298 private:
5299 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5300};
5301
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005302/**
5303 * Instruction to load a Class object.
5304 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005305class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005306 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005307 HLoadClass(HCurrentMethod* current_method,
5308 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005309 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005310 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005311 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005312 bool needs_access_check,
5313 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005314 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005315 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005316 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005317 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005318 // Referrers class should not need access check. We never inline unverified
5319 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005320 DCHECK(!is_referrers_class || !needs_access_check);
5321
5322 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5323 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5324 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5325 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005326 SetRawInputAt(0, current_method);
5327 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005328
5329 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005330
5331 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005332 // Note that we don't need to test for generate_clinit_check_.
5333 // Whether or not we need to generate the clinit check is processed in
5334 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005335 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005336 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005337 }
5338
5339 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5340
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005341 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005342 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005343
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005344 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005345 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005346 }
5347
Calin Juravle0ba218d2015-05-19 18:46:01 +01005348 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005349 // The entrypoint the code generator is going to call does not do
5350 // clinit of the class.
5351 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005352 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005353 }
5354
5355 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005356 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005357 (!IsReferrersClass() && !IsInDexCache()) ||
5358 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005359 }
5360
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005361
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005362 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005363 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005364 }
5365
Calin Juravleacf735c2015-02-12 15:25:22 +00005366 ReferenceTypeInfo GetLoadedClassRTI() {
5367 return loaded_class_rti_;
5368 }
5369
5370 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5371 // Make sure we only set exact types (the loaded class should never be merged).
5372 DCHECK(rti.IsExact());
5373 loaded_class_rti_ = rti;
5374 }
5375
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005376 const DexFile& GetDexFile() { return dex_file_; }
5377
Vladimir Markoa1de9182016-02-25 11:37:38 +00005378 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005379
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005380 static SideEffects SideEffectsForArchRuntimeCalls() {
5381 return SideEffects::CanTriggerGC();
5382 }
5383
Vladimir Markoa1de9182016-02-25 11:37:38 +00005384 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5385 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5386 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5387 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005388
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005389 DECLARE_INSTRUCTION(LoadClass);
5390
5391 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005392 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5393 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5394 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005395 // Whether this instruction must generate the initialization check.
5396 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005397 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5398 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5399 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5400
5401 const uint16_t type_index_;
5402 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005403
Calin Juravleacf735c2015-02-12 15:25:22 +00005404 ReferenceTypeInfo loaded_class_rti_;
5405
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005406 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5407};
5408
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005409class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005410 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005411 // Determines how to load the String.
5412 enum class LoadKind {
5413 // Use boot image String* address that will be known at link time.
5414 // Used for boot image strings referenced by boot image code in non-PIC mode.
5415 kBootImageLinkTimeAddress,
5416
5417 // Use PC-relative boot image String* address that will be known at link time.
5418 // Used for boot image strings referenced by boot image code in PIC mode.
5419 kBootImageLinkTimePcRelative,
5420
5421 // Use a known boot image String* address, embedded in the code by the codegen.
5422 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5423 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5424 // GetIncludePatchInformation().
5425 kBootImageAddress,
5426
5427 // Load from the resolved strings array at an absolute address.
5428 // Used for strings outside the boot image referenced by JIT-compiled code.
5429 kDexCacheAddress,
5430
5431 // Load from resolved strings array in the dex cache using a PC-relative load.
5432 // Used for strings outside boot image when we know that we can access
5433 // the dex cache arrays using a PC-relative load.
5434 kDexCachePcRelative,
5435
5436 // Load from resolved strings array accessed through the class loaded from
5437 // the compiled method's own ArtMethod*. This is the default access type when
5438 // all other types are unavailable.
5439 kDexCacheViaMethod,
5440
5441 kLast = kDexCacheViaMethod
5442 };
5443
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005444 HLoadString(HCurrentMethod* current_method,
5445 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005446 const DexFile& dex_file,
5447 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005448 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005449 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005450 SetPackedFlag<kFlagIsInDexCache>(false);
5451 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5452 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005453 SetRawInputAt(0, current_method);
5454 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005455
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005456 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5457 DCHECK(HasAddress(load_kind));
5458 load_data_.address = address;
5459 SetLoadKindInternal(load_kind);
5460 }
5461
5462 void SetLoadKindWithStringReference(LoadKind load_kind,
5463 const DexFile& dex_file,
5464 uint32_t string_index) {
5465 DCHECK(HasStringReference(load_kind));
5466 load_data_.ref.dex_file = &dex_file;
5467 string_index_ = string_index;
5468 SetLoadKindInternal(load_kind);
5469 }
5470
5471 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5472 const DexFile& dex_file,
5473 uint32_t element_index) {
5474 DCHECK(HasDexCacheReference(load_kind));
5475 load_data_.ref.dex_file = &dex_file;
5476 load_data_.ref.dex_cache_element_index = element_index;
5477 SetLoadKindInternal(load_kind);
5478 }
5479
5480 LoadKind GetLoadKind() const {
5481 return GetPackedField<LoadKindField>();
5482 }
5483
5484 const DexFile& GetDexFile() const;
5485
5486 uint32_t GetStringIndex() const {
5487 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5488 return string_index_;
5489 }
5490
5491 uint32_t GetDexCacheElementOffset() const;
5492
5493 uint64_t GetAddress() const {
5494 DCHECK(HasAddress(GetLoadKind()));
5495 return load_data_.address;
5496 }
5497
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005498 bool CanBeMoved() const OVERRIDE { return true; }
5499
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005500 bool InstructionDataEquals(HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005501
5502 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5503
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005504 // Will call the runtime if we need to load the string through
5505 // the dex cache and the string is not guaranteed to be there yet.
5506 bool NeedsEnvironment() const OVERRIDE {
5507 LoadKind load_kind = GetLoadKind();
5508 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5509 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5510 load_kind == LoadKind::kBootImageAddress) {
5511 return false;
5512 }
5513 return !IsInDexCache();
5514 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005515
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005516 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5517 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5518 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005519
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005520 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005521 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005522
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005523 static SideEffects SideEffectsForArchRuntimeCalls() {
5524 return SideEffects::CanTriggerGC();
5525 }
5526
Vladimir Markoa1de9182016-02-25 11:37:38 +00005527 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5528
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005529 void MarkInDexCache() {
5530 SetPackedFlag<kFlagIsInDexCache>(true);
5531 DCHECK(!NeedsEnvironment());
5532 RemoveEnvironment();
5533 }
5534
5535 size_t InputCount() const OVERRIDE {
5536 return (InputAt(0) != nullptr) ? 1u : 0u;
5537 }
5538
5539 void AddSpecialInput(HInstruction* special_input);
5540
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005541 DECLARE_INSTRUCTION(LoadString);
5542
5543 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005544 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005545 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5546 static constexpr size_t kFieldLoadKindSize =
5547 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5548 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005549 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005550 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005551
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005552 static bool HasStringReference(LoadKind load_kind) {
5553 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5554 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5555 load_kind == LoadKind::kDexCacheViaMethod;
5556 }
5557
5558 static bool HasAddress(LoadKind load_kind) {
5559 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5560 }
5561
5562 static bool HasDexCacheReference(LoadKind load_kind) {
5563 return load_kind == LoadKind::kDexCachePcRelative;
5564 }
5565
5566 void SetLoadKindInternal(LoadKind load_kind);
5567
5568 // String index serves also as the hash code and it's also needed for slow-paths,
5569 // so it must not be overwritten with other load data.
5570 uint32_t string_index_;
5571
5572 union {
5573 struct {
5574 const DexFile* dex_file; // For string reference and dex cache reference.
5575 uint32_t dex_cache_element_index; // Only for dex cache reference.
5576 } ref;
5577 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5578 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005579
5580 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5581};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005582std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5583
5584// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5585inline const DexFile& HLoadString::GetDexFile() const {
5586 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5587 << GetLoadKind();
5588 return *load_data_.ref.dex_file;
5589}
5590
5591// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5592inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5593 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5594 return load_data_.ref.dex_cache_element_index;
5595}
5596
5597// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5598inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5599 // The special input is used for PC-relative loads on some architectures.
5600 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5601 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
5602 DCHECK(InputAt(0) == nullptr);
5603 SetRawInputAt(0u, special_input);
5604 special_input->AddUseAt(this, 0);
5605}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005606
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005607/**
5608 * Performs an initialization check on its Class object input.
5609 */
5610class HClinitCheck : public HExpression<1> {
5611 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005612 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005613 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005614 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005615 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5616 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005617 SetRawInputAt(0, constant);
5618 }
5619
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005620 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005621 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005622 return true;
5623 }
5624
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005625 bool NeedsEnvironment() const OVERRIDE {
5626 // May call runtime to initialize the class.
5627 return true;
5628 }
5629
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005630 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005631
5632 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5633
5634 DECLARE_INSTRUCTION(ClinitCheck);
5635
5636 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005637 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5638};
5639
5640class HStaticFieldGet : public HExpression<1> {
5641 public:
5642 HStaticFieldGet(HInstruction* cls,
5643 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005644 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005645 bool is_volatile,
5646 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005647 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005648 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005649 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005650 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005651 : HExpression(field_type,
5652 SideEffects::FieldReadOfType(field_type, is_volatile),
5653 dex_pc),
5654 field_info_(field_offset,
5655 field_type,
5656 is_volatile,
5657 field_idx,
5658 declaring_class_def_index,
5659 dex_file,
5660 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005661 SetRawInputAt(0, cls);
5662 }
5663
Calin Juravle52c48962014-12-16 17:02:57 +00005664
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005665 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005666
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005667 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005668 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5669 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005670 }
5671
5672 size_t ComputeHashCode() const OVERRIDE {
5673 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5674 }
5675
Calin Juravle52c48962014-12-16 17:02:57 +00005676 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005677 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5678 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005679 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005680
5681 DECLARE_INSTRUCTION(StaticFieldGet);
5682
5683 private:
5684 const FieldInfo field_info_;
5685
5686 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5687};
5688
5689class HStaticFieldSet : public HTemplateInstruction<2> {
5690 public:
5691 HStaticFieldSet(HInstruction* cls,
5692 HInstruction* value,
5693 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005694 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005695 bool is_volatile,
5696 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005697 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005698 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005699 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005700 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005701 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5702 dex_pc),
5703 field_info_(field_offset,
5704 field_type,
5705 is_volatile,
5706 field_idx,
5707 declaring_class_def_index,
5708 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005709 dex_cache) {
5710 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005711 SetRawInputAt(0, cls);
5712 SetRawInputAt(1, value);
5713 }
5714
Calin Juravle52c48962014-12-16 17:02:57 +00005715 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005716 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5717 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005718 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005719
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005720 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005721 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5722 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005723
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005724 DECLARE_INSTRUCTION(StaticFieldSet);
5725
5726 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005727 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5728 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5729 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5730 "Too many packed fields.");
5731
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005732 const FieldInfo field_info_;
5733
5734 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5735};
5736
Calin Juravlee460d1d2015-09-29 04:52:17 +01005737class HUnresolvedInstanceFieldGet : public HExpression<1> {
5738 public:
5739 HUnresolvedInstanceFieldGet(HInstruction* obj,
5740 Primitive::Type field_type,
5741 uint32_t field_index,
5742 uint32_t dex_pc)
5743 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5744 field_index_(field_index) {
5745 SetRawInputAt(0, obj);
5746 }
5747
5748 bool NeedsEnvironment() const OVERRIDE { return true; }
5749 bool CanThrow() const OVERRIDE { return true; }
5750
5751 Primitive::Type GetFieldType() const { return GetType(); }
5752 uint32_t GetFieldIndex() const { return field_index_; }
5753
5754 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5755
5756 private:
5757 const uint32_t field_index_;
5758
5759 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5760};
5761
5762class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5763 public:
5764 HUnresolvedInstanceFieldSet(HInstruction* obj,
5765 HInstruction* value,
5766 Primitive::Type field_type,
5767 uint32_t field_index,
5768 uint32_t dex_pc)
5769 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005770 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005771 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005772 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005773 SetRawInputAt(0, obj);
5774 SetRawInputAt(1, value);
5775 }
5776
5777 bool NeedsEnvironment() const OVERRIDE { return true; }
5778 bool CanThrow() const OVERRIDE { return true; }
5779
Vladimir Markoa1de9182016-02-25 11:37:38 +00005780 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005781 uint32_t GetFieldIndex() const { return field_index_; }
5782
5783 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5784
5785 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005786 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5787 static constexpr size_t kFieldFieldTypeSize =
5788 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5789 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5790 kFieldFieldType + kFieldFieldTypeSize;
5791 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5792 "Too many packed fields.");
5793 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5794
Calin Juravlee460d1d2015-09-29 04:52:17 +01005795 const uint32_t field_index_;
5796
5797 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5798};
5799
5800class HUnresolvedStaticFieldGet : public HExpression<0> {
5801 public:
5802 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5803 uint32_t field_index,
5804 uint32_t dex_pc)
5805 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5806 field_index_(field_index) {
5807 }
5808
5809 bool NeedsEnvironment() const OVERRIDE { return true; }
5810 bool CanThrow() const OVERRIDE { return true; }
5811
5812 Primitive::Type GetFieldType() const { return GetType(); }
5813 uint32_t GetFieldIndex() const { return field_index_; }
5814
5815 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5816
5817 private:
5818 const uint32_t field_index_;
5819
5820 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5821};
5822
5823class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5824 public:
5825 HUnresolvedStaticFieldSet(HInstruction* value,
5826 Primitive::Type field_type,
5827 uint32_t field_index,
5828 uint32_t dex_pc)
5829 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005830 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005831 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005832 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005833 SetRawInputAt(0, value);
5834 }
5835
5836 bool NeedsEnvironment() const OVERRIDE { return true; }
5837 bool CanThrow() const OVERRIDE { return true; }
5838
Vladimir Markoa1de9182016-02-25 11:37:38 +00005839 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005840 uint32_t GetFieldIndex() const { return field_index_; }
5841
5842 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5843
5844 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005845 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5846 static constexpr size_t kFieldFieldTypeSize =
5847 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5848 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5849 kFieldFieldType + kFieldFieldTypeSize;
5850 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5851 "Too many packed fields.");
5852 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5853
Calin Juravlee460d1d2015-09-29 04:52:17 +01005854 const uint32_t field_index_;
5855
5856 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5857};
5858
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005859// Implement the move-exception DEX instruction.
5860class HLoadException : public HExpression<0> {
5861 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005862 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5863 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005864
David Brazdilbbd733e2015-08-18 17:48:17 +01005865 bool CanBeNull() const OVERRIDE { return false; }
5866
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005867 DECLARE_INSTRUCTION(LoadException);
5868
5869 private:
5870 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5871};
5872
David Brazdilcb1c0552015-08-04 16:22:25 +01005873// Implicit part of move-exception which clears thread-local exception storage.
5874// Must not be removed because the runtime expects the TLS to get cleared.
5875class HClearException : public HTemplateInstruction<0> {
5876 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005877 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5878 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005879
5880 DECLARE_INSTRUCTION(ClearException);
5881
5882 private:
5883 DISALLOW_COPY_AND_ASSIGN(HClearException);
5884};
5885
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005886class HThrow : public HTemplateInstruction<1> {
5887 public:
5888 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005889 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005890 SetRawInputAt(0, exception);
5891 }
5892
5893 bool IsControlFlow() const OVERRIDE { return true; }
5894
5895 bool NeedsEnvironment() const OVERRIDE { return true; }
5896
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005897 bool CanThrow() const OVERRIDE { return true; }
5898
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005899
5900 DECLARE_INSTRUCTION(Throw);
5901
5902 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005903 DISALLOW_COPY_AND_ASSIGN(HThrow);
5904};
5905
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005906/**
5907 * Implementation strategies for the code generator of a HInstanceOf
5908 * or `HCheckCast`.
5909 */
5910enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005911 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005912 kExactCheck, // Can do a single class compare.
5913 kClassHierarchyCheck, // Can just walk the super class chain.
5914 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5915 kInterfaceCheck, // No optimization yet when checking against an interface.
5916 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005917 kArrayCheck, // No optimization yet when checking against a generic array.
5918 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005919};
5920
Roland Levillain86503782016-02-11 19:07:30 +00005921std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5922
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005923class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005924 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005925 HInstanceOf(HInstruction* object,
5926 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005927 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005928 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005929 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005930 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005931 dex_pc) {
5932 SetPackedField<TypeCheckKindField>(check_kind);
5933 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005934 SetRawInputAt(0, object);
5935 SetRawInputAt(1, constant);
5936 }
5937
5938 bool CanBeMoved() const OVERRIDE { return true; }
5939
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005940 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005941 return true;
5942 }
5943
5944 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005945 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005946 }
5947
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005948 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005949 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5950 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5951 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5952 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005953
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005954 static bool CanCallRuntime(TypeCheckKind check_kind) {
5955 // Mips currently does runtime calls for any other checks.
5956 return check_kind != TypeCheckKind::kExactCheck;
5957 }
5958
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005959 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005960 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005961 }
5962
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005963 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005964
5965 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005966 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
5967 static constexpr size_t kFieldTypeCheckKindSize =
5968 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
5969 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
5970 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
5971 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5972 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005973
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005974 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5975};
5976
Calin Juravleb1498f62015-02-16 13:13:29 +00005977class HBoundType : public HExpression<1> {
5978 public:
David Brazdilf5552582015-12-27 13:36:12 +00005979 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005980 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005981 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
5982 SetPackedFlag<kFlagUpperCanBeNull>(true);
5983 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00005984 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005985 SetRawInputAt(0, input);
5986 }
5987
David Brazdilf5552582015-12-27 13:36:12 +00005988 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005989 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005990 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00005991 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005992
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005993 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005994 DCHECK(GetUpperCanBeNull() || !can_be_null);
5995 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005996 }
5997
Vladimir Markoa1de9182016-02-25 11:37:38 +00005998 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005999
Calin Juravleb1498f62015-02-16 13:13:29 +00006000 DECLARE_INSTRUCTION(BoundType);
6001
6002 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006003 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6004 // is false then CanBeNull() cannot be true).
6005 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6006 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6007 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6008 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6009
Calin Juravleb1498f62015-02-16 13:13:29 +00006010 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006011 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6012 // It is used to bound the type in cases like:
6013 // if (x instanceof ClassX) {
6014 // // uper_bound_ will be ClassX
6015 // }
David Brazdilf5552582015-12-27 13:36:12 +00006016 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006017
6018 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6019};
6020
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006021class HCheckCast : public HTemplateInstruction<2> {
6022 public:
6023 HCheckCast(HInstruction* object,
6024 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006025 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006026 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006027 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6028 SetPackedField<TypeCheckKindField>(check_kind);
6029 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006030 SetRawInputAt(0, object);
6031 SetRawInputAt(1, constant);
6032 }
6033
6034 bool CanBeMoved() const OVERRIDE { return true; }
6035
6036 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6037 return true;
6038 }
6039
6040 bool NeedsEnvironment() const OVERRIDE {
6041 // Instruction may throw a CheckCastError.
6042 return true;
6043 }
6044
6045 bool CanThrow() const OVERRIDE { return true; }
6046
Vladimir Markoa1de9182016-02-25 11:37:38 +00006047 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6048 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6049 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6050 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006051
6052 DECLARE_INSTRUCTION(CheckCast);
6053
6054 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006055 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6056 static constexpr size_t kFieldTypeCheckKindSize =
6057 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6058 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6059 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6060 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6061 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006062
6063 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006064};
6065
Calin Juravle27df7582015-04-17 19:12:31 +01006066class HMemoryBarrier : public HTemplateInstruction<0> {
6067 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006068 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006069 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006070 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6071 SetPackedField<BarrierKindField>(barrier_kind);
6072 }
Calin Juravle27df7582015-04-17 19:12:31 +01006073
Vladimir Markoa1de9182016-02-25 11:37:38 +00006074 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006075
6076 DECLARE_INSTRUCTION(MemoryBarrier);
6077
6078 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006079 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6080 static constexpr size_t kFieldBarrierKindSize =
6081 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6082 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6083 kFieldBarrierKind + kFieldBarrierKindSize;
6084 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6085 "Too many packed fields.");
6086 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006087
6088 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6089};
6090
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006091class HMonitorOperation : public HTemplateInstruction<1> {
6092 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006093 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006094 kEnter,
6095 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006096 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006097 };
6098
6099 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006100 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006101 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6102 dex_pc) {
6103 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006104 SetRawInputAt(0, object);
6105 }
6106
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006107 // Instruction may go into runtime, so we need an environment.
6108 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006109
6110 bool CanThrow() const OVERRIDE {
6111 // Verifier guarantees that monitor-exit cannot throw.
6112 // This is important because it allows the HGraphBuilder to remove
6113 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6114 return IsEnter();
6115 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006116
Vladimir Markoa1de9182016-02-25 11:37:38 +00006117 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6118 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006119
6120 DECLARE_INSTRUCTION(MonitorOperation);
6121
Calin Juravle52c48962014-12-16 17:02:57 +00006122 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006123 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6124 static constexpr size_t kFieldOperationKindSize =
6125 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6126 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6127 kFieldOperationKind + kFieldOperationKindSize;
6128 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6129 "Too many packed fields.");
6130 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006131
6132 private:
6133 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6134};
6135
David Brazdil74eb1b22015-12-14 11:44:01 +00006136class HSelect : public HExpression<3> {
6137 public:
6138 HSelect(HInstruction* condition,
6139 HInstruction* true_value,
6140 HInstruction* false_value,
6141 uint32_t dex_pc)
6142 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6143 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6144
6145 // First input must be `true_value` or `false_value` to allow codegens to
6146 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6147 // that architectures which implement HSelect as a conditional move also
6148 // will not need to invert the condition.
6149 SetRawInputAt(0, false_value);
6150 SetRawInputAt(1, true_value);
6151 SetRawInputAt(2, condition);
6152 }
6153
6154 HInstruction* GetFalseValue() const { return InputAt(0); }
6155 HInstruction* GetTrueValue() const { return InputAt(1); }
6156 HInstruction* GetCondition() const { return InputAt(2); }
6157
6158 bool CanBeMoved() const OVERRIDE { return true; }
6159 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6160
6161 bool CanBeNull() const OVERRIDE {
6162 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6163 }
6164
6165 DECLARE_INSTRUCTION(Select);
6166
6167 private:
6168 DISALLOW_COPY_AND_ASSIGN(HSelect);
6169};
6170
Vladimir Markof9f64412015-09-02 14:05:49 +01006171class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006172 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006173 MoveOperands(Location source,
6174 Location destination,
6175 Primitive::Type type,
6176 HInstruction* instruction)
6177 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006178
6179 Location GetSource() const { return source_; }
6180 Location GetDestination() const { return destination_; }
6181
6182 void SetSource(Location value) { source_ = value; }
6183 void SetDestination(Location value) { destination_ = value; }
6184
6185 // The parallel move resolver marks moves as "in-progress" by clearing the
6186 // destination (but not the source).
6187 Location MarkPending() {
6188 DCHECK(!IsPending());
6189 Location dest = destination_;
6190 destination_ = Location::NoLocation();
6191 return dest;
6192 }
6193
6194 void ClearPending(Location dest) {
6195 DCHECK(IsPending());
6196 destination_ = dest;
6197 }
6198
6199 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006200 DCHECK(source_.IsValid() || destination_.IsInvalid());
6201 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006202 }
6203
6204 // True if this blocks a move from the given location.
6205 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006206 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006207 }
6208
6209 // A move is redundant if it's been eliminated, if its source and
6210 // destination are the same, or if its destination is unneeded.
6211 bool IsRedundant() const {
6212 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6213 }
6214
6215 // We clear both operands to indicate move that's been eliminated.
6216 void Eliminate() {
6217 source_ = destination_ = Location::NoLocation();
6218 }
6219
6220 bool IsEliminated() const {
6221 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6222 return source_.IsInvalid();
6223 }
6224
Alexey Frunze4dda3372015-06-01 18:31:49 -07006225 Primitive::Type GetType() const { return type_; }
6226
Nicolas Geoffray90218252015-04-15 11:56:51 +01006227 bool Is64BitMove() const {
6228 return Primitive::Is64BitType(type_);
6229 }
6230
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006231 HInstruction* GetInstruction() const { return instruction_; }
6232
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006233 private:
6234 Location source_;
6235 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006236 // The type this move is for.
6237 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006238 // The instruction this move is assocatied with. Null when this move is
6239 // for moving an input in the expected locations of user (including a phi user).
6240 // This is only used in debug mode, to ensure we do not connect interval siblings
6241 // in the same parallel move.
6242 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006243};
6244
Roland Levillainc9285912015-12-18 10:38:42 +00006245std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6246
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006247static constexpr size_t kDefaultNumberOfMoves = 4;
6248
6249class HParallelMove : public HTemplateInstruction<0> {
6250 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006251 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006252 : HTemplateInstruction(SideEffects::None(), dex_pc),
6253 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6254 moves_.reserve(kDefaultNumberOfMoves);
6255 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006256
Nicolas Geoffray90218252015-04-15 11:56:51 +01006257 void AddMove(Location source,
6258 Location destination,
6259 Primitive::Type type,
6260 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006261 DCHECK(source.IsValid());
6262 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006263 if (kIsDebugBuild) {
6264 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006265 for (const MoveOperands& move : moves_) {
6266 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006267 // Special case the situation where the move is for the spill slot
6268 // of the instruction.
6269 if ((GetPrevious() == instruction)
6270 || ((GetPrevious() == nullptr)
6271 && instruction->IsPhi()
6272 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006273 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006274 << "Doing parallel moves for the same instruction.";
6275 } else {
6276 DCHECK(false) << "Doing parallel moves for the same instruction.";
6277 }
6278 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006279 }
6280 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006281 for (const MoveOperands& move : moves_) {
6282 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006283 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006284 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006285 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006286 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006287 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006288 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006289 }
6290
Vladimir Marko225b6462015-09-28 12:17:40 +01006291 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006292 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006293 }
6294
Vladimir Marko225b6462015-09-28 12:17:40 +01006295 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006296
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006297 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006298
6299 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006300 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006301
6302 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6303};
6304
Mark Mendell0616ae02015-04-17 12:49:27 -04006305} // namespace art
6306
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006307#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6308#include "nodes_shared.h"
6309#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006310#ifdef ART_ENABLE_CODEGEN_arm
6311#include "nodes_arm.h"
6312#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006313#ifdef ART_ENABLE_CODEGEN_arm64
6314#include "nodes_arm64.h"
6315#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006316#ifdef ART_ENABLE_CODEGEN_x86
6317#include "nodes_x86.h"
6318#endif
6319
6320namespace art {
6321
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006322class HGraphVisitor : public ValueObject {
6323 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006324 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6325 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006326
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006327 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006328 virtual void VisitBasicBlock(HBasicBlock* block);
6329
Roland Levillain633021e2014-10-01 14:12:25 +01006330 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006331 void VisitInsertionOrder();
6332
Roland Levillain633021e2014-10-01 14:12:25 +01006333 // Visit the graph following dominator tree reverse post-order.
6334 void VisitReversePostOrder();
6335
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006336 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006337
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006338 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006339#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006340 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6341
6342 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6343
6344#undef DECLARE_VISIT_INSTRUCTION
6345
6346 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006347 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006348
6349 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6350};
6351
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006352class HGraphDelegateVisitor : public HGraphVisitor {
6353 public:
6354 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6355 virtual ~HGraphDelegateVisitor() {}
6356
6357 // Visit functions that delegate to to super class.
6358#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006359 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006360
6361 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6362
6363#undef DECLARE_VISIT_INSTRUCTION
6364
6365 private:
6366 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6367};
6368
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006369class HInsertionOrderIterator : public ValueObject {
6370 public:
6371 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6372
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006373 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006374 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006375 void Advance() { ++index_; }
6376
6377 private:
6378 const HGraph& graph_;
6379 size_t index_;
6380
6381 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6382};
6383
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006384class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006385 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006386 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6387 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006388 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006389 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006390
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006391 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6392 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006393 void Advance() { ++index_; }
6394
6395 private:
6396 const HGraph& graph_;
6397 size_t index_;
6398
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006399 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006400};
6401
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006402class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006403 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006404 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006405 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006406 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006407 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006408 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006409
6410 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006411 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006412 void Advance() { --index_; }
6413
6414 private:
6415 const HGraph& graph_;
6416 size_t index_;
6417
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006418 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006419};
6420
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006421class HLinearPostOrderIterator : public ValueObject {
6422 public:
6423 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006424 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006425
6426 bool Done() const { return index_ == 0; }
6427
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006428 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006429
6430 void Advance() {
6431 --index_;
6432 DCHECK_GE(index_, 0U);
6433 }
6434
6435 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006436 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006437 size_t index_;
6438
6439 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6440};
6441
6442class HLinearOrderIterator : public ValueObject {
6443 public:
6444 explicit HLinearOrderIterator(const HGraph& graph)
6445 : order_(graph.GetLinearOrder()), index_(0) {}
6446
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006447 bool Done() const { return index_ == order_.size(); }
6448 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006449 void Advance() { ++index_; }
6450
6451 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006452 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006453 size_t index_;
6454
6455 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6456};
6457
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006458// Iterator over the blocks that art part of the loop. Includes blocks part
6459// of an inner loop. The order in which the blocks are iterated is on their
6460// block id.
6461class HBlocksInLoopIterator : public ValueObject {
6462 public:
6463 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6464 : blocks_in_loop_(info.GetBlocks()),
6465 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6466 index_(0) {
6467 if (!blocks_in_loop_.IsBitSet(index_)) {
6468 Advance();
6469 }
6470 }
6471
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006472 bool Done() const { return index_ == blocks_.size(); }
6473 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006474 void Advance() {
6475 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006476 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006477 if (blocks_in_loop_.IsBitSet(index_)) {
6478 break;
6479 }
6480 }
6481 }
6482
6483 private:
6484 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006485 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006486 size_t index_;
6487
6488 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6489};
6490
Mingyao Yang3584bce2015-05-19 16:01:59 -07006491// Iterator over the blocks that art part of the loop. Includes blocks part
6492// of an inner loop. The order in which the blocks are iterated is reverse
6493// post order.
6494class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6495 public:
6496 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6497 : blocks_in_loop_(info.GetBlocks()),
6498 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6499 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006500 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006501 Advance();
6502 }
6503 }
6504
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006505 bool Done() const { return index_ == blocks_.size(); }
6506 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006507 void Advance() {
6508 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006509 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6510 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006511 break;
6512 }
6513 }
6514 }
6515
6516 private:
6517 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006518 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006519 size_t index_;
6520
6521 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6522};
6523
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006524inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006525 if (constant->IsIntConstant()) {
6526 return constant->AsIntConstant()->GetValue();
6527 } else if (constant->IsLongConstant()) {
6528 return constant->AsLongConstant()->GetValue();
6529 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006530 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006531 return 0;
6532 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006533}
6534
Vladimir Marko58155012015-08-19 12:49:41 +00006535inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6536 // For the purposes of the compiler, the dex files must actually be the same object
6537 // if we want to safely treat them as the same. This is especially important for JIT
6538 // as custom class loaders can open the same underlying file (or memory) multiple
6539 // times and provide different class resolution but no two class loaders should ever
6540 // use the same DexFile object - doing so is an unsupported hack that can lead to
6541 // all sorts of weird failures.
6542 return &lhs == &rhs;
6543}
6544
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006545#define INSTRUCTION_TYPE_CHECK(type, super) \
6546 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6547 inline const H##type* HInstruction::As##type() const { \
6548 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6549 } \
6550 inline H##type* HInstruction::As##type() { \
6551 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6552 }
6553
6554 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6555#undef INSTRUCTION_TYPE_CHECK
6556
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006557// Create space in `blocks` for adding `number_of_new_blocks` entries
6558// starting at location `at`. Blocks after `at` are moved accordingly.
6559inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6560 size_t number_of_new_blocks,
6561 size_t after) {
6562 DCHECK_LT(after, blocks->size());
6563 size_t old_size = blocks->size();
6564 size_t new_size = old_size + number_of_new_blocks;
6565 blocks->resize(new_size);
6566 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6567}
6568
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006569} // namespace art
6570
6571#endif // ART_COMPILER_OPTIMIZING_NODES_H_