blob: 18a00d050f49ed7d71886a0e4783681fc4229331 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010028#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000029#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "handle.h"
31#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "primitive.h"
David Brazdild26a4112015-11-10 11:07:31 +000038#include "utils/array_ref.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010039#include "utils/intrusive_forward_list.h"
Vladimir Marko372f10e2016-05-17 16:30:10 +010040#include "utils/transform_array_ref.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000041
42namespace art {
43
David Brazdil1abb4192015-02-17 18:33:36 +000044class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010046class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010048class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000050class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000051class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000053class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000054class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000055class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000056class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010057class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010058class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010059class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010060class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000061class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010062class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000063class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000064
Mathieu Chartier736b5602015-09-02 14:54:11 -070065namespace mirror {
66class DexCache;
67} // namespace mirror
68
Nicolas Geoffray818f2102014-02-18 16:43:35 +000069static const int kDefaultNumberOfBlocks = 8;
70static const int kDefaultNumberOfSuccessors = 2;
71static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010072static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010073static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000074static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000075
Roland Levillain5b5b9312016-03-22 14:57:31 +000076// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
77static constexpr int32_t kMaxIntShiftDistance = 0x1f;
78// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
79static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000080
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010081static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070082static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010083
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010084static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
85
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060086static constexpr uint32_t kNoDexPc = -1;
87
Dave Allison20dfc792014-06-16 20:44:29 -070088enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070089 // All types.
90 kCondEQ, // ==
91 kCondNE, // !=
92 // Signed integers and floating-point numbers.
93 kCondLT, // <
94 kCondLE, // <=
95 kCondGT, // >
96 kCondGE, // >=
97 // Unsigned integers.
98 kCondB, // <
99 kCondBE, // <=
100 kCondA, // >
101 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -0700102};
103
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000104enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000105 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000106 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000107 kAnalysisFailThrowCatchLoop,
108 kAnalysisFailAmbiguousArrayOp,
109 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000110};
111
Vladimir Markof9f64412015-09-02 14:05:49 +0100112class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100113 public:
114 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
115
116 void AddInstruction(HInstruction* instruction);
117 void RemoveInstruction(HInstruction* instruction);
118
David Brazdilc3d743f2015-04-22 13:40:50 +0100119 // Insert `instruction` before/after an existing instruction `cursor`.
120 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
121 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
122
Roland Levillain6b469232014-09-25 10:10:38 +0100123 // Return true if this list contains `instruction`.
124 bool Contains(HInstruction* instruction) const;
125
Roland Levillainccc07a92014-09-16 14:48:16 +0100126 // Return true if `instruction1` is found before `instruction2` in
127 // this instruction list and false otherwise. Abort if none
128 // of these instructions is found.
129 bool FoundBefore(const HInstruction* instruction1,
130 const HInstruction* instruction2) const;
131
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000132 bool IsEmpty() const { return first_instruction_ == nullptr; }
133 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
134
135 // Update the block of all instructions to be `block`.
136 void SetBlockOfInstructions(HBasicBlock* block) const;
137
138 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000139 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000140 void Add(const HInstructionList& instruction_list);
141
David Brazdil2d7352b2015-04-20 14:52:42 +0100142 // Return the number of instructions in the list. This is an expensive operation.
143 size_t CountSize() const;
144
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100145 private:
146 HInstruction* first_instruction_;
147 HInstruction* last_instruction_;
148
149 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000150 friend class HGraph;
151 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100152 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100153 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100154
155 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
156};
157
David Brazdil4833f5a2015-12-16 10:37:39 +0000158class ReferenceTypeInfo : ValueObject {
159 public:
160 typedef Handle<mirror::Class> TypeHandle;
161
Vladimir Markoa1de9182016-02-25 11:37:38 +0000162 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
163
David Brazdil94ab38f2016-06-21 17:48:19 +0100164 static ReferenceTypeInfo Create(TypeHandle type_handle) SHARED_REQUIRES(Locks::mutator_lock_) {
165 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
166 }
167
Vladimir Markoa1de9182016-02-25 11:37:38 +0000168 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000169 return ReferenceTypeInfo(type_handle, is_exact);
170 }
171
172 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
173
Vladimir Markof39745e2016-01-26 12:16:55 +0000174 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000175 return handle.GetReference() != nullptr;
176 }
177
Vladimir Marko456307a2016-04-19 14:12:13 +0000178 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000179 return IsValidHandle(type_handle_);
180 }
181
182 bool IsExact() const { return is_exact_; }
183
184 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
185 DCHECK(IsValid());
186 return GetTypeHandle()->IsObjectClass();
187 }
188
189 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
190 DCHECK(IsValid());
191 return GetTypeHandle()->IsStringClass();
192 }
193
194 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
195 DCHECK(IsValid());
196 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
197 }
198
199 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
200 DCHECK(IsValid());
201 return GetTypeHandle()->IsInterface();
202 }
203
204 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
205 DCHECK(IsValid());
206 return GetTypeHandle()->IsArrayClass();
207 }
208
209 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
210 DCHECK(IsValid());
211 return GetTypeHandle()->IsPrimitiveArray();
212 }
213
214 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
215 DCHECK(IsValid());
216 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
217 }
218
219 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
220 DCHECK(IsValid());
221 if (!IsExact()) return false;
222 if (!IsArrayClass()) return false;
223 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
224 }
225
226 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
227 DCHECK(IsValid());
228 if (!IsExact()) return false;
229 if (!IsArrayClass()) return false;
230 if (!rti.IsArrayClass()) return false;
231 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
232 rti.GetTypeHandle()->GetComponentType());
233 }
234
235 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
236
237 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
238 DCHECK(IsValid());
239 DCHECK(rti.IsValid());
240 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
241 }
242
243 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
244 DCHECK(IsValid());
245 DCHECK(rti.IsValid());
246 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
247 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
248 }
249
250 // Returns true if the type information provide the same amount of details.
251 // Note that it does not mean that the instructions have the same actual type
252 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000253 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000254 if (!IsValid() && !rti.IsValid()) {
255 // Invalid types are equal.
256 return true;
257 }
258 if (!IsValid() || !rti.IsValid()) {
259 // One is valid, the other not.
260 return false;
261 }
262 return IsExact() == rti.IsExact()
263 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
264 }
265
266 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000267 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
268 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
269 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000270
271 // The class of the object.
272 TypeHandle type_handle_;
273 // Whether or not the type is exact or a superclass of the actual type.
274 // Whether or not we have any information about this type.
275 bool is_exact_;
276};
277
278std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
279
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000280// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100281class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000282 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100283 HGraph(ArenaAllocator* arena,
284 const DexFile& dex_file,
285 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100286 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700287 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100288 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100289 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000290 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100291 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000292 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100293 blocks_(arena->Adapter(kArenaAllocBlockList)),
294 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
295 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700296 entry_block_(nullptr),
297 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100298 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100299 number_of_vregs_(0),
300 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000301 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400302 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000303 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000304 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000305 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000306 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100307 dex_file_(dex_file),
308 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100309 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100310 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100311 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700312 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000313 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100314 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
315 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
316 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
317 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000318 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000319 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
320 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100321 blocks_.reserve(kDefaultNumberOfBlocks);
322 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000323
David Brazdilbadd8262016-02-02 16:28:56 +0000324 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
325 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
326
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000327 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100328 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
329
David Brazdil69ba7b72015-06-23 18:27:30 +0100330 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000331 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100332
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000333 HBasicBlock* GetEntryBlock() const { return entry_block_; }
334 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100335 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000336
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000337 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
338 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000339
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000340 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100341
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100342 void ComputeDominanceInformation();
343 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000344 void ClearLoopInformation();
345 void FindBackEdges(ArenaBitVector* visited);
346 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100347 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100348 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000349
David Brazdil4833f5a2015-12-16 10:37:39 +0000350 // Analyze all natural loops in this graph. Returns a code specifying that it
351 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000352 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000353 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100354
David Brazdilffee3d32015-07-06 11:48:53 +0100355 // Iterate over blocks to compute try block membership. Needs reverse post
356 // order and loop information.
357 void ComputeTryBlockInformation();
358
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000359 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000360 // Returns the instruction to replace the invoke expression or null if the
361 // invoke is for a void method. Note that the caller is responsible for replacing
362 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000363 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000364
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000365 // Update the loop and try membership of `block`, which was spawned from `reference`.
366 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
367 // should be the new back edge.
368 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
369 HBasicBlock* reference,
370 bool replace_if_back_edge);
371
Mingyao Yang3584bce2015-05-19 16:01:59 -0700372 // Need to add a couple of blocks to test if the loop body is entered and
373 // put deoptimization instructions, etc.
374 void TransformLoopHeaderForBCE(HBasicBlock* header);
375
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000376 // Removes `block` from the graph. Assumes `block` has been disconnected from
377 // other blocks and has no instructions or phis.
378 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000379
David Brazdilfc6a86a2015-06-26 10:33:45 +0000380 // Splits the edge between `block` and `successor` while preserving the
381 // indices in the predecessor/successor lists. If there are multiple edges
382 // between the blocks, the lowest indices are used.
383 // Returns the new block which is empty and has the same dex pc as `successor`.
384 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
385
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100386 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
387 void SimplifyLoop(HBasicBlock* header);
388
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000389 int32_t GetNextInstructionId() {
390 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000391 return current_instruction_id_++;
392 }
393
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000394 int32_t GetCurrentInstructionId() const {
395 return current_instruction_id_;
396 }
397
398 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000399 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000400 current_instruction_id_ = id;
401 }
402
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100403 uint16_t GetMaximumNumberOfOutVRegs() const {
404 return maximum_number_of_out_vregs_;
405 }
406
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000407 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
408 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100409 }
410
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100411 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
412 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
413 }
414
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000415 void UpdateTemporariesVRegSlots(size_t slots) {
416 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100417 }
418
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000419 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100420 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000421 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100422 }
423
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100424 void SetNumberOfVRegs(uint16_t number_of_vregs) {
425 number_of_vregs_ = number_of_vregs;
426 }
427
428 uint16_t GetNumberOfVRegs() const {
429 return number_of_vregs_;
430 }
431
432 void SetNumberOfInVRegs(uint16_t value) {
433 number_of_in_vregs_ = value;
434 }
435
David Brazdildee58d62016-04-07 09:54:26 +0000436 uint16_t GetNumberOfInVRegs() const {
437 return number_of_in_vregs_;
438 }
439
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100440 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100441 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100442 return number_of_vregs_ - number_of_in_vregs_;
443 }
444
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100445 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100446 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100447 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100448
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100449 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100450 return linear_order_;
451 }
452
Mark Mendell1152c922015-04-24 17:06:35 -0400453 bool HasBoundsChecks() const {
454 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800455 }
456
Mark Mendell1152c922015-04-24 17:06:35 -0400457 void SetHasBoundsChecks(bool value) {
458 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800459 }
460
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100461 bool ShouldGenerateConstructorBarrier() const {
462 return should_generate_constructor_barrier_;
463 }
464
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000465 bool IsDebuggable() const { return debuggable_; }
466
David Brazdil8d5b8b22015-03-24 10:51:52 +0000467 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000468 // already, it is created and inserted into the graph. This method is only for
469 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600470 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000471
472 // TODO: This is problematic for the consistency of reference type propagation
473 // because it can be created anytime after the pass and thus it will be left
474 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600475 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000476
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600477 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
478 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000479 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600480 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
481 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000482 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600483 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
484 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000485 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600486 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
487 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000488 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000489
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100490 HCurrentMethod* GetCurrentMethod();
491
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100492 const DexFile& GetDexFile() const {
493 return dex_file_;
494 }
495
496 uint32_t GetMethodIdx() const {
497 return method_idx_;
498 }
499
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100500 InvokeType GetInvokeType() const {
501 return invoke_type_;
502 }
503
Mark Mendellc4701932015-04-10 13:18:51 -0400504 InstructionSet GetInstructionSet() const {
505 return instruction_set_;
506 }
507
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000508 bool IsCompilingOsr() const { return osr_; }
509
David Brazdil77a48ae2015-09-15 12:34:04 +0000510 bool HasTryCatch() const { return has_try_catch_; }
511 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100512
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000513 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
514 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
515
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100516 ArtMethod* GetArtMethod() const { return art_method_; }
517 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
518
Mark Mendellf6529172015-11-17 11:16:56 -0500519 // Returns an instruction with the opposite boolean value from 'cond'.
520 // The instruction has been inserted into the graph, either as a constant, or
521 // before cursor.
522 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
523
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000524 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
525
David Brazdil2d7352b2015-04-20 14:52:42 +0100526 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000527 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100528 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000529
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000530 template <class InstructionType, typename ValueType>
531 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600532 ArenaSafeMap<ValueType, InstructionType*>* cache,
533 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000534 // Try to find an existing constant of the given value.
535 InstructionType* constant = nullptr;
536 auto cached_constant = cache->find(value);
537 if (cached_constant != cache->end()) {
538 constant = cached_constant->second;
539 }
540
541 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100542 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000543 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600544 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000545 cache->Overwrite(value, constant);
546 InsertConstant(constant);
547 }
548 return constant;
549 }
550
David Brazdil8d5b8b22015-03-24 10:51:52 +0000551 void InsertConstant(HConstant* instruction);
552
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000553 // Cache a float constant into the graph. This method should only be
554 // called by the SsaBuilder when creating "equivalent" instructions.
555 void CacheFloatConstant(HFloatConstant* constant);
556
557 // See CacheFloatConstant comment.
558 void CacheDoubleConstant(HDoubleConstant* constant);
559
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000560 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000561
562 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100563 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000564
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100565 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100566 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000567
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100568 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100569 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100570
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000571 HBasicBlock* entry_block_;
572 HBasicBlock* exit_block_;
573
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100574 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100575 uint16_t maximum_number_of_out_vregs_;
576
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100577 // The number of virtual registers in this method. Contains the parameters.
578 uint16_t number_of_vregs_;
579
580 // The number of virtual registers used by parameters of this method.
581 uint16_t number_of_in_vregs_;
582
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000583 // Number of vreg size slots that the temporaries use (used in baseline compiler).
584 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100585
Mark Mendell1152c922015-04-24 17:06:35 -0400586 // Has bounds checks. We can totally skip BCE if it's false.
587 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800588
David Brazdil77a48ae2015-09-15 12:34:04 +0000589 // Flag whether there are any try/catch blocks in the graph. We will skip
590 // try/catch-related passes if false.
591 bool has_try_catch_;
592
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000593 // Flag whether there are any irreducible loops in the graph.
594 bool has_irreducible_loops_;
595
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000596 // Indicates whether the graph should be compiled in a way that
597 // ensures full debuggability. If false, we can apply more
598 // aggressive optimizations that may limit the level of debugging.
599 const bool debuggable_;
600
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000601 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000602 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000603
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100604 // The dex file from which the method is from.
605 const DexFile& dex_file_;
606
607 // The method index in the dex file.
608 const uint32_t method_idx_;
609
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100610 // If inlined, this encodes how the callee is being invoked.
611 const InvokeType invoke_type_;
612
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100613 // Whether the graph has been transformed to SSA form. Only used
614 // in debug mode to ensure we are not using properties only valid
615 // for non-SSA form (like the number of temporaries).
616 bool in_ssa_form_;
617
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100618 const bool should_generate_constructor_barrier_;
619
Mathieu Chartiere401d142015-04-22 13:56:20 -0700620 const InstructionSet instruction_set_;
621
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000622 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000623 HNullConstant* cached_null_constant_;
624 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000625 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000626 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000627 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000628
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100629 HCurrentMethod* cached_current_method_;
630
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100631 // The ArtMethod this graph is for. Note that for AOT, it may be null,
632 // for example for methods whose declaring class could not be resolved
633 // (such as when the superclass could not be found).
634 ArtMethod* art_method_;
635
David Brazdil4833f5a2015-12-16 10:37:39 +0000636 // Keep the RTI of inexact Object to avoid having to pass stack handle
637 // collection pointer to passes which may create NullConstant.
638 ReferenceTypeInfo inexact_object_rti_;
639
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000640 // Whether we are compiling this graph for on stack replacement: this will
641 // make all loops seen as irreducible and emit special stack maps to mark
642 // compiled code entries which the interpreter can directly jump to.
643 const bool osr_;
644
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000645 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100646 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000647 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000648 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000649 DISALLOW_COPY_AND_ASSIGN(HGraph);
650};
651
Vladimir Markof9f64412015-09-02 14:05:49 +0100652class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000653 public:
654 HLoopInformation(HBasicBlock* header, HGraph* graph)
655 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100656 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000657 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100658 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100659 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100660 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000661 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100662 back_edges_.reserve(kDefaultNumberOfBackEdges);
663 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100664
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000665 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100666 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000667
668 void Dump(std::ostream& os);
669
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100670 HBasicBlock* GetHeader() const {
671 return header_;
672 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000673
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000674 void SetHeader(HBasicBlock* block) {
675 header_ = block;
676 }
677
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100678 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
679 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
680 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
681
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000682 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100683 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000684 }
685
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100686 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100687 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100688 }
689
David Brazdil46e2a392015-03-16 17:31:52 +0000690 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100691 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100692 }
693
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000694 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100695 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000696 }
697
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100698 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100699
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100700 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100701 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100702 }
703
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100704 // Returns the lifetime position of the back edge that has the
705 // greatest lifetime position.
706 size_t GetLifetimeEnd() const;
707
708 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100709 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100710 }
711
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000712 // Finds blocks that are part of this loop.
713 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100714
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100715 // Returns whether this loop information contains `block`.
716 // Note that this loop information *must* be populated before entering this function.
717 bool Contains(const HBasicBlock& block) const;
718
719 // Returns whether this loop information is an inner loop of `other`.
720 // Note that `other` *must* be populated before entering this function.
721 bool IsIn(const HLoopInformation& other) const;
722
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800723 // Returns true if instruction is not defined within this loop.
724 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700725
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100726 const ArenaBitVector& GetBlocks() const { return blocks_; }
727
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000728 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000729 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000730
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000731 void ClearAllBlocks() {
732 blocks_.ClearAllBits();
733 }
734
David Brazdil3f4a5222016-05-06 12:46:21 +0100735 bool HasBackEdgeNotDominatedByHeader() const;
736
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100737 bool IsPopulated() const {
738 return blocks_.GetHighestBitSet() != -1;
739 }
740
Anton Shaminf89381f2016-05-16 16:44:13 +0600741 bool DominatesAllBackEdges(HBasicBlock* block);
742
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000743 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100744 // Internal recursive implementation of `Populate`.
745 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100746 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100747
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000748 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100749 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000750 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100751 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100752 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100753 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000754
755 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
756};
757
David Brazdilec16f792015-08-19 15:04:01 +0100758// Stores try/catch information for basic blocks.
759// Note that HGraph is constructed so that catch blocks cannot simultaneously
760// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100761class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100762 public:
763 // Try block information constructor.
764 explicit TryCatchInformation(const HTryBoundary& try_entry)
765 : try_entry_(&try_entry),
766 catch_dex_file_(nullptr),
767 catch_type_index_(DexFile::kDexNoIndex16) {
768 DCHECK(try_entry_ != nullptr);
769 }
770
771 // Catch block information constructor.
772 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
773 : try_entry_(nullptr),
774 catch_dex_file_(&dex_file),
775 catch_type_index_(catch_type_index) {}
776
777 bool IsTryBlock() const { return try_entry_ != nullptr; }
778
779 const HTryBoundary& GetTryEntry() const {
780 DCHECK(IsTryBlock());
781 return *try_entry_;
782 }
783
784 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
785
786 bool IsCatchAllTypeIndex() const {
787 DCHECK(IsCatchBlock());
788 return catch_type_index_ == DexFile::kDexNoIndex16;
789 }
790
791 uint16_t GetCatchTypeIndex() const {
792 DCHECK(IsCatchBlock());
793 return catch_type_index_;
794 }
795
796 const DexFile& GetCatchDexFile() const {
797 DCHECK(IsCatchBlock());
798 return *catch_dex_file_;
799 }
800
801 private:
802 // One of possibly several TryBoundary instructions entering the block's try.
803 // Only set for try blocks.
804 const HTryBoundary* try_entry_;
805
806 // Exception type information. Only set for catch blocks.
807 const DexFile* catch_dex_file_;
808 const uint16_t catch_type_index_;
809};
810
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100811static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100812static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100813
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000814// A block in a method. Contains the list of instructions represented
815// as a double linked list. Each block knows its predecessors and
816// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100817
Vladimir Markof9f64412015-09-02 14:05:49 +0100818class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000819 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600820 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000821 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000822 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
823 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000824 loop_information_(nullptr),
825 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000826 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100827 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100828 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100829 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000830 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000831 try_catch_information_(nullptr) {
832 predecessors_.reserve(kDefaultNumberOfPredecessors);
833 successors_.reserve(kDefaultNumberOfSuccessors);
834 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
835 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000836
Vladimir Marko60584552015-09-03 13:35:12 +0000837 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100838 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000839 }
840
Vladimir Marko60584552015-09-03 13:35:12 +0000841 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100842 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000843 }
844
David Brazdild26a4112015-11-10 11:07:31 +0000845 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
846 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
847
Vladimir Marko60584552015-09-03 13:35:12 +0000848 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
849 return ContainsElement(successors_, block, start_from);
850 }
851
852 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100853 return dominated_blocks_;
854 }
855
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100856 bool IsEntryBlock() const {
857 return graph_->GetEntryBlock() == this;
858 }
859
860 bool IsExitBlock() const {
861 return graph_->GetExitBlock() == this;
862 }
863
David Brazdil46e2a392015-03-16 17:31:52 +0000864 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000865 bool IsSingleTryBoundary() const;
866
867 // Returns true if this block emits nothing but a jump.
868 bool IsSingleJump() const {
869 HLoopInformation* loop_info = GetLoopInformation();
870 return (IsSingleGoto() || IsSingleTryBoundary())
871 // Back edges generate a suspend check.
872 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
873 }
David Brazdil46e2a392015-03-16 17:31:52 +0000874
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000875 void AddBackEdge(HBasicBlock* back_edge) {
876 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000877 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000878 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100879 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000880 loop_information_->AddBackEdge(back_edge);
881 }
882
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000883 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000884 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000885
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100886 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000887 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600888 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000889
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000890 HBasicBlock* GetDominator() const { return dominator_; }
891 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000892 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
893
894 void RemoveDominatedBlock(HBasicBlock* block) {
895 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100896 }
Vladimir Marko60584552015-09-03 13:35:12 +0000897
898 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
899 ReplaceElement(dominated_blocks_, existing, new_block);
900 }
901
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100902 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000903
904 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100905 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000906 }
907
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100908 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
909 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100910 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100911 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100912 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
913 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000914
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000915 HInstruction* GetFirstInstructionDisregardMoves() const;
916
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000917 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000918 successors_.push_back(block);
919 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000920 }
921
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100922 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
923 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100924 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000925 new_block->predecessors_.push_back(this);
926 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000927 }
928
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000929 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
930 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000931 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000932 new_block->successors_.push_back(this);
933 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000934 }
935
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100936 // Insert `this` between `predecessor` and `successor. This method
937 // preserves the indicies, and will update the first edge found between
938 // `predecessor` and `successor`.
939 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
940 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100941 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000942 successor->predecessors_[predecessor_index] = this;
943 predecessor->successors_[successor_index] = this;
944 successors_.push_back(successor);
945 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100946 }
947
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100948 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000949 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100950 }
951
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000952 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000953 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000954 }
955
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100956 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000957 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100958 }
959
960 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000961 predecessors_.push_back(block);
962 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100963 }
964
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100965 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000966 DCHECK_EQ(predecessors_.size(), 2u);
967 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100968 }
969
David Brazdil769c9e52015-04-27 13:54:09 +0100970 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000971 DCHECK_EQ(successors_.size(), 2u);
972 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100973 }
974
David Brazdilfc6a86a2015-06-26 10:33:45 +0000975 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000976 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100977 }
978
David Brazdilfc6a86a2015-06-26 10:33:45 +0000979 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000980 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100981 }
982
David Brazdilfc6a86a2015-06-26 10:33:45 +0000983 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000984 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100985 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000986 }
987
988 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000989 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100990 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000991 }
992
993 // Returns whether the first occurrence of `predecessor` in the list of
994 // predecessors is at index `idx`.
995 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100996 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000997 return GetPredecessorIndexOf(predecessor) == idx;
998 }
999
David Brazdild7558da2015-09-22 13:04:14 +01001000 // Create a new block between this block and its predecessors. The new block
1001 // is added to the graph, all predecessor edges are relinked to it and an edge
1002 // is created to `this`. Returns the new empty block. Reverse post order or
1003 // loop and try/catch information are not updated.
1004 HBasicBlock* CreateImmediateDominator();
1005
David Brazdilfc6a86a2015-06-26 10:33:45 +00001006 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001007 // created, latter block. Note that this method will add the block to the
1008 // graph, create a Goto at the end of the former block and will create an edge
1009 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001010 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001011 HBasicBlock* SplitBefore(HInstruction* cursor);
1012
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001013 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001014 // created block. Note that this method just updates raw block information,
1015 // like predecessors, successors, dominators, and instruction list. It does not
1016 // update the graph, reverse post order, loop information, nor make sure the
1017 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001018 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1019
1020 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1021 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001022
1023 // Merge `other` at the end of `this`. Successors and dominated blocks of
1024 // `other` are changed to be successors and dominated blocks of `this`. Note
1025 // that this method does not update the graph, reverse post order, loop
1026 // information, nor make sure the blocks are consistent (for example ending
1027 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001028 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001029
1030 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1031 // of `this` are moved to `other`.
1032 // Note that this method does not update the graph, reverse post order, loop
1033 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001034 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001035 void ReplaceWith(HBasicBlock* other);
1036
David Brazdil2d7352b2015-04-20 14:52:42 +01001037 // Merge `other` at the end of `this`. This method updates loops, reverse post
1038 // order, links to predecessors, successors, dominators and deletes the block
1039 // from the graph. The two blocks must be successive, i.e. `this` the only
1040 // predecessor of `other` and vice versa.
1041 void MergeWith(HBasicBlock* other);
1042
1043 // Disconnects `this` from all its predecessors, successors and dominator,
1044 // removes it from all loops it is included in and eventually from the graph.
1045 // The block must not dominate any other block. Predecessors and successors
1046 // are safely updated.
1047 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001048
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001049 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001050 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001051 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001052 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001053 // Replace instruction `initial` with `replacement` within this block.
1054 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1055 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001056 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001057 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001058 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1059 // instruction list. With 'ensure_safety' set to true, it verifies that the
1060 // instruction is not in use and removes it from the use lists of its inputs.
1061 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1062 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001063 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001064
1065 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001066 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001067 }
1068
Roland Levillain6b879dd2014-09-22 17:13:44 +01001069 bool IsLoopPreHeaderFirstPredecessor() const {
1070 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001071 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001072 }
1073
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001074 bool IsFirstPredecessorBackEdge() const {
1075 DCHECK(IsLoopHeader());
1076 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1077 }
1078
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001079 HLoopInformation* GetLoopInformation() const {
1080 return loop_information_;
1081 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001082
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001083 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001084 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001085 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001086 void SetInLoop(HLoopInformation* info) {
1087 if (IsLoopHeader()) {
1088 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001089 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001090 loop_information_ = info;
1091 } else if (loop_information_->Contains(*info->GetHeader())) {
1092 // Block is currently part of an outer loop. Make it part of this inner loop.
1093 // Note that a non loop header having a loop information means this loop information
1094 // has already been populated
1095 loop_information_ = info;
1096 } else {
1097 // Block is part of an inner loop. Do not update the loop information.
1098 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1099 // at this point, because this method is being called while populating `info`.
1100 }
1101 }
1102
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001103 // Raw update of the loop information.
1104 void SetLoopInformation(HLoopInformation* info) {
1105 loop_information_ = info;
1106 }
1107
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001108 bool IsInLoop() const { return loop_information_ != nullptr; }
1109
David Brazdilec16f792015-08-19 15:04:01 +01001110 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1111
1112 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1113 try_catch_information_ = try_catch_information;
1114 }
1115
1116 bool IsTryBlock() const {
1117 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1118 }
1119
1120 bool IsCatchBlock() const {
1121 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1122 }
David Brazdilffee3d32015-07-06 11:48:53 +01001123
1124 // Returns the try entry that this block's successors should have. They will
1125 // be in the same try, unless the block ends in a try boundary. In that case,
1126 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001127 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001128
David Brazdild7558da2015-09-22 13:04:14 +01001129 bool HasThrowingInstructions() const;
1130
David Brazdila4b8c212015-05-07 09:59:30 +01001131 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001132 bool Dominates(HBasicBlock* block) const;
1133
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001134 size_t GetLifetimeStart() const { return lifetime_start_; }
1135 size_t GetLifetimeEnd() const { return lifetime_end_; }
1136
1137 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1138 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1139
David Brazdil8d5b8b22015-03-24 10:51:52 +00001140 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001141 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001142 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001143 bool HasSinglePhi() const;
1144
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001145 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001146 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001147 ArenaVector<HBasicBlock*> predecessors_;
1148 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001149 HInstructionList instructions_;
1150 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001151 HLoopInformation* loop_information_;
1152 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001153 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001154 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001155 // The dex program counter of the first instruction of this block.
1156 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001157 size_t lifetime_start_;
1158 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001159 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001160
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001161 friend class HGraph;
1162 friend class HInstruction;
1163
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001164 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1165};
1166
David Brazdilb2bd1c52015-03-25 11:17:37 +00001167// Iterates over the LoopInformation of all loops which contain 'block'
1168// from the innermost to the outermost.
1169class HLoopInformationOutwardIterator : public ValueObject {
1170 public:
1171 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1172 : current_(block.GetLoopInformation()) {}
1173
1174 bool Done() const { return current_ == nullptr; }
1175
1176 void Advance() {
1177 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001178 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001179 }
1180
1181 HLoopInformation* Current() const {
1182 DCHECK(!Done());
1183 return current_;
1184 }
1185
1186 private:
1187 HLoopInformation* current_;
1188
1189 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1190};
1191
Alexandre Ramesef20f712015-06-09 10:29:30 +01001192#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001193 M(Above, Condition) \
1194 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001195 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001196 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001197 M(ArrayGet, Instruction) \
1198 M(ArrayLength, Instruction) \
1199 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001200 M(Below, Condition) \
1201 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001202 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001203 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001204 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001205 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001206 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001207 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001208 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001209 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001210 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001211 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001212 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001213 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001214 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001215 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001216 M(Exit, Instruction) \
1217 M(FloatConstant, Constant) \
1218 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001219 M(GreaterThan, Condition) \
1220 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001221 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001222 M(InstanceFieldGet, Instruction) \
1223 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001224 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001225 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001226 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001227 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001228 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001229 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001230 M(LessThan, Condition) \
1231 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001232 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001233 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001234 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001235 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001236 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001237 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001238 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001239 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001240 M(Neg, UnaryOperation) \
1241 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001242 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001243 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001244 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001245 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001246 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001247 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001248 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001249 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001250 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001251 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001252 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001253 M(Return, Instruction) \
1254 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001255 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001256 M(Shl, BinaryOperation) \
1257 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001258 M(StaticFieldGet, Instruction) \
1259 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001260 M(UnresolvedInstanceFieldGet, Instruction) \
1261 M(UnresolvedInstanceFieldSet, Instruction) \
1262 M(UnresolvedStaticFieldGet, Instruction) \
1263 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001264 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001265 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001266 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001267 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001268 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001269 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001270 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001271 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001272
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001273/*
1274 * Instructions, shared across several (not all) architectures.
1275 */
1276#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1277#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1278#else
1279#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001280 M(BitwiseNegatedRight, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001281 M(MultiplyAccumulate, Instruction)
1282#endif
1283
Vladimir Markob4536b72015-11-24 13:45:23 +00001284#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001285#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001286#else
1287#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1288 M(ArmDexCacheArraysBase, Instruction)
1289#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001290
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001291#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001292#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001293#else
1294#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001295 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001296 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001297#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001298
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001299#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1300
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001301#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1302
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001303#ifndef ART_ENABLE_CODEGEN_x86
1304#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1305#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001306#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1307 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001308 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001309 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001310 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001311#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001312
1313#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1314
1315#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1316 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001317 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001318 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1319 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001320 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001321 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001322 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1323 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1324
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001325#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1326 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001327 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001328 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001329 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001330 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001331
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001332#define FOR_EACH_INSTRUCTION(M) \
1333 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1334 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1335
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001336#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001337FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1338#undef FORWARD_DECLARATION
1339
Vladimir Marko372f10e2016-05-17 16:30:10 +01001340#define DECLARE_INSTRUCTION(type) \
1341 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1342 const char* DebugName() const OVERRIDE { return #type; } \
1343 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1344 return other->Is##type(); \
1345 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001346 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001347
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001348#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1349 bool Is##type() const { return As##type() != nullptr; } \
1350 const H##type* As##type() const { return this; } \
1351 H##type* As##type() { return this; }
1352
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001353template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001354class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001355 public:
David Brazdiled596192015-01-23 10:39:45 +00001356 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001357 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001358 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001359
Vladimir Marko46817b82016-03-29 12:21:58 +01001360 // Hook for the IntrusiveForwardList<>.
1361 // TODO: Hide this better.
1362 IntrusiveForwardListHook hook;
1363
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001364 private:
David Brazdiled596192015-01-23 10:39:45 +00001365 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001366 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001367
1368 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001369 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001370
Vladimir Marko46817b82016-03-29 12:21:58 +01001371 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001372
1373 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1374};
1375
David Brazdiled596192015-01-23 10:39:45 +00001376template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001377using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001378
David Brazdil1abb4192015-02-17 18:33:36 +00001379// This class is used by HEnvironment and HInstruction classes to record the
1380// instructions they use and pointers to the corresponding HUseListNodes kept
1381// by the used instructions.
1382template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001383class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001384 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001385 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1386 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001387
Vladimir Marko46817b82016-03-29 12:21:58 +01001388 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1389 : HUserRecord(old_record.instruction_, before_use_node) {}
1390 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1391 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001392 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001393 }
1394
1395 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001396 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1397 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001398
1399 private:
1400 // Instruction used by the user.
1401 HInstruction* instruction_;
1402
Vladimir Marko46817b82016-03-29 12:21:58 +01001403 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1404 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001405};
1406
Aart Bik854a02b2015-07-14 16:07:00 -07001407/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001408 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001409 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001410 * For write/read dependences on fields/arrays, the dependence analysis uses
1411 * type disambiguation (e.g. a float field write cannot modify the value of an
1412 * integer field read) and the access type (e.g. a reference array write cannot
1413 * modify the value of a reference field read [although it may modify the
1414 * reference fetch prior to reading the field, which is represented by its own
1415 * write/read dependence]). The analysis makes conservative points-to
1416 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1417 * the same, and any reference read depends on any reference read without
1418 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001419 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001420 * The internal representation uses 38-bit and is described in the table below.
1421 * The first line indicates the side effect, and for field/array accesses the
1422 * second line indicates the type of the access (in the order of the
1423 * Primitive::Type enum).
1424 * The two numbered lines below indicate the bit position in the bitfield (read
1425 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001426 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001427 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1428 * +-------------+---------+---------+--------------+---------+---------+
1429 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1430 * | 3 |333333322|222222221| 1 |111111110|000000000|
1431 * | 7 |654321098|765432109| 8 |765432109|876543210|
1432 *
1433 * Note that, to ease the implementation, 'changes' bits are least significant
1434 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001435 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001436class SideEffects : public ValueObject {
1437 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001438 SideEffects() : flags_(0) {}
1439
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001440 static SideEffects None() {
1441 return SideEffects(0);
1442 }
1443
1444 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001445 return SideEffects(kAllChangeBits | kAllDependOnBits);
1446 }
1447
1448 static SideEffects AllChanges() {
1449 return SideEffects(kAllChangeBits);
1450 }
1451
1452 static SideEffects AllDependencies() {
1453 return SideEffects(kAllDependOnBits);
1454 }
1455
1456 static SideEffects AllExceptGCDependency() {
1457 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1458 }
1459
1460 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001461 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001462 }
1463
Aart Bik34c3ba92015-07-20 14:08:59 -07001464 static SideEffects AllWrites() {
1465 return SideEffects(kAllWrites);
1466 }
1467
1468 static SideEffects AllReads() {
1469 return SideEffects(kAllReads);
1470 }
1471
1472 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1473 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001474 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001475 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001476 }
1477
Aart Bik854a02b2015-07-14 16:07:00 -07001478 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001479 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001480 }
1481
Aart Bik34c3ba92015-07-20 14:08:59 -07001482 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1483 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001484 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001485 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001486 }
1487
1488 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001489 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001490 }
1491
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001492 static SideEffects CanTriggerGC() {
1493 return SideEffects(1ULL << kCanTriggerGCBit);
1494 }
1495
1496 static SideEffects DependsOnGC() {
1497 return SideEffects(1ULL << kDependsOnGCBit);
1498 }
1499
Aart Bik854a02b2015-07-14 16:07:00 -07001500 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001501 SideEffects Union(SideEffects other) const {
1502 return SideEffects(flags_ | other.flags_);
1503 }
1504
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001505 SideEffects Exclusion(SideEffects other) const {
1506 return SideEffects(flags_ & ~other.flags_);
1507 }
1508
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001509 void Add(SideEffects other) {
1510 flags_ |= other.flags_;
1511 }
1512
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001513 bool Includes(SideEffects other) const {
1514 return (other.flags_ & flags_) == other.flags_;
1515 }
1516
1517 bool HasSideEffects() const {
1518 return (flags_ & kAllChangeBits);
1519 }
1520
1521 bool HasDependencies() const {
1522 return (flags_ & kAllDependOnBits);
1523 }
1524
1525 // Returns true if there are no side effects or dependencies.
1526 bool DoesNothing() const {
1527 return flags_ == 0;
1528 }
1529
Aart Bik854a02b2015-07-14 16:07:00 -07001530 // Returns true if something is written.
1531 bool DoesAnyWrite() const {
1532 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001533 }
1534
Aart Bik854a02b2015-07-14 16:07:00 -07001535 // Returns true if something is read.
1536 bool DoesAnyRead() const {
1537 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001538 }
1539
Aart Bik854a02b2015-07-14 16:07:00 -07001540 // Returns true if potentially everything is written and read
1541 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001542 bool DoesAllReadWrite() const {
1543 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1544 }
1545
Aart Bik854a02b2015-07-14 16:07:00 -07001546 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001547 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001548 }
1549
Roland Levillain0d5a2812015-11-13 10:07:31 +00001550 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001551 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001552 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1553 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001554 }
1555
1556 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001557 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001558 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001559 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001560 for (int s = kLastBit; s >= 0; s--) {
1561 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1562 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1563 // This is a bit for the GC side effect.
1564 if (current_bit_is_set) {
1565 flags += "GC";
1566 }
Aart Bik854a02b2015-07-14 16:07:00 -07001567 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001568 } else {
1569 // This is a bit for the array/field analysis.
1570 // The underscore character stands for the 'can trigger GC' bit.
1571 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1572 if (current_bit_is_set) {
1573 flags += kDebug[s];
1574 }
1575 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1576 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1577 flags += "|";
1578 }
1579 }
Aart Bik854a02b2015-07-14 16:07:00 -07001580 }
1581 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001582 }
1583
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001584 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001585
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001586 private:
1587 static constexpr int kFieldArrayAnalysisBits = 9;
1588
1589 static constexpr int kFieldWriteOffset = 0;
1590 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1591 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1592 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1593
1594 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1595
1596 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1597 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1598 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1599 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1600
1601 static constexpr int kLastBit = kDependsOnGCBit;
1602 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1603
1604 // Aliases.
1605
1606 static_assert(kChangeBits == kDependOnBits,
1607 "the 'change' bits should match the 'depend on' bits.");
1608
1609 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1610 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1611 static constexpr uint64_t kAllWrites =
1612 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1613 static constexpr uint64_t kAllReads =
1614 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001615
Aart Bik854a02b2015-07-14 16:07:00 -07001616 // Translates type to bit flag.
1617 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1618 CHECK_NE(type, Primitive::kPrimVoid);
1619 const uint64_t one = 1;
1620 const int shift = type; // 0-based consecutive enum
1621 DCHECK_LE(kFieldWriteOffset, shift);
1622 DCHECK_LT(shift, kArrayWriteOffset);
1623 return one << (type + offset);
1624 }
1625
1626 // Private constructor on direct flags value.
1627 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1628
1629 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001630};
1631
David Brazdiled596192015-01-23 10:39:45 +00001632// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001633class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001634 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001635 HEnvironment(ArenaAllocator* arena,
1636 size_t number_of_vregs,
1637 const DexFile& dex_file,
1638 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001639 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001640 InvokeType invoke_type,
1641 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001642 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1643 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001644 parent_(nullptr),
1645 dex_file_(dex_file),
1646 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001647 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001648 invoke_type_(invoke_type),
1649 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001650 }
1651
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001652 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001653 : HEnvironment(arena,
1654 to_copy.Size(),
1655 to_copy.GetDexFile(),
1656 to_copy.GetMethodIdx(),
1657 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001658 to_copy.GetInvokeType(),
1659 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001660
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001661 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001662 if (parent_ != nullptr) {
1663 parent_->SetAndCopyParentChain(allocator, parent);
1664 } else {
1665 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1666 parent_->CopyFrom(parent);
1667 if (parent->GetParent() != nullptr) {
1668 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1669 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001670 }
David Brazdiled596192015-01-23 10:39:45 +00001671 }
1672
Vladimir Marko71bf8092015-09-15 15:33:14 +01001673 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001674 void CopyFrom(HEnvironment* environment);
1675
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001676 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1677 // input to the loop phi instead. This is for inserting instructions that
1678 // require an environment (like HDeoptimization) in the loop pre-header.
1679 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001680
1681 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001682 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001683 }
1684
1685 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001686 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001687 }
1688
David Brazdil1abb4192015-02-17 18:33:36 +00001689 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001690
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001691 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001692
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001693 HEnvironment* GetParent() const { return parent_; }
1694
1695 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001696 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001697 }
1698
1699 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001700 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001701 }
1702
1703 uint32_t GetDexPc() const {
1704 return dex_pc_;
1705 }
1706
1707 uint32_t GetMethodIdx() const {
1708 return method_idx_;
1709 }
1710
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001711 InvokeType GetInvokeType() const {
1712 return invoke_type_;
1713 }
1714
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001715 const DexFile& GetDexFile() const {
1716 return dex_file_;
1717 }
1718
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001719 HInstruction* GetHolder() const {
1720 return holder_;
1721 }
1722
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001723
1724 bool IsFromInlinedInvoke() const {
1725 return GetParent() != nullptr;
1726 }
1727
David Brazdiled596192015-01-23 10:39:45 +00001728 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001729 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1730 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001731 HEnvironment* parent_;
1732 const DexFile& dex_file_;
1733 const uint32_t method_idx_;
1734 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001735 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001736
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001737 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001738 HInstruction* const holder_;
1739
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001740 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001741
1742 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1743};
1744
Vladimir Markof9f64412015-09-02 14:05:49 +01001745class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001746 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001747 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001748 : previous_(nullptr),
1749 next_(nullptr),
1750 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001751 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001752 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001753 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001754 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001755 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001756 locations_(nullptr),
1757 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001758 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001759 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001760 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1761 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1762 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001763
Dave Allison20dfc792014-06-16 20:44:29 -07001764 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001765
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001766#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001767 enum InstructionKind {
1768 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1769 };
1770#undef DECLARE_KIND
1771
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001772 HInstruction* GetNext() const { return next_; }
1773 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001774
Calin Juravle77520bc2015-01-12 18:45:46 +00001775 HInstruction* GetNextDisregardingMoves() const;
1776 HInstruction* GetPreviousDisregardingMoves() const;
1777
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001778 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001779 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001780 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001781 bool IsInBlock() const { return block_ != nullptr; }
1782 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001783 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1784 bool IsIrreducibleLoopHeaderPhi() const {
1785 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1786 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001787
Vladimir Marko372f10e2016-05-17 16:30:10 +01001788 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1789
1790 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1791 // One virtual method is enough, just const_cast<> and then re-add the const.
1792 return ArrayRef<const HUserRecord<HInstruction*>>(
1793 const_cast<HInstruction*>(this)->GetInputRecords());
1794 }
1795
1796 auto GetInputs() {
1797 return MakeTransformArrayRef(
1798 GetInputRecords(),
1799 [](HUserRecord<HInstruction*>& record) -> HInstruction* {
1800 return record.GetInstruction();
1801 });
1802 }
1803
1804 auto GetInputs() const {
1805 return MakeTransformArrayRef(
1806 GetInputRecords(),
1807 [](const HUserRecord<HInstruction*>& record) -> const HInstruction* {
1808 return record.GetInstruction();
1809 });
1810 }
1811
1812 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001813 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001814
Vladimir Marko372f10e2016-05-17 16:30:10 +01001815 void SetRawInputAt(size_t index, HInstruction* input) {
1816 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1817 }
1818
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001819 virtual void Accept(HGraphVisitor* visitor) = 0;
1820 virtual const char* DebugName() const = 0;
1821
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001822 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1823
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001824 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001825
1826 uint32_t GetDexPc() const { return dex_pc_; }
1827
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001828 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001829
Roland Levillaine161a2a2014-10-03 12:45:18 +01001830 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001831 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001832
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001833 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001834 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001835
Calin Juravle10e244f2015-01-26 18:54:32 +00001836 // Does not apply for all instructions, but having this at top level greatly
1837 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001838 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001839 virtual bool CanBeNull() const {
1840 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1841 return true;
1842 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001843
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001844 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001845 return false;
1846 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001847
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001848 virtual bool IsActualObject() const {
1849 return GetType() == Primitive::kPrimNot;
1850 }
1851
Calin Juravle2e768302015-07-28 14:41:11 +00001852 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001853
Calin Juravle61d544b2015-02-23 16:46:57 +00001854 ReferenceTypeInfo GetReferenceTypeInfo() const {
1855 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001856 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001857 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001858 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001859
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001860 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001861 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001862 // Note: fixup_end remains valid across push_front().
1863 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1864 HUseListNode<HInstruction*>* new_node =
1865 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1866 uses_.push_front(*new_node);
1867 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001868 }
1869
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001870 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001871 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001872 // Note: env_fixup_end remains valid across push_front().
1873 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1874 HUseListNode<HEnvironment*>* new_node =
1875 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1876 env_uses_.push_front(*new_node);
1877 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001878 }
1879
David Brazdil1abb4192015-02-17 18:33:36 +00001880 void RemoveAsUserOfInput(size_t input) {
1881 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001882 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1883 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1884 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001885 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001886
Vladimir Marko372f10e2016-05-17 16:30:10 +01001887 void RemoveAsUserOfAllInputs() {
1888 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1889 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1890 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1891 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1892 }
1893 }
1894
David Brazdil1abb4192015-02-17 18:33:36 +00001895 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1896 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001897
Vladimir Marko46817b82016-03-29 12:21:58 +01001898 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1899 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1900 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001901 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001902 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001903 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001904
Roland Levillain6c82d402014-10-13 16:10:27 +01001905 // Does this instruction strictly dominate `other_instruction`?
1906 // Returns false if this instruction and `other_instruction` are the same.
1907 // Aborts if this instruction and `other_instruction` are both phis.
1908 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001909
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001910 int GetId() const { return id_; }
1911 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001912
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001913 int GetSsaIndex() const { return ssa_index_; }
1914 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1915 bool HasSsaIndex() const { return ssa_index_ != -1; }
1916
1917 bool HasEnvironment() const { return environment_ != nullptr; }
1918 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001919 // Set the `environment_` field. Raw because this method does not
1920 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001921 void SetRawEnvironment(HEnvironment* environment) {
1922 DCHECK(environment_ == nullptr);
1923 DCHECK_EQ(environment->GetHolder(), this);
1924 environment_ = environment;
1925 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001926
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001927 void RemoveEnvironment();
1928
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001929 // Set the environment of this instruction, copying it from `environment`. While
1930 // copying, the uses lists are being updated.
1931 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001932 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001933 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001934 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001935 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001936 if (environment->GetParent() != nullptr) {
1937 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1938 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001939 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001940
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001941 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1942 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001943 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001944 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001945 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001946 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001947 if (environment->GetParent() != nullptr) {
1948 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1949 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001950 }
1951
Nicolas Geoffray39468442014-09-02 15:17:15 +01001952 // Returns the number of entries in the environment. Typically, that is the
1953 // number of dex registers in a method. It could be more in case of inlining.
1954 size_t EnvironmentSize() const;
1955
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001956 LocationSummary* GetLocations() const { return locations_; }
1957 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001958
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001959 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001960 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001961
Alexandre Rames188d4312015-04-09 18:30:21 +01001962 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1963 // uses of this instruction by `other` are *not* updated.
1964 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1965 ReplaceWith(other);
1966 other->ReplaceInput(this, use_index);
1967 }
1968
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001969 // Move `this` instruction before `cursor`.
1970 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001971
Vladimir Markofb337ea2015-11-25 15:25:10 +00001972 // Move `this` before its first user and out of any loops. If there is no
1973 // out-of-loop user that dominates all other users, move the instruction
1974 // to the end of the out-of-loop common dominator of the user's blocks.
1975 //
1976 // This can be used only on non-throwing instructions with no side effects that
1977 // have at least one use but no environment uses.
1978 void MoveBeforeFirstUserAndOutOfLoops();
1979
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001980#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001981 bool Is##type() const; \
1982 const H##type* As##type() const; \
1983 H##type* As##type();
1984
1985 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1986#undef INSTRUCTION_TYPE_CHECK
1987
1988#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001989 bool Is##type() const { return (As##type() != nullptr); } \
1990 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001991 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001992 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001993#undef INSTRUCTION_TYPE_CHECK
1994
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001995 // Returns whether the instruction can be moved within the graph.
1996 virtual bool CanBeMoved() const { return false; }
1997
1998 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01001999 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002000 return false;
2001 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002002
2003 // Returns whether any data encoded in the two instructions is equal.
2004 // This method does not look at the inputs. Both instructions must be
2005 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002006 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002007 return false;
2008 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002009
2010 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002011 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002012 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002013 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002014
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002015 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2016 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2017 // the virtual function because the __attribute__((__pure__)) doesn't really
2018 // apply the strong requirement for virtual functions, preventing optimizations.
2019 InstructionKind GetKind() const PURE;
2020 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002021
2022 virtual size_t ComputeHashCode() const {
2023 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002024 for (const HInstruction* input : GetInputs()) {
2025 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002026 }
2027 return result;
2028 }
2029
2030 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002031 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002032 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002033
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002034 size_t GetLifetimePosition() const { return lifetime_position_; }
2035 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2036 LiveInterval* GetLiveInterval() const { return live_interval_; }
2037 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2038 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2039
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002040 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2041
2042 // Returns whether the code generation of the instruction will require to have access
2043 // to the current method. Such instructions are:
2044 // (1): Instructions that require an environment, as calling the runtime requires
2045 // to walk the stack and have the current method stored at a specific stack address.
2046 // (2): Object literals like classes and strings, that are loaded from the dex cache
2047 // fields of the current method.
2048 bool NeedsCurrentMethod() const {
2049 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2050 }
2051
Vladimir Markodc151b22015-10-15 18:02:30 +01002052 // Returns whether the code generation of the instruction will require to have access
2053 // to the dex cache of the current method's declaring class via the current method.
2054 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002055
Mark Mendellc4701932015-04-10 13:18:51 -04002056 // Does this instruction have any use in an environment before
2057 // control flow hits 'other'?
2058 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2059
2060 // Remove all references to environment uses of this instruction.
2061 // The caller must ensure that this is safe to do.
2062 void RemoveEnvironmentUsers();
2063
Vladimir Markoa1de9182016-02-25 11:37:38 +00002064 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2065 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002066
David Brazdil1abb4192015-02-17 18:33:36 +00002067 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002068 // If set, the machine code for this instruction is assumed to be generated by
2069 // its users. Used by liveness analysis to compute use positions accordingly.
2070 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2071 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2072 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2073 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2074
Vladimir Marko372f10e2016-05-17 16:30:10 +01002075 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2076 return GetInputRecords()[i];
2077 }
2078
2079 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2080 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2081 input_records[index] = input;
2082 }
David Brazdil1abb4192015-02-17 18:33:36 +00002083
Vladimir Markoa1de9182016-02-25 11:37:38 +00002084 uint32_t GetPackedFields() const {
2085 return packed_fields_;
2086 }
2087
2088 template <size_t flag>
2089 bool GetPackedFlag() const {
2090 return (packed_fields_ & (1u << flag)) != 0u;
2091 }
2092
2093 template <size_t flag>
2094 void SetPackedFlag(bool value = true) {
2095 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2096 }
2097
2098 template <typename BitFieldType>
2099 typename BitFieldType::value_type GetPackedField() const {
2100 return BitFieldType::Decode(packed_fields_);
2101 }
2102
2103 template <typename BitFieldType>
2104 void SetPackedField(typename BitFieldType::value_type value) {
2105 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2106 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2107 }
2108
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002109 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002110 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2111 auto before_use_node = uses_.before_begin();
2112 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2113 HInstruction* user = use_node->GetUser();
2114 size_t input_index = use_node->GetIndex();
2115 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2116 before_use_node = use_node;
2117 }
2118 }
2119
2120 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2121 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2122 if (next != uses_.end()) {
2123 HInstruction* next_user = next->GetUser();
2124 size_t next_index = next->GetIndex();
2125 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2126 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2127 }
2128 }
2129
2130 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2131 auto before_env_use_node = env_uses_.before_begin();
2132 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2133 HEnvironment* user = env_use_node->GetUser();
2134 size_t input_index = env_use_node->GetIndex();
2135 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2136 before_env_use_node = env_use_node;
2137 }
2138 }
2139
2140 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2141 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2142 if (next != env_uses_.end()) {
2143 HEnvironment* next_user = next->GetUser();
2144 size_t next_index = next->GetIndex();
2145 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2146 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2147 }
2148 }
David Brazdil1abb4192015-02-17 18:33:36 +00002149
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002150 HInstruction* previous_;
2151 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002152 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002153 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002154
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002155 // An instruction gets an id when it is added to the graph.
2156 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002157 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002158 int id_;
2159
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002160 // When doing liveness analysis, instructions that have uses get an SSA index.
2161 int ssa_index_;
2162
Vladimir Markoa1de9182016-02-25 11:37:38 +00002163 // Packed fields.
2164 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002165
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002166 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002167 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002168
2169 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002170 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002171
Nicolas Geoffray39468442014-09-02 15:17:15 +01002172 // The environment associated with this instruction. Not null if the instruction
2173 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002174 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002175
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002176 // Set by the code generator.
2177 LocationSummary* locations_;
2178
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002179 // Set by the liveness analysis.
2180 LiveInterval* live_interval_;
2181
2182 // Set by the liveness analysis, this is the position in a linear
2183 // order of blocks where this instruction's live interval start.
2184 size_t lifetime_position_;
2185
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002186 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002187
Vladimir Markoa1de9182016-02-25 11:37:38 +00002188 // The reference handle part of the reference type info.
2189 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002190 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002191 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002192
David Brazdil1abb4192015-02-17 18:33:36 +00002193 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002194 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002195 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002196 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002197 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002198
2199 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2200};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002201std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002202
2203class HInstructionIterator : public ValueObject {
2204 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002205 explicit HInstructionIterator(const HInstructionList& instructions)
2206 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002207 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002208 }
2209
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002210 bool Done() const { return instruction_ == nullptr; }
2211 HInstruction* Current() const { return instruction_; }
2212 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002213 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002214 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002215 }
2216
2217 private:
2218 HInstruction* instruction_;
2219 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002220
2221 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002222};
2223
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002224class HBackwardInstructionIterator : public ValueObject {
2225 public:
2226 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2227 : instruction_(instructions.last_instruction_) {
2228 next_ = Done() ? nullptr : instruction_->GetPrevious();
2229 }
2230
2231 bool Done() const { return instruction_ == nullptr; }
2232 HInstruction* Current() const { return instruction_; }
2233 void Advance() {
2234 instruction_ = next_;
2235 next_ = Done() ? nullptr : instruction_->GetPrevious();
2236 }
2237
2238 private:
2239 HInstruction* instruction_;
2240 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002241
2242 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002243};
2244
Vladimir Markof9f64412015-09-02 14:05:49 +01002245template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002246class HTemplateInstruction: public HInstruction {
2247 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002248 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002249 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002250 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002251
Vladimir Marko372f10e2016-05-17 16:30:10 +01002252 using HInstruction::GetInputRecords; // Keep the const version visible.
2253 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2254 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002255 }
2256
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002257 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002258 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002259
2260 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002261};
2262
Vladimir Markof9f64412015-09-02 14:05:49 +01002263// HTemplateInstruction specialization for N=0.
2264template<>
2265class HTemplateInstruction<0>: public HInstruction {
2266 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002267 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002268 : HInstruction(side_effects, dex_pc) {}
2269
Vladimir Markof9f64412015-09-02 14:05:49 +01002270 virtual ~HTemplateInstruction() {}
2271
Vladimir Marko372f10e2016-05-17 16:30:10 +01002272 using HInstruction::GetInputRecords; // Keep the const version visible.
2273 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2274 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002275 }
2276
2277 private:
2278 friend class SsaBuilder;
2279};
2280
Dave Allison20dfc792014-06-16 20:44:29 -07002281template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002282class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002283 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002284 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002285 : HTemplateInstruction<N>(side_effects, dex_pc) {
2286 this->template SetPackedField<TypeField>(type);
2287 }
Dave Allison20dfc792014-06-16 20:44:29 -07002288 virtual ~HExpression() {}
2289
Vladimir Markoa1de9182016-02-25 11:37:38 +00002290 Primitive::Type GetType() const OVERRIDE {
2291 return TypeField::Decode(this->GetPackedFields());
2292 }
Dave Allison20dfc792014-06-16 20:44:29 -07002293
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002294 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002295 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2296 static constexpr size_t kFieldTypeSize =
2297 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2298 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2299 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2300 "Too many packed fields.");
2301 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002302};
2303
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002304// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2305// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002306class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002307 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002308 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2309 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002310
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002311 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002312
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002313 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002314
2315 private:
2316 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2317};
2318
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002319// Represents dex's RETURN opcodes. A HReturn is a control flow
2320// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002321class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002322 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002323 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2324 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002325 SetRawInputAt(0, value);
2326 }
2327
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002328 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002329
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002330 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002331
2332 private:
2333 DISALLOW_COPY_AND_ASSIGN(HReturn);
2334};
2335
Vladimir Markofcb503c2016-05-18 12:48:17 +01002336class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002337 public:
2338 HPhi(ArenaAllocator* arena,
2339 uint32_t reg_number,
2340 size_t number_of_inputs,
2341 Primitive::Type type,
2342 uint32_t dex_pc = kNoDexPc)
2343 : HInstruction(SideEffects::None(), dex_pc),
2344 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2345 reg_number_(reg_number) {
2346 SetPackedField<TypeField>(ToPhiType(type));
2347 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2348 // Phis are constructed live and marked dead if conflicting or unused.
2349 // Individual steps of SsaBuilder should assume that if a phi has been
2350 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2351 SetPackedFlag<kFlagIsLive>(true);
2352 SetPackedFlag<kFlagCanBeNull>(true);
2353 }
2354
2355 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2356 static Primitive::Type ToPhiType(Primitive::Type type) {
2357 return Primitive::PrimitiveKind(type);
2358 }
2359
2360 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2361
Vladimir Marko372f10e2016-05-17 16:30:10 +01002362 using HInstruction::GetInputRecords; // Keep the const version visible.
2363 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2364 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2365 }
David Brazdildee58d62016-04-07 09:54:26 +00002366
2367 void AddInput(HInstruction* input);
2368 void RemoveInputAt(size_t index);
2369
2370 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2371 void SetType(Primitive::Type new_type) {
2372 // Make sure that only valid type changes occur. The following are allowed:
2373 // (1) int -> float/ref (primitive type propagation),
2374 // (2) long -> double (primitive type propagation).
2375 DCHECK(GetType() == new_type ||
2376 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2377 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2378 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2379 SetPackedField<TypeField>(new_type);
2380 }
2381
2382 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2383 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2384
2385 uint32_t GetRegNumber() const { return reg_number_; }
2386
2387 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2388 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2389 bool IsDead() const { return !IsLive(); }
2390 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2391
2392 bool IsVRegEquivalentOf(HInstruction* other) const {
2393 return other != nullptr
2394 && other->IsPhi()
2395 && other->AsPhi()->GetBlock() == GetBlock()
2396 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2397 }
2398
2399 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2400 // An equivalent phi is a phi having the same dex register and type.
2401 // It assumes that phis with the same dex register are adjacent.
2402 HPhi* GetNextEquivalentPhiWithSameType() {
2403 HInstruction* next = GetNext();
2404 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2405 if (next->GetType() == GetType()) {
2406 return next->AsPhi();
2407 }
2408 next = next->GetNext();
2409 }
2410 return nullptr;
2411 }
2412
2413 DECLARE_INSTRUCTION(Phi);
2414
David Brazdildee58d62016-04-07 09:54:26 +00002415 private:
2416 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2417 static constexpr size_t kFieldTypeSize =
2418 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2419 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2420 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2421 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2422 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2423 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2424
Vladimir Marko372f10e2016-05-17 16:30:10 +01002425 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002426 const uint32_t reg_number_;
2427
2428 DISALLOW_COPY_AND_ASSIGN(HPhi);
2429};
2430
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002431// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002432// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002433// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002434class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002435 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002436 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002437
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002438 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002439
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002440 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002441
2442 private:
2443 DISALLOW_COPY_AND_ASSIGN(HExit);
2444};
2445
2446// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002447class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002448 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002449 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002450
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002451 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002452
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002453 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002454 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002455 }
2456
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002457 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002458
2459 private:
2460 DISALLOW_COPY_AND_ASSIGN(HGoto);
2461};
2462
Roland Levillain9867bc72015-08-05 10:21:34 +01002463class HConstant : public HExpression<0> {
2464 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002465 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2466 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002467
2468 bool CanBeMoved() const OVERRIDE { return true; }
2469
Roland Levillain1a653882016-03-18 18:05:57 +00002470 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002471 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002472 // Is this constant 0 in the arithmetic sense?
2473 virtual bool IsArithmeticZero() const { return false; }
2474 // Is this constant a 0-bit pattern?
2475 virtual bool IsZeroBitPattern() const { return false; }
2476 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002477 virtual bool IsOne() const { return false; }
2478
David Brazdil77a48ae2015-09-15 12:34:04 +00002479 virtual uint64_t GetValueAsUint64() const = 0;
2480
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002481 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002482
2483 private:
2484 DISALLOW_COPY_AND_ASSIGN(HConstant);
2485};
2486
Vladimir Markofcb503c2016-05-18 12:48:17 +01002487class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002488 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002489 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002490 return true;
2491 }
2492
David Brazdil77a48ae2015-09-15 12:34:04 +00002493 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2494
Roland Levillain9867bc72015-08-05 10:21:34 +01002495 size_t ComputeHashCode() const OVERRIDE { return 0; }
2496
Roland Levillain1a653882016-03-18 18:05:57 +00002497 // The null constant representation is a 0-bit pattern.
2498 virtual bool IsZeroBitPattern() const { return true; }
2499
Roland Levillain9867bc72015-08-05 10:21:34 +01002500 DECLARE_INSTRUCTION(NullConstant);
2501
2502 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002503 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002504
2505 friend class HGraph;
2506 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2507};
2508
2509// Constants of the type int. Those can be from Dex instructions, or
2510// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002511class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002512 public:
2513 int32_t GetValue() const { return value_; }
2514
David Brazdil9f389d42015-10-01 14:32:56 +01002515 uint64_t GetValueAsUint64() const OVERRIDE {
2516 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2517 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002518
Vladimir Marko372f10e2016-05-17 16:30:10 +01002519 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002520 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002521 return other->AsIntConstant()->value_ == value_;
2522 }
2523
2524 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2525
2526 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002527 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2528 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002529 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2530
Roland Levillain1a653882016-03-18 18:05:57 +00002531 // Integer constants are used to encode Boolean values as well,
2532 // where 1 means true and 0 means false.
2533 bool IsTrue() const { return GetValue() == 1; }
2534 bool IsFalse() const { return GetValue() == 0; }
2535
Roland Levillain9867bc72015-08-05 10:21:34 +01002536 DECLARE_INSTRUCTION(IntConstant);
2537
2538 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002539 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2540 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2541 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2542 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002543
2544 const int32_t value_;
2545
2546 friend class HGraph;
2547 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2548 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2549 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2550};
2551
Vladimir Markofcb503c2016-05-18 12:48:17 +01002552class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002553 public:
2554 int64_t GetValue() const { return value_; }
2555
David Brazdil77a48ae2015-09-15 12:34:04 +00002556 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2557
Vladimir Marko372f10e2016-05-17 16:30:10 +01002558 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002559 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002560 return other->AsLongConstant()->value_ == value_;
2561 }
2562
2563 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2564
2565 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002566 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2567 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002568 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2569
2570 DECLARE_INSTRUCTION(LongConstant);
2571
2572 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002573 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2574 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002575
2576 const int64_t value_;
2577
2578 friend class HGraph;
2579 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2580};
Dave Allison20dfc792014-06-16 20:44:29 -07002581
Vladimir Markofcb503c2016-05-18 12:48:17 +01002582class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002583 public:
2584 float GetValue() const { return value_; }
2585
2586 uint64_t GetValueAsUint64() const OVERRIDE {
2587 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2588 }
2589
Vladimir Marko372f10e2016-05-17 16:30:10 +01002590 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002591 DCHECK(other->IsFloatConstant()) << other->DebugName();
2592 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2593 }
2594
2595 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2596
2597 bool IsMinusOne() const OVERRIDE {
2598 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2599 }
Roland Levillain1a653882016-03-18 18:05:57 +00002600 bool IsArithmeticZero() const OVERRIDE {
2601 return std::fpclassify(value_) == FP_ZERO;
2602 }
2603 bool IsArithmeticPositiveZero() const {
2604 return IsArithmeticZero() && !std::signbit(value_);
2605 }
2606 bool IsArithmeticNegativeZero() const {
2607 return IsArithmeticZero() && std::signbit(value_);
2608 }
2609 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002610 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002611 }
2612 bool IsOne() const OVERRIDE {
2613 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2614 }
2615 bool IsNaN() const {
2616 return std::isnan(value_);
2617 }
2618
2619 DECLARE_INSTRUCTION(FloatConstant);
2620
2621 private:
2622 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2623 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2624 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2625 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2626
2627 const float value_;
2628
2629 // Only the SsaBuilder and HGraph can create floating-point constants.
2630 friend class SsaBuilder;
2631 friend class HGraph;
2632 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2633};
2634
Vladimir Markofcb503c2016-05-18 12:48:17 +01002635class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002636 public:
2637 double GetValue() const { return value_; }
2638
2639 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2640
Vladimir Marko372f10e2016-05-17 16:30:10 +01002641 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002642 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2643 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2644 }
2645
2646 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2647
2648 bool IsMinusOne() const OVERRIDE {
2649 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2650 }
Roland Levillain1a653882016-03-18 18:05:57 +00002651 bool IsArithmeticZero() const OVERRIDE {
2652 return std::fpclassify(value_) == FP_ZERO;
2653 }
2654 bool IsArithmeticPositiveZero() const {
2655 return IsArithmeticZero() && !std::signbit(value_);
2656 }
2657 bool IsArithmeticNegativeZero() const {
2658 return IsArithmeticZero() && std::signbit(value_);
2659 }
2660 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002661 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002662 }
2663 bool IsOne() const OVERRIDE {
2664 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2665 }
2666 bool IsNaN() const {
2667 return std::isnan(value_);
2668 }
2669
2670 DECLARE_INSTRUCTION(DoubleConstant);
2671
2672 private:
2673 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2674 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2675 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2676 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2677
2678 const double value_;
2679
2680 // Only the SsaBuilder and HGraph can create floating-point constants.
2681 friend class SsaBuilder;
2682 friend class HGraph;
2683 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2684};
2685
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002686// Conditional branch. A block ending with an HIf instruction must have
2687// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002688class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002689 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002690 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2691 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002692 SetRawInputAt(0, input);
2693 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002694
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002695 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002696
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002697 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002698 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002699 }
2700
2701 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002702 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002703 }
2704
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002705 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002706
2707 private:
2708 DISALLOW_COPY_AND_ASSIGN(HIf);
2709};
2710
David Brazdilfc6a86a2015-06-26 10:33:45 +00002711
2712// Abstract instruction which marks the beginning and/or end of a try block and
2713// links it to the respective exception handlers. Behaves the same as a Goto in
2714// non-exceptional control flow.
2715// Normal-flow successor is stored at index zero, exception handlers under
2716// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002717class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002718 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002719 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002720 kEntry,
2721 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002722 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002723 };
2724
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002725 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002726 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2727 SetPackedField<BoundaryKindField>(kind);
2728 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002729
2730 bool IsControlFlow() const OVERRIDE { return true; }
2731
2732 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002733 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002734
David Brazdild26a4112015-11-10 11:07:31 +00002735 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2736 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2737 }
2738
David Brazdilfc6a86a2015-06-26 10:33:45 +00002739 // Returns whether `handler` is among its exception handlers (non-zero index
2740 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002741 bool HasExceptionHandler(const HBasicBlock& handler) const {
2742 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002743 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002744 }
2745
2746 // If not present already, adds `handler` to its block's list of exception
2747 // handlers.
2748 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002749 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002750 GetBlock()->AddSuccessor(handler);
2751 }
2752 }
2753
Vladimir Markoa1de9182016-02-25 11:37:38 +00002754 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2755 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002756
David Brazdilffee3d32015-07-06 11:48:53 +01002757 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2758
David Brazdilfc6a86a2015-06-26 10:33:45 +00002759 DECLARE_INSTRUCTION(TryBoundary);
2760
2761 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002762 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2763 static constexpr size_t kFieldBoundaryKindSize =
2764 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2765 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2766 kFieldBoundaryKind + kFieldBoundaryKindSize;
2767 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2768 "Too many packed fields.");
2769 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002770
2771 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2772};
2773
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002774// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002775class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002776 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002777 // We set CanTriggerGC to prevent any intermediate address to be live
2778 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002779 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002780 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002781 SetRawInputAt(0, cond);
2782 }
2783
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002784 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002785 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002786 return true;
2787 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002788 bool NeedsEnvironment() const OVERRIDE { return true; }
2789 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002790
2791 DECLARE_INSTRUCTION(Deoptimize);
2792
2793 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002794 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2795};
2796
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002797// Represents the ArtMethod that was passed as a first argument to
2798// the method. It is used by instructions that depend on it, like
2799// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002800class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002801 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002802 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2803 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002804
2805 DECLARE_INSTRUCTION(CurrentMethod);
2806
2807 private:
2808 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2809};
2810
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002811// Fetches an ArtMethod from the virtual table or the interface method table
2812// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002813class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002814 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002815 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002816 kVTable,
2817 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002818 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002819 };
2820 HClassTableGet(HInstruction* cls,
2821 Primitive::Type type,
2822 TableKind kind,
2823 size_t index,
2824 uint32_t dex_pc)
2825 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002826 index_(index) {
2827 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002828 SetRawInputAt(0, cls);
2829 }
2830
2831 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002832 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002833 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002834 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002835 }
2836
Vladimir Markoa1de9182016-02-25 11:37:38 +00002837 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002838 size_t GetIndex() const { return index_; }
2839
2840 DECLARE_INSTRUCTION(ClassTableGet);
2841
2842 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002843 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2844 static constexpr size_t kFieldTableKindSize =
2845 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2846 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2847 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2848 "Too many packed fields.");
2849 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2850
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002851 // The index of the ArtMethod in the table.
2852 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002853
2854 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2855};
2856
Mark Mendellfe57faa2015-09-18 09:26:15 -04002857// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2858// have one successor for each entry in the switch table, and the final successor
2859// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002860class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002861 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002862 HPackedSwitch(int32_t start_value,
2863 uint32_t num_entries,
2864 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002865 uint32_t dex_pc = kNoDexPc)
2866 : HTemplateInstruction(SideEffects::None(), dex_pc),
2867 start_value_(start_value),
2868 num_entries_(num_entries) {
2869 SetRawInputAt(0, input);
2870 }
2871
2872 bool IsControlFlow() const OVERRIDE { return true; }
2873
2874 int32_t GetStartValue() const { return start_value_; }
2875
Vladimir Marko211c2112015-09-24 16:52:33 +01002876 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002877
2878 HBasicBlock* GetDefaultBlock() const {
2879 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002880 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002881 }
2882 DECLARE_INSTRUCTION(PackedSwitch);
2883
2884 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002885 const int32_t start_value_;
2886 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002887
2888 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2889};
2890
Roland Levillain88cb1752014-10-20 16:36:47 +01002891class HUnaryOperation : public HExpression<1> {
2892 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002893 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2894 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002895 SetRawInputAt(0, input);
2896 }
2897
2898 HInstruction* GetInput() const { return InputAt(0); }
2899 Primitive::Type GetResultType() const { return GetType(); }
2900
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002901 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002902 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002903 return true;
2904 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002905
Roland Levillain31dd3d62016-02-16 12:21:02 +00002906 // Try to statically evaluate `this` and return a HConstant
2907 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002908 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002909 HConstant* TryStaticEvaluation() const;
2910
2911 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002912 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2913 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002914 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2915 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002916
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002917 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002918
2919 private:
2920 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2921};
2922
Dave Allison20dfc792014-06-16 20:44:29 -07002923class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002924 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002925 HBinaryOperation(Primitive::Type result_type,
2926 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002927 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002928 SideEffects side_effects = SideEffects::None(),
2929 uint32_t dex_pc = kNoDexPc)
2930 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002931 SetRawInputAt(0, left);
2932 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002933 }
2934
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002935 HInstruction* GetLeft() const { return InputAt(0); }
2936 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002937 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002938
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002939 virtual bool IsCommutative() const { return false; }
2940
2941 // Put constant on the right.
2942 // Returns whether order is changed.
2943 bool OrderInputsWithConstantOnTheRight() {
2944 HInstruction* left = InputAt(0);
2945 HInstruction* right = InputAt(1);
2946 if (left->IsConstant() && !right->IsConstant()) {
2947 ReplaceInput(right, 0);
2948 ReplaceInput(left, 1);
2949 return true;
2950 }
2951 return false;
2952 }
2953
2954 // Order inputs by instruction id, but favor constant on the right side.
2955 // This helps GVN for commutative ops.
2956 void OrderInputs() {
2957 DCHECK(IsCommutative());
2958 HInstruction* left = InputAt(0);
2959 HInstruction* right = InputAt(1);
2960 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2961 return;
2962 }
2963 if (OrderInputsWithConstantOnTheRight()) {
2964 return;
2965 }
2966 // Order according to instruction id.
2967 if (left->GetId() > right->GetId()) {
2968 ReplaceInput(right, 0);
2969 ReplaceInput(left, 1);
2970 }
2971 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002972
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002973 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002974 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002975 return true;
2976 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002977
Roland Levillain31dd3d62016-02-16 12:21:02 +00002978 // Try to statically evaluate `this` and return a HConstant
2979 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002980 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002981 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002982
2983 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002984 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2985 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002986 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2987 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002988 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002989 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2990 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002991 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2992 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002993 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2994 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01002995 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002996 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2997 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002998
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002999 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003000 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003001 HConstant* GetConstantRight() const;
3002
3003 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003004 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003005 HInstruction* GetLeastConstantLeft() const;
3006
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003007 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003008
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003009 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003010 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3011};
3012
Mark Mendellc4701932015-04-10 13:18:51 -04003013// The comparison bias applies for floating point operations and indicates how NaN
3014// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003015enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003016 kNoBias, // bias is not applicable (i.e. for long operation)
3017 kGtBias, // return 1 for NaN comparisons
3018 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003019 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003020};
3021
Roland Levillain31dd3d62016-02-16 12:21:02 +00003022std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3023
Dave Allison20dfc792014-06-16 20:44:29 -07003024class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003025 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003026 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003027 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3028 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3029 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003030
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003031 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003032 // `instruction`, and disregard moves in between.
3033 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003034
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003035 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003036
3037 virtual IfCondition GetCondition() const = 0;
3038
Mark Mendellc4701932015-04-10 13:18:51 -04003039 virtual IfCondition GetOppositeCondition() const = 0;
3040
Vladimir Markoa1de9182016-02-25 11:37:38 +00003041 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003042 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3043
Vladimir Markoa1de9182016-02-25 11:37:38 +00003044 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3045 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003046
Vladimir Marko372f10e2016-05-17 16:30:10 +01003047 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003048 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003049 }
3050
Roland Levillain4fa13f62015-07-06 18:11:54 +01003051 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003052 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003053 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003054 if (if_cond == kCondNE) {
3055 return true;
3056 } else if (if_cond == kCondEQ) {
3057 return false;
3058 }
3059 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003060 }
3061
3062 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003063 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003064 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003065 if (if_cond == kCondEQ) {
3066 return true;
3067 } else if (if_cond == kCondNE) {
3068 return false;
3069 }
3070 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003071 }
3072
Roland Levillain31dd3d62016-02-16 12:21:02 +00003073 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003074 // Needed if we merge a HCompare into a HCondition.
3075 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3076 static constexpr size_t kFieldComparisonBiasSize =
3077 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3078 static constexpr size_t kNumberOfConditionPackedBits =
3079 kFieldComparisonBias + kFieldComparisonBiasSize;
3080 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3081 using ComparisonBiasField =
3082 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3083
Roland Levillain31dd3d62016-02-16 12:21:02 +00003084 template <typename T>
3085 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3086
3087 template <typename T>
3088 int32_t CompareFP(T x, T y) const {
3089 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3090 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3091 // Handle the bias.
3092 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3093 }
3094
3095 // Return an integer constant containing the result of a condition evaluated at compile time.
3096 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3097 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3098 }
3099
Dave Allison20dfc792014-06-16 20:44:29 -07003100 private:
3101 DISALLOW_COPY_AND_ASSIGN(HCondition);
3102};
3103
3104// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003105class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003106 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003107 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3108 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003109
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003110 bool IsCommutative() const OVERRIDE { return true; }
3111
Vladimir Marko9e23df52015-11-10 17:14:35 +00003112 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3113 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003114 return MakeConstantCondition(true, GetDexPc());
3115 }
3116 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3117 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3118 }
3119 // In the following Evaluate methods, a HCompare instruction has
3120 // been merged into this HEqual instruction; evaluate it as
3121 // `Compare(x, y) == 0`.
3122 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3123 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3124 GetDexPc());
3125 }
3126 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3127 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3128 }
3129 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3130 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003131 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003132
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003133 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003134
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003135 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003136 return kCondEQ;
3137 }
3138
Mark Mendellc4701932015-04-10 13:18:51 -04003139 IfCondition GetOppositeCondition() const OVERRIDE {
3140 return kCondNE;
3141 }
3142
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003143 private:
Aart Bike9f37602015-10-09 11:15:55 -07003144 template <typename T> bool Compute(T x, T y) const { return x == y; }
3145
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003146 DISALLOW_COPY_AND_ASSIGN(HEqual);
3147};
3148
Vladimir Markofcb503c2016-05-18 12:48:17 +01003149class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003150 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003151 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3152 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003153
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003154 bool IsCommutative() const OVERRIDE { return true; }
3155
Vladimir Marko9e23df52015-11-10 17:14:35 +00003156 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3157 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003158 return MakeConstantCondition(false, GetDexPc());
3159 }
3160 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3161 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3162 }
3163 // In the following Evaluate methods, a HCompare instruction has
3164 // been merged into this HNotEqual instruction; evaluate it as
3165 // `Compare(x, y) != 0`.
3166 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3167 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3168 }
3169 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3170 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3171 }
3172 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3173 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003174 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003175
Dave Allison20dfc792014-06-16 20:44:29 -07003176 DECLARE_INSTRUCTION(NotEqual);
3177
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003178 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003179 return kCondNE;
3180 }
3181
Mark Mendellc4701932015-04-10 13:18:51 -04003182 IfCondition GetOppositeCondition() const OVERRIDE {
3183 return kCondEQ;
3184 }
3185
Dave Allison20dfc792014-06-16 20:44:29 -07003186 private:
Aart Bike9f37602015-10-09 11:15:55 -07003187 template <typename T> bool Compute(T x, T y) const { return x != y; }
3188
Dave Allison20dfc792014-06-16 20:44:29 -07003189 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3190};
3191
Vladimir Markofcb503c2016-05-18 12:48:17 +01003192class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003193 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003194 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3195 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003196
Roland Levillain9867bc72015-08-05 10:21:34 +01003197 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003198 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003199 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003200 // In the following Evaluate methods, a HCompare instruction has
3201 // been merged into this HLessThan instruction; evaluate it as
3202 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003203 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003204 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3205 }
3206 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3207 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3208 }
3209 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3210 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003211 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003212
Dave Allison20dfc792014-06-16 20:44:29 -07003213 DECLARE_INSTRUCTION(LessThan);
3214
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003215 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003216 return kCondLT;
3217 }
3218
Mark Mendellc4701932015-04-10 13:18:51 -04003219 IfCondition GetOppositeCondition() const OVERRIDE {
3220 return kCondGE;
3221 }
3222
Dave Allison20dfc792014-06-16 20:44:29 -07003223 private:
Aart Bike9f37602015-10-09 11:15:55 -07003224 template <typename T> bool Compute(T x, T y) const { return x < y; }
3225
Dave Allison20dfc792014-06-16 20:44:29 -07003226 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3227};
3228
Vladimir Markofcb503c2016-05-18 12:48:17 +01003229class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003230 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003231 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3232 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003233
Roland Levillain9867bc72015-08-05 10:21:34 +01003234 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003235 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003236 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003237 // In the following Evaluate methods, a HCompare instruction has
3238 // been merged into this HLessThanOrEqual instruction; evaluate it as
3239 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003240 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003241 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3242 }
3243 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3244 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3245 }
3246 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3247 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003248 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003249
Dave Allison20dfc792014-06-16 20:44:29 -07003250 DECLARE_INSTRUCTION(LessThanOrEqual);
3251
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003252 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003253 return kCondLE;
3254 }
3255
Mark Mendellc4701932015-04-10 13:18:51 -04003256 IfCondition GetOppositeCondition() const OVERRIDE {
3257 return kCondGT;
3258 }
3259
Dave Allison20dfc792014-06-16 20:44:29 -07003260 private:
Aart Bike9f37602015-10-09 11:15:55 -07003261 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3262
Dave Allison20dfc792014-06-16 20:44:29 -07003263 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3264};
3265
Vladimir Markofcb503c2016-05-18 12:48:17 +01003266class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003267 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003268 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3269 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003270
Roland Levillain9867bc72015-08-05 10:21:34 +01003271 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003272 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003273 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003274 // In the following Evaluate methods, a HCompare instruction has
3275 // been merged into this HGreaterThan instruction; evaluate it as
3276 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003277 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003278 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3279 }
3280 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3281 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3282 }
3283 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3284 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003285 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003286
Dave Allison20dfc792014-06-16 20:44:29 -07003287 DECLARE_INSTRUCTION(GreaterThan);
3288
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003289 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003290 return kCondGT;
3291 }
3292
Mark Mendellc4701932015-04-10 13:18:51 -04003293 IfCondition GetOppositeCondition() const OVERRIDE {
3294 return kCondLE;
3295 }
3296
Dave Allison20dfc792014-06-16 20:44:29 -07003297 private:
Aart Bike9f37602015-10-09 11:15:55 -07003298 template <typename T> bool Compute(T x, T y) const { return x > y; }
3299
Dave Allison20dfc792014-06-16 20:44:29 -07003300 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3301};
3302
Vladimir Markofcb503c2016-05-18 12:48:17 +01003303class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003304 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003305 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3306 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003307
Roland Levillain9867bc72015-08-05 10:21:34 +01003308 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003309 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003310 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003311 // In the following Evaluate methods, a HCompare instruction has
3312 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3313 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003314 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003315 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3316 }
3317 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3318 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3319 }
3320 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3321 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003322 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003323
Dave Allison20dfc792014-06-16 20:44:29 -07003324 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3325
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003326 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003327 return kCondGE;
3328 }
3329
Mark Mendellc4701932015-04-10 13:18:51 -04003330 IfCondition GetOppositeCondition() const OVERRIDE {
3331 return kCondLT;
3332 }
3333
Dave Allison20dfc792014-06-16 20:44:29 -07003334 private:
Aart Bike9f37602015-10-09 11:15:55 -07003335 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3336
Dave Allison20dfc792014-06-16 20:44:29 -07003337 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3338};
3339
Vladimir Markofcb503c2016-05-18 12:48:17 +01003340class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003341 public:
3342 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3343 : HCondition(first, second, dex_pc) {}
3344
3345 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003346 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003347 }
3348 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003349 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3350 }
3351 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3352 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3353 LOG(FATAL) << DebugName() << " is not defined for float values";
3354 UNREACHABLE();
3355 }
3356 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3357 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3358 LOG(FATAL) << DebugName() << " is not defined for double values";
3359 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003360 }
3361
3362 DECLARE_INSTRUCTION(Below);
3363
3364 IfCondition GetCondition() const OVERRIDE {
3365 return kCondB;
3366 }
3367
3368 IfCondition GetOppositeCondition() const OVERRIDE {
3369 return kCondAE;
3370 }
3371
3372 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003373 template <typename T> bool Compute(T x, T y) const {
3374 return MakeUnsigned(x) < MakeUnsigned(y);
3375 }
Aart Bike9f37602015-10-09 11:15:55 -07003376
3377 DISALLOW_COPY_AND_ASSIGN(HBelow);
3378};
3379
Vladimir Markofcb503c2016-05-18 12:48:17 +01003380class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003381 public:
3382 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3383 : HCondition(first, second, dex_pc) {}
3384
3385 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003386 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003387 }
3388 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003389 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3390 }
3391 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3392 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3393 LOG(FATAL) << DebugName() << " is not defined for float values";
3394 UNREACHABLE();
3395 }
3396 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3397 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3398 LOG(FATAL) << DebugName() << " is not defined for double values";
3399 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003400 }
3401
3402 DECLARE_INSTRUCTION(BelowOrEqual);
3403
3404 IfCondition GetCondition() const OVERRIDE {
3405 return kCondBE;
3406 }
3407
3408 IfCondition GetOppositeCondition() const OVERRIDE {
3409 return kCondA;
3410 }
3411
3412 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003413 template <typename T> bool Compute(T x, T y) const {
3414 return MakeUnsigned(x) <= MakeUnsigned(y);
3415 }
Aart Bike9f37602015-10-09 11:15:55 -07003416
3417 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3418};
3419
Vladimir Markofcb503c2016-05-18 12:48:17 +01003420class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003421 public:
3422 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3423 : HCondition(first, second, dex_pc) {}
3424
3425 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003426 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003427 }
3428 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003429 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3430 }
3431 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3432 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3433 LOG(FATAL) << DebugName() << " is not defined for float values";
3434 UNREACHABLE();
3435 }
3436 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3437 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3438 LOG(FATAL) << DebugName() << " is not defined for double values";
3439 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003440 }
3441
3442 DECLARE_INSTRUCTION(Above);
3443
3444 IfCondition GetCondition() const OVERRIDE {
3445 return kCondA;
3446 }
3447
3448 IfCondition GetOppositeCondition() const OVERRIDE {
3449 return kCondBE;
3450 }
3451
3452 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003453 template <typename T> bool Compute(T x, T y) const {
3454 return MakeUnsigned(x) > MakeUnsigned(y);
3455 }
Aart Bike9f37602015-10-09 11:15:55 -07003456
3457 DISALLOW_COPY_AND_ASSIGN(HAbove);
3458};
3459
Vladimir Markofcb503c2016-05-18 12:48:17 +01003460class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003461 public:
3462 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3463 : HCondition(first, second, dex_pc) {}
3464
3465 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003466 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003467 }
3468 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003469 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3470 }
3471 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3472 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3473 LOG(FATAL) << DebugName() << " is not defined for float values";
3474 UNREACHABLE();
3475 }
3476 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3477 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3478 LOG(FATAL) << DebugName() << " is not defined for double values";
3479 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003480 }
3481
3482 DECLARE_INSTRUCTION(AboveOrEqual);
3483
3484 IfCondition GetCondition() const OVERRIDE {
3485 return kCondAE;
3486 }
3487
3488 IfCondition GetOppositeCondition() const OVERRIDE {
3489 return kCondB;
3490 }
3491
3492 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003493 template <typename T> bool Compute(T x, T y) const {
3494 return MakeUnsigned(x) >= MakeUnsigned(y);
3495 }
Aart Bike9f37602015-10-09 11:15:55 -07003496
3497 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3498};
Dave Allison20dfc792014-06-16 20:44:29 -07003499
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003500// Instruction to check how two inputs compare to each other.
3501// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003502class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003503 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003504 // Note that `comparison_type` is the type of comparison performed
3505 // between the comparison's inputs, not the type of the instantiated
3506 // HCompare instruction (which is always Primitive::kPrimInt).
3507 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003508 HInstruction* first,
3509 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003510 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003511 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003512 : HBinaryOperation(Primitive::kPrimInt,
3513 first,
3514 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003515 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003516 dex_pc) {
3517 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003518 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3519 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003520 }
3521
Roland Levillain9867bc72015-08-05 10:21:34 +01003522 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003523 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3524
3525 template <typename T>
3526 int32_t ComputeFP(T x, T y) const {
3527 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3528 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3529 // Handle the bias.
3530 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3531 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003532
Roland Levillain9867bc72015-08-05 10:21:34 +01003533 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003534 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3535 // reach this code path when processing a freshly built HIR
3536 // graph. However HCompare integer instructions can be synthesized
3537 // by the instruction simplifier to implement IntegerCompare and
3538 // IntegerSignum intrinsics, so we have to handle this case.
3539 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003540 }
3541 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003542 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3543 }
3544 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3545 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3546 }
3547 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3548 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003549 }
3550
Vladimir Marko372f10e2016-05-17 16:30:10 +01003551 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003552 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003553 }
3554
Vladimir Markoa1de9182016-02-25 11:37:38 +00003555 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003556
Roland Levillain31dd3d62016-02-16 12:21:02 +00003557 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003558 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003559 bool IsGtBias() const {
3560 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003561 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003562 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003563
Roland Levillain1693a1f2016-03-15 14:57:31 +00003564 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3565 // Comparisons do not require a runtime call in any back end.
3566 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003567 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003568
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003569 DECLARE_INSTRUCTION(Compare);
3570
Roland Levillain31dd3d62016-02-16 12:21:02 +00003571 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003572 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3573 static constexpr size_t kFieldComparisonBiasSize =
3574 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3575 static constexpr size_t kNumberOfComparePackedBits =
3576 kFieldComparisonBias + kFieldComparisonBiasSize;
3577 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3578 using ComparisonBiasField =
3579 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3580
Roland Levillain31dd3d62016-02-16 12:21:02 +00003581 // Return an integer constant containing the result of a comparison evaluated at compile time.
3582 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3583 DCHECK(value == -1 || value == 0 || value == 1) << value;
3584 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3585 }
3586
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003587 private:
3588 DISALLOW_COPY_AND_ASSIGN(HCompare);
3589};
3590
Vladimir Markofcb503c2016-05-18 12:48:17 +01003591class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003592 public:
3593 HNewInstance(HInstruction* cls,
3594 HCurrentMethod* current_method,
3595 uint32_t dex_pc,
3596 uint16_t type_index,
3597 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003598 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003599 bool finalizable,
3600 QuickEntrypointEnum entrypoint)
3601 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3602 type_index_(type_index),
3603 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003604 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003605 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003606 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003607 SetRawInputAt(0, cls);
3608 SetRawInputAt(1, current_method);
3609 }
3610
3611 uint16_t GetTypeIndex() const { return type_index_; }
3612 const DexFile& GetDexFile() const { return dex_file_; }
3613
3614 // Calls runtime so needs an environment.
3615 bool NeedsEnvironment() const OVERRIDE { return true; }
3616
Mingyao Yang062157f2016-03-02 10:15:36 -08003617 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3618 bool CanThrow() const OVERRIDE { return true; }
3619
3620 // Needs to call into runtime to make sure it's instantiable/accessible.
3621 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003622
Vladimir Markoa1de9182016-02-25 11:37:38 +00003623 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003624
3625 bool CanBeNull() const OVERRIDE { return false; }
3626
3627 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3628
3629 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3630 entrypoint_ = entrypoint;
3631 }
3632
3633 bool IsStringAlloc() const;
3634
3635 DECLARE_INSTRUCTION(NewInstance);
3636
3637 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003638 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3639 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003640 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3641 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3642 "Too many packed fields.");
3643
David Brazdil6de19382016-01-08 17:37:10 +00003644 const uint16_t type_index_;
3645 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003646 QuickEntrypointEnum entrypoint_;
3647
3648 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3649};
3650
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003651enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003652#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3653 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003654#include "intrinsics_list.h"
3655 kNone,
3656 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3657#undef INTRINSICS_LIST
3658#undef OPTIMIZING_INTRINSICS
3659};
3660std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3661
Agi Csaki05f20562015-08-19 14:58:14 -07003662enum IntrinsicNeedsEnvironmentOrCache {
3663 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3664 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003665};
3666
Aart Bik5d75afe2015-12-14 11:57:01 -08003667enum IntrinsicSideEffects {
3668 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3669 kReadSideEffects, // Intrinsic may read heap memory.
3670 kWriteSideEffects, // Intrinsic may write heap memory.
3671 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3672};
3673
3674enum IntrinsicExceptions {
3675 kNoThrow, // Intrinsic does not throw any exceptions.
3676 kCanThrow // Intrinsic may throw exceptions.
3677};
3678
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003679class HInvoke : public HInstruction {
3680 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003681 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003682
Vladimir Marko372f10e2016-05-17 16:30:10 +01003683 using HInstruction::GetInputRecords; // Keep the const version visible.
3684 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3685 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3686 }
3687
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003688 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003689 SetRawInputAt(index, argument);
3690 }
3691
Roland Levillain3e3d7332015-04-28 11:00:54 +01003692 // Return the number of arguments. This number can be lower than
3693 // the number of inputs returned by InputCount(), as some invoke
3694 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3695 // inputs at the end of their list of inputs.
3696 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3697
Vladimir Markoa1de9182016-02-25 11:37:38 +00003698 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003699
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003700 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003701 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003702
Vladimir Markoa1de9182016-02-25 11:37:38 +00003703 InvokeType GetOriginalInvokeType() const {
3704 return GetPackedField<OriginalInvokeTypeField>();
3705 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003706
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003707 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003708 return intrinsic_;
3709 }
3710
Aart Bik5d75afe2015-12-14 11:57:01 -08003711 void SetIntrinsic(Intrinsics intrinsic,
3712 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3713 IntrinsicSideEffects side_effects,
3714 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003715
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003716 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003717 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003718 }
3719
Vladimir Markoa1de9182016-02-25 11:37:38 +00003720 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003721
3722 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3723
Vladimir Marko372f10e2016-05-17 16:30:10 +01003724 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003725 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3726 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003727
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003728 uint32_t* GetIntrinsicOptimizations() {
3729 return &intrinsic_optimizations_;
3730 }
3731
3732 const uint32_t* GetIntrinsicOptimizations() const {
3733 return &intrinsic_optimizations_;
3734 }
3735
3736 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3737
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003738 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003739
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003740 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003741 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3742 static constexpr size_t kFieldOriginalInvokeTypeSize =
3743 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3744 static constexpr size_t kFieldReturnType =
3745 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3746 static constexpr size_t kFieldReturnTypeSize =
3747 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3748 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3749 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3750 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3751 using OriginalInvokeTypeField =
3752 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3753 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3754
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003755 HInvoke(ArenaAllocator* arena,
3756 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003757 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003758 Primitive::Type return_type,
3759 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003760 uint32_t dex_method_index,
3761 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003762 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003763 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003764 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003765 inputs_(number_of_arguments + number_of_other_inputs,
3766 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003767 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003768 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003769 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003770 SetPackedField<ReturnTypeField>(return_type);
3771 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3772 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003773 }
3774
Vladimir Markoa1de9182016-02-25 11:37:38 +00003775 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003776
Roland Levillain3e3d7332015-04-28 11:00:54 +01003777 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003778 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003779 const uint32_t dex_method_index_;
3780 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003781
3782 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3783 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003784
3785 private:
3786 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3787};
3788
Vladimir Markofcb503c2016-05-18 12:48:17 +01003789class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003790 public:
3791 HInvokeUnresolved(ArenaAllocator* arena,
3792 uint32_t number_of_arguments,
3793 Primitive::Type return_type,
3794 uint32_t dex_pc,
3795 uint32_t dex_method_index,
3796 InvokeType invoke_type)
3797 : HInvoke(arena,
3798 number_of_arguments,
3799 0u /* number_of_other_inputs */,
3800 return_type,
3801 dex_pc,
3802 dex_method_index,
3803 invoke_type) {
3804 }
3805
3806 DECLARE_INSTRUCTION(InvokeUnresolved);
3807
3808 private:
3809 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3810};
3811
Vladimir Markofcb503c2016-05-18 12:48:17 +01003812class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003813 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003814 // Requirements of this method call regarding the class
3815 // initialization (clinit) check of its declaring class.
3816 enum class ClinitCheckRequirement {
3817 kNone, // Class already initialized.
3818 kExplicit, // Static call having explicit clinit check as last input.
3819 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003820 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003821 };
3822
Vladimir Marko58155012015-08-19 12:49:41 +00003823 // Determines how to load the target ArtMethod*.
3824 enum class MethodLoadKind {
3825 // Use a String init ArtMethod* loaded from Thread entrypoints.
3826 kStringInit,
3827
3828 // Use the method's own ArtMethod* loaded by the register allocator.
3829 kRecursive,
3830
3831 // Use ArtMethod* at a known address, embed the direct address in the code.
3832 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3833 kDirectAddress,
3834
3835 // Use ArtMethod* at an address that will be known at link time, embed the direct
3836 // address in the code. If the image is relocatable, emit .patch_oat entry.
3837 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3838 // the image relocatable or not.
3839 kDirectAddressWithFixup,
3840
Vladimir Markoa1de9182016-02-25 11:37:38 +00003841 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003842 // Used when we need to use the dex cache, for example for invoke-static that
3843 // may cause class initialization (the entry may point to a resolution method),
3844 // and we know that we can access the dex cache arrays using a PC-relative load.
3845 kDexCachePcRelative,
3846
3847 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3848 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3849 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3850 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3851 kDexCacheViaMethod,
3852 };
3853
3854 // Determines the location of the code pointer.
3855 enum class CodePtrLocation {
3856 // Recursive call, use local PC-relative call instruction.
3857 kCallSelf,
3858
3859 // Use PC-relative call instruction patched at link time.
3860 // Used for calls within an oat file, boot->boot or app->app.
3861 kCallPCRelative,
3862
3863 // Call to a known target address, embed the direct address in code.
3864 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3865 kCallDirect,
3866
3867 // Call to a target address that will be known at link time, embed the direct
3868 // address in code. If the image is relocatable, emit .patch_oat entry.
3869 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3870 // the image relocatable or not.
3871 kCallDirectWithFixup,
3872
3873 // Use code pointer from the ArtMethod*.
3874 // Used when we don't know the target code. This is also the last-resort-kind used when
3875 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3876 kCallArtMethod,
3877 };
3878
3879 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003880 MethodLoadKind method_load_kind;
3881 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003882 // The method load data holds
3883 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3884 // Note that there are multiple string init methods, each having its own offset.
3885 // - the method address for kDirectAddress
3886 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003887 uint64_t method_load_data;
3888 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003889 };
3890
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003891 HInvokeStaticOrDirect(ArenaAllocator* arena,
3892 uint32_t number_of_arguments,
3893 Primitive::Type return_type,
3894 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003895 uint32_t method_index,
3896 MethodReference target_method,
3897 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003898 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003899 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003900 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003901 : HInvoke(arena,
3902 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003903 // There is potentially one extra argument for the HCurrentMethod node, and
3904 // potentially one other if the clinit check is explicit, and potentially
3905 // one other if the method is a string factory.
3906 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003907 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003908 return_type,
3909 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003910 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003911 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003912 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003913 dispatch_info_(dispatch_info) {
3914 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3915 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3916 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003917
Vladimir Markodc151b22015-10-15 18:02:30 +01003918 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003919 bool had_current_method_input = HasCurrentMethodInput();
3920 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3921
3922 // Using the current method is the default and once we find a better
3923 // method load kind, we should not go back to using the current method.
3924 DCHECK(had_current_method_input || !needs_current_method_input);
3925
3926 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003927 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3928 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003929 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003930 dispatch_info_ = dispatch_info;
3931 }
3932
Vladimir Markoc53c0792015-11-19 15:48:33 +00003933 void AddSpecialInput(HInstruction* input) {
3934 // We allow only one special input.
3935 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3936 DCHECK(InputCount() == GetSpecialInputIndex() ||
3937 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3938 InsertInputAt(GetSpecialInputIndex(), input);
3939 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003940
Vladimir Marko372f10e2016-05-17 16:30:10 +01003941 using HInstruction::GetInputRecords; // Keep the const version visible.
3942 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3943 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
3944 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
3945 DCHECK(!input_records.empty());
3946 DCHECK_GT(input_records.size(), GetNumberOfArguments());
3947 HInstruction* last_input = input_records.back().GetInstruction();
3948 // Note: `last_input` may be null during arguments setup.
3949 if (last_input != nullptr) {
3950 // `last_input` is the last input of a static invoke marked as having
3951 // an explicit clinit check. It must either be:
3952 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3953 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3954 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
3955 }
3956 }
3957 return input_records;
3958 }
3959
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003960 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003961 // We access the method via the dex cache so we can't do an implicit null check.
3962 // TODO: for intrinsics we can generate implicit null checks.
3963 return false;
3964 }
3965
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003966 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003967 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003968 }
3969
Vladimir Markoc53c0792015-11-19 15:48:33 +00003970 // Get the index of the special input, if any.
3971 //
David Brazdil6de19382016-01-08 17:37:10 +00003972 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3973 // method pointer; otherwise there may be one platform-specific special input,
3974 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003975 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003976 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003977
Vladimir Markoa1de9182016-02-25 11:37:38 +00003978 InvokeType GetOptimizedInvokeType() const {
3979 return GetPackedField<OptimizedInvokeTypeField>();
3980 }
3981
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003982 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003983 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003984 }
3985
Vladimir Marko58155012015-08-19 12:49:41 +00003986 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3987 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3988 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003989 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003990 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003991 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003992 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003993 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3994 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003995 bool HasCurrentMethodInput() const {
3996 // This function can be called only after the invoke has been fully initialized by the builder.
3997 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003998 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003999 return true;
4000 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004001 DCHECK(InputCount() == GetSpecialInputIndex() ||
4002 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004003 return false;
4004 }
4005 }
Vladimir Marko58155012015-08-19 12:49:41 +00004006 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
4007 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004008 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00004009
4010 int32_t GetStringInitOffset() const {
4011 DCHECK(IsStringInit());
4012 return dispatch_info_.method_load_data;
4013 }
4014
4015 uint64_t GetMethodAddress() const {
4016 DCHECK(HasMethodAddress());
4017 return dispatch_info_.method_load_data;
4018 }
4019
4020 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004021 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004022 return dispatch_info_.method_load_data;
4023 }
4024
4025 uint64_t GetDirectCodePtr() const {
4026 DCHECK(HasDirectCodePtr());
4027 return dispatch_info_.direct_code_ptr;
4028 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004029
Vladimir Markoa1de9182016-02-25 11:37:38 +00004030 ClinitCheckRequirement GetClinitCheckRequirement() const {
4031 return GetPackedField<ClinitCheckRequirementField>();
4032 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004033
Roland Levillain4c0eb422015-04-24 16:43:49 +01004034 // Is this instruction a call to a static method?
4035 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004036 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004037 }
4038
Vladimir Markofbb184a2015-11-13 14:47:00 +00004039 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4040 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4041 // instruction; only relevant for static calls with explicit clinit check.
4042 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004043 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004044 size_t last_input_index = inputs_.size() - 1u;
4045 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004046 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004047 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004048 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004049 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004050 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004051 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004052 }
4053
4054 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004055 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004056 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004057 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004058 }
4059
4060 // Is this a call to a static method whose declaring class has an
4061 // implicit intialization check requirement?
4062 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004063 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004064 }
4065
Vladimir Markob554b5a2015-11-06 12:57:55 +00004066 // Does this method load kind need the current method as an input?
4067 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4068 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4069 }
4070
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004071 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004072
Roland Levillain4c0eb422015-04-24 16:43:49 +01004073 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004074 void InsertInputAt(size_t index, HInstruction* input);
4075 void RemoveInputAt(size_t index);
4076
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004077 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004078 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4079 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4080 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4081 static constexpr size_t kFieldClinitCheckRequirement =
4082 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4083 static constexpr size_t kFieldClinitCheckRequirementSize =
4084 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4085 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4086 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4087 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4088 "Too many packed fields.");
4089 using OptimizedInvokeTypeField =
4090 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4091 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4092 kFieldClinitCheckRequirement,
4093 kFieldClinitCheckRequirementSize>;
4094
Vladimir Marko58155012015-08-19 12:49:41 +00004095 // The target method may refer to different dex file or method index than the original
4096 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4097 // in derived class to increase visibility.
4098 MethodReference target_method_;
4099 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004100
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004101 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004102};
Vladimir Markof64242a2015-12-01 14:58:23 +00004103std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004104std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004105
Vladimir Markofcb503c2016-05-18 12:48:17 +01004106class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004107 public:
4108 HInvokeVirtual(ArenaAllocator* arena,
4109 uint32_t number_of_arguments,
4110 Primitive::Type return_type,
4111 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004112 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004113 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004114 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004115 vtable_index_(vtable_index) {}
4116
Calin Juravle641547a2015-04-21 22:08:51 +01004117 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004118 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004119 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004120 }
4121
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004122 uint32_t GetVTableIndex() const { return vtable_index_; }
4123
4124 DECLARE_INSTRUCTION(InvokeVirtual);
4125
4126 private:
4127 const uint32_t vtable_index_;
4128
4129 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4130};
4131
Vladimir Markofcb503c2016-05-18 12:48:17 +01004132class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004133 public:
4134 HInvokeInterface(ArenaAllocator* arena,
4135 uint32_t number_of_arguments,
4136 Primitive::Type return_type,
4137 uint32_t dex_pc,
4138 uint32_t dex_method_index,
4139 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004140 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004141 imt_index_(imt_index) {}
4142
Calin Juravle641547a2015-04-21 22:08:51 +01004143 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004144 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004145 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004146 }
4147
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004148 uint32_t GetImtIndex() const { return imt_index_; }
4149 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4150
4151 DECLARE_INSTRUCTION(InvokeInterface);
4152
4153 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004154 const uint32_t imt_index_;
4155
4156 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4157};
4158
Vladimir Markofcb503c2016-05-18 12:48:17 +01004159class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004160 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004161 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004162 : HUnaryOperation(result_type, input, dex_pc) {
4163 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4164 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004165
Roland Levillain9867bc72015-08-05 10:21:34 +01004166 template <typename T> T Compute(T x) const { return -x; }
4167
4168 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004169 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004170 }
4171 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004172 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004173 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004174 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4175 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4176 }
4177 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4178 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4179 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004180
Roland Levillain88cb1752014-10-20 16:36:47 +01004181 DECLARE_INSTRUCTION(Neg);
4182
4183 private:
4184 DISALLOW_COPY_AND_ASSIGN(HNeg);
4185};
4186
Vladimir Markofcb503c2016-05-18 12:48:17 +01004187class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004188 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004189 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004190 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004191 uint32_t dex_pc,
4192 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004193 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004194 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004195 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004196 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004197 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004198 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004199 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004200 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004201 }
4202
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004203 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004204 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004205
4206 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004207 bool NeedsEnvironment() const OVERRIDE { return true; }
4208
Mingyao Yang0c365e62015-03-31 15:09:29 -07004209 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4210 bool CanThrow() const OVERRIDE { return true; }
4211
Calin Juravle10e244f2015-01-26 18:54:32 +00004212 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004213
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004214 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4215
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004216 DECLARE_INSTRUCTION(NewArray);
4217
4218 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004219 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004220 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004221 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004222
4223 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4224};
4225
Vladimir Markofcb503c2016-05-18 12:48:17 +01004226class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004227 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004228 HAdd(Primitive::Type result_type,
4229 HInstruction* left,
4230 HInstruction* right,
4231 uint32_t dex_pc = kNoDexPc)
4232 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004233
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004234 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004235
Roland Levillain9867bc72015-08-05 10:21:34 +01004236 template <typename T> T Compute(T x, T y) const { return x + y; }
4237
4238 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004239 return GetBlock()->GetGraph()->GetIntConstant(
4240 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004241 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004242 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004243 return GetBlock()->GetGraph()->GetLongConstant(
4244 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004245 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004246 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4247 return GetBlock()->GetGraph()->GetFloatConstant(
4248 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4249 }
4250 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4251 return GetBlock()->GetGraph()->GetDoubleConstant(
4252 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4253 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004254
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004255 DECLARE_INSTRUCTION(Add);
4256
4257 private:
4258 DISALLOW_COPY_AND_ASSIGN(HAdd);
4259};
4260
Vladimir Markofcb503c2016-05-18 12:48:17 +01004261class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004262 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004263 HSub(Primitive::Type result_type,
4264 HInstruction* left,
4265 HInstruction* right,
4266 uint32_t dex_pc = kNoDexPc)
4267 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004268
Roland Levillain9867bc72015-08-05 10:21:34 +01004269 template <typename T> T Compute(T x, T y) const { return x - y; }
4270
4271 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004272 return GetBlock()->GetGraph()->GetIntConstant(
4273 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004274 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004275 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004276 return GetBlock()->GetGraph()->GetLongConstant(
4277 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004278 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004279 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4280 return GetBlock()->GetGraph()->GetFloatConstant(
4281 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4282 }
4283 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4284 return GetBlock()->GetGraph()->GetDoubleConstant(
4285 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4286 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004287
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004288 DECLARE_INSTRUCTION(Sub);
4289
4290 private:
4291 DISALLOW_COPY_AND_ASSIGN(HSub);
4292};
4293
Vladimir Markofcb503c2016-05-18 12:48:17 +01004294class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004295 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004296 HMul(Primitive::Type result_type,
4297 HInstruction* left,
4298 HInstruction* right,
4299 uint32_t dex_pc = kNoDexPc)
4300 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004301
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004302 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004303
Roland Levillain9867bc72015-08-05 10:21:34 +01004304 template <typename T> T Compute(T x, T y) const { return x * y; }
4305
4306 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004307 return GetBlock()->GetGraph()->GetIntConstant(
4308 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004309 }
4310 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004311 return GetBlock()->GetGraph()->GetLongConstant(
4312 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004313 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004314 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4315 return GetBlock()->GetGraph()->GetFloatConstant(
4316 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4317 }
4318 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4319 return GetBlock()->GetGraph()->GetDoubleConstant(
4320 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4321 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004322
4323 DECLARE_INSTRUCTION(Mul);
4324
4325 private:
4326 DISALLOW_COPY_AND_ASSIGN(HMul);
4327};
4328
Vladimir Markofcb503c2016-05-18 12:48:17 +01004329class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004330 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004331 HDiv(Primitive::Type result_type,
4332 HInstruction* left,
4333 HInstruction* right,
4334 uint32_t dex_pc)
4335 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004336
Roland Levillain9867bc72015-08-05 10:21:34 +01004337 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004338 T ComputeIntegral(T x, T y) const {
4339 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004340 // Our graph structure ensures we never have 0 for `y` during
4341 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004342 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004343 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004344 return (y == -1) ? -x : x / y;
4345 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004346
Roland Levillain31dd3d62016-02-16 12:21:02 +00004347 template <typename T>
4348 T ComputeFP(T x, T y) const {
4349 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4350 return x / y;
4351 }
4352
Roland Levillain9867bc72015-08-05 10:21:34 +01004353 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004354 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004355 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004356 }
4357 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004358 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004359 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4360 }
4361 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4362 return GetBlock()->GetGraph()->GetFloatConstant(
4363 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4364 }
4365 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4366 return GetBlock()->GetGraph()->GetDoubleConstant(
4367 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004368 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004369
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004370 static SideEffects SideEffectsForArchRuntimeCalls() {
4371 // The generated code can use a runtime call.
4372 return SideEffects::CanTriggerGC();
4373 }
4374
Calin Juravle7c4954d2014-10-28 16:57:40 +00004375 DECLARE_INSTRUCTION(Div);
4376
4377 private:
4378 DISALLOW_COPY_AND_ASSIGN(HDiv);
4379};
4380
Vladimir Markofcb503c2016-05-18 12:48:17 +01004381class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004382 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004383 HRem(Primitive::Type result_type,
4384 HInstruction* left,
4385 HInstruction* right,
4386 uint32_t dex_pc)
4387 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004388
Roland Levillain9867bc72015-08-05 10:21:34 +01004389 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004390 T ComputeIntegral(T x, T y) const {
4391 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004392 // Our graph structure ensures we never have 0 for `y` during
4393 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004394 DCHECK_NE(y, 0);
4395 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4396 return (y == -1) ? 0 : x % y;
4397 }
4398
Roland Levillain31dd3d62016-02-16 12:21:02 +00004399 template <typename T>
4400 T ComputeFP(T x, T y) const {
4401 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4402 return std::fmod(x, y);
4403 }
4404
Roland Levillain9867bc72015-08-05 10:21:34 +01004405 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004406 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004407 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004408 }
4409 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004410 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004411 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004412 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004413 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4414 return GetBlock()->GetGraph()->GetFloatConstant(
4415 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4416 }
4417 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4418 return GetBlock()->GetGraph()->GetDoubleConstant(
4419 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4420 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004421
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004422 static SideEffects SideEffectsForArchRuntimeCalls() {
4423 return SideEffects::CanTriggerGC();
4424 }
4425
Calin Juravlebacfec32014-11-14 15:54:36 +00004426 DECLARE_INSTRUCTION(Rem);
4427
4428 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004429 DISALLOW_COPY_AND_ASSIGN(HRem);
4430};
4431
Vladimir Markofcb503c2016-05-18 12:48:17 +01004432class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004433 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004434 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4435 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004436 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004437 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004438 SetRawInputAt(0, value);
4439 }
4440
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004441 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4442
Calin Juravled0d48522014-11-04 16:40:20 +00004443 bool CanBeMoved() const OVERRIDE { return true; }
4444
Vladimir Marko372f10e2016-05-17 16:30:10 +01004445 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004446 return true;
4447 }
4448
4449 bool NeedsEnvironment() const OVERRIDE { return true; }
4450 bool CanThrow() const OVERRIDE { return true; }
4451
Calin Juravled0d48522014-11-04 16:40:20 +00004452 DECLARE_INSTRUCTION(DivZeroCheck);
4453
4454 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004455 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4456};
4457
Vladimir Markofcb503c2016-05-18 12:48:17 +01004458class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004459 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004460 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004461 HInstruction* value,
4462 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004463 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004464 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4465 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4466 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004467 }
4468
Roland Levillain5b5b9312016-03-22 14:57:31 +00004469 template <typename T>
4470 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4471 return value << (distance & max_shift_distance);
4472 }
4473
4474 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004475 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004476 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004477 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004478 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004479 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004480 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004481 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004482 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4483 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4484 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4485 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004486 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004487 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4488 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004489 LOG(FATAL) << DebugName() << " is not defined for float values";
4490 UNREACHABLE();
4491 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004492 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4493 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004494 LOG(FATAL) << DebugName() << " is not defined for double values";
4495 UNREACHABLE();
4496 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004497
4498 DECLARE_INSTRUCTION(Shl);
4499
4500 private:
4501 DISALLOW_COPY_AND_ASSIGN(HShl);
4502};
4503
Vladimir Markofcb503c2016-05-18 12:48:17 +01004504class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004505 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004506 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004507 HInstruction* value,
4508 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004509 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004510 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4511 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4512 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004513 }
4514
Roland Levillain5b5b9312016-03-22 14:57:31 +00004515 template <typename T>
4516 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4517 return value >> (distance & max_shift_distance);
4518 }
4519
4520 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004521 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004522 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004523 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004524 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004525 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004526 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004527 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004528 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4529 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4530 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4531 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004532 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004533 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4534 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004535 LOG(FATAL) << DebugName() << " is not defined for float values";
4536 UNREACHABLE();
4537 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004538 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4539 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004540 LOG(FATAL) << DebugName() << " is not defined for double values";
4541 UNREACHABLE();
4542 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004543
4544 DECLARE_INSTRUCTION(Shr);
4545
4546 private:
4547 DISALLOW_COPY_AND_ASSIGN(HShr);
4548};
4549
Vladimir Markofcb503c2016-05-18 12:48:17 +01004550class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004551 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004552 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004553 HInstruction* value,
4554 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004555 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004556 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4557 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4558 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004559 }
4560
Roland Levillain5b5b9312016-03-22 14:57:31 +00004561 template <typename T>
4562 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4563 typedef typename std::make_unsigned<T>::type V;
4564 V ux = static_cast<V>(value);
4565 return static_cast<T>(ux >> (distance & max_shift_distance));
4566 }
4567
4568 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004569 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004570 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004571 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004572 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004573 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004574 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004575 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004576 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4577 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4578 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4579 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004580 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004581 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4582 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004583 LOG(FATAL) << DebugName() << " is not defined for float values";
4584 UNREACHABLE();
4585 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004586 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4587 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004588 LOG(FATAL) << DebugName() << " is not defined for double values";
4589 UNREACHABLE();
4590 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004591
4592 DECLARE_INSTRUCTION(UShr);
4593
4594 private:
4595 DISALLOW_COPY_AND_ASSIGN(HUShr);
4596};
4597
Vladimir Markofcb503c2016-05-18 12:48:17 +01004598class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004599 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004600 HAnd(Primitive::Type result_type,
4601 HInstruction* left,
4602 HInstruction* right,
4603 uint32_t dex_pc = kNoDexPc)
4604 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004605
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004606 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004607
Roland Levillaine53bd812016-02-24 14:54:18 +00004608 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004609
4610 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004611 return GetBlock()->GetGraph()->GetIntConstant(
4612 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004613 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004614 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004615 return GetBlock()->GetGraph()->GetLongConstant(
4616 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004617 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004618 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4619 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4620 LOG(FATAL) << DebugName() << " is not defined for float values";
4621 UNREACHABLE();
4622 }
4623 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4624 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4625 LOG(FATAL) << DebugName() << " is not defined for double values";
4626 UNREACHABLE();
4627 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004628
4629 DECLARE_INSTRUCTION(And);
4630
4631 private:
4632 DISALLOW_COPY_AND_ASSIGN(HAnd);
4633};
4634
Vladimir Markofcb503c2016-05-18 12:48:17 +01004635class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004636 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004637 HOr(Primitive::Type result_type,
4638 HInstruction* left,
4639 HInstruction* right,
4640 uint32_t dex_pc = kNoDexPc)
4641 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004642
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004643 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004644
Roland Levillaine53bd812016-02-24 14:54:18 +00004645 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004646
4647 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004648 return GetBlock()->GetGraph()->GetIntConstant(
4649 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004650 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004651 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004652 return GetBlock()->GetGraph()->GetLongConstant(
4653 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004654 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004655 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4656 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4657 LOG(FATAL) << DebugName() << " is not defined for float values";
4658 UNREACHABLE();
4659 }
4660 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4661 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4662 LOG(FATAL) << DebugName() << " is not defined for double values";
4663 UNREACHABLE();
4664 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004665
4666 DECLARE_INSTRUCTION(Or);
4667
4668 private:
4669 DISALLOW_COPY_AND_ASSIGN(HOr);
4670};
4671
Vladimir Markofcb503c2016-05-18 12:48:17 +01004672class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004673 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004674 HXor(Primitive::Type result_type,
4675 HInstruction* left,
4676 HInstruction* right,
4677 uint32_t dex_pc = kNoDexPc)
4678 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004679
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004680 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004681
Roland Levillaine53bd812016-02-24 14:54:18 +00004682 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004683
4684 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004685 return GetBlock()->GetGraph()->GetIntConstant(
4686 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004687 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004688 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004689 return GetBlock()->GetGraph()->GetLongConstant(
4690 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004691 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004692 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4693 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4694 LOG(FATAL) << DebugName() << " is not defined for float values";
4695 UNREACHABLE();
4696 }
4697 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4698 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4699 LOG(FATAL) << DebugName() << " is not defined for double values";
4700 UNREACHABLE();
4701 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004702
4703 DECLARE_INSTRUCTION(Xor);
4704
4705 private:
4706 DISALLOW_COPY_AND_ASSIGN(HXor);
4707};
4708
Vladimir Markofcb503c2016-05-18 12:48:17 +01004709class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004710 public:
4711 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004712 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004713 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4714 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004715 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004716
Roland Levillain5b5b9312016-03-22 14:57:31 +00004717 template <typename T>
4718 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4719 typedef typename std::make_unsigned<T>::type V;
4720 V ux = static_cast<V>(value);
4721 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004722 return static_cast<T>(ux);
4723 } else {
4724 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004725 return static_cast<T>(ux >> (distance & max_shift_value)) |
4726 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004727 }
4728 }
4729
Roland Levillain5b5b9312016-03-22 14:57:31 +00004730 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004731 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004732 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004733 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004734 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004735 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004736 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004737 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004738 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4739 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4740 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4741 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004742 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004743 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4744 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004745 LOG(FATAL) << DebugName() << " is not defined for float values";
4746 UNREACHABLE();
4747 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004748 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4749 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004750 LOG(FATAL) << DebugName() << " is not defined for double values";
4751 UNREACHABLE();
4752 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004753
4754 DECLARE_INSTRUCTION(Ror);
4755
4756 private:
4757 DISALLOW_COPY_AND_ASSIGN(HRor);
4758};
4759
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004760// The value of a parameter in this method. Its location depends on
4761// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004762class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004763 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004764 HParameterValue(const DexFile& dex_file,
4765 uint16_t type_index,
4766 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004767 Primitive::Type parameter_type,
4768 bool is_this = false)
4769 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004770 dex_file_(dex_file),
4771 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004772 index_(index) {
4773 SetPackedFlag<kFlagIsThis>(is_this);
4774 SetPackedFlag<kFlagCanBeNull>(!is_this);
4775 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004776
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004777 const DexFile& GetDexFile() const { return dex_file_; }
4778 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004779 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004780 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004781
Vladimir Markoa1de9182016-02-25 11:37:38 +00004782 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4783 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004784
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004785 DECLARE_INSTRUCTION(ParameterValue);
4786
4787 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004788 // Whether or not the parameter value corresponds to 'this' argument.
4789 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4790 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4791 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4792 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4793 "Too many packed fields.");
4794
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004795 const DexFile& dex_file_;
4796 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004797 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004798 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004799 const uint8_t index_;
4800
4801 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4802};
4803
Vladimir Markofcb503c2016-05-18 12:48:17 +01004804class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004805 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004806 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4807 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004808
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004809 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004810 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004811 return true;
4812 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004813
Roland Levillain9867bc72015-08-05 10:21:34 +01004814 template <typename T> T Compute(T x) const { return ~x; }
4815
4816 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004817 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004818 }
4819 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004820 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004821 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004822 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4823 LOG(FATAL) << DebugName() << " is not defined for float values";
4824 UNREACHABLE();
4825 }
4826 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4827 LOG(FATAL) << DebugName() << " is not defined for double values";
4828 UNREACHABLE();
4829 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004830
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004831 DECLARE_INSTRUCTION(Not);
4832
4833 private:
4834 DISALLOW_COPY_AND_ASSIGN(HNot);
4835};
4836
Vladimir Markofcb503c2016-05-18 12:48:17 +01004837class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004838 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004839 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4840 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004841
4842 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004843 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004844 return true;
4845 }
4846
Roland Levillain9867bc72015-08-05 10:21:34 +01004847 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004848 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004849 return !x;
4850 }
4851
Roland Levillain9867bc72015-08-05 10:21:34 +01004852 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004853 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004854 }
4855 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4856 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004857 UNREACHABLE();
4858 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004859 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4860 LOG(FATAL) << DebugName() << " is not defined for float values";
4861 UNREACHABLE();
4862 }
4863 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4864 LOG(FATAL) << DebugName() << " is not defined for double values";
4865 UNREACHABLE();
4866 }
David Brazdil66d126e2015-04-03 16:02:44 +01004867
4868 DECLARE_INSTRUCTION(BooleanNot);
4869
4870 private:
4871 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4872};
4873
Vladimir Markofcb503c2016-05-18 12:48:17 +01004874class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004875 public:
4876 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004877 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004878 : HExpression(result_type,
4879 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4880 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004881 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004882 // Invariant: We should never generate a conversion to a Boolean value.
4883 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004884 }
4885
4886 HInstruction* GetInput() const { return InputAt(0); }
4887 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4888 Primitive::Type GetResultType() const { return GetType(); }
4889
4890 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004891 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4892 return true;
4893 }
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
Vladimir Markofcb503c2016-05-18 12:48:17 +01004919class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004920 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; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004929 bool InstructionDataEquals(const 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
Vladimir Markofcb503c2016-05-18 12:48:17 +01004981class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004982 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
Vladimir Marko372f10e2016-05-17 16:30:10 +01005007 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5008 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005009 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
Vladimir Markofcb503c2016-05-18 12:48:17 +01005033class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005034 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
Vladimir Markofcb503c2016-05-18 12:48:17 +01005084class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005085 public:
Aart Bik18b36ab2016-04-13 16:41:35 -07005086 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type, uint32_t dex_pc)
5087 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005088 SetRawInputAt(0, array);
5089 SetRawInputAt(1, index);
5090 }
5091
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005092 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005093 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005094 return true;
5095 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005096 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005097 // TODO: We can be smarter here.
5098 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5099 // which generates the implicit null check. There are cases when these can be removed
5100 // to produce better code. If we ever add optimizations to do so we should allow an
5101 // implicit check here (as long as the address falls in the first page).
5102 return false;
5103 }
5104
David Brazdil4833f5a2015-12-16 10:37:39 +00005105 bool IsEquivalentOf(HArrayGet* other) const {
5106 bool result = (GetDexPc() == other->GetDexPc());
5107 if (kIsDebugBuild && result) {
5108 DCHECK_EQ(GetBlock(), other->GetBlock());
5109 DCHECK_EQ(GetArray(), other->GetArray());
5110 DCHECK_EQ(GetIndex(), other->GetIndex());
5111 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005112 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005113 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005114 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5115 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005116 }
5117 }
5118 return result;
5119 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005120
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005121 HInstruction* GetArray() const { return InputAt(0); }
5122 HInstruction* GetIndex() const { return InputAt(1); }
5123
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005124 DECLARE_INSTRUCTION(ArrayGet);
5125
5126 private:
5127 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5128};
5129
Vladimir Markofcb503c2016-05-18 12:48:17 +01005130class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005131 public:
5132 HArraySet(HInstruction* array,
5133 HInstruction* index,
5134 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005135 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005136 uint32_t dex_pc)
5137 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005138 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5139 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5140 SetPackedFlag<kFlagValueCanBeNull>(true);
5141 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005142 SetRawInputAt(0, array);
5143 SetRawInputAt(1, index);
5144 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005145 // Make a best guess now, may be refined during SSA building.
5146 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005147 }
5148
Calin Juravle77520bc2015-01-12 18:45:46 +00005149 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005150 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005151 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005152 }
5153
Mingyao Yang81014cb2015-06-02 03:16:27 -07005154 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005155 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005156
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005157 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005158 // TODO: Same as for ArrayGet.
5159 return false;
5160 }
5161
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005162 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005163 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005164 }
5165
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005166 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005167 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005168 }
5169
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005170 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005171 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005172 }
5173
Vladimir Markoa1de9182016-02-25 11:37:38 +00005174 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5175 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5176 bool StaticTypeOfArrayIsObjectArray() const {
5177 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5178 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005179
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005180 HInstruction* GetArray() const { return InputAt(0); }
5181 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005182 HInstruction* GetValue() const { return InputAt(2); }
5183
5184 Primitive::Type GetComponentType() const {
5185 // The Dex format does not type floating point index operations. Since the
5186 // `expected_component_type_` is set during building and can therefore not
5187 // be correct, we also check what is the value type. If it is a floating
5188 // point type, we must use that type.
5189 Primitive::Type value_type = GetValue()->GetType();
5190 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5191 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005192 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005193 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005194
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005195 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005196 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005197 }
5198
Aart Bik18b36ab2016-04-13 16:41:35 -07005199 void ComputeSideEffects() {
5200 Primitive::Type type = GetComponentType();
5201 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5202 SideEffectsForArchRuntimeCalls(type)));
5203 }
5204
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005205 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5206 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5207 }
5208
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005209 DECLARE_INSTRUCTION(ArraySet);
5210
5211 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005212 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5213 static constexpr size_t kFieldExpectedComponentTypeSize =
5214 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5215 static constexpr size_t kFlagNeedsTypeCheck =
5216 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5217 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005218 // Cached information for the reference_type_info_ so that codegen
5219 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005220 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5221 static constexpr size_t kNumberOfArraySetPackedBits =
5222 kFlagStaticTypeOfArrayIsObjectArray + 1;
5223 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5224 using ExpectedComponentTypeField =
5225 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005226
5227 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5228};
5229
Vladimir Markofcb503c2016-05-18 12:48:17 +01005230class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005231 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005232 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005233 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005234 // Note that arrays do not change length, so the instruction does not
5235 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005236 SetRawInputAt(0, array);
5237 }
5238
Calin Juravle77520bc2015-01-12 18:45:46 +00005239 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005240 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005241 return true;
5242 }
Calin Juravle641547a2015-04-21 22:08:51 +01005243 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5244 return obj == InputAt(0);
5245 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005246
Vladimir Markodce016e2016-04-28 13:10:02 +01005247 void MarkAsStringLength() { SetPackedFlag<kFlagIsStringLength>(); }
5248 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5249
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005250 DECLARE_INSTRUCTION(ArrayLength);
5251
5252 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005253 // We treat a String as an array, creating the HArrayLength from String.length()
5254 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5255 // determine whether a particular HArrayLength is actually a String.length() by
5256 // looking at the type of the input but that requires holding the mutator lock, so
5257 // we prefer to use a flag, so that code generators don't need to do the locking.
5258 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5259 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5260 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5261 "Too many packed fields.");
5262
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005263 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5264};
5265
Vladimir Markofcb503c2016-05-18 12:48:17 +01005266class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005267 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005268 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5269 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005270 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005271 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005272 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005273 SetRawInputAt(0, index);
5274 SetRawInputAt(1, length);
5275 }
5276
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005277 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005278 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005279 return true;
5280 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005281
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005282 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005283
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005284 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005285
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005286 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005287
5288 DECLARE_INSTRUCTION(BoundsCheck);
5289
5290 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005291 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5292};
5293
Vladimir Markofcb503c2016-05-18 12:48:17 +01005294class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005295 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005296 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005297 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005298
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005299 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005300 return true;
5301 }
5302
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005303 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5304 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005305
5306 DECLARE_INSTRUCTION(SuspendCheck);
5307
5308 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005309 // Only used for code generation, in order to share the same slow path between back edges
5310 // of a same loop.
5311 SlowPathCode* slow_path_;
5312
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005313 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5314};
5315
David Srbecky0cf44932015-12-09 14:09:59 +00005316// Pseudo-instruction which provides the native debugger with mapping information.
5317// It ensures that we can generate line number and local variables at this point.
5318class HNativeDebugInfo : public HTemplateInstruction<0> {
5319 public:
5320 explicit HNativeDebugInfo(uint32_t dex_pc)
5321 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5322
5323 bool NeedsEnvironment() const OVERRIDE {
5324 return true;
5325 }
5326
5327 DECLARE_INSTRUCTION(NativeDebugInfo);
5328
5329 private:
5330 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5331};
5332
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005333/**
5334 * Instruction to load a Class object.
5335 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005336class HLoadClass FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005337 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005338 HLoadClass(HCurrentMethod* current_method,
5339 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005340 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005341 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005342 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005343 bool needs_access_check,
5344 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005345 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005346 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005347 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005348 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005349 // Referrers class should not need access check. We never inline unverified
5350 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005351 DCHECK(!is_referrers_class || !needs_access_check);
5352
5353 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5354 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5355 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5356 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005357 SetRawInputAt(0, current_method);
5358 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005359
5360 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005361
Vladimir Marko372f10e2016-05-17 16:30:10 +01005362 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005363 // Note that we don't need to test for generate_clinit_check_.
5364 // Whether or not we need to generate the clinit check is processed in
5365 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005366 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005367 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005368 }
5369
5370 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5371
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005372 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005373 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005374
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005375 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005376 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005377 }
5378
Calin Juravle0ba218d2015-05-19 18:46:01 +01005379 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005380 // The entrypoint the code generator is going to call does not do
5381 // clinit of the class.
5382 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005383 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005384 }
5385
5386 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005387 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005388 (!IsReferrersClass() && !IsInDexCache()) ||
5389 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005390 }
5391
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005392
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005393 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005394 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005395 }
5396
Calin Juravleacf735c2015-02-12 15:25:22 +00005397 ReferenceTypeInfo GetLoadedClassRTI() {
5398 return loaded_class_rti_;
5399 }
5400
5401 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5402 // Make sure we only set exact types (the loaded class should never be merged).
5403 DCHECK(rti.IsExact());
5404 loaded_class_rti_ = rti;
5405 }
5406
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005407 const DexFile& GetDexFile() { return dex_file_; }
5408
Vladimir Markoa1de9182016-02-25 11:37:38 +00005409 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005410
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005411 static SideEffects SideEffectsForArchRuntimeCalls() {
5412 return SideEffects::CanTriggerGC();
5413 }
5414
Vladimir Markoa1de9182016-02-25 11:37:38 +00005415 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5416 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5417 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5418 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005419
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005420 DECLARE_INSTRUCTION(LoadClass);
5421
5422 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005423 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5424 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5425 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005426 // Whether this instruction must generate the initialization check.
5427 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005428 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5429 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5430 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5431
5432 const uint16_t type_index_;
5433 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005434
Calin Juravleacf735c2015-02-12 15:25:22 +00005435 ReferenceTypeInfo loaded_class_rti_;
5436
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005437 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5438};
5439
Vladimir Marko372f10e2016-05-17 16:30:10 +01005440class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005441 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005442 // Determines how to load the String.
5443 enum class LoadKind {
5444 // Use boot image String* address that will be known at link time.
5445 // Used for boot image strings referenced by boot image code in non-PIC mode.
5446 kBootImageLinkTimeAddress,
5447
5448 // Use PC-relative boot image String* address that will be known at link time.
5449 // Used for boot image strings referenced by boot image code in PIC mode.
5450 kBootImageLinkTimePcRelative,
5451
5452 // Use a known boot image String* address, embedded in the code by the codegen.
5453 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5454 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5455 // GetIncludePatchInformation().
5456 kBootImageAddress,
5457
5458 // Load from the resolved strings array at an absolute address.
5459 // Used for strings outside the boot image referenced by JIT-compiled code.
5460 kDexCacheAddress,
5461
5462 // Load from resolved strings array in the dex cache using a PC-relative load.
5463 // Used for strings outside boot image when we know that we can access
5464 // the dex cache arrays using a PC-relative load.
5465 kDexCachePcRelative,
5466
5467 // Load from resolved strings array accessed through the class loaded from
5468 // the compiled method's own ArtMethod*. This is the default access type when
5469 // all other types are unavailable.
5470 kDexCacheViaMethod,
5471
5472 kLast = kDexCacheViaMethod
5473 };
5474
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005475 HLoadString(HCurrentMethod* current_method,
5476 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005477 const DexFile& dex_file,
5478 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005479 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5480 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005481 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005482 SetPackedFlag<kFlagIsInDexCache>(false);
5483 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5484 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005485 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005486
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005487 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5488 DCHECK(HasAddress(load_kind));
5489 load_data_.address = address;
5490 SetLoadKindInternal(load_kind);
5491 }
5492
5493 void SetLoadKindWithStringReference(LoadKind load_kind,
5494 const DexFile& dex_file,
5495 uint32_t string_index) {
5496 DCHECK(HasStringReference(load_kind));
5497 load_data_.ref.dex_file = &dex_file;
5498 string_index_ = string_index;
5499 SetLoadKindInternal(load_kind);
5500 }
5501
5502 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5503 const DexFile& dex_file,
5504 uint32_t element_index) {
5505 DCHECK(HasDexCacheReference(load_kind));
5506 load_data_.ref.dex_file = &dex_file;
5507 load_data_.ref.dex_cache_element_index = element_index;
5508 SetLoadKindInternal(load_kind);
5509 }
5510
5511 LoadKind GetLoadKind() const {
5512 return GetPackedField<LoadKindField>();
5513 }
5514
5515 const DexFile& GetDexFile() const;
5516
5517 uint32_t GetStringIndex() const {
5518 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5519 return string_index_;
5520 }
5521
5522 uint32_t GetDexCacheElementOffset() const;
5523
5524 uint64_t GetAddress() const {
5525 DCHECK(HasAddress(GetLoadKind()));
5526 return load_data_.address;
5527 }
5528
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005529 bool CanBeMoved() const OVERRIDE { return true; }
5530
Vladimir Marko372f10e2016-05-17 16:30:10 +01005531 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005532
5533 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5534
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005535 // Will call the runtime if we need to load the string through
5536 // the dex cache and the string is not guaranteed to be there yet.
5537 bool NeedsEnvironment() const OVERRIDE {
5538 LoadKind load_kind = GetLoadKind();
5539 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5540 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5541 load_kind == LoadKind::kBootImageAddress) {
5542 return false;
5543 }
5544 return !IsInDexCache();
5545 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005546
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005547 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5548 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5549 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005550
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005551 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005552 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005553
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005554 static SideEffects SideEffectsForArchRuntimeCalls() {
5555 return SideEffects::CanTriggerGC();
5556 }
5557
Vladimir Markoa1de9182016-02-25 11:37:38 +00005558 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5559
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005560 void MarkInDexCache() {
5561 SetPackedFlag<kFlagIsInDexCache>(true);
5562 DCHECK(!NeedsEnvironment());
5563 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005564 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005565 }
5566
Vladimir Marko372f10e2016-05-17 16:30:10 +01005567 void AddSpecialInput(HInstruction* special_input);
5568
5569 using HInstruction::GetInputRecords; // Keep the const version visible.
5570 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5571 return ArrayRef<HUserRecord<HInstruction*>>(
5572 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005573 }
5574
Vladimir Marko372f10e2016-05-17 16:30:10 +01005575 Primitive::Type GetType() const OVERRIDE {
5576 return Primitive::kPrimNot;
5577 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005578
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005579 DECLARE_INSTRUCTION(LoadString);
5580
5581 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005582 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005583 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5584 static constexpr size_t kFieldLoadKindSize =
5585 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5586 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005587 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005588 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005589
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005590 static bool HasStringReference(LoadKind load_kind) {
5591 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5592 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5593 load_kind == LoadKind::kDexCacheViaMethod;
5594 }
5595
5596 static bool HasAddress(LoadKind load_kind) {
5597 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5598 }
5599
5600 static bool HasDexCacheReference(LoadKind load_kind) {
5601 return load_kind == LoadKind::kDexCachePcRelative;
5602 }
5603
5604 void SetLoadKindInternal(LoadKind load_kind);
5605
Vladimir Marko372f10e2016-05-17 16:30:10 +01005606 HUserRecord<HInstruction*> special_input_;
5607
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005608 // String index serves also as the hash code and it's also needed for slow-paths,
5609 // so it must not be overwritten with other load data.
5610 uint32_t string_index_;
5611
5612 union {
5613 struct {
5614 const DexFile* dex_file; // For string reference and dex cache reference.
5615 uint32_t dex_cache_element_index; // Only for dex cache reference.
5616 } ref;
5617 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5618 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005619
5620 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5621};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005622std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5623
5624// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5625inline const DexFile& HLoadString::GetDexFile() const {
5626 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5627 << GetLoadKind();
5628 return *load_data_.ref.dex_file;
5629}
5630
5631// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5632inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5633 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5634 return load_data_.ref.dex_cache_element_index;
5635}
5636
5637// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5638inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5639 // The special input is used for PC-relative loads on some architectures.
5640 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5641 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005642 // HLoadString::GetInputRecords() returns an empty array at this point,
5643 // so use the GetInputRecords() from the base class to set the input record.
5644 DCHECK(special_input_.GetInstruction() == nullptr);
5645 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005646 special_input->AddUseAt(this, 0);
5647}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005648
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005649/**
5650 * Performs an initialization check on its Class object input.
5651 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005652class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005653 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005654 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005655 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005656 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005657 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5658 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005659 SetRawInputAt(0, constant);
5660 }
5661
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005662 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005663 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005664 return true;
5665 }
5666
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005667 bool NeedsEnvironment() const OVERRIDE {
5668 // May call runtime to initialize the class.
5669 return true;
5670 }
5671
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005672 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005673
5674 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5675
5676 DECLARE_INSTRUCTION(ClinitCheck);
5677
5678 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005679 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5680};
5681
Vladimir Markofcb503c2016-05-18 12:48:17 +01005682class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005683 public:
5684 HStaticFieldGet(HInstruction* cls,
5685 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005686 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005687 bool is_volatile,
5688 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005689 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005690 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005691 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005692 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005693 : HExpression(field_type,
5694 SideEffects::FieldReadOfType(field_type, is_volatile),
5695 dex_pc),
5696 field_info_(field_offset,
5697 field_type,
5698 is_volatile,
5699 field_idx,
5700 declaring_class_def_index,
5701 dex_file,
5702 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005703 SetRawInputAt(0, cls);
5704 }
5705
Calin Juravle52c48962014-12-16 17:02:57 +00005706
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005707 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005708
Vladimir Marko372f10e2016-05-17 16:30:10 +01005709 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5710 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005711 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005712 }
5713
5714 size_t ComputeHashCode() const OVERRIDE {
5715 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5716 }
5717
Calin Juravle52c48962014-12-16 17:02:57 +00005718 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005719 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5720 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005721 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005722
5723 DECLARE_INSTRUCTION(StaticFieldGet);
5724
5725 private:
5726 const FieldInfo field_info_;
5727
5728 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5729};
5730
Vladimir Markofcb503c2016-05-18 12:48:17 +01005731class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005732 public:
5733 HStaticFieldSet(HInstruction* cls,
5734 HInstruction* value,
5735 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005736 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005737 bool is_volatile,
5738 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005739 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005740 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005741 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005742 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005743 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5744 dex_pc),
5745 field_info_(field_offset,
5746 field_type,
5747 is_volatile,
5748 field_idx,
5749 declaring_class_def_index,
5750 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005751 dex_cache) {
5752 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005753 SetRawInputAt(0, cls);
5754 SetRawInputAt(1, value);
5755 }
5756
Calin Juravle52c48962014-12-16 17:02:57 +00005757 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005758 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5759 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005760 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005761
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005762 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005763 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5764 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005765
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005766 DECLARE_INSTRUCTION(StaticFieldSet);
5767
5768 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005769 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5770 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5771 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5772 "Too many packed fields.");
5773
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005774 const FieldInfo field_info_;
5775
5776 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5777};
5778
Vladimir Markofcb503c2016-05-18 12:48:17 +01005779class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005780 public:
5781 HUnresolvedInstanceFieldGet(HInstruction* obj,
5782 Primitive::Type field_type,
5783 uint32_t field_index,
5784 uint32_t dex_pc)
5785 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5786 field_index_(field_index) {
5787 SetRawInputAt(0, obj);
5788 }
5789
5790 bool NeedsEnvironment() const OVERRIDE { return true; }
5791 bool CanThrow() const OVERRIDE { return true; }
5792
5793 Primitive::Type GetFieldType() const { return GetType(); }
5794 uint32_t GetFieldIndex() const { return field_index_; }
5795
5796 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5797
5798 private:
5799 const uint32_t field_index_;
5800
5801 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5802};
5803
Vladimir Markofcb503c2016-05-18 12:48:17 +01005804class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005805 public:
5806 HUnresolvedInstanceFieldSet(HInstruction* obj,
5807 HInstruction* value,
5808 Primitive::Type field_type,
5809 uint32_t field_index,
5810 uint32_t dex_pc)
5811 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005812 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005813 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005814 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005815 SetRawInputAt(0, obj);
5816 SetRawInputAt(1, value);
5817 }
5818
5819 bool NeedsEnvironment() const OVERRIDE { return true; }
5820 bool CanThrow() const OVERRIDE { return true; }
5821
Vladimir Markoa1de9182016-02-25 11:37:38 +00005822 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005823 uint32_t GetFieldIndex() const { return field_index_; }
5824
5825 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5826
5827 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005828 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5829 static constexpr size_t kFieldFieldTypeSize =
5830 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5831 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5832 kFieldFieldType + kFieldFieldTypeSize;
5833 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5834 "Too many packed fields.");
5835 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5836
Calin Juravlee460d1d2015-09-29 04:52:17 +01005837 const uint32_t field_index_;
5838
5839 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5840};
5841
Vladimir Markofcb503c2016-05-18 12:48:17 +01005842class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005843 public:
5844 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5845 uint32_t field_index,
5846 uint32_t dex_pc)
5847 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5848 field_index_(field_index) {
5849 }
5850
5851 bool NeedsEnvironment() const OVERRIDE { return true; }
5852 bool CanThrow() const OVERRIDE { return true; }
5853
5854 Primitive::Type GetFieldType() const { return GetType(); }
5855 uint32_t GetFieldIndex() const { return field_index_; }
5856
5857 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5858
5859 private:
5860 const uint32_t field_index_;
5861
5862 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5863};
5864
Vladimir Markofcb503c2016-05-18 12:48:17 +01005865class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005866 public:
5867 HUnresolvedStaticFieldSet(HInstruction* value,
5868 Primitive::Type field_type,
5869 uint32_t field_index,
5870 uint32_t dex_pc)
5871 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005872 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005873 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005874 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005875 SetRawInputAt(0, value);
5876 }
5877
5878 bool NeedsEnvironment() const OVERRIDE { return true; }
5879 bool CanThrow() const OVERRIDE { return true; }
5880
Vladimir Markoa1de9182016-02-25 11:37:38 +00005881 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005882 uint32_t GetFieldIndex() const { return field_index_; }
5883
5884 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5885
5886 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005887 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5888 static constexpr size_t kFieldFieldTypeSize =
5889 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5890 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5891 kFieldFieldType + kFieldFieldTypeSize;
5892 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5893 "Too many packed fields.");
5894 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5895
Calin Juravlee460d1d2015-09-29 04:52:17 +01005896 const uint32_t field_index_;
5897
5898 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5899};
5900
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005901// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005902class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005903 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005904 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5905 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005906
David Brazdilbbd733e2015-08-18 17:48:17 +01005907 bool CanBeNull() const OVERRIDE { return false; }
5908
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005909 DECLARE_INSTRUCTION(LoadException);
5910
5911 private:
5912 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5913};
5914
David Brazdilcb1c0552015-08-04 16:22:25 +01005915// Implicit part of move-exception which clears thread-local exception storage.
5916// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005917class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01005918 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005919 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5920 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005921
5922 DECLARE_INSTRUCTION(ClearException);
5923
5924 private:
5925 DISALLOW_COPY_AND_ASSIGN(HClearException);
5926};
5927
Vladimir Markofcb503c2016-05-18 12:48:17 +01005928class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005929 public:
5930 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005931 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005932 SetRawInputAt(0, exception);
5933 }
5934
5935 bool IsControlFlow() const OVERRIDE { return true; }
5936
5937 bool NeedsEnvironment() const OVERRIDE { return true; }
5938
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005939 bool CanThrow() const OVERRIDE { return true; }
5940
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005941
5942 DECLARE_INSTRUCTION(Throw);
5943
5944 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005945 DISALLOW_COPY_AND_ASSIGN(HThrow);
5946};
5947
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005948/**
5949 * Implementation strategies for the code generator of a HInstanceOf
5950 * or `HCheckCast`.
5951 */
5952enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005953 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005954 kExactCheck, // Can do a single class compare.
5955 kClassHierarchyCheck, // Can just walk the super class chain.
5956 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5957 kInterfaceCheck, // No optimization yet when checking against an interface.
5958 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005959 kArrayCheck, // No optimization yet when checking against a generic array.
5960 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005961};
5962
Roland Levillain86503782016-02-11 19:07:30 +00005963std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5964
Vladimir Markofcb503c2016-05-18 12:48:17 +01005965class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005966 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005967 HInstanceOf(HInstruction* object,
5968 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005969 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005970 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005971 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005972 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005973 dex_pc) {
5974 SetPackedField<TypeCheckKindField>(check_kind);
5975 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005976 SetRawInputAt(0, object);
5977 SetRawInputAt(1, constant);
5978 }
5979
5980 bool CanBeMoved() const OVERRIDE { return true; }
5981
Vladimir Marko372f10e2016-05-17 16:30:10 +01005982 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005983 return true;
5984 }
5985
5986 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005987 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005988 }
5989
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005990 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005991 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5992 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5993 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5994 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005995
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005996 static bool CanCallRuntime(TypeCheckKind check_kind) {
5997 // Mips currently does runtime calls for any other checks.
5998 return check_kind != TypeCheckKind::kExactCheck;
5999 }
6000
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006001 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006002 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006003 }
6004
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006005 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006006
6007 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006008 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6009 static constexpr size_t kFieldTypeCheckKindSize =
6010 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6011 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6012 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6013 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6014 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006015
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006016 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6017};
6018
Vladimir Markofcb503c2016-05-18 12:48:17 +01006019class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006020 public:
David Brazdilf5552582015-12-27 13:36:12 +00006021 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006022 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006023 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6024 SetPackedFlag<kFlagUpperCanBeNull>(true);
6025 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006026 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006027 SetRawInputAt(0, input);
6028 }
6029
David Brazdilf5552582015-12-27 13:36:12 +00006030 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006031 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006032 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006033 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006034
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006035 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006036 DCHECK(GetUpperCanBeNull() || !can_be_null);
6037 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006038 }
6039
Vladimir Markoa1de9182016-02-25 11:37:38 +00006040 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006041
Calin Juravleb1498f62015-02-16 13:13:29 +00006042 DECLARE_INSTRUCTION(BoundType);
6043
6044 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006045 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6046 // is false then CanBeNull() cannot be true).
6047 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6048 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6049 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6050 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6051
Calin Juravleb1498f62015-02-16 13:13:29 +00006052 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006053 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6054 // It is used to bound the type in cases like:
6055 // if (x instanceof ClassX) {
6056 // // uper_bound_ will be ClassX
6057 // }
David Brazdilf5552582015-12-27 13:36:12 +00006058 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006059
6060 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6061};
6062
Vladimir Markofcb503c2016-05-18 12:48:17 +01006063class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006064 public:
6065 HCheckCast(HInstruction* object,
6066 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006067 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006068 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006069 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6070 SetPackedField<TypeCheckKindField>(check_kind);
6071 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006072 SetRawInputAt(0, object);
6073 SetRawInputAt(1, constant);
6074 }
6075
6076 bool CanBeMoved() const OVERRIDE { return true; }
6077
Vladimir Marko372f10e2016-05-17 16:30:10 +01006078 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006079 return true;
6080 }
6081
6082 bool NeedsEnvironment() const OVERRIDE {
6083 // Instruction may throw a CheckCastError.
6084 return true;
6085 }
6086
6087 bool CanThrow() const OVERRIDE { return true; }
6088
Vladimir Markoa1de9182016-02-25 11:37:38 +00006089 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6090 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6091 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6092 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006093
6094 DECLARE_INSTRUCTION(CheckCast);
6095
6096 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006097 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6098 static constexpr size_t kFieldTypeCheckKindSize =
6099 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6100 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6101 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6102 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6103 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006104
6105 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006106};
6107
Vladimir Markofcb503c2016-05-18 12:48:17 +01006108class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006109 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006110 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006111 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006112 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6113 SetPackedField<BarrierKindField>(barrier_kind);
6114 }
Calin Juravle27df7582015-04-17 19:12:31 +01006115
Vladimir Markoa1de9182016-02-25 11:37:38 +00006116 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006117
6118 DECLARE_INSTRUCTION(MemoryBarrier);
6119
6120 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006121 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6122 static constexpr size_t kFieldBarrierKindSize =
6123 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6124 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6125 kFieldBarrierKind + kFieldBarrierKindSize;
6126 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6127 "Too many packed fields.");
6128 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006129
6130 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6131};
6132
Vladimir Markofcb503c2016-05-18 12:48:17 +01006133class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006134 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006135 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006136 kEnter,
6137 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006138 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006139 };
6140
6141 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006142 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006143 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6144 dex_pc) {
6145 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006146 SetRawInputAt(0, object);
6147 }
6148
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006149 // Instruction may go into runtime, so we need an environment.
6150 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006151
6152 bool CanThrow() const OVERRIDE {
6153 // Verifier guarantees that monitor-exit cannot throw.
6154 // This is important because it allows the HGraphBuilder to remove
6155 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6156 return IsEnter();
6157 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006158
Vladimir Markoa1de9182016-02-25 11:37:38 +00006159 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6160 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006161
6162 DECLARE_INSTRUCTION(MonitorOperation);
6163
Calin Juravle52c48962014-12-16 17:02:57 +00006164 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006165 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6166 static constexpr size_t kFieldOperationKindSize =
6167 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6168 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6169 kFieldOperationKind + kFieldOperationKindSize;
6170 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6171 "Too many packed fields.");
6172 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006173
6174 private:
6175 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6176};
6177
Vladimir Markofcb503c2016-05-18 12:48:17 +01006178class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006179 public:
6180 HSelect(HInstruction* condition,
6181 HInstruction* true_value,
6182 HInstruction* false_value,
6183 uint32_t dex_pc)
6184 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6185 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6186
6187 // First input must be `true_value` or `false_value` to allow codegens to
6188 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6189 // that architectures which implement HSelect as a conditional move also
6190 // will not need to invert the condition.
6191 SetRawInputAt(0, false_value);
6192 SetRawInputAt(1, true_value);
6193 SetRawInputAt(2, condition);
6194 }
6195
6196 HInstruction* GetFalseValue() const { return InputAt(0); }
6197 HInstruction* GetTrueValue() const { return InputAt(1); }
6198 HInstruction* GetCondition() const { return InputAt(2); }
6199
6200 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006201 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6202 return true;
6203 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006204
6205 bool CanBeNull() const OVERRIDE {
6206 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6207 }
6208
6209 DECLARE_INSTRUCTION(Select);
6210
6211 private:
6212 DISALLOW_COPY_AND_ASSIGN(HSelect);
6213};
6214
Vladimir Markof9f64412015-09-02 14:05:49 +01006215class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006216 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006217 MoveOperands(Location source,
6218 Location destination,
6219 Primitive::Type type,
6220 HInstruction* instruction)
6221 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006222
6223 Location GetSource() const { return source_; }
6224 Location GetDestination() const { return destination_; }
6225
6226 void SetSource(Location value) { source_ = value; }
6227 void SetDestination(Location value) { destination_ = value; }
6228
6229 // The parallel move resolver marks moves as "in-progress" by clearing the
6230 // destination (but not the source).
6231 Location MarkPending() {
6232 DCHECK(!IsPending());
6233 Location dest = destination_;
6234 destination_ = Location::NoLocation();
6235 return dest;
6236 }
6237
6238 void ClearPending(Location dest) {
6239 DCHECK(IsPending());
6240 destination_ = dest;
6241 }
6242
6243 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006244 DCHECK(source_.IsValid() || destination_.IsInvalid());
6245 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006246 }
6247
6248 // True if this blocks a move from the given location.
6249 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006250 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006251 }
6252
6253 // A move is redundant if it's been eliminated, if its source and
6254 // destination are the same, or if its destination is unneeded.
6255 bool IsRedundant() const {
6256 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6257 }
6258
6259 // We clear both operands to indicate move that's been eliminated.
6260 void Eliminate() {
6261 source_ = destination_ = Location::NoLocation();
6262 }
6263
6264 bool IsEliminated() const {
6265 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6266 return source_.IsInvalid();
6267 }
6268
Alexey Frunze4dda3372015-06-01 18:31:49 -07006269 Primitive::Type GetType() const { return type_; }
6270
Nicolas Geoffray90218252015-04-15 11:56:51 +01006271 bool Is64BitMove() const {
6272 return Primitive::Is64BitType(type_);
6273 }
6274
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006275 HInstruction* GetInstruction() const { return instruction_; }
6276
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006277 private:
6278 Location source_;
6279 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006280 // The type this move is for.
6281 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006282 // The instruction this move is assocatied with. Null when this move is
6283 // for moving an input in the expected locations of user (including a phi user).
6284 // This is only used in debug mode, to ensure we do not connect interval siblings
6285 // in the same parallel move.
6286 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006287};
6288
Roland Levillainc9285912015-12-18 10:38:42 +00006289std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6290
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006291static constexpr size_t kDefaultNumberOfMoves = 4;
6292
Vladimir Markofcb503c2016-05-18 12:48:17 +01006293class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006294 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006295 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006296 : HTemplateInstruction(SideEffects::None(), dex_pc),
6297 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6298 moves_.reserve(kDefaultNumberOfMoves);
6299 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006300
Nicolas Geoffray90218252015-04-15 11:56:51 +01006301 void AddMove(Location source,
6302 Location destination,
6303 Primitive::Type type,
6304 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006305 DCHECK(source.IsValid());
6306 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006307 if (kIsDebugBuild) {
6308 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006309 for (const MoveOperands& move : moves_) {
6310 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006311 // Special case the situation where the move is for the spill slot
6312 // of the instruction.
6313 if ((GetPrevious() == instruction)
6314 || ((GetPrevious() == nullptr)
6315 && instruction->IsPhi()
6316 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006317 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006318 << "Doing parallel moves for the same instruction.";
6319 } else {
6320 DCHECK(false) << "Doing parallel moves for the same instruction.";
6321 }
6322 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006323 }
6324 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006325 for (const MoveOperands& move : moves_) {
6326 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006327 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006328 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006329 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006330 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006331 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006332 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006333 }
6334
Vladimir Marko225b6462015-09-28 12:17:40 +01006335 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006336 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006337 }
6338
Vladimir Marko225b6462015-09-28 12:17:40 +01006339 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006340
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006341 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006342
6343 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006344 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006345
6346 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6347};
6348
Mark Mendell0616ae02015-04-17 12:49:27 -04006349} // namespace art
6350
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006351#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6352#include "nodes_shared.h"
6353#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006354#ifdef ART_ENABLE_CODEGEN_arm
6355#include "nodes_arm.h"
6356#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006357#ifdef ART_ENABLE_CODEGEN_arm64
6358#include "nodes_arm64.h"
6359#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006360#ifdef ART_ENABLE_CODEGEN_x86
6361#include "nodes_x86.h"
6362#endif
6363
6364namespace art {
6365
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006366class HGraphVisitor : public ValueObject {
6367 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006368 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6369 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006370
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006371 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006372 virtual void VisitBasicBlock(HBasicBlock* block);
6373
Roland Levillain633021e2014-10-01 14:12:25 +01006374 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006375 void VisitInsertionOrder();
6376
Roland Levillain633021e2014-10-01 14:12:25 +01006377 // Visit the graph following dominator tree reverse post-order.
6378 void VisitReversePostOrder();
6379
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006380 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006381
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006382 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006383#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006384 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6385
6386 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6387
6388#undef DECLARE_VISIT_INSTRUCTION
6389
6390 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006391 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006392
6393 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6394};
6395
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006396class HGraphDelegateVisitor : public HGraphVisitor {
6397 public:
6398 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6399 virtual ~HGraphDelegateVisitor() {}
6400
6401 // Visit functions that delegate to to super class.
6402#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006403 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006404
6405 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6406
6407#undef DECLARE_VISIT_INSTRUCTION
6408
6409 private:
6410 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6411};
6412
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006413class HInsertionOrderIterator : public ValueObject {
6414 public:
6415 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6416
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006417 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006418 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006419 void Advance() { ++index_; }
6420
6421 private:
6422 const HGraph& graph_;
6423 size_t index_;
6424
6425 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6426};
6427
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006428class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006429 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006430 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6431 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006432 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006433 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006434
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006435 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6436 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006437 void Advance() { ++index_; }
6438
6439 private:
6440 const HGraph& graph_;
6441 size_t index_;
6442
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006443 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006444};
6445
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006446class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006447 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006448 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006449 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006450 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006451 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006452 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006453
6454 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006455 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006456 void Advance() { --index_; }
6457
6458 private:
6459 const HGraph& graph_;
6460 size_t index_;
6461
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006462 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006463};
6464
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006465class HLinearPostOrderIterator : public ValueObject {
6466 public:
6467 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006468 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006469
6470 bool Done() const { return index_ == 0; }
6471
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006472 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006473
6474 void Advance() {
6475 --index_;
6476 DCHECK_GE(index_, 0U);
6477 }
6478
6479 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006480 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006481 size_t index_;
6482
6483 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6484};
6485
6486class HLinearOrderIterator : public ValueObject {
6487 public:
6488 explicit HLinearOrderIterator(const HGraph& graph)
6489 : order_(graph.GetLinearOrder()), index_(0) {}
6490
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006491 bool Done() const { return index_ == order_.size(); }
6492 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006493 void Advance() { ++index_; }
6494
6495 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006496 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006497 size_t index_;
6498
6499 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6500};
6501
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006502// Iterator over the blocks that art part of the loop. Includes blocks part
6503// of an inner loop. The order in which the blocks are iterated is on their
6504// block id.
6505class HBlocksInLoopIterator : public ValueObject {
6506 public:
6507 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6508 : blocks_in_loop_(info.GetBlocks()),
6509 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6510 index_(0) {
6511 if (!blocks_in_loop_.IsBitSet(index_)) {
6512 Advance();
6513 }
6514 }
6515
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006516 bool Done() const { return index_ == blocks_.size(); }
6517 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006518 void Advance() {
6519 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006520 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006521 if (blocks_in_loop_.IsBitSet(index_)) {
6522 break;
6523 }
6524 }
6525 }
6526
6527 private:
6528 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006529 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006530 size_t index_;
6531
6532 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6533};
6534
Mingyao Yang3584bce2015-05-19 16:01:59 -07006535// Iterator over the blocks that art part of the loop. Includes blocks part
6536// of an inner loop. The order in which the blocks are iterated is reverse
6537// post order.
6538class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6539 public:
6540 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6541 : blocks_in_loop_(info.GetBlocks()),
6542 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6543 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006544 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006545 Advance();
6546 }
6547 }
6548
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006549 bool Done() const { return index_ == blocks_.size(); }
6550 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006551 void Advance() {
6552 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006553 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6554 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006555 break;
6556 }
6557 }
6558 }
6559
6560 private:
6561 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006562 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006563 size_t index_;
6564
6565 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6566};
6567
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006568inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006569 if (constant->IsIntConstant()) {
6570 return constant->AsIntConstant()->GetValue();
6571 } else if (constant->IsLongConstant()) {
6572 return constant->AsLongConstant()->GetValue();
6573 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006574 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006575 return 0;
6576 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006577}
6578
Vladimir Marko58155012015-08-19 12:49:41 +00006579inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6580 // For the purposes of the compiler, the dex files must actually be the same object
6581 // if we want to safely treat them as the same. This is especially important for JIT
6582 // as custom class loaders can open the same underlying file (or memory) multiple
6583 // times and provide different class resolution but no two class loaders should ever
6584 // use the same DexFile object - doing so is an unsupported hack that can lead to
6585 // all sorts of weird failures.
6586 return &lhs == &rhs;
6587}
6588
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006589#define INSTRUCTION_TYPE_CHECK(type, super) \
6590 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6591 inline const H##type* HInstruction::As##type() const { \
6592 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6593 } \
6594 inline H##type* HInstruction::As##type() { \
6595 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6596 }
6597
6598 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6599#undef INSTRUCTION_TYPE_CHECK
6600
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006601// Create space in `blocks` for adding `number_of_new_blocks` entries
6602// starting at location `at`. Blocks after `at` are moved accordingly.
6603inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6604 size_t number_of_new_blocks,
6605 size_t after) {
6606 DCHECK_LT(after, blocks->size());
6607 size_t old_size = blocks->size();
6608 size_t new_size = old_size + number_of_new_blocks;
6609 blocks->resize(new_size);
6610 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6611}
6612
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006613} // namespace art
6614
6615#endif // ART_COMPILER_OPTIMIZING_NODES_H_