blob: 71ad8e57a73d54c92f20b4b2510c6db0c1a5e76e [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010028#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000029#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "handle.h"
31#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "primitive.h"
David Brazdild26a4112015-11-10 11:07:31 +000038#include "utils/array_ref.h"
Vladimir Markod59f3b12016-03-29 12:21:58 +010039#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040
41namespace art {
42
David Brazdil1abb4192015-02-17 18:33:36 +000043class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010045class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000046class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010047class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000049class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000052class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000053class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000055class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010056class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010057class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010058class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010059class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000060class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010061class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000062class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000063
Mathieu Chartier736b5602015-09-02 14:54:11 -070064namespace mirror {
65class DexCache;
66} // namespace mirror
67
Nicolas Geoffray818f2102014-02-18 16:43:35 +000068static const int kDefaultNumberOfBlocks = 8;
69static const int kDefaultNumberOfSuccessors = 2;
70static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010071static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010072static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000073static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000074
Roland Levillain5b5b9312016-03-22 14:57:31 +000075// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
76static constexpr int32_t kMaxIntShiftDistance = 0x1f;
77// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
78static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000079
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010080static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070081static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010082
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010083static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
84
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060085static constexpr uint32_t kNoDexPc = -1;
86
Dave Allison20dfc792014-06-16 20:44:29 -070087enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070088 // All types.
89 kCondEQ, // ==
90 kCondNE, // !=
91 // Signed integers and floating-point numbers.
92 kCondLT, // <
93 kCondLE, // <=
94 kCondGT, // >
95 kCondGE, // >=
96 // Unsigned integers.
97 kCondB, // <
98 kCondBE, // <=
99 kCondA, // >
100 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -0700101};
102
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000103enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000104 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000105 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000106 kAnalysisFailThrowCatchLoop,
107 kAnalysisFailAmbiguousArrayOp,
108 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000109};
110
Vladimir Markof9f64412015-09-02 14:05:49 +0100111class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100112 public:
113 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
114
115 void AddInstruction(HInstruction* instruction);
116 void RemoveInstruction(HInstruction* instruction);
117
David Brazdilc3d743f2015-04-22 13:40:50 +0100118 // Insert `instruction` before/after an existing instruction `cursor`.
119 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
120 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
121
Roland Levillain6b469232014-09-25 10:10:38 +0100122 // Return true if this list contains `instruction`.
123 bool Contains(HInstruction* instruction) const;
124
Roland Levillainccc07a92014-09-16 14:48:16 +0100125 // Return true if `instruction1` is found before `instruction2` in
126 // this instruction list and false otherwise. Abort if none
127 // of these instructions is found.
128 bool FoundBefore(const HInstruction* instruction1,
129 const HInstruction* instruction2) const;
130
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000131 bool IsEmpty() const { return first_instruction_ == nullptr; }
132 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
133
134 // Update the block of all instructions to be `block`.
135 void SetBlockOfInstructions(HBasicBlock* block) const;
136
137 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000138 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000139 void Add(const HInstructionList& instruction_list);
140
David Brazdil2d7352b2015-04-20 14:52:42 +0100141 // Return the number of instructions in the list. This is an expensive operation.
142 size_t CountSize() const;
143
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100144 private:
145 HInstruction* first_instruction_;
146 HInstruction* last_instruction_;
147
148 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000149 friend class HGraph;
150 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100151 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100152 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100153
154 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
155};
156
David Brazdil4833f5a2015-12-16 10:37:39 +0000157class ReferenceTypeInfo : ValueObject {
158 public:
159 typedef Handle<mirror::Class> TypeHandle;
160
Vladimir Markoa1de9182016-02-25 11:37:38 +0000161 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
162
163 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000164 return ReferenceTypeInfo(type_handle, is_exact);
165 }
166
167 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
168
Vladimir Markof39745e2016-01-26 12:16:55 +0000169 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000170 return handle.GetReference() != nullptr;
171 }
172
Vladimir Markoa4336d22016-04-19 14:12:13 +0000173 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000174 return IsValidHandle(type_handle_);
175 }
176
177 bool IsExact() const { return is_exact_; }
178
179 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
180 DCHECK(IsValid());
181 return GetTypeHandle()->IsObjectClass();
182 }
183
184 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
185 DCHECK(IsValid());
186 return GetTypeHandle()->IsStringClass();
187 }
188
189 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
190 DCHECK(IsValid());
191 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
192 }
193
194 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
195 DCHECK(IsValid());
196 return GetTypeHandle()->IsInterface();
197 }
198
199 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
200 DCHECK(IsValid());
201 return GetTypeHandle()->IsArrayClass();
202 }
203
204 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
205 DCHECK(IsValid());
206 return GetTypeHandle()->IsPrimitiveArray();
207 }
208
209 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
210 DCHECK(IsValid());
211 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
212 }
213
214 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
215 DCHECK(IsValid());
216 if (!IsExact()) return false;
217 if (!IsArrayClass()) return false;
218 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
219 }
220
221 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
222 DCHECK(IsValid());
223 if (!IsExact()) return false;
224 if (!IsArrayClass()) return false;
225 if (!rti.IsArrayClass()) return false;
226 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
227 rti.GetTypeHandle()->GetComponentType());
228 }
229
230 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
231
232 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
233 DCHECK(IsValid());
234 DCHECK(rti.IsValid());
235 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
236 }
237
238 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
239 DCHECK(IsValid());
240 DCHECK(rti.IsValid());
241 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
242 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
243 }
244
245 // Returns true if the type information provide the same amount of details.
246 // Note that it does not mean that the instructions have the same actual type
247 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000248 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000249 if (!IsValid() && !rti.IsValid()) {
250 // Invalid types are equal.
251 return true;
252 }
253 if (!IsValid() || !rti.IsValid()) {
254 // One is valid, the other not.
255 return false;
256 }
257 return IsExact() == rti.IsExact()
258 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
259 }
260
261 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000262 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
263 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
264 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000265
266 // The class of the object.
267 TypeHandle type_handle_;
268 // Whether or not the type is exact or a superclass of the actual type.
269 // Whether or not we have any information about this type.
270 bool is_exact_;
271};
272
273std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
274
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000275// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100276class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000277 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100278 HGraph(ArenaAllocator* arena,
279 const DexFile& dex_file,
280 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100281 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700282 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100283 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100284 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000285 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100286 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000287 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100288 blocks_(arena->Adapter(kArenaAllocBlockList)),
289 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
290 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700291 entry_block_(nullptr),
292 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100293 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100294 number_of_vregs_(0),
295 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000296 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400297 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000298 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000299 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000300 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000301 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100302 dex_file_(dex_file),
303 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100304 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100305 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100306 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700307 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000308 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100309 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
310 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
311 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
312 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000313 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000314 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
315 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100316 blocks_.reserve(kDefaultNumberOfBlocks);
317 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000318
David Brazdilbadd8262016-02-02 16:28:56 +0000319 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
320 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
321
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000322 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100323 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
324
David Brazdil69ba7b72015-06-23 18:27:30 +0100325 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000326 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100327
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000328 HBasicBlock* GetEntryBlock() const { return entry_block_; }
329 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100330 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000331
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000332 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
333 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000334
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000335 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100336
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100337 void ComputeDominanceInformation();
338 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000339 void ClearLoopInformation();
340 void FindBackEdges(ArenaBitVector* visited);
341 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100342 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100343 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000344
David Brazdil4833f5a2015-12-16 10:37:39 +0000345 // Analyze all natural loops in this graph. Returns a code specifying that it
346 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000347 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000348 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100349
David Brazdilffee3d32015-07-06 11:48:53 +0100350 // Iterate over blocks to compute try block membership. Needs reverse post
351 // order and loop information.
352 void ComputeTryBlockInformation();
353
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000354 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000355 // Returns the instruction to replace the invoke expression or null if the
356 // invoke is for a void method. Note that the caller is responsible for replacing
357 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000358 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000359
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000360 // Update the loop and try membership of `block`, which was spawned from `reference`.
361 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
362 // should be the new back edge.
363 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
364 HBasicBlock* reference,
365 bool replace_if_back_edge);
366
Mingyao Yang3584bce2015-05-19 16:01:59 -0700367 // Need to add a couple of blocks to test if the loop body is entered and
368 // put deoptimization instructions, etc.
369 void TransformLoopHeaderForBCE(HBasicBlock* header);
370
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000371 // Removes `block` from the graph. Assumes `block` has been disconnected from
372 // other blocks and has no instructions or phis.
373 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000374
David Brazdilfc6a86a2015-06-26 10:33:45 +0000375 // Splits the edge between `block` and `successor` while preserving the
376 // indices in the predecessor/successor lists. If there are multiple edges
377 // between the blocks, the lowest indices are used.
378 // Returns the new block which is empty and has the same dex pc as `successor`.
379 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
380
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100381 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
382 void SimplifyLoop(HBasicBlock* header);
383
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000384 int32_t GetNextInstructionId() {
385 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000386 return current_instruction_id_++;
387 }
388
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000389 int32_t GetCurrentInstructionId() const {
390 return current_instruction_id_;
391 }
392
393 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000394 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000395 current_instruction_id_ = id;
396 }
397
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100398 uint16_t GetMaximumNumberOfOutVRegs() const {
399 return maximum_number_of_out_vregs_;
400 }
401
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000402 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
403 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100404 }
405
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100406 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
407 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
408 }
409
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000410 void UpdateTemporariesVRegSlots(size_t slots) {
411 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 }
413
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000414 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100415 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000416 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100417 }
418
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100419 void SetNumberOfVRegs(uint16_t number_of_vregs) {
420 number_of_vregs_ = number_of_vregs;
421 }
422
423 uint16_t GetNumberOfVRegs() const {
424 return number_of_vregs_;
425 }
426
427 void SetNumberOfInVRegs(uint16_t value) {
428 number_of_in_vregs_ = value;
429 }
430
David Brazdildee58d62016-04-07 09:54:26 +0000431 uint16_t GetNumberOfInVRegs() const {
432 return number_of_in_vregs_;
433 }
434
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100435 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100436 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100437 return number_of_vregs_ - number_of_in_vregs_;
438 }
439
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100440 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100441 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100442 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100443
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100444 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100445 return linear_order_;
446 }
447
Mark Mendell1152c922015-04-24 17:06:35 -0400448 bool HasBoundsChecks() const {
449 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800450 }
451
Mark Mendell1152c922015-04-24 17:06:35 -0400452 void SetHasBoundsChecks(bool value) {
453 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800454 }
455
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100456 bool ShouldGenerateConstructorBarrier() const {
457 return should_generate_constructor_barrier_;
458 }
459
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000460 bool IsDebuggable() const { return debuggable_; }
461
David Brazdil8d5b8b22015-03-24 10:51:52 +0000462 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000463 // already, it is created and inserted into the graph. This method is only for
464 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600465 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000466
467 // TODO: This is problematic for the consistency of reference type propagation
468 // because it can be created anytime after the pass and thus it will be left
469 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600470 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000471
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600472 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
473 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000474 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600475 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
476 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000477 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600478 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
479 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000480 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600481 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
482 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000483 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000484
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100485 HCurrentMethod* GetCurrentMethod();
486
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100487 const DexFile& GetDexFile() const {
488 return dex_file_;
489 }
490
491 uint32_t GetMethodIdx() const {
492 return method_idx_;
493 }
494
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100495 InvokeType GetInvokeType() const {
496 return invoke_type_;
497 }
498
Mark Mendellc4701932015-04-10 13:18:51 -0400499 InstructionSet GetInstructionSet() const {
500 return instruction_set_;
501 }
502
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000503 bool IsCompilingOsr() const { return osr_; }
504
David Brazdil77a48ae2015-09-15 12:34:04 +0000505 bool HasTryCatch() const { return has_try_catch_; }
506 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100507
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000508 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
509 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
510
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100511 ArtMethod* GetArtMethod() const { return art_method_; }
512 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
513
Mark Mendellf6529172015-11-17 11:16:56 -0500514 // Returns an instruction with the opposite boolean value from 'cond'.
515 // The instruction has been inserted into the graph, either as a constant, or
516 // before cursor.
517 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
518
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000519 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
520
David Brazdil2d7352b2015-04-20 14:52:42 +0100521 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000522 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100523 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000524
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000525 template <class InstructionType, typename ValueType>
526 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600527 ArenaSafeMap<ValueType, InstructionType*>* cache,
528 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000529 // Try to find an existing constant of the given value.
530 InstructionType* constant = nullptr;
531 auto cached_constant = cache->find(value);
532 if (cached_constant != cache->end()) {
533 constant = cached_constant->second;
534 }
535
536 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100537 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000538 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600539 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000540 cache->Overwrite(value, constant);
541 InsertConstant(constant);
542 }
543 return constant;
544 }
545
David Brazdil8d5b8b22015-03-24 10:51:52 +0000546 void InsertConstant(HConstant* instruction);
547
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000548 // Cache a float constant into the graph. This method should only be
549 // called by the SsaBuilder when creating "equivalent" instructions.
550 void CacheFloatConstant(HFloatConstant* constant);
551
552 // See CacheFloatConstant comment.
553 void CacheDoubleConstant(HDoubleConstant* constant);
554
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000555 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000556
557 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100558 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000559
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100560 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100561 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000562
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100563 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100564 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100565
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000566 HBasicBlock* entry_block_;
567 HBasicBlock* exit_block_;
568
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100569 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100570 uint16_t maximum_number_of_out_vregs_;
571
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100572 // The number of virtual registers in this method. Contains the parameters.
573 uint16_t number_of_vregs_;
574
575 // The number of virtual registers used by parameters of this method.
576 uint16_t number_of_in_vregs_;
577
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000578 // Number of vreg size slots that the temporaries use (used in baseline compiler).
579 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100580
Mark Mendell1152c922015-04-24 17:06:35 -0400581 // Has bounds checks. We can totally skip BCE if it's false.
582 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800583
David Brazdil77a48ae2015-09-15 12:34:04 +0000584 // Flag whether there are any try/catch blocks in the graph. We will skip
585 // try/catch-related passes if false.
586 bool has_try_catch_;
587
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000588 // Flag whether there are any irreducible loops in the graph.
589 bool has_irreducible_loops_;
590
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000591 // Indicates whether the graph should be compiled in a way that
592 // ensures full debuggability. If false, we can apply more
593 // aggressive optimizations that may limit the level of debugging.
594 const bool debuggable_;
595
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000596 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000597 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000598
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100599 // The dex file from which the method is from.
600 const DexFile& dex_file_;
601
602 // The method index in the dex file.
603 const uint32_t method_idx_;
604
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100605 // If inlined, this encodes how the callee is being invoked.
606 const InvokeType invoke_type_;
607
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100608 // Whether the graph has been transformed to SSA form. Only used
609 // in debug mode to ensure we are not using properties only valid
610 // for non-SSA form (like the number of temporaries).
611 bool in_ssa_form_;
612
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100613 const bool should_generate_constructor_barrier_;
614
Mathieu Chartiere401d142015-04-22 13:56:20 -0700615 const InstructionSet instruction_set_;
616
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000617 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000618 HNullConstant* cached_null_constant_;
619 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000620 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000621 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000622 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000623
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100624 HCurrentMethod* cached_current_method_;
625
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100626 // The ArtMethod this graph is for. Note that for AOT, it may be null,
627 // for example for methods whose declaring class could not be resolved
628 // (such as when the superclass could not be found).
629 ArtMethod* art_method_;
630
David Brazdil4833f5a2015-12-16 10:37:39 +0000631 // Keep the RTI of inexact Object to avoid having to pass stack handle
632 // collection pointer to passes which may create NullConstant.
633 ReferenceTypeInfo inexact_object_rti_;
634
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000635 // Whether we are compiling this graph for on stack replacement: this will
636 // make all loops seen as irreducible and emit special stack maps to mark
637 // compiled code entries which the interpreter can directly jump to.
638 const bool osr_;
639
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000640 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100641 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000642 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000643 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000644 DISALLOW_COPY_AND_ASSIGN(HGraph);
645};
646
Vladimir Markof9f64412015-09-02 14:05:49 +0100647class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000648 public:
649 HLoopInformation(HBasicBlock* header, HGraph* graph)
650 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100651 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000652 irreducible_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100653 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100654 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000655 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100656 back_edges_.reserve(kDefaultNumberOfBackEdges);
657 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100658
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000659 bool IsIrreducible() const { return irreducible_; }
660
661 void Dump(std::ostream& os);
662
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100663 HBasicBlock* GetHeader() const {
664 return header_;
665 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000666
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000667 void SetHeader(HBasicBlock* block) {
668 header_ = block;
669 }
670
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100671 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
672 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
673 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
674
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000675 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100676 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000677 }
678
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100679 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100680 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100681 }
682
David Brazdil46e2a392015-03-16 17:31:52 +0000683 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100684 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100685 }
686
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000687 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100688 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000689 }
690
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100691 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100692
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100693 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100694 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100695 }
696
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100697 // Returns the lifetime position of the back edge that has the
698 // greatest lifetime position.
699 size_t GetLifetimeEnd() const;
700
701 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100702 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100703 }
704
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000705 // Finds blocks that are part of this loop.
706 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100707
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100708 // Returns whether this loop information contains `block`.
709 // Note that this loop information *must* be populated before entering this function.
710 bool Contains(const HBasicBlock& block) const;
711
712 // Returns whether this loop information is an inner loop of `other`.
713 // Note that `other` *must* be populated before entering this function.
714 bool IsIn(const HLoopInformation& other) const;
715
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800716 // Returns true if instruction is not defined within this loop.
717 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700718
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100719 const ArenaBitVector& GetBlocks() const { return blocks_; }
720
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000721 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000722 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000723
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000724 void ClearAllBlocks() {
725 blocks_.ClearAllBits();
726 }
727
David Brazdil7e589fe2016-05-06 12:46:21 +0100728 bool HasBackEdgeNotDominatedByHeader() const;
729
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000730 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100731 // Internal recursive implementation of `Populate`.
732 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100733 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100734
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000735 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100736 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000737 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100738 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100739 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000740
741 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
742};
743
David Brazdilec16f792015-08-19 15:04:01 +0100744// Stores try/catch information for basic blocks.
745// Note that HGraph is constructed so that catch blocks cannot simultaneously
746// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100747class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100748 public:
749 // Try block information constructor.
750 explicit TryCatchInformation(const HTryBoundary& try_entry)
751 : try_entry_(&try_entry),
752 catch_dex_file_(nullptr),
753 catch_type_index_(DexFile::kDexNoIndex16) {
754 DCHECK(try_entry_ != nullptr);
755 }
756
757 // Catch block information constructor.
758 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
759 : try_entry_(nullptr),
760 catch_dex_file_(&dex_file),
761 catch_type_index_(catch_type_index) {}
762
763 bool IsTryBlock() const { return try_entry_ != nullptr; }
764
765 const HTryBoundary& GetTryEntry() const {
766 DCHECK(IsTryBlock());
767 return *try_entry_;
768 }
769
770 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
771
772 bool IsCatchAllTypeIndex() const {
773 DCHECK(IsCatchBlock());
774 return catch_type_index_ == DexFile::kDexNoIndex16;
775 }
776
777 uint16_t GetCatchTypeIndex() const {
778 DCHECK(IsCatchBlock());
779 return catch_type_index_;
780 }
781
782 const DexFile& GetCatchDexFile() const {
783 DCHECK(IsCatchBlock());
784 return *catch_dex_file_;
785 }
786
787 private:
788 // One of possibly several TryBoundary instructions entering the block's try.
789 // Only set for try blocks.
790 const HTryBoundary* try_entry_;
791
792 // Exception type information. Only set for catch blocks.
793 const DexFile* catch_dex_file_;
794 const uint16_t catch_type_index_;
795};
796
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100797static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100798static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100799
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000800// A block in a method. Contains the list of instructions represented
801// as a double linked list. Each block knows its predecessors and
802// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100803
Vladimir Markof9f64412015-09-02 14:05:49 +0100804class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000805 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600806 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000807 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000808 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
809 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000810 loop_information_(nullptr),
811 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000812 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100813 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100814 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100815 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000816 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000817 try_catch_information_(nullptr) {
818 predecessors_.reserve(kDefaultNumberOfPredecessors);
819 successors_.reserve(kDefaultNumberOfSuccessors);
820 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
821 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000822
Vladimir Marko60584552015-09-03 13:35:12 +0000823 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100824 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000825 }
826
Vladimir Marko60584552015-09-03 13:35:12 +0000827 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100828 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000829 }
830
David Brazdild26a4112015-11-10 11:07:31 +0000831 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
832 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
833
Vladimir Marko60584552015-09-03 13:35:12 +0000834 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
835 return ContainsElement(successors_, block, start_from);
836 }
837
838 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100839 return dominated_blocks_;
840 }
841
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100842 bool IsEntryBlock() const {
843 return graph_->GetEntryBlock() == this;
844 }
845
846 bool IsExitBlock() const {
847 return graph_->GetExitBlock() == this;
848 }
849
David Brazdil46e2a392015-03-16 17:31:52 +0000850 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000851 bool IsSingleTryBoundary() const;
852
853 // Returns true if this block emits nothing but a jump.
854 bool IsSingleJump() const {
855 HLoopInformation* loop_info = GetLoopInformation();
856 return (IsSingleGoto() || IsSingleTryBoundary())
857 // Back edges generate a suspend check.
858 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
859 }
David Brazdil46e2a392015-03-16 17:31:52 +0000860
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000861 void AddBackEdge(HBasicBlock* back_edge) {
862 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000863 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000864 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100865 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000866 loop_information_->AddBackEdge(back_edge);
867 }
868
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000869 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000870 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000871
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100872 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000873 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600874 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000875
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000876 HBasicBlock* GetDominator() const { return dominator_; }
877 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000878 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
879
880 void RemoveDominatedBlock(HBasicBlock* block) {
881 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100882 }
Vladimir Marko60584552015-09-03 13:35:12 +0000883
884 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
885 ReplaceElement(dominated_blocks_, existing, new_block);
886 }
887
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100888 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000889
890 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100891 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000892 }
893
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100894 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
895 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100896 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100897 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100898 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
899 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000900
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000901 HInstruction* GetFirstInstructionDisregardMoves() const;
902
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000903 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000904 successors_.push_back(block);
905 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000906 }
907
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100908 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
909 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100910 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000911 new_block->predecessors_.push_back(this);
912 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000913 }
914
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000915 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
916 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000917 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000918 new_block->successors_.push_back(this);
919 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000920 }
921
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100922 // Insert `this` between `predecessor` and `successor. This method
923 // preserves the indicies, and will update the first edge found between
924 // `predecessor` and `successor`.
925 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
926 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100927 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000928 successor->predecessors_[predecessor_index] = this;
929 predecessor->successors_[successor_index] = this;
930 successors_.push_back(successor);
931 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100932 }
933
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100934 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000935 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100936 }
937
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000938 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000939 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000940 }
941
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100942 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000943 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100944 }
945
946 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000947 predecessors_.push_back(block);
948 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100949 }
950
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100951 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000952 DCHECK_EQ(predecessors_.size(), 2u);
953 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100954 }
955
David Brazdil769c9e52015-04-27 13:54:09 +0100956 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000957 DCHECK_EQ(successors_.size(), 2u);
958 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100959 }
960
David Brazdilfc6a86a2015-06-26 10:33:45 +0000961 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000962 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100963 }
964
David Brazdilfc6a86a2015-06-26 10:33:45 +0000965 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000966 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100967 }
968
David Brazdilfc6a86a2015-06-26 10:33:45 +0000969 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000970 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100971 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000972 }
973
974 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000975 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100976 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000977 }
978
979 // Returns whether the first occurrence of `predecessor` in the list of
980 // predecessors is at index `idx`.
981 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100982 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000983 return GetPredecessorIndexOf(predecessor) == idx;
984 }
985
David Brazdild7558da2015-09-22 13:04:14 +0100986 // Create a new block between this block and its predecessors. The new block
987 // is added to the graph, all predecessor edges are relinked to it and an edge
988 // is created to `this`. Returns the new empty block. Reverse post order or
989 // loop and try/catch information are not updated.
990 HBasicBlock* CreateImmediateDominator();
991
David Brazdilfc6a86a2015-06-26 10:33:45 +0000992 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100993 // created, latter block. Note that this method will add the block to the
994 // graph, create a Goto at the end of the former block and will create an edge
995 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100996 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000997 HBasicBlock* SplitBefore(HInstruction* cursor);
998
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000999 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001000 // created block. Note that this method just updates raw block information,
1001 // like predecessors, successors, dominators, and instruction list. It does not
1002 // update the graph, reverse post order, loop information, nor make sure the
1003 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001004 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1005
1006 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1007 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001008
1009 // Merge `other` at the end of `this`. Successors and dominated blocks of
1010 // `other` are changed to be successors and dominated blocks of `this`. Note
1011 // that this method does not update the graph, reverse post order, loop
1012 // information, nor make sure the blocks are consistent (for example ending
1013 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001014 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001015
1016 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1017 // of `this` are moved to `other`.
1018 // Note that this method does not update the graph, reverse post order, loop
1019 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001020 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001021 void ReplaceWith(HBasicBlock* other);
1022
David Brazdil2d7352b2015-04-20 14:52:42 +01001023 // Merge `other` at the end of `this`. This method updates loops, reverse post
1024 // order, links to predecessors, successors, dominators and deletes the block
1025 // from the graph. The two blocks must be successive, i.e. `this` the only
1026 // predecessor of `other` and vice versa.
1027 void MergeWith(HBasicBlock* other);
1028
1029 // Disconnects `this` from all its predecessors, successors and dominator,
1030 // removes it from all loops it is included in and eventually from the graph.
1031 // The block must not dominate any other block. Predecessors and successors
1032 // are safely updated.
1033 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001034
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001035 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001036 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001037 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001038 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001039 // Replace instruction `initial` with `replacement` within this block.
1040 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1041 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001042 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001043 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001044 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001045 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1046 // instruction list. With 'ensure_safety' set to true, it verifies that the
1047 // instruction is not in use and removes it from the use lists of its inputs.
1048 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1049 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001050 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001051
1052 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001053 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001054 }
1055
Roland Levillain6b879dd2014-09-22 17:13:44 +01001056 bool IsLoopPreHeaderFirstPredecessor() const {
1057 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001058 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001059 }
1060
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001061 bool IsFirstPredecessorBackEdge() const {
1062 DCHECK(IsLoopHeader());
1063 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1064 }
1065
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001066 HLoopInformation* GetLoopInformation() const {
1067 return loop_information_;
1068 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001069
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001070 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001071 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001072 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001073 void SetInLoop(HLoopInformation* info) {
1074 if (IsLoopHeader()) {
1075 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001076 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001077 loop_information_ = info;
1078 } else if (loop_information_->Contains(*info->GetHeader())) {
1079 // Block is currently part of an outer loop. Make it part of this inner loop.
1080 // Note that a non loop header having a loop information means this loop information
1081 // has already been populated
1082 loop_information_ = info;
1083 } else {
1084 // Block is part of an inner loop. Do not update the loop information.
1085 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1086 // at this point, because this method is being called while populating `info`.
1087 }
1088 }
1089
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001090 // Raw update of the loop information.
1091 void SetLoopInformation(HLoopInformation* info) {
1092 loop_information_ = info;
1093 }
1094
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001095 bool IsInLoop() const { return loop_information_ != nullptr; }
1096
David Brazdilec16f792015-08-19 15:04:01 +01001097 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1098
1099 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1100 try_catch_information_ = try_catch_information;
1101 }
1102
1103 bool IsTryBlock() const {
1104 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1105 }
1106
1107 bool IsCatchBlock() const {
1108 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1109 }
David Brazdilffee3d32015-07-06 11:48:53 +01001110
1111 // Returns the try entry that this block's successors should have. They will
1112 // be in the same try, unless the block ends in a try boundary. In that case,
1113 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001114 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001115
David Brazdild7558da2015-09-22 13:04:14 +01001116 bool HasThrowingInstructions() const;
1117
David Brazdila4b8c212015-05-07 09:59:30 +01001118 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001119 bool Dominates(HBasicBlock* block) const;
1120
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001121 size_t GetLifetimeStart() const { return lifetime_start_; }
1122 size_t GetLifetimeEnd() const { return lifetime_end_; }
1123
1124 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1125 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1126
David Brazdil8d5b8b22015-03-24 10:51:52 +00001127 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001128 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001129 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001130 bool HasSinglePhi() const;
1131
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001132 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001133 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001134 ArenaVector<HBasicBlock*> predecessors_;
1135 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001136 HInstructionList instructions_;
1137 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001138 HLoopInformation* loop_information_;
1139 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001140 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001141 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001142 // The dex program counter of the first instruction of this block.
1143 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001144 size_t lifetime_start_;
1145 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001146 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001147
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001148 friend class HGraph;
1149 friend class HInstruction;
1150
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001151 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1152};
1153
David Brazdilb2bd1c52015-03-25 11:17:37 +00001154// Iterates over the LoopInformation of all loops which contain 'block'
1155// from the innermost to the outermost.
1156class HLoopInformationOutwardIterator : public ValueObject {
1157 public:
1158 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1159 : current_(block.GetLoopInformation()) {}
1160
1161 bool Done() const { return current_ == nullptr; }
1162
1163 void Advance() {
1164 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001165 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001166 }
1167
1168 HLoopInformation* Current() const {
1169 DCHECK(!Done());
1170 return current_;
1171 }
1172
1173 private:
1174 HLoopInformation* current_;
1175
1176 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1177};
1178
Alexandre Ramesef20f712015-06-09 10:29:30 +01001179#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001180 M(Above, Condition) \
1181 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001182 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001183 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001184 M(ArrayGet, Instruction) \
1185 M(ArrayLength, Instruction) \
1186 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001187 M(Below, Condition) \
1188 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001189 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001190 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001191 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001192 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001193 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001194 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001195 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001196 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001197 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001198 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001199 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001200 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001201 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001202 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001203 M(Exit, Instruction) \
1204 M(FloatConstant, Constant) \
1205 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001206 M(GreaterThan, Condition) \
1207 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001208 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001209 M(InstanceFieldGet, Instruction) \
1210 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001211 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001212 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001213 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001214 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001215 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001216 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001217 M(LessThan, Condition) \
1218 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001219 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001220 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001221 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001222 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001223 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001224 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001225 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001226 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001227 M(Neg, UnaryOperation) \
1228 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001229 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001230 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001231 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001232 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001233 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001234 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001235 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001236 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001237 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001238 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001239 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001240 M(Return, Instruction) \
1241 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001242 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001243 M(Shl, BinaryOperation) \
1244 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001245 M(StaticFieldGet, Instruction) \
1246 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001247 M(UnresolvedInstanceFieldGet, Instruction) \
1248 M(UnresolvedInstanceFieldSet, Instruction) \
1249 M(UnresolvedStaticFieldGet, Instruction) \
1250 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001251 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001252 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001253 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001254 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001255 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001256 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001257 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001258 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001259
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001260/*
1261 * Instructions, shared across several (not all) architectures.
1262 */
1263#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1264#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1265#else
1266#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001267 M(BitwiseNegatedRight, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001268 M(MultiplyAccumulate, Instruction)
1269#endif
1270
Vladimir Markob4536b72015-11-24 13:45:23 +00001271#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001272#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001273#else
1274#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1275 M(ArmDexCacheArraysBase, Instruction)
1276#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001277
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001278#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001279#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001280#else
1281#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001282 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001283 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001284#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001285
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001286#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1287
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001288#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1289
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001290#ifndef ART_ENABLE_CODEGEN_x86
1291#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1292#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001293#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1294 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001295 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001296 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001297 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001298#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001299
1300#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1301
1302#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1303 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001304 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001305 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1306 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001307 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001308 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001309 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1310 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1311
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001312#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1313 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001314 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001315 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001316 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001317 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001318
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001319#define FOR_EACH_INSTRUCTION(M) \
1320 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1321 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1322
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001323#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001324FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1325#undef FORWARD_DECLARATION
1326
Roland Levillainccc07a92014-09-16 14:48:16 +01001327#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001328 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001329 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001330 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001331 return other->Is##type(); \
1332 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001333 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001334
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001335#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1336 bool Is##type() const { return As##type() != nullptr; } \
1337 const H##type* As##type() const { return this; } \
1338 H##type* As##type() { return this; }
1339
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001340template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001341class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001342 public:
David Brazdiled596192015-01-23 10:39:45 +00001343 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001344 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001345 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001346
Vladimir Markod59f3b12016-03-29 12:21:58 +01001347 // Hook for the IntrusiveForwardList<>.
1348 // TODO: Hide this better.
1349 IntrusiveForwardListHook hook;
1350
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001351 private:
David Brazdiled596192015-01-23 10:39:45 +00001352 HUseListNode(T user, size_t index)
Vladimir Markod59f3b12016-03-29 12:21:58 +01001353 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001354
1355 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001356 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001357
Vladimir Markod59f3b12016-03-29 12:21:58 +01001358 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001359
1360 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1361};
1362
David Brazdiled596192015-01-23 10:39:45 +00001363template <typename T>
Vladimir Markod59f3b12016-03-29 12:21:58 +01001364using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001365
David Brazdil1abb4192015-02-17 18:33:36 +00001366// This class is used by HEnvironment and HInstruction classes to record the
1367// instructions they use and pointers to the corresponding HUseListNodes kept
1368// by the used instructions.
1369template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001370class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001371 public:
Vladimir Markod59f3b12016-03-29 12:21:58 +01001372 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1373 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001374
Vladimir Markod59f3b12016-03-29 12:21:58 +01001375 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1376 : HUserRecord(old_record.instruction_, before_use_node) {}
1377 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1378 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001379 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001380 }
1381
1382 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Markod59f3b12016-03-29 12:21:58 +01001383 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1384 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001385
1386 private:
1387 // Instruction used by the user.
1388 HInstruction* instruction_;
1389
Vladimir Markod59f3b12016-03-29 12:21:58 +01001390 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1391 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001392};
1393
Aart Bik854a02b2015-07-14 16:07:00 -07001394/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001395 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001396 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001397 * For write/read dependences on fields/arrays, the dependence analysis uses
1398 * type disambiguation (e.g. a float field write cannot modify the value of an
1399 * integer field read) and the access type (e.g. a reference array write cannot
1400 * modify the value of a reference field read [although it may modify the
1401 * reference fetch prior to reading the field, which is represented by its own
1402 * write/read dependence]). The analysis makes conservative points-to
1403 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1404 * the same, and any reference read depends on any reference read without
1405 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001406 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001407 * The internal representation uses 38-bit and is described in the table below.
1408 * The first line indicates the side effect, and for field/array accesses the
1409 * second line indicates the type of the access (in the order of the
1410 * Primitive::Type enum).
1411 * The two numbered lines below indicate the bit position in the bitfield (read
1412 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001413 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001414 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1415 * +-------------+---------+---------+--------------+---------+---------+
1416 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1417 * | 3 |333333322|222222221| 1 |111111110|000000000|
1418 * | 7 |654321098|765432109| 8 |765432109|876543210|
1419 *
1420 * Note that, to ease the implementation, 'changes' bits are least significant
1421 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001422 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001423class SideEffects : public ValueObject {
1424 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001425 SideEffects() : flags_(0) {}
1426
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001427 static SideEffects None() {
1428 return SideEffects(0);
1429 }
1430
1431 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001432 return SideEffects(kAllChangeBits | kAllDependOnBits);
1433 }
1434
1435 static SideEffects AllChanges() {
1436 return SideEffects(kAllChangeBits);
1437 }
1438
1439 static SideEffects AllDependencies() {
1440 return SideEffects(kAllDependOnBits);
1441 }
1442
1443 static SideEffects AllExceptGCDependency() {
1444 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1445 }
1446
1447 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001448 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001449 }
1450
Aart Bik34c3ba92015-07-20 14:08:59 -07001451 static SideEffects AllWrites() {
1452 return SideEffects(kAllWrites);
1453 }
1454
1455 static SideEffects AllReads() {
1456 return SideEffects(kAllReads);
1457 }
1458
1459 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1460 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001461 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001462 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001463 }
1464
Aart Bik854a02b2015-07-14 16:07:00 -07001465 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1466 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001467 }
1468
Aart Bik34c3ba92015-07-20 14:08:59 -07001469 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1470 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001471 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001472 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001473 }
1474
1475 static SideEffects ArrayReadOfType(Primitive::Type type) {
1476 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1477 }
1478
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001479 static SideEffects CanTriggerGC() {
1480 return SideEffects(1ULL << kCanTriggerGCBit);
1481 }
1482
1483 static SideEffects DependsOnGC() {
1484 return SideEffects(1ULL << kDependsOnGCBit);
1485 }
1486
Aart Bik854a02b2015-07-14 16:07:00 -07001487 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001488 SideEffects Union(SideEffects other) const {
1489 return SideEffects(flags_ | other.flags_);
1490 }
1491
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001492 SideEffects Exclusion(SideEffects other) const {
1493 return SideEffects(flags_ & ~other.flags_);
1494 }
1495
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001496 void Add(SideEffects other) {
1497 flags_ |= other.flags_;
1498 }
1499
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001500 bool Includes(SideEffects other) const {
1501 return (other.flags_ & flags_) == other.flags_;
1502 }
1503
1504 bool HasSideEffects() const {
1505 return (flags_ & kAllChangeBits);
1506 }
1507
1508 bool HasDependencies() const {
1509 return (flags_ & kAllDependOnBits);
1510 }
1511
1512 // Returns true if there are no side effects or dependencies.
1513 bool DoesNothing() const {
1514 return flags_ == 0;
1515 }
1516
Aart Bik854a02b2015-07-14 16:07:00 -07001517 // Returns true if something is written.
1518 bool DoesAnyWrite() const {
1519 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001520 }
1521
Aart Bik854a02b2015-07-14 16:07:00 -07001522 // Returns true if something is read.
1523 bool DoesAnyRead() const {
1524 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001525 }
1526
Aart Bik854a02b2015-07-14 16:07:00 -07001527 // Returns true if potentially everything is written and read
1528 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001529 bool DoesAllReadWrite() const {
1530 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1531 }
1532
Aart Bik854a02b2015-07-14 16:07:00 -07001533 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001534 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001535 }
1536
Roland Levillain0d5a2812015-11-13 10:07:31 +00001537 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001538 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001539 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1540 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001541 }
1542
1543 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001544 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001545 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001546 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001547 for (int s = kLastBit; s >= 0; s--) {
1548 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1549 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1550 // This is a bit for the GC side effect.
1551 if (current_bit_is_set) {
1552 flags += "GC";
1553 }
Aart Bik854a02b2015-07-14 16:07:00 -07001554 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001555 } else {
1556 // This is a bit for the array/field analysis.
1557 // The underscore character stands for the 'can trigger GC' bit.
1558 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1559 if (current_bit_is_set) {
1560 flags += kDebug[s];
1561 }
1562 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1563 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1564 flags += "|";
1565 }
1566 }
Aart Bik854a02b2015-07-14 16:07:00 -07001567 }
1568 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001569 }
1570
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001571 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001572
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001573 private:
1574 static constexpr int kFieldArrayAnalysisBits = 9;
1575
1576 static constexpr int kFieldWriteOffset = 0;
1577 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1578 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1579 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1580
1581 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1582
1583 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1584 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1585 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1586 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1587
1588 static constexpr int kLastBit = kDependsOnGCBit;
1589 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1590
1591 // Aliases.
1592
1593 static_assert(kChangeBits == kDependOnBits,
1594 "the 'change' bits should match the 'depend on' bits.");
1595
1596 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1597 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1598 static constexpr uint64_t kAllWrites =
1599 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1600 static constexpr uint64_t kAllReads =
1601 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001602
Aart Bik854a02b2015-07-14 16:07:00 -07001603 // Work around the fact that HIR aliases I/F and J/D.
1604 // TODO: remove this interceptor once HIR types are clean
1605 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1606 switch (type) {
1607 case Primitive::kPrimInt:
1608 case Primitive::kPrimFloat:
1609 return TypeFlag(Primitive::kPrimInt, offset) |
1610 TypeFlag(Primitive::kPrimFloat, offset);
1611 case Primitive::kPrimLong:
1612 case Primitive::kPrimDouble:
1613 return TypeFlag(Primitive::kPrimLong, offset) |
1614 TypeFlag(Primitive::kPrimDouble, offset);
1615 default:
1616 return TypeFlag(type, offset);
1617 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001618 }
1619
Aart Bik854a02b2015-07-14 16:07:00 -07001620 // Translates type to bit flag.
1621 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1622 CHECK_NE(type, Primitive::kPrimVoid);
1623 const uint64_t one = 1;
1624 const int shift = type; // 0-based consecutive enum
1625 DCHECK_LE(kFieldWriteOffset, shift);
1626 DCHECK_LT(shift, kArrayWriteOffset);
1627 return one << (type + offset);
1628 }
1629
1630 // Private constructor on direct flags value.
1631 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1632
1633 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001634};
1635
David Brazdiled596192015-01-23 10:39:45 +00001636// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001637class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001638 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001639 HEnvironment(ArenaAllocator* arena,
1640 size_t number_of_vregs,
1641 const DexFile& dex_file,
1642 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001643 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001644 InvokeType invoke_type,
1645 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001646 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1647 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001648 parent_(nullptr),
1649 dex_file_(dex_file),
1650 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001651 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001652 invoke_type_(invoke_type),
1653 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001654 }
1655
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001656 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001657 : HEnvironment(arena,
1658 to_copy.Size(),
1659 to_copy.GetDexFile(),
1660 to_copy.GetMethodIdx(),
1661 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001662 to_copy.GetInvokeType(),
1663 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001664
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001665 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001666 if (parent_ != nullptr) {
1667 parent_->SetAndCopyParentChain(allocator, parent);
1668 } else {
1669 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1670 parent_->CopyFrom(parent);
1671 if (parent->GetParent() != nullptr) {
1672 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1673 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001674 }
David Brazdiled596192015-01-23 10:39:45 +00001675 }
1676
Vladimir Marko71bf8092015-09-15 15:33:14 +01001677 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001678 void CopyFrom(HEnvironment* environment);
1679
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001680 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1681 // input to the loop phi instead. This is for inserting instructions that
1682 // require an environment (like HDeoptimization) in the loop pre-header.
1683 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001684
1685 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001686 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001687 }
1688
1689 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001690 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001691 }
1692
David Brazdil1abb4192015-02-17 18:33:36 +00001693 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001694
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001695 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001696
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001697 HEnvironment* GetParent() const { return parent_; }
1698
1699 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001700 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001701 }
1702
1703 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001704 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001705 }
1706
1707 uint32_t GetDexPc() const {
1708 return dex_pc_;
1709 }
1710
1711 uint32_t GetMethodIdx() const {
1712 return method_idx_;
1713 }
1714
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001715 InvokeType GetInvokeType() const {
1716 return invoke_type_;
1717 }
1718
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001719 const DexFile& GetDexFile() const {
1720 return dex_file_;
1721 }
1722
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001723 HInstruction* GetHolder() const {
1724 return holder_;
1725 }
1726
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001727
1728 bool IsFromInlinedInvoke() const {
1729 return GetParent() != nullptr;
1730 }
1731
David Brazdiled596192015-01-23 10:39:45 +00001732 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001733 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1734 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001735 HEnvironment* parent_;
1736 const DexFile& dex_file_;
1737 const uint32_t method_idx_;
1738 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001739 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001740
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001741 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001742 HInstruction* const holder_;
1743
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001744 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001745
1746 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1747};
1748
Vladimir Markof9f64412015-09-02 14:05:49 +01001749class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001750 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001751 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001752 : previous_(nullptr),
1753 next_(nullptr),
1754 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001755 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001756 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001757 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001758 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001759 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001760 locations_(nullptr),
1761 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001762 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001763 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001764 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1765 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1766 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001767
Dave Allison20dfc792014-06-16 20:44:29 -07001768 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001769
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001770#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001771 enum InstructionKind {
1772 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1773 };
1774#undef DECLARE_KIND
1775
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001776 HInstruction* GetNext() const { return next_; }
1777 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001778
Calin Juravle77520bc2015-01-12 18:45:46 +00001779 HInstruction* GetNextDisregardingMoves() const;
1780 HInstruction* GetPreviousDisregardingMoves() const;
1781
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001782 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001783 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001784 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001785 bool IsInBlock() const { return block_ != nullptr; }
1786 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001787 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1788 bool IsIrreducibleLoopHeaderPhi() const {
1789 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1790 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001791
Roland Levillain6b879dd2014-09-22 17:13:44 +01001792 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001793 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001794
1795 virtual void Accept(HGraphVisitor* visitor) = 0;
1796 virtual const char* DebugName() const = 0;
1797
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001798 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001799 void SetRawInputAt(size_t index, HInstruction* input) {
1800 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1801 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001802
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001803 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001804
1805 uint32_t GetDexPc() const { return dex_pc_; }
1806
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001807 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001808
Roland Levillaine161a2a2014-10-03 12:45:18 +01001809 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001810 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001811
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001812 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001813 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001814
Calin Juravle10e244f2015-01-26 18:54:32 +00001815 // Does not apply for all instructions, but having this at top level greatly
1816 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001817 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001818 virtual bool CanBeNull() const {
1819 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1820 return true;
1821 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001822
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001823 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001824 return false;
1825 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001826
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001827 virtual bool IsActualObject() const {
1828 return GetType() == Primitive::kPrimNot;
1829 }
1830
Calin Juravle2e768302015-07-28 14:41:11 +00001831 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001832
Calin Juravle61d544b2015-02-23 16:46:57 +00001833 ReferenceTypeInfo GetReferenceTypeInfo() const {
1834 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001835 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Markoa4336d22016-04-19 14:12:13 +00001836 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001837 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001838
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001839 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001840 DCHECK(user != nullptr);
Vladimir Markod59f3b12016-03-29 12:21:58 +01001841 // Note: fixup_end remains valid across push_front().
1842 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1843 HUseListNode<HInstruction*>* new_node =
1844 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1845 uses_.push_front(*new_node);
1846 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001847 }
1848
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001849 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001850 DCHECK(user != nullptr);
Vladimir Markod59f3b12016-03-29 12:21:58 +01001851 // Note: env_fixup_end remains valid across push_front().
1852 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1853 HUseListNode<HEnvironment*>* new_node =
1854 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1855 env_uses_.push_front(*new_node);
1856 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001857 }
1858
David Brazdil1abb4192015-02-17 18:33:36 +00001859 void RemoveAsUserOfInput(size_t input) {
1860 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Markod59f3b12016-03-29 12:21:58 +01001861 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1862 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1863 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001864 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001865
David Brazdil1abb4192015-02-17 18:33:36 +00001866 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1867 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001868
Vladimir Markod59f3b12016-03-29 12:21:58 +01001869 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1870 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1871 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001872 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Markod59f3b12016-03-29 12:21:58 +01001873 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001874 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001875
Roland Levillain6c82d402014-10-13 16:10:27 +01001876 // Does this instruction strictly dominate `other_instruction`?
1877 // Returns false if this instruction and `other_instruction` are the same.
1878 // Aborts if this instruction and `other_instruction` are both phis.
1879 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001880
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001881 int GetId() const { return id_; }
1882 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001883
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001884 int GetSsaIndex() const { return ssa_index_; }
1885 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1886 bool HasSsaIndex() const { return ssa_index_ != -1; }
1887
1888 bool HasEnvironment() const { return environment_ != nullptr; }
1889 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001890 // Set the `environment_` field. Raw because this method does not
1891 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001892 void SetRawEnvironment(HEnvironment* environment) {
1893 DCHECK(environment_ == nullptr);
1894 DCHECK_EQ(environment->GetHolder(), this);
1895 environment_ = environment;
1896 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001897
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001898 void RemoveEnvironment();
1899
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001900 // Set the environment of this instruction, copying it from `environment`. While
1901 // copying, the uses lists are being updated.
1902 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001903 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001904 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001905 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001906 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001907 if (environment->GetParent() != nullptr) {
1908 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1909 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001910 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001911
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001912 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1913 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001914 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001915 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001916 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001917 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001918 if (environment->GetParent() != nullptr) {
1919 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1920 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001921 }
1922
Nicolas Geoffray39468442014-09-02 15:17:15 +01001923 // Returns the number of entries in the environment. Typically, that is the
1924 // number of dex registers in a method. It could be more in case of inlining.
1925 size_t EnvironmentSize() const;
1926
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001927 LocationSummary* GetLocations() const { return locations_; }
1928 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001929
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001930 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001931 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001932
Alexandre Rames188d4312015-04-09 18:30:21 +01001933 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1934 // uses of this instruction by `other` are *not* updated.
1935 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1936 ReplaceWith(other);
1937 other->ReplaceInput(this, use_index);
1938 }
1939
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001940 // Move `this` instruction before `cursor`.
1941 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001942
Vladimir Markofb337ea2015-11-25 15:25:10 +00001943 // Move `this` before its first user and out of any loops. If there is no
1944 // out-of-loop user that dominates all other users, move the instruction
1945 // to the end of the out-of-loop common dominator of the user's blocks.
1946 //
1947 // This can be used only on non-throwing instructions with no side effects that
1948 // have at least one use but no environment uses.
1949 void MoveBeforeFirstUserAndOutOfLoops();
1950
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001951#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001952 bool Is##type() const; \
1953 const H##type* As##type() const; \
1954 H##type* As##type();
1955
1956 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1957#undef INSTRUCTION_TYPE_CHECK
1958
1959#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001960 bool Is##type() const { return (As##type() != nullptr); } \
1961 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001962 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001963 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001964#undef INSTRUCTION_TYPE_CHECK
1965
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001966 // Returns whether the instruction can be moved within the graph.
1967 virtual bool CanBeMoved() const { return false; }
1968
1969 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001970 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001971 return false;
1972 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001973
1974 // Returns whether any data encoded in the two instructions is equal.
1975 // This method does not look at the inputs. Both instructions must be
1976 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001977 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001978 return false;
1979 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001980
1981 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001982 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001983 // 2) Their inputs are identical.
1984 bool Equals(HInstruction* other) const;
1985
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001986 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
1987 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
1988 // the virtual function because the __attribute__((__pure__)) doesn't really
1989 // apply the strong requirement for virtual functions, preventing optimizations.
1990 InstructionKind GetKind() const PURE;
1991 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001992
1993 virtual size_t ComputeHashCode() const {
1994 size_t result = GetKind();
1995 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1996 result = (result * 31) + InputAt(i)->GetId();
1997 }
1998 return result;
1999 }
2000
2001 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002002 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002003 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002004
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002005 size_t GetLifetimePosition() const { return lifetime_position_; }
2006 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2007 LiveInterval* GetLiveInterval() const { return live_interval_; }
2008 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2009 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2010
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002011 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2012
2013 // Returns whether the code generation of the instruction will require to have access
2014 // to the current method. Such instructions are:
2015 // (1): Instructions that require an environment, as calling the runtime requires
2016 // to walk the stack and have the current method stored at a specific stack address.
2017 // (2): Object literals like classes and strings, that are loaded from the dex cache
2018 // fields of the current method.
2019 bool NeedsCurrentMethod() const {
2020 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2021 }
2022
Vladimir Markodc151b22015-10-15 18:02:30 +01002023 // Returns whether the code generation of the instruction will require to have access
2024 // to the dex cache of the current method's declaring class via the current method.
2025 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002026
Mark Mendellc4701932015-04-10 13:18:51 -04002027 // Does this instruction have any use in an environment before
2028 // control flow hits 'other'?
2029 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2030
2031 // Remove all references to environment uses of this instruction.
2032 // The caller must ensure that this is safe to do.
2033 void RemoveEnvironmentUsers();
2034
Vladimir Markoa1de9182016-02-25 11:37:38 +00002035 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2036 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002037
David Brazdil1abb4192015-02-17 18:33:36 +00002038 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002039 // If set, the machine code for this instruction is assumed to be generated by
2040 // its users. Used by liveness analysis to compute use positions accordingly.
2041 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2042 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2043 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2044 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2045
David Brazdil1abb4192015-02-17 18:33:36 +00002046 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2047 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2048
Vladimir Markoa1de9182016-02-25 11:37:38 +00002049 uint32_t GetPackedFields() const {
2050 return packed_fields_;
2051 }
2052
2053 template <size_t flag>
2054 bool GetPackedFlag() const {
2055 return (packed_fields_ & (1u << flag)) != 0u;
2056 }
2057
2058 template <size_t flag>
2059 void SetPackedFlag(bool value = true) {
2060 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2061 }
2062
2063 template <typename BitFieldType>
2064 typename BitFieldType::value_type GetPackedField() const {
2065 return BitFieldType::Decode(packed_fields_);
2066 }
2067
2068 template <typename BitFieldType>
2069 void SetPackedField(typename BitFieldType::value_type value) {
2070 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2071 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2072 }
2073
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002074 private:
Vladimir Markod59f3b12016-03-29 12:21:58 +01002075 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2076 auto before_use_node = uses_.before_begin();
2077 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2078 HInstruction* user = use_node->GetUser();
2079 size_t input_index = use_node->GetIndex();
2080 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2081 before_use_node = use_node;
2082 }
2083 }
2084
2085 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2086 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2087 if (next != uses_.end()) {
2088 HInstruction* next_user = next->GetUser();
2089 size_t next_index = next->GetIndex();
2090 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2091 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2092 }
2093 }
2094
2095 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2096 auto before_env_use_node = env_uses_.before_begin();
2097 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2098 HEnvironment* user = env_use_node->GetUser();
2099 size_t input_index = env_use_node->GetIndex();
2100 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2101 before_env_use_node = env_use_node;
2102 }
2103 }
2104
2105 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2106 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2107 if (next != env_uses_.end()) {
2108 HEnvironment* next_user = next->GetUser();
2109 size_t next_index = next->GetIndex();
2110 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2111 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2112 }
2113 }
David Brazdil1abb4192015-02-17 18:33:36 +00002114
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002115 HInstruction* previous_;
2116 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002117 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002118 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002119
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002120 // An instruction gets an id when it is added to the graph.
2121 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002122 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002123 int id_;
2124
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002125 // When doing liveness analysis, instructions that have uses get an SSA index.
2126 int ssa_index_;
2127
Vladimir Markoa1de9182016-02-25 11:37:38 +00002128 // Packed fields.
2129 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002130
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002131 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002132 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002133
2134 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002135 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002136
Nicolas Geoffray39468442014-09-02 15:17:15 +01002137 // The environment associated with this instruction. Not null if the instruction
2138 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002139 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002140
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002141 // Set by the code generator.
2142 LocationSummary* locations_;
2143
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002144 // Set by the liveness analysis.
2145 LiveInterval* live_interval_;
2146
2147 // Set by the liveness analysis, this is the position in a linear
2148 // order of blocks where this instruction's live interval start.
2149 size_t lifetime_position_;
2150
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002151 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002152
Vladimir Markoa1de9182016-02-25 11:37:38 +00002153 // The reference handle part of the reference type info.
2154 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002155 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002156 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002157
David Brazdil1abb4192015-02-17 18:33:36 +00002158 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002159 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002160 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002161 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002162 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002163
2164 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2165};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002166std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002167
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002168class HInputIterator : public ValueObject {
2169 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002170 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002171
2172 bool Done() const { return index_ == instruction_->InputCount(); }
2173 HInstruction* Current() const { return instruction_->InputAt(index_); }
2174 void Advance() { index_++; }
2175
2176 private:
2177 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002178 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002179
2180 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2181};
2182
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002183class HInstructionIterator : public ValueObject {
2184 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002185 explicit HInstructionIterator(const HInstructionList& instructions)
2186 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002187 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002188 }
2189
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002190 bool Done() const { return instruction_ == nullptr; }
2191 HInstruction* Current() const { return instruction_; }
2192 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002193 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002194 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002195 }
2196
2197 private:
2198 HInstruction* instruction_;
2199 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002200
2201 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002202};
2203
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002204class HBackwardInstructionIterator : public ValueObject {
2205 public:
2206 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2207 : instruction_(instructions.last_instruction_) {
2208 next_ = Done() ? nullptr : instruction_->GetPrevious();
2209 }
2210
2211 bool Done() const { return instruction_ == nullptr; }
2212 HInstruction* Current() const { return instruction_; }
2213 void Advance() {
2214 instruction_ = next_;
2215 next_ = Done() ? nullptr : instruction_->GetPrevious();
2216 }
2217
2218 private:
2219 HInstruction* instruction_;
2220 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002221
2222 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002223};
2224
Vladimir Markof9f64412015-09-02 14:05:49 +01002225template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002226class HTemplateInstruction: public HInstruction {
2227 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002228 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002229 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002230 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002231
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002232 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002233
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002234 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002235 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2236 DCHECK_LT(i, N);
2237 return inputs_[i];
2238 }
David Brazdil1abb4192015-02-17 18:33:36 +00002239
2240 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002241 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002242 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002243 }
2244
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002245 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002246 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002247
2248 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002249};
2250
Vladimir Markof9f64412015-09-02 14:05:49 +01002251// HTemplateInstruction specialization for N=0.
2252template<>
2253class HTemplateInstruction<0>: public HInstruction {
2254 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002255 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002256 : HInstruction(side_effects, dex_pc) {}
2257
Vladimir Markof9f64412015-09-02 14:05:49 +01002258 virtual ~HTemplateInstruction() {}
2259
2260 size_t InputCount() const OVERRIDE { return 0; }
2261
2262 protected:
2263 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2264 LOG(FATAL) << "Unreachable";
2265 UNREACHABLE();
2266 }
2267
2268 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2269 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2270 LOG(FATAL) << "Unreachable";
2271 UNREACHABLE();
2272 }
2273
2274 private:
2275 friend class SsaBuilder;
2276};
2277
Dave Allison20dfc792014-06-16 20:44:29 -07002278template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002279class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002280 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002281 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002282 : HTemplateInstruction<N>(side_effects, dex_pc) {
2283 this->template SetPackedField<TypeField>(type);
2284 }
Dave Allison20dfc792014-06-16 20:44:29 -07002285 virtual ~HExpression() {}
2286
Vladimir Markoa1de9182016-02-25 11:37:38 +00002287 Primitive::Type GetType() const OVERRIDE {
2288 return TypeField::Decode(this->GetPackedFields());
2289 }
Dave Allison20dfc792014-06-16 20:44:29 -07002290
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002291 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002292 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2293 static constexpr size_t kFieldTypeSize =
2294 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2295 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2296 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2297 "Too many packed fields.");
2298 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002299};
2300
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002301// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2302// instruction that branches to the exit block.
2303class HReturnVoid : public HTemplateInstruction<0> {
2304 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002305 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2306 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002307
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002308 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002309
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002310 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002311
2312 private:
2313 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2314};
2315
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002316// Represents dex's RETURN opcodes. A HReturn is a control flow
2317// instruction that branches to the exit block.
2318class HReturn : public HTemplateInstruction<1> {
2319 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002320 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2321 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002322 SetRawInputAt(0, value);
2323 }
2324
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002325 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002326
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002327 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002328
2329 private:
2330 DISALLOW_COPY_AND_ASSIGN(HReturn);
2331};
2332
David Brazdildee58d62016-04-07 09:54:26 +00002333class HPhi : public HInstruction {
2334 public:
2335 HPhi(ArenaAllocator* arena,
2336 uint32_t reg_number,
2337 size_t number_of_inputs,
2338 Primitive::Type type,
2339 uint32_t dex_pc = kNoDexPc)
2340 : HInstruction(SideEffects::None(), dex_pc),
2341 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2342 reg_number_(reg_number) {
2343 SetPackedField<TypeField>(ToPhiType(type));
2344 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2345 // Phis are constructed live and marked dead if conflicting or unused.
2346 // Individual steps of SsaBuilder should assume that if a phi has been
2347 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2348 SetPackedFlag<kFlagIsLive>(true);
2349 SetPackedFlag<kFlagCanBeNull>(true);
2350 }
2351
2352 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2353 static Primitive::Type ToPhiType(Primitive::Type type) {
2354 return Primitive::PrimitiveKind(type);
2355 }
2356
2357 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2358
2359 size_t InputCount() const OVERRIDE { return inputs_.size(); }
2360
2361 void AddInput(HInstruction* input);
2362 void RemoveInputAt(size_t index);
2363
2364 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2365 void SetType(Primitive::Type new_type) {
2366 // Make sure that only valid type changes occur. The following are allowed:
2367 // (1) int -> float/ref (primitive type propagation),
2368 // (2) long -> double (primitive type propagation).
2369 DCHECK(GetType() == new_type ||
2370 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2371 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2372 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2373 SetPackedField<TypeField>(new_type);
2374 }
2375
2376 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2377 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2378
2379 uint32_t GetRegNumber() const { return reg_number_; }
2380
2381 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2382 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2383 bool IsDead() const { return !IsLive(); }
2384 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2385
2386 bool IsVRegEquivalentOf(HInstruction* other) const {
2387 return other != nullptr
2388 && other->IsPhi()
2389 && other->AsPhi()->GetBlock() == GetBlock()
2390 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2391 }
2392
2393 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2394 // An equivalent phi is a phi having the same dex register and type.
2395 // It assumes that phis with the same dex register are adjacent.
2396 HPhi* GetNextEquivalentPhiWithSameType() {
2397 HInstruction* next = GetNext();
2398 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2399 if (next->GetType() == GetType()) {
2400 return next->AsPhi();
2401 }
2402 next = next->GetNext();
2403 }
2404 return nullptr;
2405 }
2406
2407 DECLARE_INSTRUCTION(Phi);
2408
2409 protected:
2410 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
2411 return inputs_[index];
2412 }
2413
2414 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2415 inputs_[index] = input;
2416 }
2417
2418 private:
2419 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2420 static constexpr size_t kFieldTypeSize =
2421 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2422 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2423 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2424 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2425 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2426 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2427
2428 ArenaVector<HUserRecord<HInstruction*> > inputs_;
2429 const uint32_t reg_number_;
2430
2431 DISALLOW_COPY_AND_ASSIGN(HPhi);
2432};
2433
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002434// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002435// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002436// exit block.
2437class HExit : public HTemplateInstruction<0> {
2438 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002439 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002440
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002441 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002442
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002443 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002444
2445 private:
2446 DISALLOW_COPY_AND_ASSIGN(HExit);
2447};
2448
2449// Jumps from one block to another.
2450class HGoto : public HTemplateInstruction<0> {
2451 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002452 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002453
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002454 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002455
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002456 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002457 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002458 }
2459
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002460 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002461
2462 private:
2463 DISALLOW_COPY_AND_ASSIGN(HGoto);
2464};
2465
Roland Levillain9867bc72015-08-05 10:21:34 +01002466class HConstant : public HExpression<0> {
2467 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002468 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2469 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002470
2471 bool CanBeMoved() const OVERRIDE { return true; }
2472
Roland Levillain1a653882016-03-18 18:05:57 +00002473 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002474 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002475 // Is this constant 0 in the arithmetic sense?
2476 virtual bool IsArithmeticZero() const { return false; }
2477 // Is this constant a 0-bit pattern?
2478 virtual bool IsZeroBitPattern() const { return false; }
2479 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002480 virtual bool IsOne() const { return false; }
2481
David Brazdil77a48ae2015-09-15 12:34:04 +00002482 virtual uint64_t GetValueAsUint64() const = 0;
2483
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002484 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002485
2486 private:
2487 DISALLOW_COPY_AND_ASSIGN(HConstant);
2488};
2489
2490class HNullConstant : public HConstant {
2491 public:
2492 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2493 return true;
2494 }
2495
David Brazdil77a48ae2015-09-15 12:34:04 +00002496 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2497
Roland Levillain9867bc72015-08-05 10:21:34 +01002498 size_t ComputeHashCode() const OVERRIDE { return 0; }
2499
Roland Levillain1a653882016-03-18 18:05:57 +00002500 // The null constant representation is a 0-bit pattern.
2501 virtual bool IsZeroBitPattern() const { return true; }
2502
Roland Levillain9867bc72015-08-05 10:21:34 +01002503 DECLARE_INSTRUCTION(NullConstant);
2504
2505 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002506 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002507
2508 friend class HGraph;
2509 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2510};
2511
2512// Constants of the type int. Those can be from Dex instructions, or
2513// synthesized (for example with the if-eqz instruction).
2514class HIntConstant : public HConstant {
2515 public:
2516 int32_t GetValue() const { return value_; }
2517
David Brazdil9f389d42015-10-01 14:32:56 +01002518 uint64_t GetValueAsUint64() const OVERRIDE {
2519 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2520 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002521
Roland Levillain9867bc72015-08-05 10:21:34 +01002522 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002523 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002524 return other->AsIntConstant()->value_ == value_;
2525 }
2526
2527 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2528
2529 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002530 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2531 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002532 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2533
Roland Levillain1a653882016-03-18 18:05:57 +00002534 // Integer constants are used to encode Boolean values as well,
2535 // where 1 means true and 0 means false.
2536 bool IsTrue() const { return GetValue() == 1; }
2537 bool IsFalse() const { return GetValue() == 0; }
2538
Roland Levillain9867bc72015-08-05 10:21:34 +01002539 DECLARE_INSTRUCTION(IntConstant);
2540
2541 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002542 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2543 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2544 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2545 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002546
2547 const int32_t value_;
2548
2549 friend class HGraph;
2550 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2551 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2552 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2553};
2554
2555class HLongConstant : public HConstant {
2556 public:
2557 int64_t GetValue() const { return value_; }
2558
David Brazdil77a48ae2015-09-15 12:34:04 +00002559 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2560
Roland Levillain9867bc72015-08-05 10:21:34 +01002561 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002562 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002563 return other->AsLongConstant()->value_ == value_;
2564 }
2565
2566 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2567
2568 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002569 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2570 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002571 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2572
2573 DECLARE_INSTRUCTION(LongConstant);
2574
2575 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002576 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2577 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002578
2579 const int64_t value_;
2580
2581 friend class HGraph;
2582 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2583};
Dave Allison20dfc792014-06-16 20:44:29 -07002584
Roland Levillain31dd3d62016-02-16 12:21:02 +00002585class HFloatConstant : public HConstant {
2586 public:
2587 float GetValue() const { return value_; }
2588
2589 uint64_t GetValueAsUint64() const OVERRIDE {
2590 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2591 }
2592
2593 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2594 DCHECK(other->IsFloatConstant()) << other->DebugName();
2595 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2596 }
2597
2598 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2599
2600 bool IsMinusOne() const OVERRIDE {
2601 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2602 }
Roland Levillain1a653882016-03-18 18:05:57 +00002603 bool IsArithmeticZero() const OVERRIDE {
2604 return std::fpclassify(value_) == FP_ZERO;
2605 }
2606 bool IsArithmeticPositiveZero() const {
2607 return IsArithmeticZero() && !std::signbit(value_);
2608 }
2609 bool IsArithmeticNegativeZero() const {
2610 return IsArithmeticZero() && std::signbit(value_);
2611 }
2612 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002613 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002614 }
2615 bool IsOne() const OVERRIDE {
2616 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2617 }
2618 bool IsNaN() const {
2619 return std::isnan(value_);
2620 }
2621
2622 DECLARE_INSTRUCTION(FloatConstant);
2623
2624 private:
2625 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2626 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2627 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2628 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2629
2630 const float value_;
2631
2632 // Only the SsaBuilder and HGraph can create floating-point constants.
2633 friend class SsaBuilder;
2634 friend class HGraph;
2635 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2636};
2637
2638class HDoubleConstant : public HConstant {
2639 public:
2640 double GetValue() const { return value_; }
2641
2642 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2643
2644 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2645 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2646 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2647 }
2648
2649 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2650
2651 bool IsMinusOne() const OVERRIDE {
2652 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2653 }
Roland Levillain1a653882016-03-18 18:05:57 +00002654 bool IsArithmeticZero() const OVERRIDE {
2655 return std::fpclassify(value_) == FP_ZERO;
2656 }
2657 bool IsArithmeticPositiveZero() const {
2658 return IsArithmeticZero() && !std::signbit(value_);
2659 }
2660 bool IsArithmeticNegativeZero() const {
2661 return IsArithmeticZero() && std::signbit(value_);
2662 }
2663 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002664 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002665 }
2666 bool IsOne() const OVERRIDE {
2667 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2668 }
2669 bool IsNaN() const {
2670 return std::isnan(value_);
2671 }
2672
2673 DECLARE_INSTRUCTION(DoubleConstant);
2674
2675 private:
2676 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2677 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2678 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2679 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2680
2681 const double value_;
2682
2683 // Only the SsaBuilder and HGraph can create floating-point constants.
2684 friend class SsaBuilder;
2685 friend class HGraph;
2686 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2687};
2688
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002689// Conditional branch. A block ending with an HIf instruction must have
2690// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002691class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002692 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002693 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2694 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002695 SetRawInputAt(0, input);
2696 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002697
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002698 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002699
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002700 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002701 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002702 }
2703
2704 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002705 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002706 }
2707
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002708 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002709
2710 private:
2711 DISALLOW_COPY_AND_ASSIGN(HIf);
2712};
2713
David Brazdilfc6a86a2015-06-26 10:33:45 +00002714
2715// Abstract instruction which marks the beginning and/or end of a try block and
2716// links it to the respective exception handlers. Behaves the same as a Goto in
2717// non-exceptional control flow.
2718// Normal-flow successor is stored at index zero, exception handlers under
2719// higher indices in no particular order.
2720class HTryBoundary : public HTemplateInstruction<0> {
2721 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002722 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002723 kEntry,
2724 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002725 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002726 };
2727
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002728 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002729 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2730 SetPackedField<BoundaryKindField>(kind);
2731 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002732
2733 bool IsControlFlow() const OVERRIDE { return true; }
2734
2735 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002736 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002737
David Brazdild26a4112015-11-10 11:07:31 +00002738 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2739 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2740 }
2741
David Brazdilfc6a86a2015-06-26 10:33:45 +00002742 // Returns whether `handler` is among its exception handlers (non-zero index
2743 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002744 bool HasExceptionHandler(const HBasicBlock& handler) const {
2745 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002746 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002747 }
2748
2749 // If not present already, adds `handler` to its block's list of exception
2750 // handlers.
2751 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002752 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002753 GetBlock()->AddSuccessor(handler);
2754 }
2755 }
2756
Vladimir Markoa1de9182016-02-25 11:37:38 +00002757 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2758 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002759
David Brazdilffee3d32015-07-06 11:48:53 +01002760 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2761
David Brazdilfc6a86a2015-06-26 10:33:45 +00002762 DECLARE_INSTRUCTION(TryBoundary);
2763
2764 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002765 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2766 static constexpr size_t kFieldBoundaryKindSize =
2767 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2768 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2769 kFieldBoundaryKind + kFieldBoundaryKindSize;
2770 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2771 "Too many packed fields.");
2772 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002773
2774 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2775};
2776
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002777// Deoptimize to interpreter, upon checking a condition.
2778class HDeoptimize : public HTemplateInstruction<1> {
2779 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002780 // We set CanTriggerGC to prevent any intermediate address to be live
2781 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002782 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002783 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002784 SetRawInputAt(0, cond);
2785 }
2786
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002787 bool CanBeMoved() const OVERRIDE { return true; }
2788 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2789 return true;
2790 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002791 bool NeedsEnvironment() const OVERRIDE { return true; }
2792 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002793
2794 DECLARE_INSTRUCTION(Deoptimize);
2795
2796 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002797 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2798};
2799
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002800// Represents the ArtMethod that was passed as a first argument to
2801// the method. It is used by instructions that depend on it, like
2802// instructions that work with the dex cache.
2803class HCurrentMethod : public HExpression<0> {
2804 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002805 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2806 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002807
2808 DECLARE_INSTRUCTION(CurrentMethod);
2809
2810 private:
2811 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2812};
2813
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002814// Fetches an ArtMethod from the virtual table or the interface method table
2815// of a class.
2816class HClassTableGet : public HExpression<1> {
2817 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002818 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002819 kVTable,
2820 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002821 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002822 };
2823 HClassTableGet(HInstruction* cls,
2824 Primitive::Type type,
2825 TableKind kind,
2826 size_t index,
2827 uint32_t dex_pc)
2828 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002829 index_(index) {
2830 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002831 SetRawInputAt(0, cls);
2832 }
2833
2834 bool CanBeMoved() const OVERRIDE { return true; }
2835 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2836 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002837 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002838 }
2839
Vladimir Markoa1de9182016-02-25 11:37:38 +00002840 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002841 size_t GetIndex() const { return index_; }
2842
2843 DECLARE_INSTRUCTION(ClassTableGet);
2844
2845 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002846 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2847 static constexpr size_t kFieldTableKindSize =
2848 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2849 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2850 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2851 "Too many packed fields.");
2852 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2853
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002854 // The index of the ArtMethod in the table.
2855 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002856
2857 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2858};
2859
Mark Mendellfe57faa2015-09-18 09:26:15 -04002860// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2861// have one successor for each entry in the switch table, and the final successor
2862// will be the block containing the next Dex opcode.
2863class HPackedSwitch : public HTemplateInstruction<1> {
2864 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002865 HPackedSwitch(int32_t start_value,
2866 uint32_t num_entries,
2867 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002868 uint32_t dex_pc = kNoDexPc)
2869 : HTemplateInstruction(SideEffects::None(), dex_pc),
2870 start_value_(start_value),
2871 num_entries_(num_entries) {
2872 SetRawInputAt(0, input);
2873 }
2874
2875 bool IsControlFlow() const OVERRIDE { return true; }
2876
2877 int32_t GetStartValue() const { return start_value_; }
2878
Vladimir Marko211c2112015-09-24 16:52:33 +01002879 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002880
2881 HBasicBlock* GetDefaultBlock() const {
2882 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002883 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002884 }
2885 DECLARE_INSTRUCTION(PackedSwitch);
2886
2887 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002888 const int32_t start_value_;
2889 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002890
2891 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2892};
2893
Roland Levillain88cb1752014-10-20 16:36:47 +01002894class HUnaryOperation : public HExpression<1> {
2895 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002896 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2897 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002898 SetRawInputAt(0, input);
2899 }
2900
2901 HInstruction* GetInput() const { return InputAt(0); }
2902 Primitive::Type GetResultType() const { return GetType(); }
2903
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002904 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002905 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002906 return true;
2907 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002908
Roland Levillain31dd3d62016-02-16 12:21:02 +00002909 // Try to statically evaluate `this` and return a HConstant
2910 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002911 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002912 HConstant* TryStaticEvaluation() const;
2913
2914 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002915 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2916 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002917 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2918 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002919
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002920 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002921
2922 private:
2923 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2924};
2925
Dave Allison20dfc792014-06-16 20:44:29 -07002926class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002927 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002928 HBinaryOperation(Primitive::Type result_type,
2929 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002930 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002931 SideEffects side_effects = SideEffects::None(),
2932 uint32_t dex_pc = kNoDexPc)
2933 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002934 SetRawInputAt(0, left);
2935 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002936 }
2937
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002938 HInstruction* GetLeft() const { return InputAt(0); }
2939 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002940 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002941
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002942 virtual bool IsCommutative() const { return false; }
2943
2944 // Put constant on the right.
2945 // Returns whether order is changed.
2946 bool OrderInputsWithConstantOnTheRight() {
2947 HInstruction* left = InputAt(0);
2948 HInstruction* right = InputAt(1);
2949 if (left->IsConstant() && !right->IsConstant()) {
2950 ReplaceInput(right, 0);
2951 ReplaceInput(left, 1);
2952 return true;
2953 }
2954 return false;
2955 }
2956
2957 // Order inputs by instruction id, but favor constant on the right side.
2958 // This helps GVN for commutative ops.
2959 void OrderInputs() {
2960 DCHECK(IsCommutative());
2961 HInstruction* left = InputAt(0);
2962 HInstruction* right = InputAt(1);
2963 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2964 return;
2965 }
2966 if (OrderInputsWithConstantOnTheRight()) {
2967 return;
2968 }
2969 // Order according to instruction id.
2970 if (left->GetId() > right->GetId()) {
2971 ReplaceInput(right, 0);
2972 ReplaceInput(left, 1);
2973 }
2974 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002975
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002976 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002977 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002978 return true;
2979 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002980
Roland Levillain31dd3d62016-02-16 12:21:02 +00002981 // Try to statically evaluate `this` and return a HConstant
2982 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002983 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002984 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002985
2986 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002987 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2988 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002989 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2990 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002991 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002992 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2993 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002994 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2995 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002996 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2997 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01002998 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002999 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3000 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003001
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003002 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003003 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003004 HConstant* GetConstantRight() const;
3005
3006 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003007 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003008 HInstruction* GetLeastConstantLeft() const;
3009
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003010 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003011
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003012 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003013 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3014};
3015
Mark Mendellc4701932015-04-10 13:18:51 -04003016// The comparison bias applies for floating point operations and indicates how NaN
3017// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003018enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003019 kNoBias, // bias is not applicable (i.e. for long operation)
3020 kGtBias, // return 1 for NaN comparisons
3021 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003022 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003023};
3024
Roland Levillain31dd3d62016-02-16 12:21:02 +00003025std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3026
Dave Allison20dfc792014-06-16 20:44:29 -07003027class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003028 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003029 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003030 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3031 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3032 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003033
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003034 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003035 // `instruction`, and disregard moves in between.
3036 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003037
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003038 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003039
3040 virtual IfCondition GetCondition() const = 0;
3041
Mark Mendellc4701932015-04-10 13:18:51 -04003042 virtual IfCondition GetOppositeCondition() const = 0;
3043
Vladimir Markoa1de9182016-02-25 11:37:38 +00003044 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003045 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3046
Vladimir Markoa1de9182016-02-25 11:37:38 +00003047 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3048 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003049
3050 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003051 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003052 }
3053
Roland Levillain4fa13f62015-07-06 18:11:54 +01003054 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003055 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003056 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003057 if (if_cond == kCondNE) {
3058 return true;
3059 } else if (if_cond == kCondEQ) {
3060 return false;
3061 }
3062 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003063 }
3064
3065 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003066 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003067 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003068 if (if_cond == kCondEQ) {
3069 return true;
3070 } else if (if_cond == kCondNE) {
3071 return false;
3072 }
3073 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003074 }
3075
Roland Levillain31dd3d62016-02-16 12:21:02 +00003076 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003077 // Needed if we merge a HCompare into a HCondition.
3078 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3079 static constexpr size_t kFieldComparisonBiasSize =
3080 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3081 static constexpr size_t kNumberOfConditionPackedBits =
3082 kFieldComparisonBias + kFieldComparisonBiasSize;
3083 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3084 using ComparisonBiasField =
3085 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3086
Roland Levillain31dd3d62016-02-16 12:21:02 +00003087 template <typename T>
3088 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3089
3090 template <typename T>
3091 int32_t CompareFP(T x, T y) const {
3092 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3093 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3094 // Handle the bias.
3095 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3096 }
3097
3098 // Return an integer constant containing the result of a condition evaluated at compile time.
3099 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3100 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3101 }
3102
Dave Allison20dfc792014-06-16 20:44:29 -07003103 private:
3104 DISALLOW_COPY_AND_ASSIGN(HCondition);
3105};
3106
3107// Instruction to check if two inputs are equal to each other.
3108class HEqual : public HCondition {
3109 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003110 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3111 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003112
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003113 bool IsCommutative() const OVERRIDE { return true; }
3114
Vladimir Marko9e23df52015-11-10 17:14:35 +00003115 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3116 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003117 return MakeConstantCondition(true, GetDexPc());
3118 }
3119 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3120 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3121 }
3122 // In the following Evaluate methods, a HCompare instruction has
3123 // been merged into this HEqual instruction; evaluate it as
3124 // `Compare(x, y) == 0`.
3125 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3126 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3127 GetDexPc());
3128 }
3129 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3130 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3131 }
3132 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3133 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003134 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003135
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003136 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003137
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003138 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003139 return kCondEQ;
3140 }
3141
Mark Mendellc4701932015-04-10 13:18:51 -04003142 IfCondition GetOppositeCondition() const OVERRIDE {
3143 return kCondNE;
3144 }
3145
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003146 private:
Aart Bike9f37602015-10-09 11:15:55 -07003147 template <typename T> bool Compute(T x, T y) const { return x == y; }
3148
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003149 DISALLOW_COPY_AND_ASSIGN(HEqual);
3150};
3151
Dave Allison20dfc792014-06-16 20:44:29 -07003152class HNotEqual : public HCondition {
3153 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003154 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3155 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003156
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003157 bool IsCommutative() const OVERRIDE { return true; }
3158
Vladimir Marko9e23df52015-11-10 17:14:35 +00003159 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3160 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003161 return MakeConstantCondition(false, GetDexPc());
3162 }
3163 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3164 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3165 }
3166 // In the following Evaluate methods, a HCompare instruction has
3167 // been merged into this HNotEqual instruction; evaluate it as
3168 // `Compare(x, y) != 0`.
3169 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3170 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3171 }
3172 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3173 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3174 }
3175 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3176 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003177 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003178
Dave Allison20dfc792014-06-16 20:44:29 -07003179 DECLARE_INSTRUCTION(NotEqual);
3180
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003181 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003182 return kCondNE;
3183 }
3184
Mark Mendellc4701932015-04-10 13:18:51 -04003185 IfCondition GetOppositeCondition() const OVERRIDE {
3186 return kCondEQ;
3187 }
3188
Dave Allison20dfc792014-06-16 20:44:29 -07003189 private:
Aart Bike9f37602015-10-09 11:15:55 -07003190 template <typename T> bool Compute(T x, T y) const { return x != y; }
3191
Dave Allison20dfc792014-06-16 20:44:29 -07003192 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3193};
3194
3195class HLessThan : public HCondition {
3196 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003197 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3198 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003199
Roland Levillain9867bc72015-08-05 10:21:34 +01003200 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003201 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003202 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003203 // In the following Evaluate methods, a HCompare instruction has
3204 // been merged into this HLessThan instruction; evaluate it as
3205 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003206 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003207 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3208 }
3209 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3210 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3211 }
3212 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3213 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003214 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003215
Dave Allison20dfc792014-06-16 20:44:29 -07003216 DECLARE_INSTRUCTION(LessThan);
3217
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003218 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003219 return kCondLT;
3220 }
3221
Mark Mendellc4701932015-04-10 13:18:51 -04003222 IfCondition GetOppositeCondition() const OVERRIDE {
3223 return kCondGE;
3224 }
3225
Dave Allison20dfc792014-06-16 20:44:29 -07003226 private:
Aart Bike9f37602015-10-09 11:15:55 -07003227 template <typename T> bool Compute(T x, T y) const { return x < y; }
3228
Dave Allison20dfc792014-06-16 20:44:29 -07003229 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3230};
3231
3232class HLessThanOrEqual : public HCondition {
3233 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003234 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3235 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003236
Roland Levillain9867bc72015-08-05 10:21:34 +01003237 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003238 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003239 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003240 // In the following Evaluate methods, a HCompare instruction has
3241 // been merged into this HLessThanOrEqual instruction; evaluate it as
3242 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003243 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003244 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3245 }
3246 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3247 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3248 }
3249 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3250 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003251 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003252
Dave Allison20dfc792014-06-16 20:44:29 -07003253 DECLARE_INSTRUCTION(LessThanOrEqual);
3254
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003255 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003256 return kCondLE;
3257 }
3258
Mark Mendellc4701932015-04-10 13:18:51 -04003259 IfCondition GetOppositeCondition() const OVERRIDE {
3260 return kCondGT;
3261 }
3262
Dave Allison20dfc792014-06-16 20:44:29 -07003263 private:
Aart Bike9f37602015-10-09 11:15:55 -07003264 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3265
Dave Allison20dfc792014-06-16 20:44:29 -07003266 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3267};
3268
3269class HGreaterThan : public HCondition {
3270 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003271 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3272 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003273
Roland Levillain9867bc72015-08-05 10:21:34 +01003274 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003275 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003276 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003277 // In the following Evaluate methods, a HCompare instruction has
3278 // been merged into this HGreaterThan instruction; evaluate it as
3279 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003280 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003281 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3282 }
3283 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3284 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3285 }
3286 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3287 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003288 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003289
Dave Allison20dfc792014-06-16 20:44:29 -07003290 DECLARE_INSTRUCTION(GreaterThan);
3291
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003292 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003293 return kCondGT;
3294 }
3295
Mark Mendellc4701932015-04-10 13:18:51 -04003296 IfCondition GetOppositeCondition() const OVERRIDE {
3297 return kCondLE;
3298 }
3299
Dave Allison20dfc792014-06-16 20:44:29 -07003300 private:
Aart Bike9f37602015-10-09 11:15:55 -07003301 template <typename T> bool Compute(T x, T y) const { return x > y; }
3302
Dave Allison20dfc792014-06-16 20:44:29 -07003303 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3304};
3305
3306class HGreaterThanOrEqual : public HCondition {
3307 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003308 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3309 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003310
Roland Levillain9867bc72015-08-05 10:21:34 +01003311 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003312 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003313 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003314 // In the following Evaluate methods, a HCompare instruction has
3315 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3316 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003317 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003318 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3319 }
3320 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3321 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3322 }
3323 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3324 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003325 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003326
Dave Allison20dfc792014-06-16 20:44:29 -07003327 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3328
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003329 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003330 return kCondGE;
3331 }
3332
Mark Mendellc4701932015-04-10 13:18:51 -04003333 IfCondition GetOppositeCondition() const OVERRIDE {
3334 return kCondLT;
3335 }
3336
Dave Allison20dfc792014-06-16 20:44:29 -07003337 private:
Aart Bike9f37602015-10-09 11:15:55 -07003338 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3339
Dave Allison20dfc792014-06-16 20:44:29 -07003340 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3341};
3342
Aart Bike9f37602015-10-09 11:15:55 -07003343class HBelow : public HCondition {
3344 public:
3345 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3346 : HCondition(first, second, dex_pc) {}
3347
3348 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003349 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003350 }
3351 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003352 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3353 }
3354 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3355 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3356 LOG(FATAL) << DebugName() << " is not defined for float values";
3357 UNREACHABLE();
3358 }
3359 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3360 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3361 LOG(FATAL) << DebugName() << " is not defined for double values";
3362 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003363 }
3364
3365 DECLARE_INSTRUCTION(Below);
3366
3367 IfCondition GetCondition() const OVERRIDE {
3368 return kCondB;
3369 }
3370
3371 IfCondition GetOppositeCondition() const OVERRIDE {
3372 return kCondAE;
3373 }
3374
3375 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003376 template <typename T> bool Compute(T x, T y) const {
3377 return MakeUnsigned(x) < MakeUnsigned(y);
3378 }
Aart Bike9f37602015-10-09 11:15:55 -07003379
3380 DISALLOW_COPY_AND_ASSIGN(HBelow);
3381};
3382
3383class HBelowOrEqual : public HCondition {
3384 public:
3385 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3386 : HCondition(first, second, dex_pc) {}
3387
3388 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003389 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003390 }
3391 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003392 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3393 }
3394 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3395 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3396 LOG(FATAL) << DebugName() << " is not defined for float values";
3397 UNREACHABLE();
3398 }
3399 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3400 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3401 LOG(FATAL) << DebugName() << " is not defined for double values";
3402 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003403 }
3404
3405 DECLARE_INSTRUCTION(BelowOrEqual);
3406
3407 IfCondition GetCondition() const OVERRIDE {
3408 return kCondBE;
3409 }
3410
3411 IfCondition GetOppositeCondition() const OVERRIDE {
3412 return kCondA;
3413 }
3414
3415 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003416 template <typename T> bool Compute(T x, T y) const {
3417 return MakeUnsigned(x) <= MakeUnsigned(y);
3418 }
Aart Bike9f37602015-10-09 11:15:55 -07003419
3420 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3421};
3422
3423class HAbove : public HCondition {
3424 public:
3425 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3426 : HCondition(first, second, dex_pc) {}
3427
3428 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003429 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003430 }
3431 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003432 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3433 }
3434 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3435 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3436 LOG(FATAL) << DebugName() << " is not defined for float values";
3437 UNREACHABLE();
3438 }
3439 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3440 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3441 LOG(FATAL) << DebugName() << " is not defined for double values";
3442 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003443 }
3444
3445 DECLARE_INSTRUCTION(Above);
3446
3447 IfCondition GetCondition() const OVERRIDE {
3448 return kCondA;
3449 }
3450
3451 IfCondition GetOppositeCondition() const OVERRIDE {
3452 return kCondBE;
3453 }
3454
3455 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003456 template <typename T> bool Compute(T x, T y) const {
3457 return MakeUnsigned(x) > MakeUnsigned(y);
3458 }
Aart Bike9f37602015-10-09 11:15:55 -07003459
3460 DISALLOW_COPY_AND_ASSIGN(HAbove);
3461};
3462
3463class HAboveOrEqual : public HCondition {
3464 public:
3465 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3466 : HCondition(first, second, dex_pc) {}
3467
3468 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003469 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003470 }
3471 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003472 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3473 }
3474 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3475 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3476 LOG(FATAL) << DebugName() << " is not defined for float values";
3477 UNREACHABLE();
3478 }
3479 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3480 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3481 LOG(FATAL) << DebugName() << " is not defined for double values";
3482 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003483 }
3484
3485 DECLARE_INSTRUCTION(AboveOrEqual);
3486
3487 IfCondition GetCondition() const OVERRIDE {
3488 return kCondAE;
3489 }
3490
3491 IfCondition GetOppositeCondition() const OVERRIDE {
3492 return kCondB;
3493 }
3494
3495 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003496 template <typename T> bool Compute(T x, T y) const {
3497 return MakeUnsigned(x) >= MakeUnsigned(y);
3498 }
Aart Bike9f37602015-10-09 11:15:55 -07003499
3500 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3501};
Dave Allison20dfc792014-06-16 20:44:29 -07003502
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003503// Instruction to check how two inputs compare to each other.
3504// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3505class HCompare : public HBinaryOperation {
3506 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003507 // Note that `comparison_type` is the type of comparison performed
3508 // between the comparison's inputs, not the type of the instantiated
3509 // HCompare instruction (which is always Primitive::kPrimInt).
3510 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003511 HInstruction* first,
3512 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003513 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003514 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003515 : HBinaryOperation(Primitive::kPrimInt,
3516 first,
3517 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003518 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003519 dex_pc) {
3520 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003521 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3522 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003523 }
3524
Roland Levillain9867bc72015-08-05 10:21:34 +01003525 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003526 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3527
3528 template <typename T>
3529 int32_t ComputeFP(T x, T y) const {
3530 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3531 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3532 // Handle the bias.
3533 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3534 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003535
Roland Levillain9867bc72015-08-05 10:21:34 +01003536 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003537 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3538 // reach this code path when processing a freshly built HIR
3539 // graph. However HCompare integer instructions can be synthesized
3540 // by the instruction simplifier to implement IntegerCompare and
3541 // IntegerSignum intrinsics, so we have to handle this case.
3542 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003543 }
3544 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003545 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3546 }
3547 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3548 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3549 }
3550 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3551 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003552 }
3553
Calin Juravleddb7df22014-11-25 20:56:51 +00003554 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003555 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003556 }
3557
Vladimir Markoa1de9182016-02-25 11:37:38 +00003558 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003559
Roland Levillain31dd3d62016-02-16 12:21:02 +00003560 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003561 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003562 bool IsGtBias() const {
3563 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003564 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003565 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003566
Roland Levillain1693a1f2016-03-15 14:57:31 +00003567 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3568 // Comparisons do not require a runtime call in any back end.
3569 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003570 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003571
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003572 DECLARE_INSTRUCTION(Compare);
3573
Roland Levillain31dd3d62016-02-16 12:21:02 +00003574 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003575 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3576 static constexpr size_t kFieldComparisonBiasSize =
3577 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3578 static constexpr size_t kNumberOfComparePackedBits =
3579 kFieldComparisonBias + kFieldComparisonBiasSize;
3580 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3581 using ComparisonBiasField =
3582 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3583
Roland Levillain31dd3d62016-02-16 12:21:02 +00003584 // Return an integer constant containing the result of a comparison evaluated at compile time.
3585 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3586 DCHECK(value == -1 || value == 0 || value == 1) << value;
3587 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3588 }
3589
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003590 private:
3591 DISALLOW_COPY_AND_ASSIGN(HCompare);
3592};
3593
David Brazdil6de19382016-01-08 17:37:10 +00003594class HNewInstance : public HExpression<2> {
3595 public:
3596 HNewInstance(HInstruction* cls,
3597 HCurrentMethod* current_method,
3598 uint32_t dex_pc,
3599 uint16_t type_index,
3600 const DexFile& dex_file,
3601 bool can_throw,
3602 bool finalizable,
3603 QuickEntrypointEnum entrypoint)
3604 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3605 type_index_(type_index),
3606 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003607 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003608 SetPackedFlag<kFlagCanThrow>(can_throw);
3609 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003610 SetRawInputAt(0, cls);
3611 SetRawInputAt(1, current_method);
3612 }
3613
3614 uint16_t GetTypeIndex() const { return type_index_; }
3615 const DexFile& GetDexFile() const { return dex_file_; }
3616
3617 // Calls runtime so needs an environment.
3618 bool NeedsEnvironment() const OVERRIDE { return true; }
3619
3620 // It may throw when called on type that's not instantiable/accessible.
3621 // It can throw OOME.
3622 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003623 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>() || true; }
David Brazdil6de19382016-01-08 17:37:10 +00003624
Vladimir Markoa1de9182016-02-25 11:37:38 +00003625 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003626
3627 bool CanBeNull() const OVERRIDE { return false; }
3628
3629 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3630
3631 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3632 entrypoint_ = entrypoint;
3633 }
3634
3635 bool IsStringAlloc() const;
3636
3637 DECLARE_INSTRUCTION(NewInstance);
3638
3639 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003640 static constexpr size_t kFlagCanThrow = kNumberOfExpressionPackedBits;
3641 static constexpr size_t kFlagFinalizable = kFlagCanThrow + 1;
3642 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3643 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3644 "Too many packed fields.");
3645
David Brazdil6de19382016-01-08 17:37:10 +00003646 const uint16_t type_index_;
3647 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003648 QuickEntrypointEnum entrypoint_;
3649
3650 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3651};
3652
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003653enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003654#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3655 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003656#include "intrinsics_list.h"
3657 kNone,
3658 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3659#undef INTRINSICS_LIST
3660#undef OPTIMIZING_INTRINSICS
3661};
3662std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3663
Agi Csaki05f20562015-08-19 14:58:14 -07003664enum IntrinsicNeedsEnvironmentOrCache {
3665 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3666 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003667};
3668
Aart Bik5d75afe2015-12-14 11:57:01 -08003669enum IntrinsicSideEffects {
3670 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3671 kReadSideEffects, // Intrinsic may read heap memory.
3672 kWriteSideEffects, // Intrinsic may write heap memory.
3673 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3674};
3675
3676enum IntrinsicExceptions {
3677 kNoThrow, // Intrinsic does not throw any exceptions.
3678 kCanThrow // Intrinsic may throw exceptions.
3679};
3680
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003681class HInvoke : public HInstruction {
3682 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003683 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003684
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003685 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003686
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003687 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003688 SetRawInputAt(index, argument);
3689 }
3690
Roland Levillain3e3d7332015-04-28 11:00:54 +01003691 // Return the number of arguments. This number can be lower than
3692 // the number of inputs returned by InputCount(), as some invoke
3693 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3694 // inputs at the end of their list of inputs.
3695 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3696
Vladimir Markoa1de9182016-02-25 11:37:38 +00003697 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003698
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003699 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003700 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003701
Vladimir Markoa1de9182016-02-25 11:37:38 +00003702 InvokeType GetOriginalInvokeType() const {
3703 return GetPackedField<OriginalInvokeTypeField>();
3704 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003705
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003706 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003707 return intrinsic_;
3708 }
3709
Aart Bik5d75afe2015-12-14 11:57:01 -08003710 void SetIntrinsic(Intrinsics intrinsic,
3711 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3712 IntrinsicSideEffects side_effects,
3713 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003714
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003715 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003716 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003717 }
3718
Vladimir Markoa1de9182016-02-25 11:37:38 +00003719 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003720
3721 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3722
3723 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3724 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3725 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003726
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003727 uint32_t* GetIntrinsicOptimizations() {
3728 return &intrinsic_optimizations_;
3729 }
3730
3731 const uint32_t* GetIntrinsicOptimizations() const {
3732 return &intrinsic_optimizations_;
3733 }
3734
3735 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3736
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003737 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003738
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003739 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003740 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3741 static constexpr size_t kFieldOriginalInvokeTypeSize =
3742 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3743 static constexpr size_t kFieldReturnType =
3744 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3745 static constexpr size_t kFieldReturnTypeSize =
3746 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3747 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3748 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3749 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3750 using OriginalInvokeTypeField =
3751 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3752 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3753
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003754 HInvoke(ArenaAllocator* arena,
3755 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003756 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003757 Primitive::Type return_type,
3758 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003759 uint32_t dex_method_index,
3760 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003761 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003762 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003763 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003764 inputs_(number_of_arguments + number_of_other_inputs,
3765 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003766 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003767 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003768 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003769 SetPackedField<ReturnTypeField>(return_type);
3770 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3771 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003772 }
3773
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003774 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003775 return inputs_[index];
3776 }
3777
David Brazdil1abb4192015-02-17 18:33:36 +00003778 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003779 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003780 }
3781
Vladimir Markoa1de9182016-02-25 11:37:38 +00003782 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003783
Roland Levillain3e3d7332015-04-28 11:00:54 +01003784 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003785 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003786 const uint32_t dex_method_index_;
3787 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003788
3789 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3790 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003791
3792 private:
3793 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3794};
3795
Calin Juravle175dc732015-08-25 15:42:32 +01003796class HInvokeUnresolved : public HInvoke {
3797 public:
3798 HInvokeUnresolved(ArenaAllocator* arena,
3799 uint32_t number_of_arguments,
3800 Primitive::Type return_type,
3801 uint32_t dex_pc,
3802 uint32_t dex_method_index,
3803 InvokeType invoke_type)
3804 : HInvoke(arena,
3805 number_of_arguments,
3806 0u /* number_of_other_inputs */,
3807 return_type,
3808 dex_pc,
3809 dex_method_index,
3810 invoke_type) {
3811 }
3812
3813 DECLARE_INSTRUCTION(InvokeUnresolved);
3814
3815 private:
3816 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3817};
3818
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003819class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003820 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003821 // Requirements of this method call regarding the class
3822 // initialization (clinit) check of its declaring class.
3823 enum class ClinitCheckRequirement {
3824 kNone, // Class already initialized.
3825 kExplicit, // Static call having explicit clinit check as last input.
3826 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003827 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003828 };
3829
Vladimir Marko58155012015-08-19 12:49:41 +00003830 // Determines how to load the target ArtMethod*.
3831 enum class MethodLoadKind {
3832 // Use a String init ArtMethod* loaded from Thread entrypoints.
3833 kStringInit,
3834
3835 // Use the method's own ArtMethod* loaded by the register allocator.
3836 kRecursive,
3837
3838 // Use ArtMethod* at a known address, embed the direct address in the code.
3839 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3840 kDirectAddress,
3841
3842 // Use ArtMethod* at an address that will be known at link time, embed the direct
3843 // address in the code. If the image is relocatable, emit .patch_oat entry.
3844 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3845 // the image relocatable or not.
3846 kDirectAddressWithFixup,
3847
Vladimir Markoa1de9182016-02-25 11:37:38 +00003848 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003849 // Used when we need to use the dex cache, for example for invoke-static that
3850 // may cause class initialization (the entry may point to a resolution method),
3851 // and we know that we can access the dex cache arrays using a PC-relative load.
3852 kDexCachePcRelative,
3853
3854 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3855 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3856 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3857 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3858 kDexCacheViaMethod,
3859 };
3860
3861 // Determines the location of the code pointer.
3862 enum class CodePtrLocation {
3863 // Recursive call, use local PC-relative call instruction.
3864 kCallSelf,
3865
3866 // Use PC-relative call instruction patched at link time.
3867 // Used for calls within an oat file, boot->boot or app->app.
3868 kCallPCRelative,
3869
3870 // Call to a known target address, embed the direct address in code.
3871 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3872 kCallDirect,
3873
3874 // Call to a target address that will be known at link time, embed the direct
3875 // address in code. If the image is relocatable, emit .patch_oat entry.
3876 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3877 // the image relocatable or not.
3878 kCallDirectWithFixup,
3879
3880 // Use code pointer from the ArtMethod*.
3881 // Used when we don't know the target code. This is also the last-resort-kind used when
3882 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3883 kCallArtMethod,
3884 };
3885
3886 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003887 MethodLoadKind method_load_kind;
3888 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003889 // The method load data holds
3890 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3891 // Note that there are multiple string init methods, each having its own offset.
3892 // - the method address for kDirectAddress
3893 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003894 uint64_t method_load_data;
3895 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003896 };
3897
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003898 HInvokeStaticOrDirect(ArenaAllocator* arena,
3899 uint32_t number_of_arguments,
3900 Primitive::Type return_type,
3901 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003902 uint32_t method_index,
3903 MethodReference target_method,
3904 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003905 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003906 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003907 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003908 : HInvoke(arena,
3909 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003910 // There is potentially one extra argument for the HCurrentMethod node, and
3911 // potentially one other if the clinit check is explicit, and potentially
3912 // one other if the method is a string factory.
3913 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003914 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003915 return_type,
3916 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003917 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003918 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003919 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003920 dispatch_info_(dispatch_info) {
3921 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3922 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3923 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003924
Vladimir Markodc151b22015-10-15 18:02:30 +01003925 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003926 bool had_current_method_input = HasCurrentMethodInput();
3927 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3928
3929 // Using the current method is the default and once we find a better
3930 // method load kind, we should not go back to using the current method.
3931 DCHECK(had_current_method_input || !needs_current_method_input);
3932
3933 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003934 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3935 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003936 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003937 dispatch_info_ = dispatch_info;
3938 }
3939
Vladimir Markoc53c0792015-11-19 15:48:33 +00003940 void AddSpecialInput(HInstruction* input) {
3941 // We allow only one special input.
3942 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3943 DCHECK(InputCount() == GetSpecialInputIndex() ||
3944 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3945 InsertInputAt(GetSpecialInputIndex(), input);
3946 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003947
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003948 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003949 // We access the method via the dex cache so we can't do an implicit null check.
3950 // TODO: for intrinsics we can generate implicit null checks.
3951 return false;
3952 }
3953
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003954 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003955 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003956 }
3957
Vladimir Markoc53c0792015-11-19 15:48:33 +00003958 // Get the index of the special input, if any.
3959 //
David Brazdil6de19382016-01-08 17:37:10 +00003960 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3961 // method pointer; otherwise there may be one platform-specific special input,
3962 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003963 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003964 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003965
Vladimir Markoa1de9182016-02-25 11:37:38 +00003966 InvokeType GetOptimizedInvokeType() const {
3967 return GetPackedField<OptimizedInvokeTypeField>();
3968 }
3969
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003970 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003971 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003972 }
3973
Vladimir Marko58155012015-08-19 12:49:41 +00003974 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3975 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3976 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003977 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003978 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003979 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003980 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003981 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3982 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003983 bool HasCurrentMethodInput() const {
3984 // This function can be called only after the invoke has been fully initialized by the builder.
3985 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003986 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003987 return true;
3988 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003989 DCHECK(InputCount() == GetSpecialInputIndex() ||
3990 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003991 return false;
3992 }
3993 }
Vladimir Marko58155012015-08-19 12:49:41 +00003994 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3995 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003996 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003997
3998 int32_t GetStringInitOffset() const {
3999 DCHECK(IsStringInit());
4000 return dispatch_info_.method_load_data;
4001 }
4002
4003 uint64_t GetMethodAddress() const {
4004 DCHECK(HasMethodAddress());
4005 return dispatch_info_.method_load_data;
4006 }
4007
4008 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004009 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004010 return dispatch_info_.method_load_data;
4011 }
4012
4013 uint64_t GetDirectCodePtr() const {
4014 DCHECK(HasDirectCodePtr());
4015 return dispatch_info_.direct_code_ptr;
4016 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004017
Vladimir Markoa1de9182016-02-25 11:37:38 +00004018 ClinitCheckRequirement GetClinitCheckRequirement() const {
4019 return GetPackedField<ClinitCheckRequirementField>();
4020 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004021
Roland Levillain4c0eb422015-04-24 16:43:49 +01004022 // Is this instruction a call to a static method?
4023 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004024 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004025 }
4026
Vladimir Markofbb184a2015-11-13 14:47:00 +00004027 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4028 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4029 // instruction; only relevant for static calls with explicit clinit check.
4030 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004031 DCHECK(IsStaticWithExplicitClinitCheck());
4032 size_t last_input_index = InputCount() - 1;
4033 HInstruction* last_input = InputAt(last_input_index);
4034 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004035 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004036 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004037 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004038 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004039 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004040 }
4041
4042 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004043 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004044 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004045 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004046 }
4047
4048 // Is this a call to a static method whose declaring class has an
4049 // implicit intialization check requirement?
4050 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004051 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004052 }
4053
Vladimir Markob554b5a2015-11-06 12:57:55 +00004054 // Does this method load kind need the current method as an input?
4055 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4056 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4057 }
4058
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004059 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004060
Roland Levillain4c0eb422015-04-24 16:43:49 +01004061 protected:
4062 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
4063 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
4064 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
4065 HInstruction* input = input_record.GetInstruction();
4066 // `input` is the last input of a static invoke marked as having
4067 // an explicit clinit check. It must either be:
4068 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4069 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4070 DCHECK(input != nullptr);
4071 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
4072 }
4073 return input_record;
4074 }
4075
Vladimir Markoc53c0792015-11-19 15:48:33 +00004076 void InsertInputAt(size_t index, HInstruction* input);
4077 void RemoveInputAt(size_t index);
4078
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004079 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004080 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4081 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4082 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4083 static constexpr size_t kFieldClinitCheckRequirement =
4084 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4085 static constexpr size_t kFieldClinitCheckRequirementSize =
4086 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4087 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4088 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4089 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4090 "Too many packed fields.");
4091 using OptimizedInvokeTypeField =
4092 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4093 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4094 kFieldClinitCheckRequirement,
4095 kFieldClinitCheckRequirementSize>;
4096
Vladimir Marko58155012015-08-19 12:49:41 +00004097 // The target method may refer to different dex file or method index than the original
4098 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4099 // in derived class to increase visibility.
4100 MethodReference target_method_;
4101 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004102
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004103 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004104};
Vladimir Markof64242a2015-12-01 14:58:23 +00004105std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004106std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004107
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004108class HInvokeVirtual : public HInvoke {
4109 public:
4110 HInvokeVirtual(ArenaAllocator* arena,
4111 uint32_t number_of_arguments,
4112 Primitive::Type return_type,
4113 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004114 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004115 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004116 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004117 vtable_index_(vtable_index) {}
4118
Calin Juravle641547a2015-04-21 22:08:51 +01004119 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004120 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004121 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004122 }
4123
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004124 uint32_t GetVTableIndex() const { return vtable_index_; }
4125
4126 DECLARE_INSTRUCTION(InvokeVirtual);
4127
4128 private:
4129 const uint32_t vtable_index_;
4130
4131 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4132};
4133
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004134class HInvokeInterface : public HInvoke {
4135 public:
4136 HInvokeInterface(ArenaAllocator* arena,
4137 uint32_t number_of_arguments,
4138 Primitive::Type return_type,
4139 uint32_t dex_pc,
4140 uint32_t dex_method_index,
4141 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004142 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004143 imt_index_(imt_index) {}
4144
Calin Juravle641547a2015-04-21 22:08:51 +01004145 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004146 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004147 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004148 }
4149
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004150 uint32_t GetImtIndex() const { return imt_index_; }
4151 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4152
4153 DECLARE_INSTRUCTION(InvokeInterface);
4154
4155 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004156 const uint32_t imt_index_;
4157
4158 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4159};
4160
Roland Levillain88cb1752014-10-20 16:36:47 +01004161class HNeg : public HUnaryOperation {
4162 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004163 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004164 : HUnaryOperation(result_type, input, dex_pc) {
4165 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4166 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004167
Roland Levillain9867bc72015-08-05 10:21:34 +01004168 template <typename T> T Compute(T x) const { return -x; }
4169
4170 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004171 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004172 }
4173 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004174 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004175 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004176 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4177 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4178 }
4179 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4180 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4181 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004182
Roland Levillain88cb1752014-10-20 16:36:47 +01004183 DECLARE_INSTRUCTION(Neg);
4184
4185 private:
4186 DISALLOW_COPY_AND_ASSIGN(HNeg);
4187};
4188
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004189class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004190 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004191 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004192 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004193 uint32_t dex_pc,
4194 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004195 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004196 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004197 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004198 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004199 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004200 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004201 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004202 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004203 }
4204
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004205 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004206 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004207
4208 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004209 bool NeedsEnvironment() const OVERRIDE { return true; }
4210
Mingyao Yang0c365e62015-03-31 15:09:29 -07004211 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4212 bool CanThrow() const OVERRIDE { return true; }
4213
Calin Juravle10e244f2015-01-26 18:54:32 +00004214 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004215
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004216 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4217
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004218 DECLARE_INSTRUCTION(NewArray);
4219
4220 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004221 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004222 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004223 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004224
4225 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4226};
4227
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004228class HAdd : public HBinaryOperation {
4229 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004230 HAdd(Primitive::Type result_type,
4231 HInstruction* left,
4232 HInstruction* right,
4233 uint32_t dex_pc = kNoDexPc)
4234 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004235
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004236 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004237
Roland Levillain9867bc72015-08-05 10:21:34 +01004238 template <typename T> T Compute(T x, T y) const { return x + y; }
4239
4240 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004241 return GetBlock()->GetGraph()->GetIntConstant(
4242 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004243 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004244 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004245 return GetBlock()->GetGraph()->GetLongConstant(
4246 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004247 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004248 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4249 return GetBlock()->GetGraph()->GetFloatConstant(
4250 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4251 }
4252 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4253 return GetBlock()->GetGraph()->GetDoubleConstant(
4254 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4255 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004256
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004257 DECLARE_INSTRUCTION(Add);
4258
4259 private:
4260 DISALLOW_COPY_AND_ASSIGN(HAdd);
4261};
4262
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004263class HSub : public HBinaryOperation {
4264 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004265 HSub(Primitive::Type result_type,
4266 HInstruction* left,
4267 HInstruction* right,
4268 uint32_t dex_pc = kNoDexPc)
4269 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004270
Roland Levillain9867bc72015-08-05 10:21:34 +01004271 template <typename T> T Compute(T x, T y) const { return x - y; }
4272
4273 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004274 return GetBlock()->GetGraph()->GetIntConstant(
4275 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004276 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004277 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004278 return GetBlock()->GetGraph()->GetLongConstant(
4279 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004280 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004281 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4282 return GetBlock()->GetGraph()->GetFloatConstant(
4283 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4284 }
4285 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4286 return GetBlock()->GetGraph()->GetDoubleConstant(
4287 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4288 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004289
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004290 DECLARE_INSTRUCTION(Sub);
4291
4292 private:
4293 DISALLOW_COPY_AND_ASSIGN(HSub);
4294};
4295
Calin Juravle34bacdf2014-10-07 20:23:36 +01004296class HMul : public HBinaryOperation {
4297 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004298 HMul(Primitive::Type result_type,
4299 HInstruction* left,
4300 HInstruction* right,
4301 uint32_t dex_pc = kNoDexPc)
4302 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004303
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004304 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004305
Roland Levillain9867bc72015-08-05 10:21:34 +01004306 template <typename T> T Compute(T x, T y) const { return x * y; }
4307
4308 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004309 return GetBlock()->GetGraph()->GetIntConstant(
4310 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004311 }
4312 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004313 return GetBlock()->GetGraph()->GetLongConstant(
4314 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004315 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004316 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4317 return GetBlock()->GetGraph()->GetFloatConstant(
4318 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4319 }
4320 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4321 return GetBlock()->GetGraph()->GetDoubleConstant(
4322 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4323 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004324
4325 DECLARE_INSTRUCTION(Mul);
4326
4327 private:
4328 DISALLOW_COPY_AND_ASSIGN(HMul);
4329};
4330
Calin Juravle7c4954d2014-10-28 16:57:40 +00004331class HDiv : public HBinaryOperation {
4332 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004333 HDiv(Primitive::Type result_type,
4334 HInstruction* left,
4335 HInstruction* right,
4336 uint32_t dex_pc)
4337 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004338
Roland Levillain9867bc72015-08-05 10:21:34 +01004339 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004340 T ComputeIntegral(T x, T y) const {
4341 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004342 // Our graph structure ensures we never have 0 for `y` during
4343 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004344 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004345 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004346 return (y == -1) ? -x : x / y;
4347 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004348
Roland Levillain31dd3d62016-02-16 12:21:02 +00004349 template <typename T>
4350 T ComputeFP(T x, T y) const {
4351 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4352 return x / y;
4353 }
4354
Roland Levillain9867bc72015-08-05 10:21:34 +01004355 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004356 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004357 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004358 }
4359 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004360 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004361 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4362 }
4363 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4364 return GetBlock()->GetGraph()->GetFloatConstant(
4365 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4366 }
4367 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4368 return GetBlock()->GetGraph()->GetDoubleConstant(
4369 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004370 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004371
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004372 static SideEffects SideEffectsForArchRuntimeCalls() {
4373 // The generated code can use a runtime call.
4374 return SideEffects::CanTriggerGC();
4375 }
4376
Calin Juravle7c4954d2014-10-28 16:57:40 +00004377 DECLARE_INSTRUCTION(Div);
4378
4379 private:
4380 DISALLOW_COPY_AND_ASSIGN(HDiv);
4381};
4382
Calin Juravlebacfec32014-11-14 15:54:36 +00004383class HRem : public HBinaryOperation {
4384 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004385 HRem(Primitive::Type result_type,
4386 HInstruction* left,
4387 HInstruction* right,
4388 uint32_t dex_pc)
4389 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004390
Roland Levillain9867bc72015-08-05 10:21:34 +01004391 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004392 T ComputeIntegral(T x, T y) const {
4393 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004394 // Our graph structure ensures we never have 0 for `y` during
4395 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004396 DCHECK_NE(y, 0);
4397 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4398 return (y == -1) ? 0 : x % y;
4399 }
4400
Roland Levillain31dd3d62016-02-16 12:21:02 +00004401 template <typename T>
4402 T ComputeFP(T x, T y) const {
4403 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4404 return std::fmod(x, y);
4405 }
4406
Roland Levillain9867bc72015-08-05 10:21:34 +01004407 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004408 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004409 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004410 }
4411 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004412 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004413 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004414 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004415 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4416 return GetBlock()->GetGraph()->GetFloatConstant(
4417 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4418 }
4419 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4420 return GetBlock()->GetGraph()->GetDoubleConstant(
4421 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4422 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004423
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004424 static SideEffects SideEffectsForArchRuntimeCalls() {
4425 return SideEffects::CanTriggerGC();
4426 }
4427
Calin Juravlebacfec32014-11-14 15:54:36 +00004428 DECLARE_INSTRUCTION(Rem);
4429
4430 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004431 DISALLOW_COPY_AND_ASSIGN(HRem);
4432};
4433
Calin Juravled0d48522014-11-04 16:40:20 +00004434class HDivZeroCheck : public HExpression<1> {
4435 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004436 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4437 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004438 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004439 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004440 SetRawInputAt(0, value);
4441 }
4442
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004443 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4444
Calin Juravled0d48522014-11-04 16:40:20 +00004445 bool CanBeMoved() const OVERRIDE { return true; }
4446
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004447 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004448 return true;
4449 }
4450
4451 bool NeedsEnvironment() const OVERRIDE { return true; }
4452 bool CanThrow() const OVERRIDE { return true; }
4453
Calin Juravled0d48522014-11-04 16:40:20 +00004454 DECLARE_INSTRUCTION(DivZeroCheck);
4455
4456 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004457 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4458};
4459
Calin Juravle9aec02f2014-11-18 23:06:35 +00004460class HShl : public HBinaryOperation {
4461 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004462 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004463 HInstruction* value,
4464 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004465 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004466 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4467 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4468 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004469 }
4470
Roland Levillain5b5b9312016-03-22 14:57:31 +00004471 template <typename T>
4472 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4473 return value << (distance & max_shift_distance);
4474 }
4475
4476 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004477 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004478 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004479 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004480 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004481 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004482 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004483 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004484 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4485 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4486 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4487 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004488 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004489 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4490 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004491 LOG(FATAL) << DebugName() << " is not defined for float values";
4492 UNREACHABLE();
4493 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004494 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4495 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004496 LOG(FATAL) << DebugName() << " is not defined for double values";
4497 UNREACHABLE();
4498 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004499
4500 DECLARE_INSTRUCTION(Shl);
4501
4502 private:
4503 DISALLOW_COPY_AND_ASSIGN(HShl);
4504};
4505
4506class HShr : public HBinaryOperation {
4507 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004508 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004509 HInstruction* value,
4510 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004511 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004512 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4513 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4514 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004515 }
4516
Roland Levillain5b5b9312016-03-22 14:57:31 +00004517 template <typename T>
4518 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4519 return value >> (distance & max_shift_distance);
4520 }
4521
4522 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004523 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004524 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004525 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004526 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004527 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004528 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004529 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004530 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4531 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4532 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4533 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004534 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004535 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4536 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004537 LOG(FATAL) << DebugName() << " is not defined for float values";
4538 UNREACHABLE();
4539 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004540 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4541 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004542 LOG(FATAL) << DebugName() << " is not defined for double values";
4543 UNREACHABLE();
4544 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004545
4546 DECLARE_INSTRUCTION(Shr);
4547
4548 private:
4549 DISALLOW_COPY_AND_ASSIGN(HShr);
4550};
4551
4552class HUShr : public HBinaryOperation {
4553 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004554 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004555 HInstruction* value,
4556 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004557 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004558 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4559 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4560 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004561 }
4562
Roland Levillain5b5b9312016-03-22 14:57:31 +00004563 template <typename T>
4564 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4565 typedef typename std::make_unsigned<T>::type V;
4566 V ux = static_cast<V>(value);
4567 return static_cast<T>(ux >> (distance & max_shift_distance));
4568 }
4569
4570 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004571 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004572 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004573 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004574 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004575 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004576 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004577 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004578 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4579 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4580 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4581 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004582 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004583 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4584 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004585 LOG(FATAL) << DebugName() << " is not defined for float values";
4586 UNREACHABLE();
4587 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004588 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4589 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004590 LOG(FATAL) << DebugName() << " is not defined for double values";
4591 UNREACHABLE();
4592 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004593
4594 DECLARE_INSTRUCTION(UShr);
4595
4596 private:
4597 DISALLOW_COPY_AND_ASSIGN(HUShr);
4598};
4599
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004600class HAnd : public HBinaryOperation {
4601 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004602 HAnd(Primitive::Type result_type,
4603 HInstruction* left,
4604 HInstruction* right,
4605 uint32_t dex_pc = kNoDexPc)
4606 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004607
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004608 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004609
Roland Levillaine53bd812016-02-24 14:54:18 +00004610 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004611
4612 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004613 return GetBlock()->GetGraph()->GetIntConstant(
4614 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004615 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004616 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004617 return GetBlock()->GetGraph()->GetLongConstant(
4618 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004619 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004620 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4621 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4622 LOG(FATAL) << DebugName() << " is not defined for float values";
4623 UNREACHABLE();
4624 }
4625 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4626 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4627 LOG(FATAL) << DebugName() << " is not defined for double values";
4628 UNREACHABLE();
4629 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004630
4631 DECLARE_INSTRUCTION(And);
4632
4633 private:
4634 DISALLOW_COPY_AND_ASSIGN(HAnd);
4635};
4636
4637class HOr : public HBinaryOperation {
4638 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004639 HOr(Primitive::Type result_type,
4640 HInstruction* left,
4641 HInstruction* right,
4642 uint32_t dex_pc = kNoDexPc)
4643 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004644
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004645 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004646
Roland Levillaine53bd812016-02-24 14:54:18 +00004647 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004648
4649 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004650 return GetBlock()->GetGraph()->GetIntConstant(
4651 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004652 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004653 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004654 return GetBlock()->GetGraph()->GetLongConstant(
4655 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004656 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004657 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4658 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4659 LOG(FATAL) << DebugName() << " is not defined for float values";
4660 UNREACHABLE();
4661 }
4662 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4663 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4664 LOG(FATAL) << DebugName() << " is not defined for double values";
4665 UNREACHABLE();
4666 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004667
4668 DECLARE_INSTRUCTION(Or);
4669
4670 private:
4671 DISALLOW_COPY_AND_ASSIGN(HOr);
4672};
4673
4674class HXor : public HBinaryOperation {
4675 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004676 HXor(Primitive::Type result_type,
4677 HInstruction* left,
4678 HInstruction* right,
4679 uint32_t dex_pc = kNoDexPc)
4680 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004681
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004682 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004683
Roland Levillaine53bd812016-02-24 14:54:18 +00004684 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004685
4686 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004687 return GetBlock()->GetGraph()->GetIntConstant(
4688 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004689 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004690 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004691 return GetBlock()->GetGraph()->GetLongConstant(
4692 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004693 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004694 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4695 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4696 LOG(FATAL) << DebugName() << " is not defined for float values";
4697 UNREACHABLE();
4698 }
4699 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4700 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4701 LOG(FATAL) << DebugName() << " is not defined for double values";
4702 UNREACHABLE();
4703 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004704
4705 DECLARE_INSTRUCTION(Xor);
4706
4707 private:
4708 DISALLOW_COPY_AND_ASSIGN(HXor);
4709};
4710
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004711class HRor : public HBinaryOperation {
4712 public:
4713 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004714 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004715 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4716 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004717 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004718
Roland Levillain5b5b9312016-03-22 14:57:31 +00004719 template <typename T>
4720 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4721 typedef typename std::make_unsigned<T>::type V;
4722 V ux = static_cast<V>(value);
4723 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004724 return static_cast<T>(ux);
4725 } else {
4726 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004727 return static_cast<T>(ux >> (distance & max_shift_value)) |
4728 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004729 }
4730 }
4731
Roland Levillain5b5b9312016-03-22 14:57:31 +00004732 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004733 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004734 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004735 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004736 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004737 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004738 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004739 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004740 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4741 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4742 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4743 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004744 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004745 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4746 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004747 LOG(FATAL) << DebugName() << " is not defined for float values";
4748 UNREACHABLE();
4749 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004750 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4751 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004752 LOG(FATAL) << DebugName() << " is not defined for double values";
4753 UNREACHABLE();
4754 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004755
4756 DECLARE_INSTRUCTION(Ror);
4757
4758 private:
4759 DISALLOW_COPY_AND_ASSIGN(HRor);
4760};
4761
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004762// The value of a parameter in this method. Its location depends on
4763// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004764class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004765 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004766 HParameterValue(const DexFile& dex_file,
4767 uint16_t type_index,
4768 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004769 Primitive::Type parameter_type,
4770 bool is_this = false)
4771 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004772 dex_file_(dex_file),
4773 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004774 index_(index) {
4775 SetPackedFlag<kFlagIsThis>(is_this);
4776 SetPackedFlag<kFlagCanBeNull>(!is_this);
4777 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004778
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004779 const DexFile& GetDexFile() const { return dex_file_; }
4780 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004781 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004782 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004783
Vladimir Markoa1de9182016-02-25 11:37:38 +00004784 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4785 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004786
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004787 DECLARE_INSTRUCTION(ParameterValue);
4788
4789 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004790 // Whether or not the parameter value corresponds to 'this' argument.
4791 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4792 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4793 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4794 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4795 "Too many packed fields.");
4796
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004797 const DexFile& dex_file_;
4798 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004799 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004800 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004801 const uint8_t index_;
4802
4803 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4804};
4805
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004806class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004807 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004808 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4809 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004810
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004811 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004812 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004813 return true;
4814 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004815
Roland Levillain9867bc72015-08-05 10:21:34 +01004816 template <typename T> T Compute(T x) const { return ~x; }
4817
4818 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004819 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004820 }
4821 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004822 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004823 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004824 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4825 LOG(FATAL) << DebugName() << " is not defined for float values";
4826 UNREACHABLE();
4827 }
4828 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4829 LOG(FATAL) << DebugName() << " is not defined for double values";
4830 UNREACHABLE();
4831 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004832
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004833 DECLARE_INSTRUCTION(Not);
4834
4835 private:
4836 DISALLOW_COPY_AND_ASSIGN(HNot);
4837};
4838
David Brazdil66d126e2015-04-03 16:02:44 +01004839class HBooleanNot : public HUnaryOperation {
4840 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004841 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4842 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004843
4844 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004845 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004846 return true;
4847 }
4848
Roland Levillain9867bc72015-08-05 10:21:34 +01004849 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004850 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004851 return !x;
4852 }
4853
Roland Levillain9867bc72015-08-05 10:21:34 +01004854 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004855 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004856 }
4857 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4858 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004859 UNREACHABLE();
4860 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004861 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4862 LOG(FATAL) << DebugName() << " is not defined for float values";
4863 UNREACHABLE();
4864 }
4865 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4866 LOG(FATAL) << DebugName() << " is not defined for double values";
4867 UNREACHABLE();
4868 }
David Brazdil66d126e2015-04-03 16:02:44 +01004869
4870 DECLARE_INSTRUCTION(BooleanNot);
4871
4872 private:
4873 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4874};
4875
Roland Levillaindff1f282014-11-05 14:15:05 +00004876class HTypeConversion : public HExpression<1> {
4877 public:
4878 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004879 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004880 : HExpression(result_type,
4881 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4882 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004883 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004884 // Invariant: We should never generate a conversion to a Boolean value.
4885 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004886 }
4887
4888 HInstruction* GetInput() const { return InputAt(0); }
4889 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4890 Primitive::Type GetResultType() const { return GetType(); }
4891
4892 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004893 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004894
Mark Mendelle82549b2015-05-06 10:55:34 -04004895 // Try to statically evaluate the conversion and return a HConstant
4896 // containing the result. If the input cannot be converted, return nullptr.
4897 HConstant* TryStaticEvaluation() const;
4898
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004899 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4900 Primitive::Type result_type) {
4901 // Some architectures may not require the 'GC' side effects, but at this point
4902 // in the compilation process we do not know what architecture we will
4903 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004904 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4905 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004906 return SideEffects::CanTriggerGC();
4907 }
4908 return SideEffects::None();
4909 }
4910
Roland Levillaindff1f282014-11-05 14:15:05 +00004911 DECLARE_INSTRUCTION(TypeConversion);
4912
4913 private:
4914 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4915};
4916
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004917static constexpr uint32_t kNoRegNumber = -1;
4918
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004919class HNullCheck : public HExpression<1> {
4920 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004921 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4922 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004923 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004924 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004925 SetRawInputAt(0, value);
4926 }
4927
Calin Juravle10e244f2015-01-26 18:54:32 +00004928 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004929 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004930 return true;
4931 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004932
Calin Juravle10e244f2015-01-26 18:54:32 +00004933 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004934
Calin Juravle10e244f2015-01-26 18:54:32 +00004935 bool CanThrow() const OVERRIDE { return true; }
4936
4937 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004938
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004939
4940 DECLARE_INSTRUCTION(NullCheck);
4941
4942 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004943 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4944};
4945
4946class FieldInfo : public ValueObject {
4947 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004948 FieldInfo(MemberOffset field_offset,
4949 Primitive::Type field_type,
4950 bool is_volatile,
4951 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004952 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004953 const DexFile& dex_file,
4954 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004955 : field_offset_(field_offset),
4956 field_type_(field_type),
4957 is_volatile_(is_volatile),
4958 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004959 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004960 dex_file_(dex_file),
4961 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004962
4963 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004964 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004965 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004966 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004967 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004968 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004969 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004970
4971 private:
4972 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004973 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004974 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004975 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004976 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004977 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004978 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004979};
4980
4981class HInstanceFieldGet : public HExpression<1> {
4982 public:
4983 HInstanceFieldGet(HInstruction* value,
4984 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004985 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004986 bool is_volatile,
4987 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004988 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004989 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004990 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004991 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004992 : HExpression(field_type,
4993 SideEffects::FieldReadOfType(field_type, is_volatile),
4994 dex_pc),
4995 field_info_(field_offset,
4996 field_type,
4997 is_volatile,
4998 field_idx,
4999 declaring_class_def_index,
5000 dex_file,
5001 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005002 SetRawInputAt(0, value);
5003 }
5004
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005005 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005006
5007 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5008 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
5009 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005010 }
5011
Calin Juravle641547a2015-04-21 22:08:51 +01005012 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5013 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005014 }
5015
5016 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005017 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5018 }
5019
Calin Juravle52c48962014-12-16 17:02:57 +00005020 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005021 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005022 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005023 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005024
5025 DECLARE_INSTRUCTION(InstanceFieldGet);
5026
5027 private:
5028 const FieldInfo field_info_;
5029
5030 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5031};
5032
5033class HInstanceFieldSet : public HTemplateInstruction<2> {
5034 public:
5035 HInstanceFieldSet(HInstruction* object,
5036 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005037 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005038 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005039 bool is_volatile,
5040 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005041 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005042 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005043 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005044 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005045 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5046 dex_pc),
5047 field_info_(field_offset,
5048 field_type,
5049 is_volatile,
5050 field_idx,
5051 declaring_class_def_index,
5052 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005053 dex_cache) {
5054 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005055 SetRawInputAt(0, object);
5056 SetRawInputAt(1, value);
5057 }
5058
Calin Juravle641547a2015-04-21 22:08:51 +01005059 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5060 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005061 }
5062
Calin Juravle52c48962014-12-16 17:02:57 +00005063 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005064 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005065 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005066 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005067 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005068 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5069 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005070
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005071 DECLARE_INSTRUCTION(InstanceFieldSet);
5072
5073 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005074 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5075 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5076 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5077 "Too many packed fields.");
5078
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005079 const FieldInfo field_info_;
5080
5081 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5082};
5083
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005084class HArrayGet : public HExpression<2> {
5085 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005086 HArrayGet(HInstruction* array,
5087 HInstruction* index,
5088 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005089 uint32_t dex_pc,
5090 SideEffects additional_side_effects = SideEffects::None())
5091 : HExpression(type,
5092 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00005093 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005094 SetRawInputAt(0, array);
5095 SetRawInputAt(1, index);
5096 }
5097
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005098 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005099 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005100 return true;
5101 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005102 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005103 // TODO: We can be smarter here.
5104 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5105 // which generates the implicit null check. There are cases when these can be removed
5106 // to produce better code. If we ever add optimizations to do so we should allow an
5107 // implicit check here (as long as the address falls in the first page).
5108 return false;
5109 }
5110
David Brazdil4833f5a2015-12-16 10:37:39 +00005111 bool IsEquivalentOf(HArrayGet* other) const {
5112 bool result = (GetDexPc() == other->GetDexPc());
5113 if (kIsDebugBuild && result) {
5114 DCHECK_EQ(GetBlock(), other->GetBlock());
5115 DCHECK_EQ(GetArray(), other->GetArray());
5116 DCHECK_EQ(GetIndex(), other->GetIndex());
5117 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005118 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005119 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005120 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5121 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005122 }
5123 }
5124 return result;
5125 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005126
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005127 HInstruction* GetArray() const { return InputAt(0); }
5128 HInstruction* GetIndex() const { return InputAt(1); }
5129
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005130 DECLARE_INSTRUCTION(ArrayGet);
5131
5132 private:
5133 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5134};
5135
5136class HArraySet : public HTemplateInstruction<3> {
5137 public:
5138 HArraySet(HInstruction* array,
5139 HInstruction* index,
5140 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005141 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005142 uint32_t dex_pc,
5143 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005144 : HTemplateInstruction(
5145 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005146 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
5147 additional_side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005148 dex_pc) {
5149 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5150 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5151 SetPackedFlag<kFlagValueCanBeNull>(true);
5152 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005153 SetRawInputAt(0, array);
5154 SetRawInputAt(1, index);
5155 SetRawInputAt(2, value);
5156 }
5157
Calin Juravle77520bc2015-01-12 18:45:46 +00005158 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005159 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005160 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005161 }
5162
Mingyao Yang81014cb2015-06-02 03:16:27 -07005163 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005164 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005165
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005166 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005167 // TODO: Same as for ArrayGet.
5168 return false;
5169 }
5170
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005171 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005172 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005173 }
5174
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005175 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005176 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005177 }
5178
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005179 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005180 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005181 }
5182
Vladimir Markoa1de9182016-02-25 11:37:38 +00005183 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5184 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5185 bool StaticTypeOfArrayIsObjectArray() const {
5186 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5187 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005188
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005189 HInstruction* GetArray() const { return InputAt(0); }
5190 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005191 HInstruction* GetValue() const { return InputAt(2); }
5192
5193 Primitive::Type GetComponentType() const {
5194 // The Dex format does not type floating point index operations. Since the
5195 // `expected_component_type_` is set during building and can therefore not
5196 // be correct, we also check what is the value type. If it is a floating
5197 // point type, we must use that type.
5198 Primitive::Type value_type = GetValue()->GetType();
5199 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5200 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005201 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005202 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005203
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005204 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005205 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005206 }
5207
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005208 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5209 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5210 }
5211
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005212 DECLARE_INSTRUCTION(ArraySet);
5213
5214 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005215 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5216 static constexpr size_t kFieldExpectedComponentTypeSize =
5217 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5218 static constexpr size_t kFlagNeedsTypeCheck =
5219 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5220 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005221 // Cached information for the reference_type_info_ so that codegen
5222 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005223 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5224 static constexpr size_t kNumberOfArraySetPackedBits =
5225 kFlagStaticTypeOfArrayIsObjectArray + 1;
5226 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5227 using ExpectedComponentTypeField =
5228 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005229
5230 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5231};
5232
5233class HArrayLength : public HExpression<1> {
5234 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005235 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005236 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005237 // Note that arrays do not change length, so the instruction does not
5238 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005239 SetRawInputAt(0, array);
5240 }
5241
Calin Juravle77520bc2015-01-12 18:45:46 +00005242 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005243 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005244 return true;
5245 }
Calin Juravle641547a2015-04-21 22:08:51 +01005246 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5247 return obj == InputAt(0);
5248 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005249
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005250 DECLARE_INSTRUCTION(ArrayLength);
5251
5252 private:
5253 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5254};
5255
5256class HBoundsCheck : public HExpression<2> {
5257 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005258 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5259 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005260 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005261 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005262 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005263 SetRawInputAt(0, index);
5264 SetRawInputAt(1, length);
5265 }
5266
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005267 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005268 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005269 return true;
5270 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005271
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005272 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005273
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005274 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005275
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005276 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005277
5278 DECLARE_INSTRUCTION(BoundsCheck);
5279
5280 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005281 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5282};
5283
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005284class HSuspendCheck : public HTemplateInstruction<0> {
5285 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005286 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005287 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005288
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005289 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005290 return true;
5291 }
5292
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005293 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5294 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005295
5296 DECLARE_INSTRUCTION(SuspendCheck);
5297
5298 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005299 // Only used for code generation, in order to share the same slow path between back edges
5300 // of a same loop.
5301 SlowPathCode* slow_path_;
5302
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005303 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5304};
5305
David Srbecky0cf44932015-12-09 14:09:59 +00005306// Pseudo-instruction which provides the native debugger with mapping information.
5307// It ensures that we can generate line number and local variables at this point.
5308class HNativeDebugInfo : public HTemplateInstruction<0> {
5309 public:
5310 explicit HNativeDebugInfo(uint32_t dex_pc)
5311 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5312
5313 bool NeedsEnvironment() const OVERRIDE {
5314 return true;
5315 }
5316
5317 DECLARE_INSTRUCTION(NativeDebugInfo);
5318
5319 private:
5320 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5321};
5322
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005323/**
5324 * Instruction to load a Class object.
5325 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005326class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005327 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005328 HLoadClass(HCurrentMethod* current_method,
5329 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005330 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005331 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005332 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005333 bool needs_access_check,
5334 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005335 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005336 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005337 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005338 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005339 // Referrers class should not need access check. We never inline unverified
5340 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005341 DCHECK(!is_referrers_class || !needs_access_check);
5342
5343 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5344 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5345 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5346 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005347 SetRawInputAt(0, current_method);
5348 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005349
5350 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005351
5352 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005353 // Note that we don't need to test for generate_clinit_check_.
5354 // Whether or not we need to generate the clinit check is processed in
5355 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005356 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005357 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005358 }
5359
5360 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5361
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005362 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005363 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005364
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005365 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005366 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005367 }
5368
Calin Juravle0ba218d2015-05-19 18:46:01 +01005369 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005370 // The entrypoint the code generator is going to call does not do
5371 // clinit of the class.
5372 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005373 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005374 }
5375
5376 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005377 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005378 (!IsReferrersClass() && !IsInDexCache()) ||
5379 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005380 }
5381
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005382
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005383 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005384 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005385 }
5386
Calin Juravleacf735c2015-02-12 15:25:22 +00005387 ReferenceTypeInfo GetLoadedClassRTI() {
5388 return loaded_class_rti_;
5389 }
5390
5391 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5392 // Make sure we only set exact types (the loaded class should never be merged).
5393 DCHECK(rti.IsExact());
5394 loaded_class_rti_ = rti;
5395 }
5396
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005397 const DexFile& GetDexFile() { return dex_file_; }
5398
Vladimir Markoa1de9182016-02-25 11:37:38 +00005399 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005400
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005401 static SideEffects SideEffectsForArchRuntimeCalls() {
5402 return SideEffects::CanTriggerGC();
5403 }
5404
Vladimir Markoa1de9182016-02-25 11:37:38 +00005405 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5406 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5407 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5408 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005409
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005410 DECLARE_INSTRUCTION(LoadClass);
5411
5412 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005413 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5414 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5415 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005416 // Whether this instruction must generate the initialization check.
5417 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005418 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5419 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5420 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5421
5422 const uint16_t type_index_;
5423 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005424
Calin Juravleacf735c2015-02-12 15:25:22 +00005425 ReferenceTypeInfo loaded_class_rti_;
5426
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005427 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5428};
5429
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005430class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005431 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005432 // Determines how to load the String.
5433 enum class LoadKind {
5434 // Use boot image String* address that will be known at link time.
5435 // Used for boot image strings referenced by boot image code in non-PIC mode.
5436 kBootImageLinkTimeAddress,
5437
5438 // Use PC-relative boot image String* address that will be known at link time.
5439 // Used for boot image strings referenced by boot image code in PIC mode.
5440 kBootImageLinkTimePcRelative,
5441
5442 // Use a known boot image String* address, embedded in the code by the codegen.
5443 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5444 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5445 // GetIncludePatchInformation().
5446 kBootImageAddress,
5447
5448 // Load from the resolved strings array at an absolute address.
5449 // Used for strings outside the boot image referenced by JIT-compiled code.
5450 kDexCacheAddress,
5451
5452 // Load from resolved strings array in the dex cache using a PC-relative load.
5453 // Used for strings outside boot image when we know that we can access
5454 // the dex cache arrays using a PC-relative load.
5455 kDexCachePcRelative,
5456
5457 // Load from resolved strings array accessed through the class loaded from
5458 // the compiled method's own ArtMethod*. This is the default access type when
5459 // all other types are unavailable.
5460 kDexCacheViaMethod,
5461
5462 kLast = kDexCacheViaMethod
5463 };
5464
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005465 HLoadString(HCurrentMethod* current_method,
5466 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005467 const DexFile& dex_file,
5468 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005469 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005470 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005471 SetPackedFlag<kFlagIsInDexCache>(false);
5472 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5473 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005474 SetRawInputAt(0, current_method);
5475 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005476
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005477 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5478 DCHECK(HasAddress(load_kind));
5479 load_data_.address = address;
5480 SetLoadKindInternal(load_kind);
5481 }
5482
5483 void SetLoadKindWithStringReference(LoadKind load_kind,
5484 const DexFile& dex_file,
5485 uint32_t string_index) {
5486 DCHECK(HasStringReference(load_kind));
5487 load_data_.ref.dex_file = &dex_file;
5488 string_index_ = string_index;
5489 SetLoadKindInternal(load_kind);
5490 }
5491
5492 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5493 const DexFile& dex_file,
5494 uint32_t element_index) {
5495 DCHECK(HasDexCacheReference(load_kind));
5496 load_data_.ref.dex_file = &dex_file;
5497 load_data_.ref.dex_cache_element_index = element_index;
5498 SetLoadKindInternal(load_kind);
5499 }
5500
5501 LoadKind GetLoadKind() const {
5502 return GetPackedField<LoadKindField>();
5503 }
5504
5505 const DexFile& GetDexFile() const;
5506
5507 uint32_t GetStringIndex() const {
5508 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5509 return string_index_;
5510 }
5511
5512 uint32_t GetDexCacheElementOffset() const;
5513
5514 uint64_t GetAddress() const {
5515 DCHECK(HasAddress(GetLoadKind()));
5516 return load_data_.address;
5517 }
5518
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005519 bool CanBeMoved() const OVERRIDE { return true; }
5520
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005521 bool InstructionDataEquals(HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005522
5523 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5524
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005525 // Will call the runtime if we need to load the string through
5526 // the dex cache and the string is not guaranteed to be there yet.
5527 bool NeedsEnvironment() const OVERRIDE {
5528 LoadKind load_kind = GetLoadKind();
5529 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5530 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5531 load_kind == LoadKind::kBootImageAddress) {
5532 return false;
5533 }
5534 return !IsInDexCache();
5535 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005536
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005537 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5538 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5539 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005540
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005541 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005542 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005543
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005544 static SideEffects SideEffectsForArchRuntimeCalls() {
5545 return SideEffects::CanTriggerGC();
5546 }
5547
Vladimir Markoa1de9182016-02-25 11:37:38 +00005548 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5549
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005550 void MarkInDexCache() {
5551 SetPackedFlag<kFlagIsInDexCache>(true);
5552 DCHECK(!NeedsEnvironment());
5553 RemoveEnvironment();
Vladimir Markobf12e4d2016-04-05 11:18:49 +01005554 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005555 }
5556
5557 size_t InputCount() const OVERRIDE {
5558 return (InputAt(0) != nullptr) ? 1u : 0u;
5559 }
5560
5561 void AddSpecialInput(HInstruction* special_input);
5562
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005563 DECLARE_INSTRUCTION(LoadString);
5564
5565 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005566 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005567 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5568 static constexpr size_t kFieldLoadKindSize =
5569 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5570 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005571 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005572 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005573
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005574 static bool HasStringReference(LoadKind load_kind) {
5575 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5576 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5577 load_kind == LoadKind::kDexCacheViaMethod;
5578 }
5579
5580 static bool HasAddress(LoadKind load_kind) {
5581 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5582 }
5583
5584 static bool HasDexCacheReference(LoadKind load_kind) {
5585 return load_kind == LoadKind::kDexCachePcRelative;
5586 }
5587
5588 void SetLoadKindInternal(LoadKind load_kind);
5589
5590 // String index serves also as the hash code and it's also needed for slow-paths,
5591 // so it must not be overwritten with other load data.
5592 uint32_t string_index_;
5593
5594 union {
5595 struct {
5596 const DexFile* dex_file; // For string reference and dex cache reference.
5597 uint32_t dex_cache_element_index; // Only for dex cache reference.
5598 } ref;
5599 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5600 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005601
5602 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5603};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005604std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5605
5606// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5607inline const DexFile& HLoadString::GetDexFile() const {
5608 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5609 << GetLoadKind();
5610 return *load_data_.ref.dex_file;
5611}
5612
5613// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5614inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5615 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5616 return load_data_.ref.dex_cache_element_index;
5617}
5618
5619// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5620inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5621 // The special input is used for PC-relative loads on some architectures.
5622 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5623 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
5624 DCHECK(InputAt(0) == nullptr);
5625 SetRawInputAt(0u, special_input);
5626 special_input->AddUseAt(this, 0);
5627}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005628
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005629/**
5630 * Performs an initialization check on its Class object input.
5631 */
5632class HClinitCheck : public HExpression<1> {
5633 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005634 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005635 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005636 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005637 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5638 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005639 SetRawInputAt(0, constant);
5640 }
5641
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005642 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005643 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005644 return true;
5645 }
5646
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005647 bool NeedsEnvironment() const OVERRIDE {
5648 // May call runtime to initialize the class.
5649 return true;
5650 }
5651
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005652 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005653
5654 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5655
5656 DECLARE_INSTRUCTION(ClinitCheck);
5657
5658 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005659 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5660};
5661
5662class HStaticFieldGet : public HExpression<1> {
5663 public:
5664 HStaticFieldGet(HInstruction* cls,
5665 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005666 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005667 bool is_volatile,
5668 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005669 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005670 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005671 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005672 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005673 : HExpression(field_type,
5674 SideEffects::FieldReadOfType(field_type, is_volatile),
5675 dex_pc),
5676 field_info_(field_offset,
5677 field_type,
5678 is_volatile,
5679 field_idx,
5680 declaring_class_def_index,
5681 dex_file,
5682 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005683 SetRawInputAt(0, cls);
5684 }
5685
Calin Juravle52c48962014-12-16 17:02:57 +00005686
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005687 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005688
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005689 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005690 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5691 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005692 }
5693
5694 size_t ComputeHashCode() const OVERRIDE {
5695 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5696 }
5697
Calin Juravle52c48962014-12-16 17:02:57 +00005698 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005699 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5700 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005701 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005702
5703 DECLARE_INSTRUCTION(StaticFieldGet);
5704
5705 private:
5706 const FieldInfo field_info_;
5707
5708 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5709};
5710
5711class HStaticFieldSet : public HTemplateInstruction<2> {
5712 public:
5713 HStaticFieldSet(HInstruction* cls,
5714 HInstruction* value,
5715 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005716 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005717 bool is_volatile,
5718 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005719 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005720 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005721 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005722 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005723 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5724 dex_pc),
5725 field_info_(field_offset,
5726 field_type,
5727 is_volatile,
5728 field_idx,
5729 declaring_class_def_index,
5730 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005731 dex_cache) {
5732 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005733 SetRawInputAt(0, cls);
5734 SetRawInputAt(1, value);
5735 }
5736
Calin Juravle52c48962014-12-16 17:02:57 +00005737 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005738 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5739 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005740 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005741
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005742 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005743 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5744 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005745
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005746 DECLARE_INSTRUCTION(StaticFieldSet);
5747
5748 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005749 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5750 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5751 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5752 "Too many packed fields.");
5753
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005754 const FieldInfo field_info_;
5755
5756 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5757};
5758
Calin Juravlee460d1d2015-09-29 04:52:17 +01005759class HUnresolvedInstanceFieldGet : public HExpression<1> {
5760 public:
5761 HUnresolvedInstanceFieldGet(HInstruction* obj,
5762 Primitive::Type field_type,
5763 uint32_t field_index,
5764 uint32_t dex_pc)
5765 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5766 field_index_(field_index) {
5767 SetRawInputAt(0, obj);
5768 }
5769
5770 bool NeedsEnvironment() const OVERRIDE { return true; }
5771 bool CanThrow() const OVERRIDE { return true; }
5772
5773 Primitive::Type GetFieldType() const { return GetType(); }
5774 uint32_t GetFieldIndex() const { return field_index_; }
5775
5776 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5777
5778 private:
5779 const uint32_t field_index_;
5780
5781 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5782};
5783
5784class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5785 public:
5786 HUnresolvedInstanceFieldSet(HInstruction* obj,
5787 HInstruction* value,
5788 Primitive::Type field_type,
5789 uint32_t field_index,
5790 uint32_t dex_pc)
5791 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005792 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005793 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005794 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005795 SetRawInputAt(0, obj);
5796 SetRawInputAt(1, value);
5797 }
5798
5799 bool NeedsEnvironment() const OVERRIDE { return true; }
5800 bool CanThrow() const OVERRIDE { return true; }
5801
Vladimir Markoa1de9182016-02-25 11:37:38 +00005802 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005803 uint32_t GetFieldIndex() const { return field_index_; }
5804
5805 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5806
5807 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005808 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5809 static constexpr size_t kFieldFieldTypeSize =
5810 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5811 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5812 kFieldFieldType + kFieldFieldTypeSize;
5813 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5814 "Too many packed fields.");
5815 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5816
Calin Juravlee460d1d2015-09-29 04:52:17 +01005817 const uint32_t field_index_;
5818
5819 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5820};
5821
5822class HUnresolvedStaticFieldGet : public HExpression<0> {
5823 public:
5824 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5825 uint32_t field_index,
5826 uint32_t dex_pc)
5827 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5828 field_index_(field_index) {
5829 }
5830
5831 bool NeedsEnvironment() const OVERRIDE { return true; }
5832 bool CanThrow() const OVERRIDE { return true; }
5833
5834 Primitive::Type GetFieldType() const { return GetType(); }
5835 uint32_t GetFieldIndex() const { return field_index_; }
5836
5837 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5838
5839 private:
5840 const uint32_t field_index_;
5841
5842 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5843};
5844
5845class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5846 public:
5847 HUnresolvedStaticFieldSet(HInstruction* value,
5848 Primitive::Type field_type,
5849 uint32_t field_index,
5850 uint32_t dex_pc)
5851 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005852 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005853 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005854 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005855 SetRawInputAt(0, value);
5856 }
5857
5858 bool NeedsEnvironment() const OVERRIDE { return true; }
5859 bool CanThrow() const OVERRIDE { return true; }
5860
Vladimir Markoa1de9182016-02-25 11:37:38 +00005861 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005862 uint32_t GetFieldIndex() const { return field_index_; }
5863
5864 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5865
5866 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005867 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5868 static constexpr size_t kFieldFieldTypeSize =
5869 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5870 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5871 kFieldFieldType + kFieldFieldTypeSize;
5872 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5873 "Too many packed fields.");
5874 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5875
Calin Juravlee460d1d2015-09-29 04:52:17 +01005876 const uint32_t field_index_;
5877
5878 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5879};
5880
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005881// Implement the move-exception DEX instruction.
5882class HLoadException : public HExpression<0> {
5883 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005884 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5885 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005886
David Brazdilbbd733e2015-08-18 17:48:17 +01005887 bool CanBeNull() const OVERRIDE { return false; }
5888
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005889 DECLARE_INSTRUCTION(LoadException);
5890
5891 private:
5892 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5893};
5894
David Brazdilcb1c0552015-08-04 16:22:25 +01005895// Implicit part of move-exception which clears thread-local exception storage.
5896// Must not be removed because the runtime expects the TLS to get cleared.
5897class HClearException : public HTemplateInstruction<0> {
5898 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005899 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5900 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005901
5902 DECLARE_INSTRUCTION(ClearException);
5903
5904 private:
5905 DISALLOW_COPY_AND_ASSIGN(HClearException);
5906};
5907
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005908class HThrow : public HTemplateInstruction<1> {
5909 public:
5910 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005911 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005912 SetRawInputAt(0, exception);
5913 }
5914
5915 bool IsControlFlow() const OVERRIDE { return true; }
5916
5917 bool NeedsEnvironment() const OVERRIDE { return true; }
5918
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005919 bool CanThrow() const OVERRIDE { return true; }
5920
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005921
5922 DECLARE_INSTRUCTION(Throw);
5923
5924 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005925 DISALLOW_COPY_AND_ASSIGN(HThrow);
5926};
5927
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005928/**
5929 * Implementation strategies for the code generator of a HInstanceOf
5930 * or `HCheckCast`.
5931 */
5932enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005933 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005934 kExactCheck, // Can do a single class compare.
5935 kClassHierarchyCheck, // Can just walk the super class chain.
5936 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5937 kInterfaceCheck, // No optimization yet when checking against an interface.
5938 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005939 kArrayCheck, // No optimization yet when checking against a generic array.
5940 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005941};
5942
Roland Levillain86503782016-02-11 19:07:30 +00005943std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5944
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005945class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005946 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005947 HInstanceOf(HInstruction* object,
5948 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005949 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005950 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005951 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005952 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005953 dex_pc) {
5954 SetPackedField<TypeCheckKindField>(check_kind);
5955 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005956 SetRawInputAt(0, object);
5957 SetRawInputAt(1, constant);
5958 }
5959
5960 bool CanBeMoved() const OVERRIDE { return true; }
5961
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005962 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005963 return true;
5964 }
5965
5966 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005967 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005968 }
5969
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005970 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005971 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5972 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5973 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5974 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005975
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005976 static bool CanCallRuntime(TypeCheckKind check_kind) {
5977 // Mips currently does runtime calls for any other checks.
5978 return check_kind != TypeCheckKind::kExactCheck;
5979 }
5980
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005981 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005982 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005983 }
5984
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005985 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005986
5987 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005988 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
5989 static constexpr size_t kFieldTypeCheckKindSize =
5990 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
5991 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
5992 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
5993 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5994 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005995
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005996 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5997};
5998
Calin Juravleb1498f62015-02-16 13:13:29 +00005999class HBoundType : public HExpression<1> {
6000 public:
David Brazdilf5552582015-12-27 13:36:12 +00006001 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006002 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006003 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6004 SetPackedFlag<kFlagUpperCanBeNull>(true);
6005 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006006 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006007 SetRawInputAt(0, input);
6008 }
6009
David Brazdilf5552582015-12-27 13:36:12 +00006010 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006011 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006012 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006013 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006014
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006015 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006016 DCHECK(GetUpperCanBeNull() || !can_be_null);
6017 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006018 }
6019
Vladimir Markoa1de9182016-02-25 11:37:38 +00006020 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006021
Calin Juravleb1498f62015-02-16 13:13:29 +00006022 DECLARE_INSTRUCTION(BoundType);
6023
6024 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006025 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6026 // is false then CanBeNull() cannot be true).
6027 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6028 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6029 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6030 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6031
Calin Juravleb1498f62015-02-16 13:13:29 +00006032 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006033 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6034 // It is used to bound the type in cases like:
6035 // if (x instanceof ClassX) {
6036 // // uper_bound_ will be ClassX
6037 // }
David Brazdilf5552582015-12-27 13:36:12 +00006038 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006039
6040 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6041};
6042
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006043class HCheckCast : public HTemplateInstruction<2> {
6044 public:
6045 HCheckCast(HInstruction* object,
6046 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006047 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006048 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006049 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6050 SetPackedField<TypeCheckKindField>(check_kind);
6051 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006052 SetRawInputAt(0, object);
6053 SetRawInputAt(1, constant);
6054 }
6055
6056 bool CanBeMoved() const OVERRIDE { return true; }
6057
6058 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6059 return true;
6060 }
6061
6062 bool NeedsEnvironment() const OVERRIDE {
6063 // Instruction may throw a CheckCastError.
6064 return true;
6065 }
6066
6067 bool CanThrow() const OVERRIDE { return true; }
6068
Vladimir Markoa1de9182016-02-25 11:37:38 +00006069 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6070 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6071 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6072 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006073
6074 DECLARE_INSTRUCTION(CheckCast);
6075
6076 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006077 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6078 static constexpr size_t kFieldTypeCheckKindSize =
6079 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6080 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6081 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6082 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6083 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006084
6085 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006086};
6087
Calin Juravle27df7582015-04-17 19:12:31 +01006088class HMemoryBarrier : public HTemplateInstruction<0> {
6089 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006090 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006091 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006092 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6093 SetPackedField<BarrierKindField>(barrier_kind);
6094 }
Calin Juravle27df7582015-04-17 19:12:31 +01006095
Vladimir Markoa1de9182016-02-25 11:37:38 +00006096 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006097
6098 DECLARE_INSTRUCTION(MemoryBarrier);
6099
6100 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006101 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6102 static constexpr size_t kFieldBarrierKindSize =
6103 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6104 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6105 kFieldBarrierKind + kFieldBarrierKindSize;
6106 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6107 "Too many packed fields.");
6108 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006109
6110 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6111};
6112
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006113class HMonitorOperation : public HTemplateInstruction<1> {
6114 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006115 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006116 kEnter,
6117 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006118 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006119 };
6120
6121 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006122 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006123 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6124 dex_pc) {
6125 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006126 SetRawInputAt(0, object);
6127 }
6128
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006129 // Instruction may go into runtime, so we need an environment.
6130 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006131
6132 bool CanThrow() const OVERRIDE {
6133 // Verifier guarantees that monitor-exit cannot throw.
6134 // This is important because it allows the HGraphBuilder to remove
6135 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6136 return IsEnter();
6137 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006138
Vladimir Markoa1de9182016-02-25 11:37:38 +00006139 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6140 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006141
6142 DECLARE_INSTRUCTION(MonitorOperation);
6143
Calin Juravle52c48962014-12-16 17:02:57 +00006144 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006145 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6146 static constexpr size_t kFieldOperationKindSize =
6147 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6148 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6149 kFieldOperationKind + kFieldOperationKindSize;
6150 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6151 "Too many packed fields.");
6152 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006153
6154 private:
6155 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6156};
6157
David Brazdil74eb1b22015-12-14 11:44:01 +00006158class HSelect : public HExpression<3> {
6159 public:
6160 HSelect(HInstruction* condition,
6161 HInstruction* true_value,
6162 HInstruction* false_value,
6163 uint32_t dex_pc)
6164 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6165 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6166
6167 // First input must be `true_value` or `false_value` to allow codegens to
6168 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6169 // that architectures which implement HSelect as a conditional move also
6170 // will not need to invert the condition.
6171 SetRawInputAt(0, false_value);
6172 SetRawInputAt(1, true_value);
6173 SetRawInputAt(2, condition);
6174 }
6175
6176 HInstruction* GetFalseValue() const { return InputAt(0); }
6177 HInstruction* GetTrueValue() const { return InputAt(1); }
6178 HInstruction* GetCondition() const { return InputAt(2); }
6179
6180 bool CanBeMoved() const OVERRIDE { return true; }
6181 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6182
6183 bool CanBeNull() const OVERRIDE {
6184 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6185 }
6186
6187 DECLARE_INSTRUCTION(Select);
6188
6189 private:
6190 DISALLOW_COPY_AND_ASSIGN(HSelect);
6191};
6192
Vladimir Markof9f64412015-09-02 14:05:49 +01006193class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006194 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006195 MoveOperands(Location source,
6196 Location destination,
6197 Primitive::Type type,
6198 HInstruction* instruction)
6199 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006200
6201 Location GetSource() const { return source_; }
6202 Location GetDestination() const { return destination_; }
6203
6204 void SetSource(Location value) { source_ = value; }
6205 void SetDestination(Location value) { destination_ = value; }
6206
6207 // The parallel move resolver marks moves as "in-progress" by clearing the
6208 // destination (but not the source).
6209 Location MarkPending() {
6210 DCHECK(!IsPending());
6211 Location dest = destination_;
6212 destination_ = Location::NoLocation();
6213 return dest;
6214 }
6215
6216 void ClearPending(Location dest) {
6217 DCHECK(IsPending());
6218 destination_ = dest;
6219 }
6220
6221 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006222 DCHECK(source_.IsValid() || destination_.IsInvalid());
6223 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006224 }
6225
6226 // True if this blocks a move from the given location.
6227 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006228 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006229 }
6230
6231 // A move is redundant if it's been eliminated, if its source and
6232 // destination are the same, or if its destination is unneeded.
6233 bool IsRedundant() const {
6234 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6235 }
6236
6237 // We clear both operands to indicate move that's been eliminated.
6238 void Eliminate() {
6239 source_ = destination_ = Location::NoLocation();
6240 }
6241
6242 bool IsEliminated() const {
6243 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6244 return source_.IsInvalid();
6245 }
6246
Alexey Frunze4dda3372015-06-01 18:31:49 -07006247 Primitive::Type GetType() const { return type_; }
6248
Nicolas Geoffray90218252015-04-15 11:56:51 +01006249 bool Is64BitMove() const {
6250 return Primitive::Is64BitType(type_);
6251 }
6252
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006253 HInstruction* GetInstruction() const { return instruction_; }
6254
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006255 private:
6256 Location source_;
6257 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006258 // The type this move is for.
6259 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006260 // The instruction this move is assocatied with. Null when this move is
6261 // for moving an input in the expected locations of user (including a phi user).
6262 // This is only used in debug mode, to ensure we do not connect interval siblings
6263 // in the same parallel move.
6264 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006265};
6266
Roland Levillainc9285912015-12-18 10:38:42 +00006267std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6268
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006269static constexpr size_t kDefaultNumberOfMoves = 4;
6270
6271class HParallelMove : public HTemplateInstruction<0> {
6272 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006273 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006274 : HTemplateInstruction(SideEffects::None(), dex_pc),
6275 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6276 moves_.reserve(kDefaultNumberOfMoves);
6277 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006278
Nicolas Geoffray90218252015-04-15 11:56:51 +01006279 void AddMove(Location source,
6280 Location destination,
6281 Primitive::Type type,
6282 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006283 DCHECK(source.IsValid());
6284 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006285 if (kIsDebugBuild) {
6286 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006287 for (const MoveOperands& move : moves_) {
6288 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006289 // Special case the situation where the move is for the spill slot
6290 // of the instruction.
6291 if ((GetPrevious() == instruction)
6292 || ((GetPrevious() == nullptr)
6293 && instruction->IsPhi()
6294 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006295 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006296 << "Doing parallel moves for the same instruction.";
6297 } else {
6298 DCHECK(false) << "Doing parallel moves for the same instruction.";
6299 }
6300 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006301 }
6302 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006303 for (const MoveOperands& move : moves_) {
6304 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006305 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006306 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006307 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006308 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006309 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006310 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006311 }
6312
Vladimir Marko225b6462015-09-28 12:17:40 +01006313 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006314 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006315 }
6316
Vladimir Marko225b6462015-09-28 12:17:40 +01006317 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006318
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006319 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006320
6321 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006322 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006323
6324 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6325};
6326
Mark Mendell0616ae02015-04-17 12:49:27 -04006327} // namespace art
6328
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006329#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6330#include "nodes_shared.h"
6331#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006332#ifdef ART_ENABLE_CODEGEN_arm
6333#include "nodes_arm.h"
6334#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006335#ifdef ART_ENABLE_CODEGEN_arm64
6336#include "nodes_arm64.h"
6337#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006338#ifdef ART_ENABLE_CODEGEN_x86
6339#include "nodes_x86.h"
6340#endif
6341
6342namespace art {
6343
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006344class HGraphVisitor : public ValueObject {
6345 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006346 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6347 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006348
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006349 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006350 virtual void VisitBasicBlock(HBasicBlock* block);
6351
Roland Levillain633021e2014-10-01 14:12:25 +01006352 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006353 void VisitInsertionOrder();
6354
Roland Levillain633021e2014-10-01 14:12:25 +01006355 // Visit the graph following dominator tree reverse post-order.
6356 void VisitReversePostOrder();
6357
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006358 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006359
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006360 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006361#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006362 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6363
6364 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6365
6366#undef DECLARE_VISIT_INSTRUCTION
6367
6368 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006369 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006370
6371 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6372};
6373
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006374class HGraphDelegateVisitor : public HGraphVisitor {
6375 public:
6376 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6377 virtual ~HGraphDelegateVisitor() {}
6378
6379 // Visit functions that delegate to to super class.
6380#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006381 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006382
6383 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6384
6385#undef DECLARE_VISIT_INSTRUCTION
6386
6387 private:
6388 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6389};
6390
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006391class HInsertionOrderIterator : public ValueObject {
6392 public:
6393 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6394
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006395 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006396 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006397 void Advance() { ++index_; }
6398
6399 private:
6400 const HGraph& graph_;
6401 size_t index_;
6402
6403 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6404};
6405
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006406class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006407 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006408 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6409 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006410 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006411 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006412
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006413 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6414 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006415 void Advance() { ++index_; }
6416
6417 private:
6418 const HGraph& graph_;
6419 size_t index_;
6420
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006421 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006422};
6423
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006424class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006425 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006426 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006427 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006428 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006429 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006430 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006431
6432 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006433 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006434 void Advance() { --index_; }
6435
6436 private:
6437 const HGraph& graph_;
6438 size_t index_;
6439
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006440 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006441};
6442
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006443class HLinearPostOrderIterator : public ValueObject {
6444 public:
6445 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006446 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006447
6448 bool Done() const { return index_ == 0; }
6449
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006450 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006451
6452 void Advance() {
6453 --index_;
6454 DCHECK_GE(index_, 0U);
6455 }
6456
6457 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006458 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006459 size_t index_;
6460
6461 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6462};
6463
6464class HLinearOrderIterator : public ValueObject {
6465 public:
6466 explicit HLinearOrderIterator(const HGraph& graph)
6467 : order_(graph.GetLinearOrder()), index_(0) {}
6468
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006469 bool Done() const { return index_ == order_.size(); }
6470 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006471 void Advance() { ++index_; }
6472
6473 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006474 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006475 size_t index_;
6476
6477 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6478};
6479
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006480// Iterator over the blocks that art part of the loop. Includes blocks part
6481// of an inner loop. The order in which the blocks are iterated is on their
6482// block id.
6483class HBlocksInLoopIterator : public ValueObject {
6484 public:
6485 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6486 : blocks_in_loop_(info.GetBlocks()),
6487 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6488 index_(0) {
6489 if (!blocks_in_loop_.IsBitSet(index_)) {
6490 Advance();
6491 }
6492 }
6493
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006494 bool Done() const { return index_ == blocks_.size(); }
6495 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006496 void Advance() {
6497 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006498 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006499 if (blocks_in_loop_.IsBitSet(index_)) {
6500 break;
6501 }
6502 }
6503 }
6504
6505 private:
6506 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006507 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006508 size_t index_;
6509
6510 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6511};
6512
Mingyao Yang3584bce2015-05-19 16:01:59 -07006513// Iterator over the blocks that art part of the loop. Includes blocks part
6514// of an inner loop. The order in which the blocks are iterated is reverse
6515// post order.
6516class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6517 public:
6518 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6519 : blocks_in_loop_(info.GetBlocks()),
6520 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6521 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006522 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006523 Advance();
6524 }
6525 }
6526
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006527 bool Done() const { return index_ == blocks_.size(); }
6528 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006529 void Advance() {
6530 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006531 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6532 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006533 break;
6534 }
6535 }
6536 }
6537
6538 private:
6539 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006540 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006541 size_t index_;
6542
6543 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6544};
6545
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006546inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006547 if (constant->IsIntConstant()) {
6548 return constant->AsIntConstant()->GetValue();
6549 } else if (constant->IsLongConstant()) {
6550 return constant->AsLongConstant()->GetValue();
6551 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006552 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006553 return 0;
6554 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006555}
6556
Vladimir Marko58155012015-08-19 12:49:41 +00006557inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6558 // For the purposes of the compiler, the dex files must actually be the same object
6559 // if we want to safely treat them as the same. This is especially important for JIT
6560 // as custom class loaders can open the same underlying file (or memory) multiple
6561 // times and provide different class resolution but no two class loaders should ever
6562 // use the same DexFile object - doing so is an unsupported hack that can lead to
6563 // all sorts of weird failures.
6564 return &lhs == &rhs;
6565}
6566
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006567#define INSTRUCTION_TYPE_CHECK(type, super) \
6568 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6569 inline const H##type* HInstruction::As##type() const { \
6570 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6571 } \
6572 inline H##type* HInstruction::As##type() { \
6573 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6574 }
6575
6576 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6577#undef INSTRUCTION_TYPE_CHECK
6578
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006579// Create space in `blocks` for adding `number_of_new_blocks` entries
6580// starting at location `at`. Blocks after `at` are moved accordingly.
6581inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6582 size_t number_of_new_blocks,
6583 size_t after) {
6584 DCHECK_LT(after, blocks->size());
6585 size_t old_size = blocks->size();
6586 size_t new_size = old_size + number_of_new_blocks;
6587 blocks->resize(new_size);
6588 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6589}
6590
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006591} // namespace art
6592
6593#endif // ART_COMPILER_OPTIMIZING_NODES_H_