blob: dc0fab55928da3048968ebb7b29813b9fb413f68 [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
164 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000165 return ReferenceTypeInfo(type_handle, is_exact);
166 }
167
168 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
169
Vladimir Markof39745e2016-01-26 12:16:55 +0000170 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000171 return handle.GetReference() != nullptr;
172 }
173
Vladimir Marko456307a2016-04-19 14:12:13 +0000174 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000175 return IsValidHandle(type_handle_);
176 }
177
178 bool IsExact() const { return is_exact_; }
179
180 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
181 DCHECK(IsValid());
182 return GetTypeHandle()->IsObjectClass();
183 }
184
185 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
186 DCHECK(IsValid());
187 return GetTypeHandle()->IsStringClass();
188 }
189
190 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
191 DCHECK(IsValid());
192 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
193 }
194
195 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
196 DCHECK(IsValid());
197 return GetTypeHandle()->IsInterface();
198 }
199
200 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
201 DCHECK(IsValid());
202 return GetTypeHandle()->IsArrayClass();
203 }
204
205 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
206 DCHECK(IsValid());
207 return GetTypeHandle()->IsPrimitiveArray();
208 }
209
210 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
211 DCHECK(IsValid());
212 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
213 }
214
215 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
216 DCHECK(IsValid());
217 if (!IsExact()) return false;
218 if (!IsArrayClass()) return false;
219 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
220 }
221
222 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
223 DCHECK(IsValid());
224 if (!IsExact()) return false;
225 if (!IsArrayClass()) return false;
226 if (!rti.IsArrayClass()) return false;
227 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
228 rti.GetTypeHandle()->GetComponentType());
229 }
230
231 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
232
233 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
234 DCHECK(IsValid());
235 DCHECK(rti.IsValid());
236 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
237 }
238
239 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
240 DCHECK(IsValid());
241 DCHECK(rti.IsValid());
242 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
243 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
244 }
245
246 // Returns true if the type information provide the same amount of details.
247 // Note that it does not mean that the instructions have the same actual type
248 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000249 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000250 if (!IsValid() && !rti.IsValid()) {
251 // Invalid types are equal.
252 return true;
253 }
254 if (!IsValid() || !rti.IsValid()) {
255 // One is valid, the other not.
256 return false;
257 }
258 return IsExact() == rti.IsExact()
259 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
260 }
261
262 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000263 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
264 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
265 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000266
267 // The class of the object.
268 TypeHandle type_handle_;
269 // Whether or not the type is exact or a superclass of the actual type.
270 // Whether or not we have any information about this type.
271 bool is_exact_;
272};
273
274std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
275
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000276// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100277class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000278 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100279 HGraph(ArenaAllocator* arena,
280 const DexFile& dex_file,
281 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100282 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700283 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100284 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100285 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000286 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100287 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000288 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100289 blocks_(arena->Adapter(kArenaAllocBlockList)),
290 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
291 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700292 entry_block_(nullptr),
293 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100294 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100295 number_of_vregs_(0),
296 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000297 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400298 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000299 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000300 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000301 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000302 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100303 dex_file_(dex_file),
304 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100305 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100306 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100307 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700308 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000309 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100310 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
311 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
312 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
313 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000314 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000315 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
316 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100317 blocks_.reserve(kDefaultNumberOfBlocks);
318 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000319
David Brazdilbadd8262016-02-02 16:28:56 +0000320 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
321 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
322
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000323 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100324 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
325
David Brazdil69ba7b72015-06-23 18:27:30 +0100326 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000327 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100328
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000329 HBasicBlock* GetEntryBlock() const { return entry_block_; }
330 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100331 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000332
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000333 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
334 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000335
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000336 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100337
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100338 void ComputeDominanceInformation();
339 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000340 void ClearLoopInformation();
341 void FindBackEdges(ArenaBitVector* visited);
342 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100343 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100344 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000345
David Brazdil4833f5a2015-12-16 10:37:39 +0000346 // Analyze all natural loops in this graph. Returns a code specifying that it
347 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000348 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000349 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100350
David Brazdilffee3d32015-07-06 11:48:53 +0100351 // Iterate over blocks to compute try block membership. Needs reverse post
352 // order and loop information.
353 void ComputeTryBlockInformation();
354
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000355 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000356 // Returns the instruction to replace the invoke expression or null if the
357 // invoke is for a void method. Note that the caller is responsible for replacing
358 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000359 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000360
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000361 // Update the loop and try membership of `block`, which was spawned from `reference`.
362 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
363 // should be the new back edge.
364 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
365 HBasicBlock* reference,
366 bool replace_if_back_edge);
367
Mingyao Yang3584bce2015-05-19 16:01:59 -0700368 // Need to add a couple of blocks to test if the loop body is entered and
369 // put deoptimization instructions, etc.
370 void TransformLoopHeaderForBCE(HBasicBlock* header);
371
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000372 // Removes `block` from the graph. Assumes `block` has been disconnected from
373 // other blocks and has no instructions or phis.
374 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000375
David Brazdilfc6a86a2015-06-26 10:33:45 +0000376 // Splits the edge between `block` and `successor` while preserving the
377 // indices in the predecessor/successor lists. If there are multiple edges
378 // between the blocks, the lowest indices are used.
379 // Returns the new block which is empty and has the same dex pc as `successor`.
380 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
381
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100382 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
383 void SimplifyLoop(HBasicBlock* header);
384
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000385 int32_t GetNextInstructionId() {
386 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000387 return current_instruction_id_++;
388 }
389
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000390 int32_t GetCurrentInstructionId() const {
391 return current_instruction_id_;
392 }
393
394 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000395 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000396 current_instruction_id_ = id;
397 }
398
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100399 uint16_t GetMaximumNumberOfOutVRegs() const {
400 return maximum_number_of_out_vregs_;
401 }
402
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000403 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
404 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100405 }
406
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100407 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
408 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
409 }
410
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000411 void UpdateTemporariesVRegSlots(size_t slots) {
412 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100413 }
414
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000415 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100416 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000417 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100418 }
419
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100420 void SetNumberOfVRegs(uint16_t number_of_vregs) {
421 number_of_vregs_ = number_of_vregs;
422 }
423
424 uint16_t GetNumberOfVRegs() const {
425 return number_of_vregs_;
426 }
427
428 void SetNumberOfInVRegs(uint16_t value) {
429 number_of_in_vregs_ = value;
430 }
431
David Brazdildee58d62016-04-07 09:54:26 +0000432 uint16_t GetNumberOfInVRegs() const {
433 return number_of_in_vregs_;
434 }
435
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100436 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100437 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100438 return number_of_vregs_ - number_of_in_vregs_;
439 }
440
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100441 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100442 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100443 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100444
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100445 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100446 return linear_order_;
447 }
448
Mark Mendell1152c922015-04-24 17:06:35 -0400449 bool HasBoundsChecks() const {
450 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800451 }
452
Mark Mendell1152c922015-04-24 17:06:35 -0400453 void SetHasBoundsChecks(bool value) {
454 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800455 }
456
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100457 bool ShouldGenerateConstructorBarrier() const {
458 return should_generate_constructor_barrier_;
459 }
460
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000461 bool IsDebuggable() const { return debuggable_; }
462
David Brazdil8d5b8b22015-03-24 10:51:52 +0000463 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000464 // already, it is created and inserted into the graph. This method is only for
465 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600466 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000467
468 // TODO: This is problematic for the consistency of reference type propagation
469 // because it can be created anytime after the pass and thus it will be left
470 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600471 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000472
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600473 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
474 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000475 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600476 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
477 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000478 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600479 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
480 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000481 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600482 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
483 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000484 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000485
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100486 HCurrentMethod* GetCurrentMethod();
487
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100488 const DexFile& GetDexFile() const {
489 return dex_file_;
490 }
491
492 uint32_t GetMethodIdx() const {
493 return method_idx_;
494 }
495
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100496 InvokeType GetInvokeType() const {
497 return invoke_type_;
498 }
499
Mark Mendellc4701932015-04-10 13:18:51 -0400500 InstructionSet GetInstructionSet() const {
501 return instruction_set_;
502 }
503
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000504 bool IsCompilingOsr() const { return osr_; }
505
David Brazdil77a48ae2015-09-15 12:34:04 +0000506 bool HasTryCatch() const { return has_try_catch_; }
507 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100508
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000509 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
510 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
511
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100512 ArtMethod* GetArtMethod() const { return art_method_; }
513 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
514
Mark Mendellf6529172015-11-17 11:16:56 -0500515 // Returns an instruction with the opposite boolean value from 'cond'.
516 // The instruction has been inserted into the graph, either as a constant, or
517 // before cursor.
518 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
519
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000520 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
521
David Brazdil2d7352b2015-04-20 14:52:42 +0100522 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000523 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100524 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000525
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000526 template <class InstructionType, typename ValueType>
527 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600528 ArenaSafeMap<ValueType, InstructionType*>* cache,
529 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000530 // Try to find an existing constant of the given value.
531 InstructionType* constant = nullptr;
532 auto cached_constant = cache->find(value);
533 if (cached_constant != cache->end()) {
534 constant = cached_constant->second;
535 }
536
537 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100538 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000539 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600540 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000541 cache->Overwrite(value, constant);
542 InsertConstant(constant);
543 }
544 return constant;
545 }
546
David Brazdil8d5b8b22015-03-24 10:51:52 +0000547 void InsertConstant(HConstant* instruction);
548
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000549 // Cache a float constant into the graph. This method should only be
550 // called by the SsaBuilder when creating "equivalent" instructions.
551 void CacheFloatConstant(HFloatConstant* constant);
552
553 // See CacheFloatConstant comment.
554 void CacheDoubleConstant(HDoubleConstant* constant);
555
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000556 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000557
558 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100559 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000560
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100561 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100562 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000563
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100564 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100565 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100566
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000567 HBasicBlock* entry_block_;
568 HBasicBlock* exit_block_;
569
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100570 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100571 uint16_t maximum_number_of_out_vregs_;
572
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100573 // The number of virtual registers in this method. Contains the parameters.
574 uint16_t number_of_vregs_;
575
576 // The number of virtual registers used by parameters of this method.
577 uint16_t number_of_in_vregs_;
578
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000579 // Number of vreg size slots that the temporaries use (used in baseline compiler).
580 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100581
Mark Mendell1152c922015-04-24 17:06:35 -0400582 // Has bounds checks. We can totally skip BCE if it's false.
583 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800584
David Brazdil77a48ae2015-09-15 12:34:04 +0000585 // Flag whether there are any try/catch blocks in the graph. We will skip
586 // try/catch-related passes if false.
587 bool has_try_catch_;
588
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000589 // Flag whether there are any irreducible loops in the graph.
590 bool has_irreducible_loops_;
591
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000592 // Indicates whether the graph should be compiled in a way that
593 // ensures full debuggability. If false, we can apply more
594 // aggressive optimizations that may limit the level of debugging.
595 const bool debuggable_;
596
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000597 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000598 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000599
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100600 // The dex file from which the method is from.
601 const DexFile& dex_file_;
602
603 // The method index in the dex file.
604 const uint32_t method_idx_;
605
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100606 // If inlined, this encodes how the callee is being invoked.
607 const InvokeType invoke_type_;
608
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100609 // Whether the graph has been transformed to SSA form. Only used
610 // in debug mode to ensure we are not using properties only valid
611 // for non-SSA form (like the number of temporaries).
612 bool in_ssa_form_;
613
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100614 const bool should_generate_constructor_barrier_;
615
Mathieu Chartiere401d142015-04-22 13:56:20 -0700616 const InstructionSet instruction_set_;
617
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000618 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000619 HNullConstant* cached_null_constant_;
620 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000621 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000622 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000623 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000624
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100625 HCurrentMethod* cached_current_method_;
626
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100627 // The ArtMethod this graph is for. Note that for AOT, it may be null,
628 // for example for methods whose declaring class could not be resolved
629 // (such as when the superclass could not be found).
630 ArtMethod* art_method_;
631
David Brazdil4833f5a2015-12-16 10:37:39 +0000632 // Keep the RTI of inexact Object to avoid having to pass stack handle
633 // collection pointer to passes which may create NullConstant.
634 ReferenceTypeInfo inexact_object_rti_;
635
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000636 // Whether we are compiling this graph for on stack replacement: this will
637 // make all loops seen as irreducible and emit special stack maps to mark
638 // compiled code entries which the interpreter can directly jump to.
639 const bool osr_;
640
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000641 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100642 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000643 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000644 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000645 DISALLOW_COPY_AND_ASSIGN(HGraph);
646};
647
Vladimir Markof9f64412015-09-02 14:05:49 +0100648class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000649 public:
650 HLoopInformation(HBasicBlock* header, HGraph* graph)
651 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100652 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000653 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100654 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100655 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100656 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000657 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100658 back_edges_.reserve(kDefaultNumberOfBackEdges);
659 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100660
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000661 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100662 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000663
664 void Dump(std::ostream& os);
665
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100666 HBasicBlock* GetHeader() const {
667 return header_;
668 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000669
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000670 void SetHeader(HBasicBlock* block) {
671 header_ = block;
672 }
673
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100674 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
675 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
676 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
677
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000678 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100679 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000680 }
681
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100682 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100683 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100684 }
685
David Brazdil46e2a392015-03-16 17:31:52 +0000686 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100687 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100688 }
689
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000690 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100691 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000692 }
693
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100694 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100695
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100696 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100697 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100698 }
699
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100700 // Returns the lifetime position of the back edge that has the
701 // greatest lifetime position.
702 size_t GetLifetimeEnd() const;
703
704 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100705 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100706 }
707
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000708 // Finds blocks that are part of this loop.
709 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100710
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100711 // Returns whether this loop information contains `block`.
712 // Note that this loop information *must* be populated before entering this function.
713 bool Contains(const HBasicBlock& block) const;
714
715 // Returns whether this loop information is an inner loop of `other`.
716 // Note that `other` *must* be populated before entering this function.
717 bool IsIn(const HLoopInformation& other) const;
718
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800719 // Returns true if instruction is not defined within this loop.
720 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700721
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100722 const ArenaBitVector& GetBlocks() const { return blocks_; }
723
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000724 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000725 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000726
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000727 void ClearAllBlocks() {
728 blocks_.ClearAllBits();
729 }
730
David Brazdil3f4a5222016-05-06 12:46:21 +0100731 bool HasBackEdgeNotDominatedByHeader() const;
732
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100733 bool IsPopulated() const {
734 return blocks_.GetHighestBitSet() != -1;
735 }
736
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000737 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100738 // Internal recursive implementation of `Populate`.
739 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100740 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100741
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000742 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100743 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000744 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100745 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100746 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100747 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000748
749 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
750};
751
David Brazdilec16f792015-08-19 15:04:01 +0100752// Stores try/catch information for basic blocks.
753// Note that HGraph is constructed so that catch blocks cannot simultaneously
754// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100755class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100756 public:
757 // Try block information constructor.
758 explicit TryCatchInformation(const HTryBoundary& try_entry)
759 : try_entry_(&try_entry),
760 catch_dex_file_(nullptr),
761 catch_type_index_(DexFile::kDexNoIndex16) {
762 DCHECK(try_entry_ != nullptr);
763 }
764
765 // Catch block information constructor.
766 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
767 : try_entry_(nullptr),
768 catch_dex_file_(&dex_file),
769 catch_type_index_(catch_type_index) {}
770
771 bool IsTryBlock() const { return try_entry_ != nullptr; }
772
773 const HTryBoundary& GetTryEntry() const {
774 DCHECK(IsTryBlock());
775 return *try_entry_;
776 }
777
778 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
779
780 bool IsCatchAllTypeIndex() const {
781 DCHECK(IsCatchBlock());
782 return catch_type_index_ == DexFile::kDexNoIndex16;
783 }
784
785 uint16_t GetCatchTypeIndex() const {
786 DCHECK(IsCatchBlock());
787 return catch_type_index_;
788 }
789
790 const DexFile& GetCatchDexFile() const {
791 DCHECK(IsCatchBlock());
792 return *catch_dex_file_;
793 }
794
795 private:
796 // One of possibly several TryBoundary instructions entering the block's try.
797 // Only set for try blocks.
798 const HTryBoundary* try_entry_;
799
800 // Exception type information. Only set for catch blocks.
801 const DexFile* catch_dex_file_;
802 const uint16_t catch_type_index_;
803};
804
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100805static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100806static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100807
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000808// A block in a method. Contains the list of instructions represented
809// as a double linked list. Each block knows its predecessors and
810// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100811
Vladimir Markof9f64412015-09-02 14:05:49 +0100812class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000813 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600814 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000815 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000816 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
817 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000818 loop_information_(nullptr),
819 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000820 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100821 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100822 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100823 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000824 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000825 try_catch_information_(nullptr) {
826 predecessors_.reserve(kDefaultNumberOfPredecessors);
827 successors_.reserve(kDefaultNumberOfSuccessors);
828 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
829 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000830
Vladimir Marko60584552015-09-03 13:35:12 +0000831 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100832 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000833 }
834
Vladimir Marko60584552015-09-03 13:35:12 +0000835 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100836 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000837 }
838
David Brazdild26a4112015-11-10 11:07:31 +0000839 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
840 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
841
Vladimir Marko60584552015-09-03 13:35:12 +0000842 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
843 return ContainsElement(successors_, block, start_from);
844 }
845
846 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100847 return dominated_blocks_;
848 }
849
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100850 bool IsEntryBlock() const {
851 return graph_->GetEntryBlock() == this;
852 }
853
854 bool IsExitBlock() const {
855 return graph_->GetExitBlock() == this;
856 }
857
David Brazdil46e2a392015-03-16 17:31:52 +0000858 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000859 bool IsSingleTryBoundary() const;
860
861 // Returns true if this block emits nothing but a jump.
862 bool IsSingleJump() const {
863 HLoopInformation* loop_info = GetLoopInformation();
864 return (IsSingleGoto() || IsSingleTryBoundary())
865 // Back edges generate a suspend check.
866 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
867 }
David Brazdil46e2a392015-03-16 17:31:52 +0000868
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000869 void AddBackEdge(HBasicBlock* back_edge) {
870 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000871 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000872 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100873 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000874 loop_information_->AddBackEdge(back_edge);
875 }
876
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000877 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000878 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000879
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100880 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000881 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600882 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000883
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000884 HBasicBlock* GetDominator() const { return dominator_; }
885 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000886 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
887
888 void RemoveDominatedBlock(HBasicBlock* block) {
889 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100890 }
Vladimir Marko60584552015-09-03 13:35:12 +0000891
892 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
893 ReplaceElement(dominated_blocks_, existing, new_block);
894 }
895
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100896 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000897
898 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100899 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000900 }
901
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100902 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
903 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100904 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100905 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100906 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
907 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000908
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000909 HInstruction* GetFirstInstructionDisregardMoves() const;
910
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000911 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000912 successors_.push_back(block);
913 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000914 }
915
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100916 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
917 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100918 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000919 new_block->predecessors_.push_back(this);
920 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000921 }
922
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000923 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
924 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000925 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000926 new_block->successors_.push_back(this);
927 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000928 }
929
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100930 // Insert `this` between `predecessor` and `successor. This method
931 // preserves the indicies, and will update the first edge found between
932 // `predecessor` and `successor`.
933 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
934 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100935 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000936 successor->predecessors_[predecessor_index] = this;
937 predecessor->successors_[successor_index] = this;
938 successors_.push_back(successor);
939 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100940 }
941
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100942 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000943 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100944 }
945
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000946 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000947 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000948 }
949
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100950 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000951 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100952 }
953
954 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000955 predecessors_.push_back(block);
956 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100957 }
958
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100959 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000960 DCHECK_EQ(predecessors_.size(), 2u);
961 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100962 }
963
David Brazdil769c9e52015-04-27 13:54:09 +0100964 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000965 DCHECK_EQ(successors_.size(), 2u);
966 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100967 }
968
David Brazdilfc6a86a2015-06-26 10:33:45 +0000969 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000970 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100971 }
972
David Brazdilfc6a86a2015-06-26 10:33:45 +0000973 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000974 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100975 }
976
David Brazdilfc6a86a2015-06-26 10:33:45 +0000977 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000978 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100979 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000980 }
981
982 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000983 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100984 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000985 }
986
987 // Returns whether the first occurrence of `predecessor` in the list of
988 // predecessors is at index `idx`.
989 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100990 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000991 return GetPredecessorIndexOf(predecessor) == idx;
992 }
993
David Brazdild7558da2015-09-22 13:04:14 +0100994 // Create a new block between this block and its predecessors. The new block
995 // is added to the graph, all predecessor edges are relinked to it and an edge
996 // is created to `this`. Returns the new empty block. Reverse post order or
997 // loop and try/catch information are not updated.
998 HBasicBlock* CreateImmediateDominator();
999
David Brazdilfc6a86a2015-06-26 10:33:45 +00001000 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001001 // created, latter block. Note that this method will add the block to the
1002 // graph, create a Goto at the end of the former block and will create an edge
1003 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001004 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001005 HBasicBlock* SplitBefore(HInstruction* cursor);
1006
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001007 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001008 // created block. Note that this method just updates raw block information,
1009 // like predecessors, successors, dominators, and instruction list. It does not
1010 // update the graph, reverse post order, loop information, nor make sure the
1011 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001012 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1013
1014 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1015 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001016
1017 // Merge `other` at the end of `this`. Successors and dominated blocks of
1018 // `other` are changed to be successors and dominated blocks of `this`. Note
1019 // that this method does not update the graph, reverse post order, loop
1020 // information, nor make sure the blocks are consistent (for example ending
1021 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001022 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001023
1024 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1025 // of `this` are moved to `other`.
1026 // Note that this method does not update the graph, reverse post order, loop
1027 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001028 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001029 void ReplaceWith(HBasicBlock* other);
1030
David Brazdil2d7352b2015-04-20 14:52:42 +01001031 // Merge `other` at the end of `this`. This method updates loops, reverse post
1032 // order, links to predecessors, successors, dominators and deletes the block
1033 // from the graph. The two blocks must be successive, i.e. `this` the only
1034 // predecessor of `other` and vice versa.
1035 void MergeWith(HBasicBlock* other);
1036
1037 // Disconnects `this` from all its predecessors, successors and dominator,
1038 // removes it from all loops it is included in and eventually from the graph.
1039 // The block must not dominate any other block. Predecessors and successors
1040 // are safely updated.
1041 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001042
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001043 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001044 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001045 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001046 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001047 // Replace instruction `initial` with `replacement` within this block.
1048 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1049 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001050 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001051 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001052 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001053 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1054 // instruction list. With 'ensure_safety' set to true, it verifies that the
1055 // instruction is not in use and removes it from the use lists of its inputs.
1056 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1057 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001058 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001059
1060 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001061 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001062 }
1063
Roland Levillain6b879dd2014-09-22 17:13:44 +01001064 bool IsLoopPreHeaderFirstPredecessor() const {
1065 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001066 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001067 }
1068
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001069 bool IsFirstPredecessorBackEdge() const {
1070 DCHECK(IsLoopHeader());
1071 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1072 }
1073
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001074 HLoopInformation* GetLoopInformation() const {
1075 return loop_information_;
1076 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001077
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001078 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001079 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001080 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001081 void SetInLoop(HLoopInformation* info) {
1082 if (IsLoopHeader()) {
1083 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001084 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001085 loop_information_ = info;
1086 } else if (loop_information_->Contains(*info->GetHeader())) {
1087 // Block is currently part of an outer loop. Make it part of this inner loop.
1088 // Note that a non loop header having a loop information means this loop information
1089 // has already been populated
1090 loop_information_ = info;
1091 } else {
1092 // Block is part of an inner loop. Do not update the loop information.
1093 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1094 // at this point, because this method is being called while populating `info`.
1095 }
1096 }
1097
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001098 // Raw update of the loop information.
1099 void SetLoopInformation(HLoopInformation* info) {
1100 loop_information_ = info;
1101 }
1102
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001103 bool IsInLoop() const { return loop_information_ != nullptr; }
1104
David Brazdilec16f792015-08-19 15:04:01 +01001105 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1106
1107 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1108 try_catch_information_ = try_catch_information;
1109 }
1110
1111 bool IsTryBlock() const {
1112 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1113 }
1114
1115 bool IsCatchBlock() const {
1116 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1117 }
David Brazdilffee3d32015-07-06 11:48:53 +01001118
1119 // Returns the try entry that this block's successors should have. They will
1120 // be in the same try, unless the block ends in a try boundary. In that case,
1121 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001122 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001123
David Brazdild7558da2015-09-22 13:04:14 +01001124 bool HasThrowingInstructions() const;
1125
David Brazdila4b8c212015-05-07 09:59:30 +01001126 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001127 bool Dominates(HBasicBlock* block) const;
1128
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001129 size_t GetLifetimeStart() const { return lifetime_start_; }
1130 size_t GetLifetimeEnd() const { return lifetime_end_; }
1131
1132 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1133 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1134
David Brazdil8d5b8b22015-03-24 10:51:52 +00001135 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001136 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001137 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001138 bool HasSinglePhi() const;
1139
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001140 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001141 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001142 ArenaVector<HBasicBlock*> predecessors_;
1143 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001144 HInstructionList instructions_;
1145 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001146 HLoopInformation* loop_information_;
1147 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001148 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001149 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001150 // The dex program counter of the first instruction of this block.
1151 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001152 size_t lifetime_start_;
1153 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001154 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001155
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001156 friend class HGraph;
1157 friend class HInstruction;
1158
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001159 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1160};
1161
David Brazdilb2bd1c52015-03-25 11:17:37 +00001162// Iterates over the LoopInformation of all loops which contain 'block'
1163// from the innermost to the outermost.
1164class HLoopInformationOutwardIterator : public ValueObject {
1165 public:
1166 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1167 : current_(block.GetLoopInformation()) {}
1168
1169 bool Done() const { return current_ == nullptr; }
1170
1171 void Advance() {
1172 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001173 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001174 }
1175
1176 HLoopInformation* Current() const {
1177 DCHECK(!Done());
1178 return current_;
1179 }
1180
1181 private:
1182 HLoopInformation* current_;
1183
1184 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1185};
1186
Alexandre Ramesef20f712015-06-09 10:29:30 +01001187#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001188 M(Above, Condition) \
1189 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001190 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001191 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001192 M(ArrayGet, Instruction) \
1193 M(ArrayLength, Instruction) \
1194 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001195 M(Below, Condition) \
1196 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001197 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001198 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001199 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001200 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001201 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001202 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001203 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001204 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001205 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001206 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001207 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001208 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001209 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001210 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001211 M(Exit, Instruction) \
1212 M(FloatConstant, Constant) \
1213 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001214 M(GreaterThan, Condition) \
1215 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001216 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001217 M(InstanceFieldGet, Instruction) \
1218 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001219 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001220 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001221 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001222 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001223 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001224 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001225 M(LessThan, Condition) \
1226 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001227 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001228 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001229 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001230 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001231 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001232 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001233 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001234 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001235 M(Neg, UnaryOperation) \
1236 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001237 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001238 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001239 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001240 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001241 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001242 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001243 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001244 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001245 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001246 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001247 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001248 M(Return, Instruction) \
1249 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001250 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001251 M(Shl, BinaryOperation) \
1252 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001253 M(StaticFieldGet, Instruction) \
1254 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001255 M(UnresolvedInstanceFieldGet, Instruction) \
1256 M(UnresolvedInstanceFieldSet, Instruction) \
1257 M(UnresolvedStaticFieldGet, Instruction) \
1258 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001259 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001260 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001261 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001262 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001263 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001264 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001265 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001266 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001267
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001268/*
1269 * Instructions, shared across several (not all) architectures.
1270 */
1271#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1272#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1273#else
1274#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001275 M(BitwiseNegatedRight, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001276 M(MultiplyAccumulate, Instruction)
1277#endif
1278
Vladimir Markob4536b72015-11-24 13:45:23 +00001279#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001280#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001281#else
1282#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1283 M(ArmDexCacheArraysBase, Instruction)
1284#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001285
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001286#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001287#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001288#else
1289#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001290 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001291 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001292#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001293
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001294#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1295
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001296#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1297
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001298#ifndef ART_ENABLE_CODEGEN_x86
1299#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1300#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001301#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1302 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001303 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001304 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001305 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001306#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001307
1308#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1309
1310#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1311 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001312 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001313 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1314 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001315 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001316 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001317 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1318 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1319
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001320#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1321 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001322 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001323 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001324 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001325 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001326
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001327#define FOR_EACH_INSTRUCTION(M) \
1328 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1329 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1330
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001331#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001332FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1333#undef FORWARD_DECLARATION
1334
Vladimir Marko372f10e2016-05-17 16:30:10 +01001335#define DECLARE_INSTRUCTION(type) \
1336 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1337 const char* DebugName() const OVERRIDE { return #type; } \
1338 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1339 return other->Is##type(); \
1340 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001341 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001342
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001343#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1344 bool Is##type() const { return As##type() != nullptr; } \
1345 const H##type* As##type() const { return this; } \
1346 H##type* As##type() { return this; }
1347
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001348template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001349class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001350 public:
David Brazdiled596192015-01-23 10:39:45 +00001351 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001352 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001353 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001354
Vladimir Marko46817b82016-03-29 12:21:58 +01001355 // Hook for the IntrusiveForwardList<>.
1356 // TODO: Hide this better.
1357 IntrusiveForwardListHook hook;
1358
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001359 private:
David Brazdiled596192015-01-23 10:39:45 +00001360 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001361 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001362
1363 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001364 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001365
Vladimir Marko46817b82016-03-29 12:21:58 +01001366 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001367
1368 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1369};
1370
David Brazdiled596192015-01-23 10:39:45 +00001371template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001372using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001373
David Brazdil1abb4192015-02-17 18:33:36 +00001374// This class is used by HEnvironment and HInstruction classes to record the
1375// instructions they use and pointers to the corresponding HUseListNodes kept
1376// by the used instructions.
1377template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001378class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001379 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001380 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1381 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001382
Vladimir Marko46817b82016-03-29 12:21:58 +01001383 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1384 : HUserRecord(old_record.instruction_, before_use_node) {}
1385 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1386 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001387 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001388 }
1389
1390 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001391 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1392 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001393
1394 private:
1395 // Instruction used by the user.
1396 HInstruction* instruction_;
1397
Vladimir Marko46817b82016-03-29 12:21:58 +01001398 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1399 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001400};
1401
Aart Bik854a02b2015-07-14 16:07:00 -07001402/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001403 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001404 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001405 * For write/read dependences on fields/arrays, the dependence analysis uses
1406 * type disambiguation (e.g. a float field write cannot modify the value of an
1407 * integer field read) and the access type (e.g. a reference array write cannot
1408 * modify the value of a reference field read [although it may modify the
1409 * reference fetch prior to reading the field, which is represented by its own
1410 * write/read dependence]). The analysis makes conservative points-to
1411 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1412 * the same, and any reference read depends on any reference read without
1413 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001414 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001415 * The internal representation uses 38-bit and is described in the table below.
1416 * The first line indicates the side effect, and for field/array accesses the
1417 * second line indicates the type of the access (in the order of the
1418 * Primitive::Type enum).
1419 * The two numbered lines below indicate the bit position in the bitfield (read
1420 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001421 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001422 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1423 * +-------------+---------+---------+--------------+---------+---------+
1424 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1425 * | 3 |333333322|222222221| 1 |111111110|000000000|
1426 * | 7 |654321098|765432109| 8 |765432109|876543210|
1427 *
1428 * Note that, to ease the implementation, 'changes' bits are least significant
1429 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001430 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001431class SideEffects : public ValueObject {
1432 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001433 SideEffects() : flags_(0) {}
1434
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001435 static SideEffects None() {
1436 return SideEffects(0);
1437 }
1438
1439 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001440 return SideEffects(kAllChangeBits | kAllDependOnBits);
1441 }
1442
1443 static SideEffects AllChanges() {
1444 return SideEffects(kAllChangeBits);
1445 }
1446
1447 static SideEffects AllDependencies() {
1448 return SideEffects(kAllDependOnBits);
1449 }
1450
1451 static SideEffects AllExceptGCDependency() {
1452 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1453 }
1454
1455 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001456 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001457 }
1458
Aart Bik34c3ba92015-07-20 14:08:59 -07001459 static SideEffects AllWrites() {
1460 return SideEffects(kAllWrites);
1461 }
1462
1463 static SideEffects AllReads() {
1464 return SideEffects(kAllReads);
1465 }
1466
1467 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1468 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001469 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001470 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001471 }
1472
Aart Bik854a02b2015-07-14 16:07:00 -07001473 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001474 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001475 }
1476
Aart Bik34c3ba92015-07-20 14:08:59 -07001477 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1478 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001479 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001480 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001481 }
1482
1483 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001484 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001485 }
1486
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001487 static SideEffects CanTriggerGC() {
1488 return SideEffects(1ULL << kCanTriggerGCBit);
1489 }
1490
1491 static SideEffects DependsOnGC() {
1492 return SideEffects(1ULL << kDependsOnGCBit);
1493 }
1494
Aart Bik854a02b2015-07-14 16:07:00 -07001495 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001496 SideEffects Union(SideEffects other) const {
1497 return SideEffects(flags_ | other.flags_);
1498 }
1499
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001500 SideEffects Exclusion(SideEffects other) const {
1501 return SideEffects(flags_ & ~other.flags_);
1502 }
1503
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001504 void Add(SideEffects other) {
1505 flags_ |= other.flags_;
1506 }
1507
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001508 bool Includes(SideEffects other) const {
1509 return (other.flags_ & flags_) == other.flags_;
1510 }
1511
1512 bool HasSideEffects() const {
1513 return (flags_ & kAllChangeBits);
1514 }
1515
1516 bool HasDependencies() const {
1517 return (flags_ & kAllDependOnBits);
1518 }
1519
1520 // Returns true if there are no side effects or dependencies.
1521 bool DoesNothing() const {
1522 return flags_ == 0;
1523 }
1524
Aart Bik854a02b2015-07-14 16:07:00 -07001525 // Returns true if something is written.
1526 bool DoesAnyWrite() const {
1527 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001528 }
1529
Aart Bik854a02b2015-07-14 16:07:00 -07001530 // Returns true if something is read.
1531 bool DoesAnyRead() const {
1532 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001533 }
1534
Aart Bik854a02b2015-07-14 16:07:00 -07001535 // Returns true if potentially everything is written and read
1536 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001537 bool DoesAllReadWrite() const {
1538 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1539 }
1540
Aart Bik854a02b2015-07-14 16:07:00 -07001541 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001542 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001543 }
1544
Roland Levillain0d5a2812015-11-13 10:07:31 +00001545 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001546 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001547 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1548 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001549 }
1550
1551 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001552 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001553 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001554 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001555 for (int s = kLastBit; s >= 0; s--) {
1556 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1557 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1558 // This is a bit for the GC side effect.
1559 if (current_bit_is_set) {
1560 flags += "GC";
1561 }
Aart Bik854a02b2015-07-14 16:07:00 -07001562 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001563 } else {
1564 // This is a bit for the array/field analysis.
1565 // The underscore character stands for the 'can trigger GC' bit.
1566 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1567 if (current_bit_is_set) {
1568 flags += kDebug[s];
1569 }
1570 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1571 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1572 flags += "|";
1573 }
1574 }
Aart Bik854a02b2015-07-14 16:07:00 -07001575 }
1576 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001577 }
1578
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001579 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001580
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001581 private:
1582 static constexpr int kFieldArrayAnalysisBits = 9;
1583
1584 static constexpr int kFieldWriteOffset = 0;
1585 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1586 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1587 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1588
1589 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1590
1591 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1592 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1593 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1594 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1595
1596 static constexpr int kLastBit = kDependsOnGCBit;
1597 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1598
1599 // Aliases.
1600
1601 static_assert(kChangeBits == kDependOnBits,
1602 "the 'change' bits should match the 'depend on' bits.");
1603
1604 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1605 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1606 static constexpr uint64_t kAllWrites =
1607 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1608 static constexpr uint64_t kAllReads =
1609 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001610
Aart Bik854a02b2015-07-14 16:07:00 -07001611 // Translates type to bit flag.
1612 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1613 CHECK_NE(type, Primitive::kPrimVoid);
1614 const uint64_t one = 1;
1615 const int shift = type; // 0-based consecutive enum
1616 DCHECK_LE(kFieldWriteOffset, shift);
1617 DCHECK_LT(shift, kArrayWriteOffset);
1618 return one << (type + offset);
1619 }
1620
1621 // Private constructor on direct flags value.
1622 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1623
1624 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001625};
1626
David Brazdiled596192015-01-23 10:39:45 +00001627// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001628class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001629 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001630 HEnvironment(ArenaAllocator* arena,
1631 size_t number_of_vregs,
1632 const DexFile& dex_file,
1633 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001634 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001635 InvokeType invoke_type,
1636 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001637 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1638 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001639 parent_(nullptr),
1640 dex_file_(dex_file),
1641 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001642 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001643 invoke_type_(invoke_type),
1644 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001645 }
1646
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001647 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001648 : HEnvironment(arena,
1649 to_copy.Size(),
1650 to_copy.GetDexFile(),
1651 to_copy.GetMethodIdx(),
1652 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001653 to_copy.GetInvokeType(),
1654 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001655
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001656 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001657 if (parent_ != nullptr) {
1658 parent_->SetAndCopyParentChain(allocator, parent);
1659 } else {
1660 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1661 parent_->CopyFrom(parent);
1662 if (parent->GetParent() != nullptr) {
1663 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1664 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001665 }
David Brazdiled596192015-01-23 10:39:45 +00001666 }
1667
Vladimir Marko71bf8092015-09-15 15:33:14 +01001668 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001669 void CopyFrom(HEnvironment* environment);
1670
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001671 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1672 // input to the loop phi instead. This is for inserting instructions that
1673 // require an environment (like HDeoptimization) in the loop pre-header.
1674 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001675
1676 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001677 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001678 }
1679
1680 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001681 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001682 }
1683
David Brazdil1abb4192015-02-17 18:33:36 +00001684 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001685
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001686 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001687
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001688 HEnvironment* GetParent() const { return parent_; }
1689
1690 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001691 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001692 }
1693
1694 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001695 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001696 }
1697
1698 uint32_t GetDexPc() const {
1699 return dex_pc_;
1700 }
1701
1702 uint32_t GetMethodIdx() const {
1703 return method_idx_;
1704 }
1705
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001706 InvokeType GetInvokeType() const {
1707 return invoke_type_;
1708 }
1709
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001710 const DexFile& GetDexFile() const {
1711 return dex_file_;
1712 }
1713
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001714 HInstruction* GetHolder() const {
1715 return holder_;
1716 }
1717
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001718
1719 bool IsFromInlinedInvoke() const {
1720 return GetParent() != nullptr;
1721 }
1722
David Brazdiled596192015-01-23 10:39:45 +00001723 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001724 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1725 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001726 HEnvironment* parent_;
1727 const DexFile& dex_file_;
1728 const uint32_t method_idx_;
1729 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001730 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001731
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001732 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001733 HInstruction* const holder_;
1734
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001735 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001736
1737 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1738};
1739
Vladimir Markof9f64412015-09-02 14:05:49 +01001740class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001741 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001742 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001743 : previous_(nullptr),
1744 next_(nullptr),
1745 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001746 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001747 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001748 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001749 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001750 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001751 locations_(nullptr),
1752 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001753 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001754 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001755 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1756 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1757 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001758
Dave Allison20dfc792014-06-16 20:44:29 -07001759 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001760
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001761#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001762 enum InstructionKind {
1763 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1764 };
1765#undef DECLARE_KIND
1766
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001767 HInstruction* GetNext() const { return next_; }
1768 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001769
Calin Juravle77520bc2015-01-12 18:45:46 +00001770 HInstruction* GetNextDisregardingMoves() const;
1771 HInstruction* GetPreviousDisregardingMoves() const;
1772
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001773 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001774 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001775 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001776 bool IsInBlock() const { return block_ != nullptr; }
1777 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001778 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1779 bool IsIrreducibleLoopHeaderPhi() const {
1780 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1781 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001782
Vladimir Marko372f10e2016-05-17 16:30:10 +01001783 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1784
1785 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1786 // One virtual method is enough, just const_cast<> and then re-add the const.
1787 return ArrayRef<const HUserRecord<HInstruction*>>(
1788 const_cast<HInstruction*>(this)->GetInputRecords());
1789 }
1790
1791 auto GetInputs() {
1792 return MakeTransformArrayRef(
1793 GetInputRecords(),
1794 [](HUserRecord<HInstruction*>& record) -> HInstruction* {
1795 return record.GetInstruction();
1796 });
1797 }
1798
1799 auto GetInputs() const {
1800 return MakeTransformArrayRef(
1801 GetInputRecords(),
1802 [](const HUserRecord<HInstruction*>& record) -> const HInstruction* {
1803 return record.GetInstruction();
1804 });
1805 }
1806
1807 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001808 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001809
Vladimir Marko372f10e2016-05-17 16:30:10 +01001810 void SetRawInputAt(size_t index, HInstruction* input) {
1811 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1812 }
1813
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001814 virtual void Accept(HGraphVisitor* visitor) = 0;
1815 virtual const char* DebugName() const = 0;
1816
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001817 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1818
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001819 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001820
1821 uint32_t GetDexPc() const { return dex_pc_; }
1822
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001823 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001824
Roland Levillaine161a2a2014-10-03 12:45:18 +01001825 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001826 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001827
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001828 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001829 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001830
Calin Juravle10e244f2015-01-26 18:54:32 +00001831 // Does not apply for all instructions, but having this at top level greatly
1832 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001833 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001834 virtual bool CanBeNull() const {
1835 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1836 return true;
1837 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001838
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001839 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001840 return false;
1841 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001842
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001843 virtual bool IsActualObject() const {
1844 return GetType() == Primitive::kPrimNot;
1845 }
1846
Calin Juravle2e768302015-07-28 14:41:11 +00001847 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001848
Calin Juravle61d544b2015-02-23 16:46:57 +00001849 ReferenceTypeInfo GetReferenceTypeInfo() const {
1850 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001851 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001852 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001853 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001854
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001855 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001856 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001857 // Note: fixup_end remains valid across push_front().
1858 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1859 HUseListNode<HInstruction*>* new_node =
1860 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1861 uses_.push_front(*new_node);
1862 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001863 }
1864
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001865 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001866 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001867 // Note: env_fixup_end remains valid across push_front().
1868 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1869 HUseListNode<HEnvironment*>* new_node =
1870 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1871 env_uses_.push_front(*new_node);
1872 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001873 }
1874
David Brazdil1abb4192015-02-17 18:33:36 +00001875 void RemoveAsUserOfInput(size_t input) {
1876 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001877 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1878 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1879 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001880 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001881
Vladimir Marko372f10e2016-05-17 16:30:10 +01001882 void RemoveAsUserOfAllInputs() {
1883 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1884 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1885 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1886 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1887 }
1888 }
1889
David Brazdil1abb4192015-02-17 18:33:36 +00001890 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1891 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001892
Vladimir Marko46817b82016-03-29 12:21:58 +01001893 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1894 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1895 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001896 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001897 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001898 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001899
Roland Levillain6c82d402014-10-13 16:10:27 +01001900 // Does this instruction strictly dominate `other_instruction`?
1901 // Returns false if this instruction and `other_instruction` are the same.
1902 // Aborts if this instruction and `other_instruction` are both phis.
1903 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001904
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001905 int GetId() const { return id_; }
1906 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001907
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001908 int GetSsaIndex() const { return ssa_index_; }
1909 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1910 bool HasSsaIndex() const { return ssa_index_ != -1; }
1911
1912 bool HasEnvironment() const { return environment_ != nullptr; }
1913 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001914 // Set the `environment_` field. Raw because this method does not
1915 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001916 void SetRawEnvironment(HEnvironment* environment) {
1917 DCHECK(environment_ == nullptr);
1918 DCHECK_EQ(environment->GetHolder(), this);
1919 environment_ = environment;
1920 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001921
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001922 void RemoveEnvironment();
1923
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001924 // Set the environment of this instruction, copying it from `environment`. While
1925 // copying, the uses lists are being updated.
1926 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001927 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001928 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001929 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001930 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001931 if (environment->GetParent() != nullptr) {
1932 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1933 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001934 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001935
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001936 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1937 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001938 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001939 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001940 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001941 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001942 if (environment->GetParent() != nullptr) {
1943 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1944 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001945 }
1946
Nicolas Geoffray39468442014-09-02 15:17:15 +01001947 // Returns the number of entries in the environment. Typically, that is the
1948 // number of dex registers in a method. It could be more in case of inlining.
1949 size_t EnvironmentSize() const;
1950
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001951 LocationSummary* GetLocations() const { return locations_; }
1952 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001953
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001954 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001955 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001956
Alexandre Rames188d4312015-04-09 18:30:21 +01001957 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1958 // uses of this instruction by `other` are *not* updated.
1959 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1960 ReplaceWith(other);
1961 other->ReplaceInput(this, use_index);
1962 }
1963
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001964 // Move `this` instruction before `cursor`.
1965 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001966
Vladimir Markofb337ea2015-11-25 15:25:10 +00001967 // Move `this` before its first user and out of any loops. If there is no
1968 // out-of-loop user that dominates all other users, move the instruction
1969 // to the end of the out-of-loop common dominator of the user's blocks.
1970 //
1971 // This can be used only on non-throwing instructions with no side effects that
1972 // have at least one use but no environment uses.
1973 void MoveBeforeFirstUserAndOutOfLoops();
1974
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001975#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001976 bool Is##type() const; \
1977 const H##type* As##type() const; \
1978 H##type* As##type();
1979
1980 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1981#undef INSTRUCTION_TYPE_CHECK
1982
1983#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001984 bool Is##type() const { return (As##type() != nullptr); } \
1985 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001986 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001987 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001988#undef INSTRUCTION_TYPE_CHECK
1989
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001990 // Returns whether the instruction can be moved within the graph.
1991 virtual bool CanBeMoved() const { return false; }
1992
1993 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01001994 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001995 return false;
1996 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001997
1998 // Returns whether any data encoded in the two instructions is equal.
1999 // This method does not look at the inputs. Both instructions must be
2000 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002001 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002002 return false;
2003 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002004
2005 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002006 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002007 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002008 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002009
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002010 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2011 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2012 // the virtual function because the __attribute__((__pure__)) doesn't really
2013 // apply the strong requirement for virtual functions, preventing optimizations.
2014 InstructionKind GetKind() const PURE;
2015 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002016
2017 virtual size_t ComputeHashCode() const {
2018 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002019 for (const HInstruction* input : GetInputs()) {
2020 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002021 }
2022 return result;
2023 }
2024
2025 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002026 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002027 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002028
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002029 size_t GetLifetimePosition() const { return lifetime_position_; }
2030 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2031 LiveInterval* GetLiveInterval() const { return live_interval_; }
2032 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2033 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2034
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002035 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2036
2037 // Returns whether the code generation of the instruction will require to have access
2038 // to the current method. Such instructions are:
2039 // (1): Instructions that require an environment, as calling the runtime requires
2040 // to walk the stack and have the current method stored at a specific stack address.
2041 // (2): Object literals like classes and strings, that are loaded from the dex cache
2042 // fields of the current method.
2043 bool NeedsCurrentMethod() const {
2044 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2045 }
2046
Vladimir Markodc151b22015-10-15 18:02:30 +01002047 // Returns whether the code generation of the instruction will require to have access
2048 // to the dex cache of the current method's declaring class via the current method.
2049 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002050
Mark Mendellc4701932015-04-10 13:18:51 -04002051 // Does this instruction have any use in an environment before
2052 // control flow hits 'other'?
2053 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2054
2055 // Remove all references to environment uses of this instruction.
2056 // The caller must ensure that this is safe to do.
2057 void RemoveEnvironmentUsers();
2058
Vladimir Markoa1de9182016-02-25 11:37:38 +00002059 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2060 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002061
David Brazdil1abb4192015-02-17 18:33:36 +00002062 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002063 // If set, the machine code for this instruction is assumed to be generated by
2064 // its users. Used by liveness analysis to compute use positions accordingly.
2065 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2066 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2067 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2068 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2069
Vladimir Marko372f10e2016-05-17 16:30:10 +01002070 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2071 return GetInputRecords()[i];
2072 }
2073
2074 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2075 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2076 input_records[index] = input;
2077 }
David Brazdil1abb4192015-02-17 18:33:36 +00002078
Vladimir Markoa1de9182016-02-25 11:37:38 +00002079 uint32_t GetPackedFields() const {
2080 return packed_fields_;
2081 }
2082
2083 template <size_t flag>
2084 bool GetPackedFlag() const {
2085 return (packed_fields_ & (1u << flag)) != 0u;
2086 }
2087
2088 template <size_t flag>
2089 void SetPackedFlag(bool value = true) {
2090 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2091 }
2092
2093 template <typename BitFieldType>
2094 typename BitFieldType::value_type GetPackedField() const {
2095 return BitFieldType::Decode(packed_fields_);
2096 }
2097
2098 template <typename BitFieldType>
2099 void SetPackedField(typename BitFieldType::value_type value) {
2100 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2101 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2102 }
2103
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002104 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002105 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2106 auto before_use_node = uses_.before_begin();
2107 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2108 HInstruction* user = use_node->GetUser();
2109 size_t input_index = use_node->GetIndex();
2110 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2111 before_use_node = use_node;
2112 }
2113 }
2114
2115 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2116 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2117 if (next != uses_.end()) {
2118 HInstruction* next_user = next->GetUser();
2119 size_t next_index = next->GetIndex();
2120 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2121 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2122 }
2123 }
2124
2125 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2126 auto before_env_use_node = env_uses_.before_begin();
2127 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2128 HEnvironment* user = env_use_node->GetUser();
2129 size_t input_index = env_use_node->GetIndex();
2130 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2131 before_env_use_node = env_use_node;
2132 }
2133 }
2134
2135 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2136 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2137 if (next != env_uses_.end()) {
2138 HEnvironment* next_user = next->GetUser();
2139 size_t next_index = next->GetIndex();
2140 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2141 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2142 }
2143 }
David Brazdil1abb4192015-02-17 18:33:36 +00002144
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002145 HInstruction* previous_;
2146 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002147 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002148 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002149
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002150 // An instruction gets an id when it is added to the graph.
2151 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002152 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002153 int id_;
2154
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002155 // When doing liveness analysis, instructions that have uses get an SSA index.
2156 int ssa_index_;
2157
Vladimir Markoa1de9182016-02-25 11:37:38 +00002158 // Packed fields.
2159 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002160
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002161 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002162 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002163
2164 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002165 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002166
Nicolas Geoffray39468442014-09-02 15:17:15 +01002167 // The environment associated with this instruction. Not null if the instruction
2168 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002169 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002170
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002171 // Set by the code generator.
2172 LocationSummary* locations_;
2173
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002174 // Set by the liveness analysis.
2175 LiveInterval* live_interval_;
2176
2177 // Set by the liveness analysis, this is the position in a linear
2178 // order of blocks where this instruction's live interval start.
2179 size_t lifetime_position_;
2180
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002181 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002182
Vladimir Markoa1de9182016-02-25 11:37:38 +00002183 // The reference handle part of the reference type info.
2184 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002185 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002186 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002187
David Brazdil1abb4192015-02-17 18:33:36 +00002188 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002189 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002190 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002191 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002192 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002193
2194 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2195};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002196std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002197
2198class HInstructionIterator : public ValueObject {
2199 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002200 explicit HInstructionIterator(const HInstructionList& instructions)
2201 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002202 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002203 }
2204
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002205 bool Done() const { return instruction_ == nullptr; }
2206 HInstruction* Current() const { return instruction_; }
2207 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002208 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002209 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002210 }
2211
2212 private:
2213 HInstruction* instruction_;
2214 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002215
2216 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002217};
2218
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002219class HBackwardInstructionIterator : public ValueObject {
2220 public:
2221 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2222 : instruction_(instructions.last_instruction_) {
2223 next_ = Done() ? nullptr : instruction_->GetPrevious();
2224 }
2225
2226 bool Done() const { return instruction_ == nullptr; }
2227 HInstruction* Current() const { return instruction_; }
2228 void Advance() {
2229 instruction_ = next_;
2230 next_ = Done() ? nullptr : instruction_->GetPrevious();
2231 }
2232
2233 private:
2234 HInstruction* instruction_;
2235 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002236
2237 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002238};
2239
Vladimir Markof9f64412015-09-02 14:05:49 +01002240template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002241class HTemplateInstruction: public HInstruction {
2242 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002243 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002244 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002245 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002246
Vladimir Marko372f10e2016-05-17 16:30:10 +01002247 using HInstruction::GetInputRecords; // Keep the const version visible.
2248 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2249 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002250 }
2251
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002252 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002253 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002254
2255 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002256};
2257
Vladimir Markof9f64412015-09-02 14:05:49 +01002258// HTemplateInstruction specialization for N=0.
2259template<>
2260class HTemplateInstruction<0>: public HInstruction {
2261 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002262 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002263 : HInstruction(side_effects, dex_pc) {}
2264
Vladimir Markof9f64412015-09-02 14:05:49 +01002265 virtual ~HTemplateInstruction() {}
2266
Vladimir Marko372f10e2016-05-17 16:30:10 +01002267 using HInstruction::GetInputRecords; // Keep the const version visible.
2268 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2269 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002270 }
2271
2272 private:
2273 friend class SsaBuilder;
2274};
2275
Dave Allison20dfc792014-06-16 20:44:29 -07002276template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002277class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002278 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002279 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002280 : HTemplateInstruction<N>(side_effects, dex_pc) {
2281 this->template SetPackedField<TypeField>(type);
2282 }
Dave Allison20dfc792014-06-16 20:44:29 -07002283 virtual ~HExpression() {}
2284
Vladimir Markoa1de9182016-02-25 11:37:38 +00002285 Primitive::Type GetType() const OVERRIDE {
2286 return TypeField::Decode(this->GetPackedFields());
2287 }
Dave Allison20dfc792014-06-16 20:44:29 -07002288
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002289 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002290 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2291 static constexpr size_t kFieldTypeSize =
2292 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2293 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2294 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2295 "Too many packed fields.");
2296 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002297};
2298
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002299// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2300// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002301class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002302 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002303 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2304 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002305
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002306 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002307
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002308 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002309
2310 private:
2311 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2312};
2313
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002314// Represents dex's RETURN opcodes. A HReturn is a control flow
2315// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002316class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002317 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002318 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2319 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002320 SetRawInputAt(0, value);
2321 }
2322
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002323 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002324
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002325 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002326
2327 private:
2328 DISALLOW_COPY_AND_ASSIGN(HReturn);
2329};
2330
Vladimir Markofcb503c2016-05-18 12:48:17 +01002331class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002332 public:
2333 HPhi(ArenaAllocator* arena,
2334 uint32_t reg_number,
2335 size_t number_of_inputs,
2336 Primitive::Type type,
2337 uint32_t dex_pc = kNoDexPc)
2338 : HInstruction(SideEffects::None(), dex_pc),
2339 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2340 reg_number_(reg_number) {
2341 SetPackedField<TypeField>(ToPhiType(type));
2342 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2343 // Phis are constructed live and marked dead if conflicting or unused.
2344 // Individual steps of SsaBuilder should assume that if a phi has been
2345 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2346 SetPackedFlag<kFlagIsLive>(true);
2347 SetPackedFlag<kFlagCanBeNull>(true);
2348 }
2349
2350 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2351 static Primitive::Type ToPhiType(Primitive::Type type) {
2352 return Primitive::PrimitiveKind(type);
2353 }
2354
2355 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2356
Vladimir Marko372f10e2016-05-17 16:30:10 +01002357 using HInstruction::GetInputRecords; // Keep the const version visible.
2358 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2359 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2360 }
David Brazdildee58d62016-04-07 09:54:26 +00002361
2362 void AddInput(HInstruction* input);
2363 void RemoveInputAt(size_t index);
2364
2365 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2366 void SetType(Primitive::Type new_type) {
2367 // Make sure that only valid type changes occur. The following are allowed:
2368 // (1) int -> float/ref (primitive type propagation),
2369 // (2) long -> double (primitive type propagation).
2370 DCHECK(GetType() == new_type ||
2371 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2372 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2373 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2374 SetPackedField<TypeField>(new_type);
2375 }
2376
2377 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2378 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2379
2380 uint32_t GetRegNumber() const { return reg_number_; }
2381
2382 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2383 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2384 bool IsDead() const { return !IsLive(); }
2385 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2386
2387 bool IsVRegEquivalentOf(HInstruction* other) const {
2388 return other != nullptr
2389 && other->IsPhi()
2390 && other->AsPhi()->GetBlock() == GetBlock()
2391 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2392 }
2393
2394 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2395 // An equivalent phi is a phi having the same dex register and type.
2396 // It assumes that phis with the same dex register are adjacent.
2397 HPhi* GetNextEquivalentPhiWithSameType() {
2398 HInstruction* next = GetNext();
2399 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2400 if (next->GetType() == GetType()) {
2401 return next->AsPhi();
2402 }
2403 next = next->GetNext();
2404 }
2405 return nullptr;
2406 }
2407
2408 DECLARE_INSTRUCTION(Phi);
2409
David Brazdildee58d62016-04-07 09:54:26 +00002410 private:
2411 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2412 static constexpr size_t kFieldTypeSize =
2413 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2414 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2415 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2416 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2417 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2418 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2419
Vladimir Marko372f10e2016-05-17 16:30:10 +01002420 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002421 const uint32_t reg_number_;
2422
2423 DISALLOW_COPY_AND_ASSIGN(HPhi);
2424};
2425
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002426// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002427// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002428// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002429class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002430 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002431 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002432
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002433 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002434
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002435 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002436
2437 private:
2438 DISALLOW_COPY_AND_ASSIGN(HExit);
2439};
2440
2441// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002442class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002443 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002444 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002445
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002446 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002447
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002448 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002449 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002450 }
2451
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002452 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002453
2454 private:
2455 DISALLOW_COPY_AND_ASSIGN(HGoto);
2456};
2457
Roland Levillain9867bc72015-08-05 10:21:34 +01002458class HConstant : public HExpression<0> {
2459 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002460 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2461 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002462
2463 bool CanBeMoved() const OVERRIDE { return true; }
2464
Roland Levillain1a653882016-03-18 18:05:57 +00002465 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002466 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002467 // Is this constant 0 in the arithmetic sense?
2468 virtual bool IsArithmeticZero() const { return false; }
2469 // Is this constant a 0-bit pattern?
2470 virtual bool IsZeroBitPattern() const { return false; }
2471 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002472 virtual bool IsOne() const { return false; }
2473
David Brazdil77a48ae2015-09-15 12:34:04 +00002474 virtual uint64_t GetValueAsUint64() const = 0;
2475
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002476 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002477
2478 private:
2479 DISALLOW_COPY_AND_ASSIGN(HConstant);
2480};
2481
Vladimir Markofcb503c2016-05-18 12:48:17 +01002482class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002483 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002484 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002485 return true;
2486 }
2487
David Brazdil77a48ae2015-09-15 12:34:04 +00002488 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2489
Roland Levillain9867bc72015-08-05 10:21:34 +01002490 size_t ComputeHashCode() const OVERRIDE { return 0; }
2491
Roland Levillain1a653882016-03-18 18:05:57 +00002492 // The null constant representation is a 0-bit pattern.
2493 virtual bool IsZeroBitPattern() const { return true; }
2494
Roland Levillain9867bc72015-08-05 10:21:34 +01002495 DECLARE_INSTRUCTION(NullConstant);
2496
2497 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002498 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002499
2500 friend class HGraph;
2501 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2502};
2503
2504// Constants of the type int. Those can be from Dex instructions, or
2505// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002506class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002507 public:
2508 int32_t GetValue() const { return value_; }
2509
David Brazdil9f389d42015-10-01 14:32:56 +01002510 uint64_t GetValueAsUint64() const OVERRIDE {
2511 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2512 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002513
Vladimir Marko372f10e2016-05-17 16:30:10 +01002514 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002515 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002516 return other->AsIntConstant()->value_ == value_;
2517 }
2518
2519 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2520
2521 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002522 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2523 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002524 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2525
Roland Levillain1a653882016-03-18 18:05:57 +00002526 // Integer constants are used to encode Boolean values as well,
2527 // where 1 means true and 0 means false.
2528 bool IsTrue() const { return GetValue() == 1; }
2529 bool IsFalse() const { return GetValue() == 0; }
2530
Roland Levillain9867bc72015-08-05 10:21:34 +01002531 DECLARE_INSTRUCTION(IntConstant);
2532
2533 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002534 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2535 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2536 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2537 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002538
2539 const int32_t value_;
2540
2541 friend class HGraph;
2542 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2543 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2544 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2545};
2546
Vladimir Markofcb503c2016-05-18 12:48:17 +01002547class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002548 public:
2549 int64_t GetValue() const { return value_; }
2550
David Brazdil77a48ae2015-09-15 12:34:04 +00002551 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2552
Vladimir Marko372f10e2016-05-17 16:30:10 +01002553 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002554 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002555 return other->AsLongConstant()->value_ == value_;
2556 }
2557
2558 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2559
2560 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002561 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2562 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002563 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2564
2565 DECLARE_INSTRUCTION(LongConstant);
2566
2567 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002568 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2569 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002570
2571 const int64_t value_;
2572
2573 friend class HGraph;
2574 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2575};
Dave Allison20dfc792014-06-16 20:44:29 -07002576
Vladimir Markofcb503c2016-05-18 12:48:17 +01002577class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002578 public:
2579 float GetValue() const { return value_; }
2580
2581 uint64_t GetValueAsUint64() const OVERRIDE {
2582 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2583 }
2584
Vladimir Marko372f10e2016-05-17 16:30:10 +01002585 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002586 DCHECK(other->IsFloatConstant()) << other->DebugName();
2587 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2588 }
2589
2590 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2591
2592 bool IsMinusOne() const OVERRIDE {
2593 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2594 }
Roland Levillain1a653882016-03-18 18:05:57 +00002595 bool IsArithmeticZero() const OVERRIDE {
2596 return std::fpclassify(value_) == FP_ZERO;
2597 }
2598 bool IsArithmeticPositiveZero() const {
2599 return IsArithmeticZero() && !std::signbit(value_);
2600 }
2601 bool IsArithmeticNegativeZero() const {
2602 return IsArithmeticZero() && std::signbit(value_);
2603 }
2604 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002605 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002606 }
2607 bool IsOne() const OVERRIDE {
2608 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2609 }
2610 bool IsNaN() const {
2611 return std::isnan(value_);
2612 }
2613
2614 DECLARE_INSTRUCTION(FloatConstant);
2615
2616 private:
2617 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2618 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2619 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2620 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2621
2622 const float value_;
2623
2624 // Only the SsaBuilder and HGraph can create floating-point constants.
2625 friend class SsaBuilder;
2626 friend class HGraph;
2627 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2628};
2629
Vladimir Markofcb503c2016-05-18 12:48:17 +01002630class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002631 public:
2632 double GetValue() const { return value_; }
2633
2634 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2635
Vladimir Marko372f10e2016-05-17 16:30:10 +01002636 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002637 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2638 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2639 }
2640
2641 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2642
2643 bool IsMinusOne() const OVERRIDE {
2644 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2645 }
Roland Levillain1a653882016-03-18 18:05:57 +00002646 bool IsArithmeticZero() const OVERRIDE {
2647 return std::fpclassify(value_) == FP_ZERO;
2648 }
2649 bool IsArithmeticPositiveZero() const {
2650 return IsArithmeticZero() && !std::signbit(value_);
2651 }
2652 bool IsArithmeticNegativeZero() const {
2653 return IsArithmeticZero() && std::signbit(value_);
2654 }
2655 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002656 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002657 }
2658 bool IsOne() const OVERRIDE {
2659 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2660 }
2661 bool IsNaN() const {
2662 return std::isnan(value_);
2663 }
2664
2665 DECLARE_INSTRUCTION(DoubleConstant);
2666
2667 private:
2668 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2669 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2670 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2671 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2672
2673 const double value_;
2674
2675 // Only the SsaBuilder and HGraph can create floating-point constants.
2676 friend class SsaBuilder;
2677 friend class HGraph;
2678 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2679};
2680
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002681// Conditional branch. A block ending with an HIf instruction must have
2682// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002683class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002684 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002685 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2686 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002687 SetRawInputAt(0, input);
2688 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002689
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002690 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002691
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002692 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002693 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002694 }
2695
2696 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002697 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002698 }
2699
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002700 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002701
2702 private:
2703 DISALLOW_COPY_AND_ASSIGN(HIf);
2704};
2705
David Brazdilfc6a86a2015-06-26 10:33:45 +00002706
2707// Abstract instruction which marks the beginning and/or end of a try block and
2708// links it to the respective exception handlers. Behaves the same as a Goto in
2709// non-exceptional control flow.
2710// Normal-flow successor is stored at index zero, exception handlers under
2711// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002712class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002713 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002714 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002715 kEntry,
2716 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002717 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002718 };
2719
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002720 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002721 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2722 SetPackedField<BoundaryKindField>(kind);
2723 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002724
2725 bool IsControlFlow() const OVERRIDE { return true; }
2726
2727 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002728 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002729
David Brazdild26a4112015-11-10 11:07:31 +00002730 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2731 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2732 }
2733
David Brazdilfc6a86a2015-06-26 10:33:45 +00002734 // Returns whether `handler` is among its exception handlers (non-zero index
2735 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002736 bool HasExceptionHandler(const HBasicBlock& handler) const {
2737 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002738 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002739 }
2740
2741 // If not present already, adds `handler` to its block's list of exception
2742 // handlers.
2743 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002744 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002745 GetBlock()->AddSuccessor(handler);
2746 }
2747 }
2748
Vladimir Markoa1de9182016-02-25 11:37:38 +00002749 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2750 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002751
David Brazdilffee3d32015-07-06 11:48:53 +01002752 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2753
David Brazdilfc6a86a2015-06-26 10:33:45 +00002754 DECLARE_INSTRUCTION(TryBoundary);
2755
2756 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002757 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2758 static constexpr size_t kFieldBoundaryKindSize =
2759 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2760 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2761 kFieldBoundaryKind + kFieldBoundaryKindSize;
2762 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2763 "Too many packed fields.");
2764 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002765
2766 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2767};
2768
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002769// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002770class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002771 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002772 // We set CanTriggerGC to prevent any intermediate address to be live
2773 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002774 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002775 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002776 SetRawInputAt(0, cond);
2777 }
2778
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002779 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002780 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002781 return true;
2782 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002783 bool NeedsEnvironment() const OVERRIDE { return true; }
2784 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002785
2786 DECLARE_INSTRUCTION(Deoptimize);
2787
2788 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002789 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2790};
2791
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002792// Represents the ArtMethod that was passed as a first argument to
2793// the method. It is used by instructions that depend on it, like
2794// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002795class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002796 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002797 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2798 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002799
2800 DECLARE_INSTRUCTION(CurrentMethod);
2801
2802 private:
2803 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2804};
2805
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002806// Fetches an ArtMethod from the virtual table or the interface method table
2807// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002808class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002809 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002810 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002811 kVTable,
2812 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002813 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002814 };
2815 HClassTableGet(HInstruction* cls,
2816 Primitive::Type type,
2817 TableKind kind,
2818 size_t index,
2819 uint32_t dex_pc)
2820 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002821 index_(index) {
2822 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002823 SetRawInputAt(0, cls);
2824 }
2825
2826 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002827 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002828 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002829 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002830 }
2831
Vladimir Markoa1de9182016-02-25 11:37:38 +00002832 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002833 size_t GetIndex() const { return index_; }
2834
2835 DECLARE_INSTRUCTION(ClassTableGet);
2836
2837 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002838 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2839 static constexpr size_t kFieldTableKindSize =
2840 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2841 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2842 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2843 "Too many packed fields.");
2844 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2845
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002846 // The index of the ArtMethod in the table.
2847 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002848
2849 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2850};
2851
Mark Mendellfe57faa2015-09-18 09:26:15 -04002852// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2853// have one successor for each entry in the switch table, and the final successor
2854// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002855class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002856 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002857 HPackedSwitch(int32_t start_value,
2858 uint32_t num_entries,
2859 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002860 uint32_t dex_pc = kNoDexPc)
2861 : HTemplateInstruction(SideEffects::None(), dex_pc),
2862 start_value_(start_value),
2863 num_entries_(num_entries) {
2864 SetRawInputAt(0, input);
2865 }
2866
2867 bool IsControlFlow() const OVERRIDE { return true; }
2868
2869 int32_t GetStartValue() const { return start_value_; }
2870
Vladimir Marko211c2112015-09-24 16:52:33 +01002871 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002872
2873 HBasicBlock* GetDefaultBlock() const {
2874 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002875 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002876 }
2877 DECLARE_INSTRUCTION(PackedSwitch);
2878
2879 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002880 const int32_t start_value_;
2881 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002882
2883 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2884};
2885
Roland Levillain88cb1752014-10-20 16:36:47 +01002886class HUnaryOperation : public HExpression<1> {
2887 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002888 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2889 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002890 SetRawInputAt(0, input);
2891 }
2892
2893 HInstruction* GetInput() const { return InputAt(0); }
2894 Primitive::Type GetResultType() const { return GetType(); }
2895
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002896 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002897 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002898 return true;
2899 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002900
Roland Levillain31dd3d62016-02-16 12:21:02 +00002901 // Try to statically evaluate `this` and return a HConstant
2902 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002903 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002904 HConstant* TryStaticEvaluation() const;
2905
2906 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002907 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2908 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002909 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2910 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002911
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002912 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002913
2914 private:
2915 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2916};
2917
Dave Allison20dfc792014-06-16 20:44:29 -07002918class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002919 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002920 HBinaryOperation(Primitive::Type result_type,
2921 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002922 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002923 SideEffects side_effects = SideEffects::None(),
2924 uint32_t dex_pc = kNoDexPc)
2925 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002926 SetRawInputAt(0, left);
2927 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002928 }
2929
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002930 HInstruction* GetLeft() const { return InputAt(0); }
2931 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002932 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002933
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002934 virtual bool IsCommutative() const { return false; }
2935
2936 // Put constant on the right.
2937 // Returns whether order is changed.
2938 bool OrderInputsWithConstantOnTheRight() {
2939 HInstruction* left = InputAt(0);
2940 HInstruction* right = InputAt(1);
2941 if (left->IsConstant() && !right->IsConstant()) {
2942 ReplaceInput(right, 0);
2943 ReplaceInput(left, 1);
2944 return true;
2945 }
2946 return false;
2947 }
2948
2949 // Order inputs by instruction id, but favor constant on the right side.
2950 // This helps GVN for commutative ops.
2951 void OrderInputs() {
2952 DCHECK(IsCommutative());
2953 HInstruction* left = InputAt(0);
2954 HInstruction* right = InputAt(1);
2955 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2956 return;
2957 }
2958 if (OrderInputsWithConstantOnTheRight()) {
2959 return;
2960 }
2961 // Order according to instruction id.
2962 if (left->GetId() > right->GetId()) {
2963 ReplaceInput(right, 0);
2964 ReplaceInput(left, 1);
2965 }
2966 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002967
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002968 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002969 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002970 return true;
2971 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002972
Roland Levillain31dd3d62016-02-16 12:21:02 +00002973 // Try to statically evaluate `this` and return a HConstant
2974 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002975 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002976 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002977
2978 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002979 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2980 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002981 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2982 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002983 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002984 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2985 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002986 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2987 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002988 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2989 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01002990 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002991 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2992 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002993
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002994 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002995 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002996 HConstant* GetConstantRight() const;
2997
2998 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002999 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003000 HInstruction* GetLeastConstantLeft() const;
3001
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003002 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003003
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003004 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003005 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3006};
3007
Mark Mendellc4701932015-04-10 13:18:51 -04003008// The comparison bias applies for floating point operations and indicates how NaN
3009// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003010enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003011 kNoBias, // bias is not applicable (i.e. for long operation)
3012 kGtBias, // return 1 for NaN comparisons
3013 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003014 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003015};
3016
Roland Levillain31dd3d62016-02-16 12:21:02 +00003017std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3018
Dave Allison20dfc792014-06-16 20:44:29 -07003019class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003020 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003021 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003022 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3023 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3024 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003025
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003026 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003027 // `instruction`, and disregard moves in between.
3028 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003029
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003030 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003031
3032 virtual IfCondition GetCondition() const = 0;
3033
Mark Mendellc4701932015-04-10 13:18:51 -04003034 virtual IfCondition GetOppositeCondition() const = 0;
3035
Vladimir Markoa1de9182016-02-25 11:37:38 +00003036 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003037 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3038
Vladimir Markoa1de9182016-02-25 11:37:38 +00003039 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3040 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003041
Vladimir Marko372f10e2016-05-17 16:30:10 +01003042 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003043 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003044 }
3045
Roland Levillain4fa13f62015-07-06 18:11:54 +01003046 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003047 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003048 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003049 if (if_cond == kCondNE) {
3050 return true;
3051 } else if (if_cond == kCondEQ) {
3052 return false;
3053 }
3054 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003055 }
3056
3057 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003058 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003059 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003060 if (if_cond == kCondEQ) {
3061 return true;
3062 } else if (if_cond == kCondNE) {
3063 return false;
3064 }
3065 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003066 }
3067
Roland Levillain31dd3d62016-02-16 12:21:02 +00003068 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003069 // Needed if we merge a HCompare into a HCondition.
3070 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3071 static constexpr size_t kFieldComparisonBiasSize =
3072 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3073 static constexpr size_t kNumberOfConditionPackedBits =
3074 kFieldComparisonBias + kFieldComparisonBiasSize;
3075 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3076 using ComparisonBiasField =
3077 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3078
Roland Levillain31dd3d62016-02-16 12:21:02 +00003079 template <typename T>
3080 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3081
3082 template <typename T>
3083 int32_t CompareFP(T x, T y) const {
3084 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3085 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3086 // Handle the bias.
3087 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3088 }
3089
3090 // Return an integer constant containing the result of a condition evaluated at compile time.
3091 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3092 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3093 }
3094
Dave Allison20dfc792014-06-16 20:44:29 -07003095 private:
3096 DISALLOW_COPY_AND_ASSIGN(HCondition);
3097};
3098
3099// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003100class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003101 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003102 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3103 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003104
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003105 bool IsCommutative() const OVERRIDE { return true; }
3106
Vladimir Marko9e23df52015-11-10 17:14:35 +00003107 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3108 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003109 return MakeConstantCondition(true, GetDexPc());
3110 }
3111 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3112 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3113 }
3114 // In the following Evaluate methods, a HCompare instruction has
3115 // been merged into this HEqual instruction; evaluate it as
3116 // `Compare(x, y) == 0`.
3117 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3118 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3119 GetDexPc());
3120 }
3121 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3122 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3123 }
3124 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3125 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003126 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003127
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003128 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003129
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003130 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003131 return kCondEQ;
3132 }
3133
Mark Mendellc4701932015-04-10 13:18:51 -04003134 IfCondition GetOppositeCondition() const OVERRIDE {
3135 return kCondNE;
3136 }
3137
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003138 private:
Aart Bike9f37602015-10-09 11:15:55 -07003139 template <typename T> bool Compute(T x, T y) const { return x == y; }
3140
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003141 DISALLOW_COPY_AND_ASSIGN(HEqual);
3142};
3143
Vladimir Markofcb503c2016-05-18 12:48:17 +01003144class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003145 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003146 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3147 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003148
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003149 bool IsCommutative() const OVERRIDE { return true; }
3150
Vladimir Marko9e23df52015-11-10 17:14:35 +00003151 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3152 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003153 return MakeConstantCondition(false, GetDexPc());
3154 }
3155 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3156 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3157 }
3158 // In the following Evaluate methods, a HCompare instruction has
3159 // been merged into this HNotEqual instruction; evaluate it as
3160 // `Compare(x, y) != 0`.
3161 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3162 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3163 }
3164 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3165 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3166 }
3167 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3168 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003169 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003170
Dave Allison20dfc792014-06-16 20:44:29 -07003171 DECLARE_INSTRUCTION(NotEqual);
3172
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003173 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003174 return kCondNE;
3175 }
3176
Mark Mendellc4701932015-04-10 13:18:51 -04003177 IfCondition GetOppositeCondition() const OVERRIDE {
3178 return kCondEQ;
3179 }
3180
Dave Allison20dfc792014-06-16 20:44:29 -07003181 private:
Aart Bike9f37602015-10-09 11:15:55 -07003182 template <typename T> bool Compute(T x, T y) const { return x != y; }
3183
Dave Allison20dfc792014-06-16 20:44:29 -07003184 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3185};
3186
Vladimir Markofcb503c2016-05-18 12:48:17 +01003187class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003188 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003189 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3190 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003191
Roland Levillain9867bc72015-08-05 10:21:34 +01003192 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003193 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003194 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003195 // In the following Evaluate methods, a HCompare instruction has
3196 // been merged into this HLessThan instruction; evaluate it as
3197 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003198 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003199 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3200 }
3201 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3202 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3203 }
3204 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3205 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003206 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003207
Dave Allison20dfc792014-06-16 20:44:29 -07003208 DECLARE_INSTRUCTION(LessThan);
3209
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003210 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003211 return kCondLT;
3212 }
3213
Mark Mendellc4701932015-04-10 13:18:51 -04003214 IfCondition GetOppositeCondition() const OVERRIDE {
3215 return kCondGE;
3216 }
3217
Dave Allison20dfc792014-06-16 20:44:29 -07003218 private:
Aart Bike9f37602015-10-09 11:15:55 -07003219 template <typename T> bool Compute(T x, T y) const { return x < y; }
3220
Dave Allison20dfc792014-06-16 20:44:29 -07003221 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3222};
3223
Vladimir Markofcb503c2016-05-18 12:48:17 +01003224class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003225 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003226 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3227 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003228
Roland Levillain9867bc72015-08-05 10:21:34 +01003229 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003230 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003231 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003232 // In the following Evaluate methods, a HCompare instruction has
3233 // been merged into this HLessThanOrEqual instruction; evaluate it as
3234 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003235 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003236 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3237 }
3238 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3239 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3240 }
3241 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3242 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003243 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003244
Dave Allison20dfc792014-06-16 20:44:29 -07003245 DECLARE_INSTRUCTION(LessThanOrEqual);
3246
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003247 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003248 return kCondLE;
3249 }
3250
Mark Mendellc4701932015-04-10 13:18:51 -04003251 IfCondition GetOppositeCondition() const OVERRIDE {
3252 return kCondGT;
3253 }
3254
Dave Allison20dfc792014-06-16 20:44:29 -07003255 private:
Aart Bike9f37602015-10-09 11:15:55 -07003256 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3257
Dave Allison20dfc792014-06-16 20:44:29 -07003258 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3259};
3260
Vladimir Markofcb503c2016-05-18 12:48:17 +01003261class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003262 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003263 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3264 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003265
Roland Levillain9867bc72015-08-05 10:21:34 +01003266 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003267 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003268 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003269 // In the following Evaluate methods, a HCompare instruction has
3270 // been merged into this HGreaterThan instruction; evaluate it as
3271 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003272 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003273 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3274 }
3275 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3276 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3277 }
3278 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3279 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003280 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003281
Dave Allison20dfc792014-06-16 20:44:29 -07003282 DECLARE_INSTRUCTION(GreaterThan);
3283
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003284 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003285 return kCondGT;
3286 }
3287
Mark Mendellc4701932015-04-10 13:18:51 -04003288 IfCondition GetOppositeCondition() const OVERRIDE {
3289 return kCondLE;
3290 }
3291
Dave Allison20dfc792014-06-16 20:44:29 -07003292 private:
Aart Bike9f37602015-10-09 11:15:55 -07003293 template <typename T> bool Compute(T x, T y) const { return x > y; }
3294
Dave Allison20dfc792014-06-16 20:44:29 -07003295 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3296};
3297
Vladimir Markofcb503c2016-05-18 12:48:17 +01003298class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003299 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003300 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3301 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003302
Roland Levillain9867bc72015-08-05 10:21:34 +01003303 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003304 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003305 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003306 // In the following Evaluate methods, a HCompare instruction has
3307 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3308 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003309 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003310 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3311 }
3312 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3313 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3314 }
3315 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3316 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003317 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003318
Dave Allison20dfc792014-06-16 20:44:29 -07003319 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3320
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003321 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003322 return kCondGE;
3323 }
3324
Mark Mendellc4701932015-04-10 13:18:51 -04003325 IfCondition GetOppositeCondition() const OVERRIDE {
3326 return kCondLT;
3327 }
3328
Dave Allison20dfc792014-06-16 20:44:29 -07003329 private:
Aart Bike9f37602015-10-09 11:15:55 -07003330 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3331
Dave Allison20dfc792014-06-16 20:44:29 -07003332 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3333};
3334
Vladimir Markofcb503c2016-05-18 12:48:17 +01003335class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003336 public:
3337 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3338 : HCondition(first, second, dex_pc) {}
3339
3340 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003341 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003342 }
3343 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003344 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3345 }
3346 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3347 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3348 LOG(FATAL) << DebugName() << " is not defined for float values";
3349 UNREACHABLE();
3350 }
3351 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3352 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3353 LOG(FATAL) << DebugName() << " is not defined for double values";
3354 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003355 }
3356
3357 DECLARE_INSTRUCTION(Below);
3358
3359 IfCondition GetCondition() const OVERRIDE {
3360 return kCondB;
3361 }
3362
3363 IfCondition GetOppositeCondition() const OVERRIDE {
3364 return kCondAE;
3365 }
3366
3367 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003368 template <typename T> bool Compute(T x, T y) const {
3369 return MakeUnsigned(x) < MakeUnsigned(y);
3370 }
Aart Bike9f37602015-10-09 11:15:55 -07003371
3372 DISALLOW_COPY_AND_ASSIGN(HBelow);
3373};
3374
Vladimir Markofcb503c2016-05-18 12:48:17 +01003375class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003376 public:
3377 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3378 : HCondition(first, second, dex_pc) {}
3379
3380 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003381 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003382 }
3383 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003384 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3385 }
3386 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3387 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3388 LOG(FATAL) << DebugName() << " is not defined for float values";
3389 UNREACHABLE();
3390 }
3391 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3392 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3393 LOG(FATAL) << DebugName() << " is not defined for double values";
3394 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003395 }
3396
3397 DECLARE_INSTRUCTION(BelowOrEqual);
3398
3399 IfCondition GetCondition() const OVERRIDE {
3400 return kCondBE;
3401 }
3402
3403 IfCondition GetOppositeCondition() const OVERRIDE {
3404 return kCondA;
3405 }
3406
3407 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003408 template <typename T> bool Compute(T x, T y) const {
3409 return MakeUnsigned(x) <= MakeUnsigned(y);
3410 }
Aart Bike9f37602015-10-09 11:15:55 -07003411
3412 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3413};
3414
Vladimir Markofcb503c2016-05-18 12:48:17 +01003415class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003416 public:
3417 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3418 : HCondition(first, second, dex_pc) {}
3419
3420 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003421 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003422 }
3423 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003424 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3425 }
3426 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3427 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3428 LOG(FATAL) << DebugName() << " is not defined for float values";
3429 UNREACHABLE();
3430 }
3431 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3432 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3433 LOG(FATAL) << DebugName() << " is not defined for double values";
3434 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003435 }
3436
3437 DECLARE_INSTRUCTION(Above);
3438
3439 IfCondition GetCondition() const OVERRIDE {
3440 return kCondA;
3441 }
3442
3443 IfCondition GetOppositeCondition() const OVERRIDE {
3444 return kCondBE;
3445 }
3446
3447 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003448 template <typename T> bool Compute(T x, T y) const {
3449 return MakeUnsigned(x) > MakeUnsigned(y);
3450 }
Aart Bike9f37602015-10-09 11:15:55 -07003451
3452 DISALLOW_COPY_AND_ASSIGN(HAbove);
3453};
3454
Vladimir Markofcb503c2016-05-18 12:48:17 +01003455class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003456 public:
3457 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3458 : HCondition(first, second, dex_pc) {}
3459
3460 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003461 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003462 }
3463 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003464 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3465 }
3466 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3467 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3468 LOG(FATAL) << DebugName() << " is not defined for float values";
3469 UNREACHABLE();
3470 }
3471 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3472 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3473 LOG(FATAL) << DebugName() << " is not defined for double values";
3474 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003475 }
3476
3477 DECLARE_INSTRUCTION(AboveOrEqual);
3478
3479 IfCondition GetCondition() const OVERRIDE {
3480 return kCondAE;
3481 }
3482
3483 IfCondition GetOppositeCondition() const OVERRIDE {
3484 return kCondB;
3485 }
3486
3487 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003488 template <typename T> bool Compute(T x, T y) const {
3489 return MakeUnsigned(x) >= MakeUnsigned(y);
3490 }
Aart Bike9f37602015-10-09 11:15:55 -07003491
3492 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3493};
Dave Allison20dfc792014-06-16 20:44:29 -07003494
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003495// Instruction to check how two inputs compare to each other.
3496// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003497class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003498 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003499 // Note that `comparison_type` is the type of comparison performed
3500 // between the comparison's inputs, not the type of the instantiated
3501 // HCompare instruction (which is always Primitive::kPrimInt).
3502 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003503 HInstruction* first,
3504 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003505 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003506 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003507 : HBinaryOperation(Primitive::kPrimInt,
3508 first,
3509 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003510 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003511 dex_pc) {
3512 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003513 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3514 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003515 }
3516
Roland Levillain9867bc72015-08-05 10:21:34 +01003517 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003518 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3519
3520 template <typename T>
3521 int32_t ComputeFP(T x, T y) const {
3522 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3523 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3524 // Handle the bias.
3525 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3526 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003527
Roland Levillain9867bc72015-08-05 10:21:34 +01003528 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003529 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3530 // reach this code path when processing a freshly built HIR
3531 // graph. However HCompare integer instructions can be synthesized
3532 // by the instruction simplifier to implement IntegerCompare and
3533 // IntegerSignum intrinsics, so we have to handle this case.
3534 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003535 }
3536 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003537 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3538 }
3539 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3540 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3541 }
3542 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3543 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003544 }
3545
Vladimir Marko372f10e2016-05-17 16:30:10 +01003546 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003547 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003548 }
3549
Vladimir Markoa1de9182016-02-25 11:37:38 +00003550 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003551
Roland Levillain31dd3d62016-02-16 12:21:02 +00003552 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003553 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003554 bool IsGtBias() const {
3555 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003556 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003557 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003558
Roland Levillain1693a1f2016-03-15 14:57:31 +00003559 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3560 // Comparisons do not require a runtime call in any back end.
3561 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003562 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003563
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003564 DECLARE_INSTRUCTION(Compare);
3565
Roland Levillain31dd3d62016-02-16 12:21:02 +00003566 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003567 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3568 static constexpr size_t kFieldComparisonBiasSize =
3569 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3570 static constexpr size_t kNumberOfComparePackedBits =
3571 kFieldComparisonBias + kFieldComparisonBiasSize;
3572 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3573 using ComparisonBiasField =
3574 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3575
Roland Levillain31dd3d62016-02-16 12:21:02 +00003576 // Return an integer constant containing the result of a comparison evaluated at compile time.
3577 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3578 DCHECK(value == -1 || value == 0 || value == 1) << value;
3579 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3580 }
3581
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003582 private:
3583 DISALLOW_COPY_AND_ASSIGN(HCompare);
3584};
3585
Vladimir Markofcb503c2016-05-18 12:48:17 +01003586class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003587 public:
3588 HNewInstance(HInstruction* cls,
3589 HCurrentMethod* current_method,
3590 uint32_t dex_pc,
3591 uint16_t type_index,
3592 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003593 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003594 bool finalizable,
3595 QuickEntrypointEnum entrypoint)
3596 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3597 type_index_(type_index),
3598 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003599 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003600 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003601 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003602 SetRawInputAt(0, cls);
3603 SetRawInputAt(1, current_method);
3604 }
3605
3606 uint16_t GetTypeIndex() const { return type_index_; }
3607 const DexFile& GetDexFile() const { return dex_file_; }
3608
3609 // Calls runtime so needs an environment.
3610 bool NeedsEnvironment() const OVERRIDE { return true; }
3611
Mingyao Yang062157f2016-03-02 10:15:36 -08003612 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3613 bool CanThrow() const OVERRIDE { return true; }
3614
3615 // Needs to call into runtime to make sure it's instantiable/accessible.
3616 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003617
Vladimir Markoa1de9182016-02-25 11:37:38 +00003618 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003619
3620 bool CanBeNull() const OVERRIDE { return false; }
3621
3622 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3623
3624 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3625 entrypoint_ = entrypoint;
3626 }
3627
3628 bool IsStringAlloc() const;
3629
3630 DECLARE_INSTRUCTION(NewInstance);
3631
3632 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003633 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3634 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003635 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3636 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3637 "Too many packed fields.");
3638
David Brazdil6de19382016-01-08 17:37:10 +00003639 const uint16_t type_index_;
3640 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003641 QuickEntrypointEnum entrypoint_;
3642
3643 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3644};
3645
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003646enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003647#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3648 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003649#include "intrinsics_list.h"
3650 kNone,
3651 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3652#undef INTRINSICS_LIST
3653#undef OPTIMIZING_INTRINSICS
3654};
3655std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3656
Agi Csaki05f20562015-08-19 14:58:14 -07003657enum IntrinsicNeedsEnvironmentOrCache {
3658 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3659 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003660};
3661
Aart Bik5d75afe2015-12-14 11:57:01 -08003662enum IntrinsicSideEffects {
3663 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3664 kReadSideEffects, // Intrinsic may read heap memory.
3665 kWriteSideEffects, // Intrinsic may write heap memory.
3666 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3667};
3668
3669enum IntrinsicExceptions {
3670 kNoThrow, // Intrinsic does not throw any exceptions.
3671 kCanThrow // Intrinsic may throw exceptions.
3672};
3673
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003674class HInvoke : public HInstruction {
3675 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003676 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003677
Vladimir Marko372f10e2016-05-17 16:30:10 +01003678 using HInstruction::GetInputRecords; // Keep the const version visible.
3679 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3680 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3681 }
3682
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003683 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003684 SetRawInputAt(index, argument);
3685 }
3686
Roland Levillain3e3d7332015-04-28 11:00:54 +01003687 // Return the number of arguments. This number can be lower than
3688 // the number of inputs returned by InputCount(), as some invoke
3689 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3690 // inputs at the end of their list of inputs.
3691 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3692
Vladimir Markoa1de9182016-02-25 11:37:38 +00003693 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003694
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003695 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003696 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003697
Vladimir Markoa1de9182016-02-25 11:37:38 +00003698 InvokeType GetOriginalInvokeType() const {
3699 return GetPackedField<OriginalInvokeTypeField>();
3700 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003701
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003702 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003703 return intrinsic_;
3704 }
3705
Aart Bik5d75afe2015-12-14 11:57:01 -08003706 void SetIntrinsic(Intrinsics intrinsic,
3707 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3708 IntrinsicSideEffects side_effects,
3709 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003710
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003711 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003712 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003713 }
3714
Vladimir Markoa1de9182016-02-25 11:37:38 +00003715 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003716
3717 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3718
Vladimir Marko372f10e2016-05-17 16:30:10 +01003719 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003720 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3721 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003722
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003723 uint32_t* GetIntrinsicOptimizations() {
3724 return &intrinsic_optimizations_;
3725 }
3726
3727 const uint32_t* GetIntrinsicOptimizations() const {
3728 return &intrinsic_optimizations_;
3729 }
3730
3731 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3732
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003733 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003734
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003735 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003736 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3737 static constexpr size_t kFieldOriginalInvokeTypeSize =
3738 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3739 static constexpr size_t kFieldReturnType =
3740 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3741 static constexpr size_t kFieldReturnTypeSize =
3742 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3743 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3744 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3745 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3746 using OriginalInvokeTypeField =
3747 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3748 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3749
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003750 HInvoke(ArenaAllocator* arena,
3751 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003752 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003753 Primitive::Type return_type,
3754 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003755 uint32_t dex_method_index,
3756 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003757 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003758 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003759 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003760 inputs_(number_of_arguments + number_of_other_inputs,
3761 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003762 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003763 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003764 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003765 SetPackedField<ReturnTypeField>(return_type);
3766 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3767 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003768 }
3769
Vladimir Markoa1de9182016-02-25 11:37:38 +00003770 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003771
Roland Levillain3e3d7332015-04-28 11:00:54 +01003772 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003773 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003774 const uint32_t dex_method_index_;
3775 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003776
3777 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3778 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003779
3780 private:
3781 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3782};
3783
Vladimir Markofcb503c2016-05-18 12:48:17 +01003784class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003785 public:
3786 HInvokeUnresolved(ArenaAllocator* arena,
3787 uint32_t number_of_arguments,
3788 Primitive::Type return_type,
3789 uint32_t dex_pc,
3790 uint32_t dex_method_index,
3791 InvokeType invoke_type)
3792 : HInvoke(arena,
3793 number_of_arguments,
3794 0u /* number_of_other_inputs */,
3795 return_type,
3796 dex_pc,
3797 dex_method_index,
3798 invoke_type) {
3799 }
3800
3801 DECLARE_INSTRUCTION(InvokeUnresolved);
3802
3803 private:
3804 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3805};
3806
Vladimir Markofcb503c2016-05-18 12:48:17 +01003807class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003808 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003809 // Requirements of this method call regarding the class
3810 // initialization (clinit) check of its declaring class.
3811 enum class ClinitCheckRequirement {
3812 kNone, // Class already initialized.
3813 kExplicit, // Static call having explicit clinit check as last input.
3814 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003815 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003816 };
3817
Vladimir Marko58155012015-08-19 12:49:41 +00003818 // Determines how to load the target ArtMethod*.
3819 enum class MethodLoadKind {
3820 // Use a String init ArtMethod* loaded from Thread entrypoints.
3821 kStringInit,
3822
3823 // Use the method's own ArtMethod* loaded by the register allocator.
3824 kRecursive,
3825
3826 // Use ArtMethod* at a known address, embed the direct address in the code.
3827 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3828 kDirectAddress,
3829
3830 // Use ArtMethod* at an address that will be known at link time, embed the direct
3831 // address in the code. If the image is relocatable, emit .patch_oat entry.
3832 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3833 // the image relocatable or not.
3834 kDirectAddressWithFixup,
3835
Vladimir Markoa1de9182016-02-25 11:37:38 +00003836 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003837 // Used when we need to use the dex cache, for example for invoke-static that
3838 // may cause class initialization (the entry may point to a resolution method),
3839 // and we know that we can access the dex cache arrays using a PC-relative load.
3840 kDexCachePcRelative,
3841
3842 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3843 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3844 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3845 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3846 kDexCacheViaMethod,
3847 };
3848
3849 // Determines the location of the code pointer.
3850 enum class CodePtrLocation {
3851 // Recursive call, use local PC-relative call instruction.
3852 kCallSelf,
3853
3854 // Use PC-relative call instruction patched at link time.
3855 // Used for calls within an oat file, boot->boot or app->app.
3856 kCallPCRelative,
3857
3858 // Call to a known target address, embed the direct address in code.
3859 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3860 kCallDirect,
3861
3862 // Call to a target address that will be known at link time, embed the direct
3863 // address in code. If the image is relocatable, emit .patch_oat entry.
3864 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3865 // the image relocatable or not.
3866 kCallDirectWithFixup,
3867
3868 // Use code pointer from the ArtMethod*.
3869 // Used when we don't know the target code. This is also the last-resort-kind used when
3870 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3871 kCallArtMethod,
3872 };
3873
3874 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003875 MethodLoadKind method_load_kind;
3876 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003877 // The method load data holds
3878 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3879 // Note that there are multiple string init methods, each having its own offset.
3880 // - the method address for kDirectAddress
3881 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003882 uint64_t method_load_data;
3883 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003884 };
3885
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003886 HInvokeStaticOrDirect(ArenaAllocator* arena,
3887 uint32_t number_of_arguments,
3888 Primitive::Type return_type,
3889 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003890 uint32_t method_index,
3891 MethodReference target_method,
3892 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003893 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003894 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003895 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003896 : HInvoke(arena,
3897 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003898 // There is potentially one extra argument for the HCurrentMethod node, and
3899 // potentially one other if the clinit check is explicit, and potentially
3900 // one other if the method is a string factory.
3901 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003902 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003903 return_type,
3904 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003905 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003906 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003907 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003908 dispatch_info_(dispatch_info) {
3909 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3910 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3911 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003912
Vladimir Markodc151b22015-10-15 18:02:30 +01003913 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003914 bool had_current_method_input = HasCurrentMethodInput();
3915 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3916
3917 // Using the current method is the default and once we find a better
3918 // method load kind, we should not go back to using the current method.
3919 DCHECK(had_current_method_input || !needs_current_method_input);
3920
3921 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003922 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3923 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003924 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003925 dispatch_info_ = dispatch_info;
3926 }
3927
Vladimir Markoc53c0792015-11-19 15:48:33 +00003928 void AddSpecialInput(HInstruction* input) {
3929 // We allow only one special input.
3930 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3931 DCHECK(InputCount() == GetSpecialInputIndex() ||
3932 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3933 InsertInputAt(GetSpecialInputIndex(), input);
3934 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003935
Vladimir Marko372f10e2016-05-17 16:30:10 +01003936 using HInstruction::GetInputRecords; // Keep the const version visible.
3937 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3938 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
3939 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
3940 DCHECK(!input_records.empty());
3941 DCHECK_GT(input_records.size(), GetNumberOfArguments());
3942 HInstruction* last_input = input_records.back().GetInstruction();
3943 // Note: `last_input` may be null during arguments setup.
3944 if (last_input != nullptr) {
3945 // `last_input` is the last input of a static invoke marked as having
3946 // an explicit clinit check. It must either be:
3947 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3948 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3949 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
3950 }
3951 }
3952 return input_records;
3953 }
3954
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003955 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003956 // We access the method via the dex cache so we can't do an implicit null check.
3957 // TODO: for intrinsics we can generate implicit null checks.
3958 return false;
3959 }
3960
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003961 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003962 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003963 }
3964
Vladimir Markoc53c0792015-11-19 15:48:33 +00003965 // Get the index of the special input, if any.
3966 //
David Brazdil6de19382016-01-08 17:37:10 +00003967 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3968 // method pointer; otherwise there may be one platform-specific special input,
3969 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003970 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003971 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003972
Vladimir Markoa1de9182016-02-25 11:37:38 +00003973 InvokeType GetOptimizedInvokeType() const {
3974 return GetPackedField<OptimizedInvokeTypeField>();
3975 }
3976
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003977 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003978 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003979 }
3980
Vladimir Marko58155012015-08-19 12:49:41 +00003981 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3982 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3983 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003984 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003985 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003986 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003987 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003988 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3989 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003990 bool HasCurrentMethodInput() const {
3991 // This function can be called only after the invoke has been fully initialized by the builder.
3992 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003993 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003994 return true;
3995 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003996 DCHECK(InputCount() == GetSpecialInputIndex() ||
3997 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003998 return false;
3999 }
4000 }
Vladimir Marko58155012015-08-19 12:49:41 +00004001 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
4002 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004003 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00004004
4005 int32_t GetStringInitOffset() const {
4006 DCHECK(IsStringInit());
4007 return dispatch_info_.method_load_data;
4008 }
4009
4010 uint64_t GetMethodAddress() const {
4011 DCHECK(HasMethodAddress());
4012 return dispatch_info_.method_load_data;
4013 }
4014
4015 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004016 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004017 return dispatch_info_.method_load_data;
4018 }
4019
4020 uint64_t GetDirectCodePtr() const {
4021 DCHECK(HasDirectCodePtr());
4022 return dispatch_info_.direct_code_ptr;
4023 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004024
Vladimir Markoa1de9182016-02-25 11:37:38 +00004025 ClinitCheckRequirement GetClinitCheckRequirement() const {
4026 return GetPackedField<ClinitCheckRequirementField>();
4027 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004028
Roland Levillain4c0eb422015-04-24 16:43:49 +01004029 // Is this instruction a call to a static method?
4030 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004031 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004032 }
4033
Vladimir Markofbb184a2015-11-13 14:47:00 +00004034 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4035 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4036 // instruction; only relevant for static calls with explicit clinit check.
4037 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004038 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004039 size_t last_input_index = inputs_.size() - 1u;
4040 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004041 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004042 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004043 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004044 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004045 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004046 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004047 }
4048
4049 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004050 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004051 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004052 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004053 }
4054
4055 // Is this a call to a static method whose declaring class has an
4056 // implicit intialization check requirement?
4057 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004058 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004059 }
4060
Vladimir Markob554b5a2015-11-06 12:57:55 +00004061 // Does this method load kind need the current method as an input?
4062 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4063 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4064 }
4065
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004066 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004067
Roland Levillain4c0eb422015-04-24 16:43:49 +01004068 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004069 void InsertInputAt(size_t index, HInstruction* input);
4070 void RemoveInputAt(size_t index);
4071
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004072 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004073 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4074 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4075 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4076 static constexpr size_t kFieldClinitCheckRequirement =
4077 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4078 static constexpr size_t kFieldClinitCheckRequirementSize =
4079 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4080 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4081 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4082 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4083 "Too many packed fields.");
4084 using OptimizedInvokeTypeField =
4085 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4086 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4087 kFieldClinitCheckRequirement,
4088 kFieldClinitCheckRequirementSize>;
4089
Vladimir Marko58155012015-08-19 12:49:41 +00004090 // The target method may refer to different dex file or method index than the original
4091 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4092 // in derived class to increase visibility.
4093 MethodReference target_method_;
4094 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004095
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004096 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004097};
Vladimir Markof64242a2015-12-01 14:58:23 +00004098std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004099std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004100
Vladimir Markofcb503c2016-05-18 12:48:17 +01004101class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004102 public:
4103 HInvokeVirtual(ArenaAllocator* arena,
4104 uint32_t number_of_arguments,
4105 Primitive::Type return_type,
4106 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004107 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004108 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004109 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004110 vtable_index_(vtable_index) {}
4111
Calin Juravle641547a2015-04-21 22:08:51 +01004112 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004113 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004114 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004115 }
4116
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004117 uint32_t GetVTableIndex() const { return vtable_index_; }
4118
4119 DECLARE_INSTRUCTION(InvokeVirtual);
4120
4121 private:
4122 const uint32_t vtable_index_;
4123
4124 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4125};
4126
Vladimir Markofcb503c2016-05-18 12:48:17 +01004127class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004128 public:
4129 HInvokeInterface(ArenaAllocator* arena,
4130 uint32_t number_of_arguments,
4131 Primitive::Type return_type,
4132 uint32_t dex_pc,
4133 uint32_t dex_method_index,
4134 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004135 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004136 imt_index_(imt_index) {}
4137
Calin Juravle641547a2015-04-21 22:08:51 +01004138 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004139 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004140 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004141 }
4142
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004143 uint32_t GetImtIndex() const { return imt_index_; }
4144 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4145
4146 DECLARE_INSTRUCTION(InvokeInterface);
4147
4148 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004149 const uint32_t imt_index_;
4150
4151 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4152};
4153
Vladimir Markofcb503c2016-05-18 12:48:17 +01004154class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004155 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004156 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004157 : HUnaryOperation(result_type, input, dex_pc) {
4158 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4159 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004160
Roland Levillain9867bc72015-08-05 10:21:34 +01004161 template <typename T> T Compute(T x) const { return -x; }
4162
4163 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004164 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004165 }
4166 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004167 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004168 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004169 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4170 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4171 }
4172 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4173 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4174 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004175
Roland Levillain88cb1752014-10-20 16:36:47 +01004176 DECLARE_INSTRUCTION(Neg);
4177
4178 private:
4179 DISALLOW_COPY_AND_ASSIGN(HNeg);
4180};
4181
Vladimir Markofcb503c2016-05-18 12:48:17 +01004182class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004183 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004184 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004185 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004186 uint32_t dex_pc,
4187 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004188 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004189 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004190 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004191 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004192 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004193 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004194 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004195 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004196 }
4197
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004198 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004199 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004200
4201 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004202 bool NeedsEnvironment() const OVERRIDE { return true; }
4203
Mingyao Yang0c365e62015-03-31 15:09:29 -07004204 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4205 bool CanThrow() const OVERRIDE { return true; }
4206
Calin Juravle10e244f2015-01-26 18:54:32 +00004207 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004208
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004209 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4210
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004211 DECLARE_INSTRUCTION(NewArray);
4212
4213 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004214 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004215 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004216 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004217
4218 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4219};
4220
Vladimir Markofcb503c2016-05-18 12:48:17 +01004221class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004222 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004223 HAdd(Primitive::Type result_type,
4224 HInstruction* left,
4225 HInstruction* right,
4226 uint32_t dex_pc = kNoDexPc)
4227 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004228
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004229 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004230
Roland Levillain9867bc72015-08-05 10:21:34 +01004231 template <typename T> T Compute(T x, T y) const { return x + y; }
4232
4233 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004234 return GetBlock()->GetGraph()->GetIntConstant(
4235 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004236 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004237 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004238 return GetBlock()->GetGraph()->GetLongConstant(
4239 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004240 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004241 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4242 return GetBlock()->GetGraph()->GetFloatConstant(
4243 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4244 }
4245 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4246 return GetBlock()->GetGraph()->GetDoubleConstant(
4247 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4248 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004249
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004250 DECLARE_INSTRUCTION(Add);
4251
4252 private:
4253 DISALLOW_COPY_AND_ASSIGN(HAdd);
4254};
4255
Vladimir Markofcb503c2016-05-18 12:48:17 +01004256class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004257 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004258 HSub(Primitive::Type result_type,
4259 HInstruction* left,
4260 HInstruction* right,
4261 uint32_t dex_pc = kNoDexPc)
4262 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004263
Roland Levillain9867bc72015-08-05 10:21:34 +01004264 template <typename T> T Compute(T x, T y) const { return x - y; }
4265
4266 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004267 return GetBlock()->GetGraph()->GetIntConstant(
4268 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004269 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004270 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004271 return GetBlock()->GetGraph()->GetLongConstant(
4272 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004273 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004274 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4275 return GetBlock()->GetGraph()->GetFloatConstant(
4276 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4277 }
4278 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4279 return GetBlock()->GetGraph()->GetDoubleConstant(
4280 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4281 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004282
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004283 DECLARE_INSTRUCTION(Sub);
4284
4285 private:
4286 DISALLOW_COPY_AND_ASSIGN(HSub);
4287};
4288
Vladimir Markofcb503c2016-05-18 12:48:17 +01004289class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004290 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004291 HMul(Primitive::Type result_type,
4292 HInstruction* left,
4293 HInstruction* right,
4294 uint32_t dex_pc = kNoDexPc)
4295 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004296
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004297 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004298
Roland Levillain9867bc72015-08-05 10:21:34 +01004299 template <typename T> T Compute(T x, T y) const { return x * y; }
4300
4301 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004302 return GetBlock()->GetGraph()->GetIntConstant(
4303 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004304 }
4305 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004306 return GetBlock()->GetGraph()->GetLongConstant(
4307 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004308 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004309 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4310 return GetBlock()->GetGraph()->GetFloatConstant(
4311 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4312 }
4313 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4314 return GetBlock()->GetGraph()->GetDoubleConstant(
4315 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4316 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004317
4318 DECLARE_INSTRUCTION(Mul);
4319
4320 private:
4321 DISALLOW_COPY_AND_ASSIGN(HMul);
4322};
4323
Vladimir Markofcb503c2016-05-18 12:48:17 +01004324class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004325 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004326 HDiv(Primitive::Type result_type,
4327 HInstruction* left,
4328 HInstruction* right,
4329 uint32_t dex_pc)
4330 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004331
Roland Levillain9867bc72015-08-05 10:21:34 +01004332 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004333 T ComputeIntegral(T x, T y) const {
4334 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004335 // Our graph structure ensures we never have 0 for `y` during
4336 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004337 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004338 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004339 return (y == -1) ? -x : x / y;
4340 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004341
Roland Levillain31dd3d62016-02-16 12:21:02 +00004342 template <typename T>
4343 T ComputeFP(T x, T y) const {
4344 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4345 return x / y;
4346 }
4347
Roland Levillain9867bc72015-08-05 10:21:34 +01004348 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004349 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004350 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004351 }
4352 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004353 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004354 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4355 }
4356 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4357 return GetBlock()->GetGraph()->GetFloatConstant(
4358 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4359 }
4360 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4361 return GetBlock()->GetGraph()->GetDoubleConstant(
4362 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004363 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004364
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004365 static SideEffects SideEffectsForArchRuntimeCalls() {
4366 // The generated code can use a runtime call.
4367 return SideEffects::CanTriggerGC();
4368 }
4369
Calin Juravle7c4954d2014-10-28 16:57:40 +00004370 DECLARE_INSTRUCTION(Div);
4371
4372 private:
4373 DISALLOW_COPY_AND_ASSIGN(HDiv);
4374};
4375
Vladimir Markofcb503c2016-05-18 12:48:17 +01004376class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004377 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004378 HRem(Primitive::Type result_type,
4379 HInstruction* left,
4380 HInstruction* right,
4381 uint32_t dex_pc)
4382 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004383
Roland Levillain9867bc72015-08-05 10:21:34 +01004384 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004385 T ComputeIntegral(T x, T y) const {
4386 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004387 // Our graph structure ensures we never have 0 for `y` during
4388 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004389 DCHECK_NE(y, 0);
4390 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4391 return (y == -1) ? 0 : x % y;
4392 }
4393
Roland Levillain31dd3d62016-02-16 12:21:02 +00004394 template <typename T>
4395 T ComputeFP(T x, T y) const {
4396 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4397 return std::fmod(x, y);
4398 }
4399
Roland Levillain9867bc72015-08-05 10:21:34 +01004400 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004401 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004402 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004403 }
4404 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004405 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004406 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004407 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004408 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4409 return GetBlock()->GetGraph()->GetFloatConstant(
4410 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4411 }
4412 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4413 return GetBlock()->GetGraph()->GetDoubleConstant(
4414 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4415 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004416
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004417 static SideEffects SideEffectsForArchRuntimeCalls() {
4418 return SideEffects::CanTriggerGC();
4419 }
4420
Calin Juravlebacfec32014-11-14 15:54:36 +00004421 DECLARE_INSTRUCTION(Rem);
4422
4423 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004424 DISALLOW_COPY_AND_ASSIGN(HRem);
4425};
4426
Vladimir Markofcb503c2016-05-18 12:48:17 +01004427class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004428 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004429 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4430 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004431 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004432 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004433 SetRawInputAt(0, value);
4434 }
4435
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004436 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4437
Calin Juravled0d48522014-11-04 16:40:20 +00004438 bool CanBeMoved() const OVERRIDE { return true; }
4439
Vladimir Marko372f10e2016-05-17 16:30:10 +01004440 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004441 return true;
4442 }
4443
4444 bool NeedsEnvironment() const OVERRIDE { return true; }
4445 bool CanThrow() const OVERRIDE { return true; }
4446
Calin Juravled0d48522014-11-04 16:40:20 +00004447 DECLARE_INSTRUCTION(DivZeroCheck);
4448
4449 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004450 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4451};
4452
Vladimir Markofcb503c2016-05-18 12:48:17 +01004453class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004454 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004455 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004456 HInstruction* value,
4457 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004458 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004459 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4460 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4461 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004462 }
4463
Roland Levillain5b5b9312016-03-22 14:57:31 +00004464 template <typename T>
4465 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4466 return value << (distance & max_shift_distance);
4467 }
4468
4469 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004470 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004471 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004472 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004473 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004474 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004475 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004476 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004477 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4478 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4479 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4480 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004481 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004482 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4483 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004484 LOG(FATAL) << DebugName() << " is not defined for float values";
4485 UNREACHABLE();
4486 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004487 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4488 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004489 LOG(FATAL) << DebugName() << " is not defined for double values";
4490 UNREACHABLE();
4491 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004492
4493 DECLARE_INSTRUCTION(Shl);
4494
4495 private:
4496 DISALLOW_COPY_AND_ASSIGN(HShl);
4497};
4498
Vladimir Markofcb503c2016-05-18 12:48:17 +01004499class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004500 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004501 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004502 HInstruction* value,
4503 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004504 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004505 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4506 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4507 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004508 }
4509
Roland Levillain5b5b9312016-03-22 14:57:31 +00004510 template <typename T>
4511 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4512 return value >> (distance & max_shift_distance);
4513 }
4514
4515 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004516 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004517 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004518 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004519 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004520 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004521 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004522 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004523 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4524 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4525 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4526 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004527 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004528 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4529 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004530 LOG(FATAL) << DebugName() << " is not defined for float values";
4531 UNREACHABLE();
4532 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004533 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4534 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004535 LOG(FATAL) << DebugName() << " is not defined for double values";
4536 UNREACHABLE();
4537 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004538
4539 DECLARE_INSTRUCTION(Shr);
4540
4541 private:
4542 DISALLOW_COPY_AND_ASSIGN(HShr);
4543};
4544
Vladimir Markofcb503c2016-05-18 12:48:17 +01004545class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004546 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004547 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004548 HInstruction* value,
4549 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004550 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004551 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4552 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4553 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004554 }
4555
Roland Levillain5b5b9312016-03-22 14:57:31 +00004556 template <typename T>
4557 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4558 typedef typename std::make_unsigned<T>::type V;
4559 V ux = static_cast<V>(value);
4560 return static_cast<T>(ux >> (distance & max_shift_distance));
4561 }
4562
4563 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004564 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004565 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004566 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004567 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004568 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004569 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004570 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004571 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4572 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4573 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4574 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004575 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004576 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4577 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004578 LOG(FATAL) << DebugName() << " is not defined for float values";
4579 UNREACHABLE();
4580 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004581 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4582 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004583 LOG(FATAL) << DebugName() << " is not defined for double values";
4584 UNREACHABLE();
4585 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004586
4587 DECLARE_INSTRUCTION(UShr);
4588
4589 private:
4590 DISALLOW_COPY_AND_ASSIGN(HUShr);
4591};
4592
Vladimir Markofcb503c2016-05-18 12:48:17 +01004593class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004594 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004595 HAnd(Primitive::Type result_type,
4596 HInstruction* left,
4597 HInstruction* right,
4598 uint32_t dex_pc = kNoDexPc)
4599 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004600
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004601 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004602
Roland Levillaine53bd812016-02-24 14:54:18 +00004603 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004604
4605 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004606 return GetBlock()->GetGraph()->GetIntConstant(
4607 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004608 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004609 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004610 return GetBlock()->GetGraph()->GetLongConstant(
4611 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004612 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004613 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4614 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4615 LOG(FATAL) << DebugName() << " is not defined for float values";
4616 UNREACHABLE();
4617 }
4618 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4619 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4620 LOG(FATAL) << DebugName() << " is not defined for double values";
4621 UNREACHABLE();
4622 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004623
4624 DECLARE_INSTRUCTION(And);
4625
4626 private:
4627 DISALLOW_COPY_AND_ASSIGN(HAnd);
4628};
4629
Vladimir Markofcb503c2016-05-18 12:48:17 +01004630class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004631 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004632 HOr(Primitive::Type result_type,
4633 HInstruction* left,
4634 HInstruction* right,
4635 uint32_t dex_pc = kNoDexPc)
4636 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004637
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004638 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004639
Roland Levillaine53bd812016-02-24 14:54:18 +00004640 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004641
4642 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004643 return GetBlock()->GetGraph()->GetIntConstant(
4644 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004645 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004646 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004647 return GetBlock()->GetGraph()->GetLongConstant(
4648 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004649 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004650 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4651 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4652 LOG(FATAL) << DebugName() << " is not defined for float values";
4653 UNREACHABLE();
4654 }
4655 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4656 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4657 LOG(FATAL) << DebugName() << " is not defined for double values";
4658 UNREACHABLE();
4659 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004660
4661 DECLARE_INSTRUCTION(Or);
4662
4663 private:
4664 DISALLOW_COPY_AND_ASSIGN(HOr);
4665};
4666
Vladimir Markofcb503c2016-05-18 12:48:17 +01004667class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004668 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004669 HXor(Primitive::Type result_type,
4670 HInstruction* left,
4671 HInstruction* right,
4672 uint32_t dex_pc = kNoDexPc)
4673 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004674
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004675 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004676
Roland Levillaine53bd812016-02-24 14:54:18 +00004677 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004678
4679 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004680 return GetBlock()->GetGraph()->GetIntConstant(
4681 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004682 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004683 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004684 return GetBlock()->GetGraph()->GetLongConstant(
4685 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004686 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004687 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4688 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4689 LOG(FATAL) << DebugName() << " is not defined for float values";
4690 UNREACHABLE();
4691 }
4692 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4693 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4694 LOG(FATAL) << DebugName() << " is not defined for double values";
4695 UNREACHABLE();
4696 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004697
4698 DECLARE_INSTRUCTION(Xor);
4699
4700 private:
4701 DISALLOW_COPY_AND_ASSIGN(HXor);
4702};
4703
Vladimir Markofcb503c2016-05-18 12:48:17 +01004704class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004705 public:
4706 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004707 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004708 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4709 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004710 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004711
Roland Levillain5b5b9312016-03-22 14:57:31 +00004712 template <typename T>
4713 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4714 typedef typename std::make_unsigned<T>::type V;
4715 V ux = static_cast<V>(value);
4716 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004717 return static_cast<T>(ux);
4718 } else {
4719 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004720 return static_cast<T>(ux >> (distance & max_shift_value)) |
4721 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004722 }
4723 }
4724
Roland Levillain5b5b9312016-03-22 14:57:31 +00004725 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004726 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004727 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004728 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004729 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004730 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004731 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004732 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004733 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4734 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4735 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4736 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004737 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004738 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4739 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004740 LOG(FATAL) << DebugName() << " is not defined for float values";
4741 UNREACHABLE();
4742 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004743 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4744 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004745 LOG(FATAL) << DebugName() << " is not defined for double values";
4746 UNREACHABLE();
4747 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004748
4749 DECLARE_INSTRUCTION(Ror);
4750
4751 private:
4752 DISALLOW_COPY_AND_ASSIGN(HRor);
4753};
4754
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004755// The value of a parameter in this method. Its location depends on
4756// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004757class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004758 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004759 HParameterValue(const DexFile& dex_file,
4760 uint16_t type_index,
4761 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004762 Primitive::Type parameter_type,
4763 bool is_this = false)
4764 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004765 dex_file_(dex_file),
4766 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004767 index_(index) {
4768 SetPackedFlag<kFlagIsThis>(is_this);
4769 SetPackedFlag<kFlagCanBeNull>(!is_this);
4770 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004771
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004772 const DexFile& GetDexFile() const { return dex_file_; }
4773 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004774 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004775 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004776
Vladimir Markoa1de9182016-02-25 11:37:38 +00004777 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4778 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004779
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004780 DECLARE_INSTRUCTION(ParameterValue);
4781
4782 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004783 // Whether or not the parameter value corresponds to 'this' argument.
4784 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4785 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4786 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4787 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4788 "Too many packed fields.");
4789
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004790 const DexFile& dex_file_;
4791 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004792 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004793 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004794 const uint8_t index_;
4795
4796 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4797};
4798
Vladimir Markofcb503c2016-05-18 12:48:17 +01004799class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004800 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004801 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4802 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004803
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004804 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004805 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004806 return true;
4807 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004808
Roland Levillain9867bc72015-08-05 10:21:34 +01004809 template <typename T> T Compute(T x) const { return ~x; }
4810
4811 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004812 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004813 }
4814 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004815 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004816 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004817 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4818 LOG(FATAL) << DebugName() << " is not defined for float values";
4819 UNREACHABLE();
4820 }
4821 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4822 LOG(FATAL) << DebugName() << " is not defined for double values";
4823 UNREACHABLE();
4824 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004825
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004826 DECLARE_INSTRUCTION(Not);
4827
4828 private:
4829 DISALLOW_COPY_AND_ASSIGN(HNot);
4830};
4831
Vladimir Markofcb503c2016-05-18 12:48:17 +01004832class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004833 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004834 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4835 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004836
4837 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004838 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004839 return true;
4840 }
4841
Roland Levillain9867bc72015-08-05 10:21:34 +01004842 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004843 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004844 return !x;
4845 }
4846
Roland Levillain9867bc72015-08-05 10:21:34 +01004847 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004848 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004849 }
4850 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4851 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004852 UNREACHABLE();
4853 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004854 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4855 LOG(FATAL) << DebugName() << " is not defined for float values";
4856 UNREACHABLE();
4857 }
4858 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4859 LOG(FATAL) << DebugName() << " is not defined for double values";
4860 UNREACHABLE();
4861 }
David Brazdil66d126e2015-04-03 16:02:44 +01004862
4863 DECLARE_INSTRUCTION(BooleanNot);
4864
4865 private:
4866 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4867};
4868
Vladimir Markofcb503c2016-05-18 12:48:17 +01004869class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004870 public:
4871 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004872 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004873 : HExpression(result_type,
4874 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4875 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004876 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004877 // Invariant: We should never generate a conversion to a Boolean value.
4878 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004879 }
4880
4881 HInstruction* GetInput() const { return InputAt(0); }
4882 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4883 Primitive::Type GetResultType() const { return GetType(); }
4884
4885 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004886 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4887 return true;
4888 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004889
Mark Mendelle82549b2015-05-06 10:55:34 -04004890 // Try to statically evaluate the conversion and return a HConstant
4891 // containing the result. If the input cannot be converted, return nullptr.
4892 HConstant* TryStaticEvaluation() const;
4893
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004894 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4895 Primitive::Type result_type) {
4896 // Some architectures may not require the 'GC' side effects, but at this point
4897 // in the compilation process we do not know what architecture we will
4898 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004899 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4900 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004901 return SideEffects::CanTriggerGC();
4902 }
4903 return SideEffects::None();
4904 }
4905
Roland Levillaindff1f282014-11-05 14:15:05 +00004906 DECLARE_INSTRUCTION(TypeConversion);
4907
4908 private:
4909 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4910};
4911
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004912static constexpr uint32_t kNoRegNumber = -1;
4913
Vladimir Markofcb503c2016-05-18 12:48:17 +01004914class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004915 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004916 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4917 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004918 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004919 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004920 SetRawInputAt(0, value);
4921 }
4922
Calin Juravle10e244f2015-01-26 18:54:32 +00004923 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004924 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004925 return true;
4926 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004927
Calin Juravle10e244f2015-01-26 18:54:32 +00004928 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004929
Calin Juravle10e244f2015-01-26 18:54:32 +00004930 bool CanThrow() const OVERRIDE { return true; }
4931
4932 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004933
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004934
4935 DECLARE_INSTRUCTION(NullCheck);
4936
4937 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004938 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4939};
4940
4941class FieldInfo : public ValueObject {
4942 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004943 FieldInfo(MemberOffset field_offset,
4944 Primitive::Type field_type,
4945 bool is_volatile,
4946 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004947 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004948 const DexFile& dex_file,
4949 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004950 : field_offset_(field_offset),
4951 field_type_(field_type),
4952 is_volatile_(is_volatile),
4953 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004954 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004955 dex_file_(dex_file),
4956 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004957
4958 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004959 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004960 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004961 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004962 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004963 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004964 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004965
4966 private:
4967 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004968 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004969 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004970 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004971 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004972 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004973 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004974};
4975
Vladimir Markofcb503c2016-05-18 12:48:17 +01004976class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004977 public:
4978 HInstanceFieldGet(HInstruction* value,
4979 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004980 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004981 bool is_volatile,
4982 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004983 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004984 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004985 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004986 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004987 : HExpression(field_type,
4988 SideEffects::FieldReadOfType(field_type, is_volatile),
4989 dex_pc),
4990 field_info_(field_offset,
4991 field_type,
4992 is_volatile,
4993 field_idx,
4994 declaring_class_def_index,
4995 dex_file,
4996 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004997 SetRawInputAt(0, value);
4998 }
4999
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005000 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005001
Vladimir Marko372f10e2016-05-17 16:30:10 +01005002 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5003 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005004 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005005 }
5006
Calin Juravle641547a2015-04-21 22:08:51 +01005007 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5008 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005009 }
5010
5011 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005012 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5013 }
5014
Calin Juravle52c48962014-12-16 17:02:57 +00005015 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005016 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005017 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005018 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005019
5020 DECLARE_INSTRUCTION(InstanceFieldGet);
5021
5022 private:
5023 const FieldInfo field_info_;
5024
5025 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5026};
5027
Vladimir Markofcb503c2016-05-18 12:48:17 +01005028class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005029 public:
5030 HInstanceFieldSet(HInstruction* object,
5031 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005032 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005033 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005034 bool is_volatile,
5035 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005036 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005037 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005038 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005039 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005040 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5041 dex_pc),
5042 field_info_(field_offset,
5043 field_type,
5044 is_volatile,
5045 field_idx,
5046 declaring_class_def_index,
5047 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005048 dex_cache) {
5049 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005050 SetRawInputAt(0, object);
5051 SetRawInputAt(1, value);
5052 }
5053
Calin Juravle641547a2015-04-21 22:08:51 +01005054 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5055 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005056 }
5057
Calin Juravle52c48962014-12-16 17:02:57 +00005058 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005059 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005060 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005061 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005062 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005063 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5064 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005065
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005066 DECLARE_INSTRUCTION(InstanceFieldSet);
5067
5068 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005069 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5070 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5071 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5072 "Too many packed fields.");
5073
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005074 const FieldInfo field_info_;
5075
5076 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5077};
5078
Vladimir Markofcb503c2016-05-18 12:48:17 +01005079class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005080 public:
Aart Bik18b36ab2016-04-13 16:41:35 -07005081 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type, uint32_t dex_pc)
5082 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005083 SetRawInputAt(0, array);
5084 SetRawInputAt(1, index);
5085 }
5086
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005087 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005088 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005089 return true;
5090 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005091 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005092 // TODO: We can be smarter here.
5093 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5094 // which generates the implicit null check. There are cases when these can be removed
5095 // to produce better code. If we ever add optimizations to do so we should allow an
5096 // implicit check here (as long as the address falls in the first page).
5097 return false;
5098 }
5099
David Brazdil4833f5a2015-12-16 10:37:39 +00005100 bool IsEquivalentOf(HArrayGet* other) const {
5101 bool result = (GetDexPc() == other->GetDexPc());
5102 if (kIsDebugBuild && result) {
5103 DCHECK_EQ(GetBlock(), other->GetBlock());
5104 DCHECK_EQ(GetArray(), other->GetArray());
5105 DCHECK_EQ(GetIndex(), other->GetIndex());
5106 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005107 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005108 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005109 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5110 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005111 }
5112 }
5113 return result;
5114 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005115
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005116 HInstruction* GetArray() const { return InputAt(0); }
5117 HInstruction* GetIndex() const { return InputAt(1); }
5118
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005119 DECLARE_INSTRUCTION(ArrayGet);
5120
5121 private:
5122 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5123};
5124
Vladimir Markofcb503c2016-05-18 12:48:17 +01005125class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005126 public:
5127 HArraySet(HInstruction* array,
5128 HInstruction* index,
5129 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005130 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005131 uint32_t dex_pc)
5132 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005133 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5134 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5135 SetPackedFlag<kFlagValueCanBeNull>(true);
5136 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005137 SetRawInputAt(0, array);
5138 SetRawInputAt(1, index);
5139 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005140 // Make a best guess now, may be refined during SSA building.
5141 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005142 }
5143
Calin Juravle77520bc2015-01-12 18:45:46 +00005144 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005145 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005146 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005147 }
5148
Mingyao Yang81014cb2015-06-02 03:16:27 -07005149 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005150 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005151
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005152 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005153 // TODO: Same as for ArrayGet.
5154 return false;
5155 }
5156
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005157 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005158 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005159 }
5160
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005161 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005162 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005163 }
5164
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005165 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005166 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005167 }
5168
Vladimir Markoa1de9182016-02-25 11:37:38 +00005169 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5170 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5171 bool StaticTypeOfArrayIsObjectArray() const {
5172 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5173 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005174
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005175 HInstruction* GetArray() const { return InputAt(0); }
5176 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005177 HInstruction* GetValue() const { return InputAt(2); }
5178
5179 Primitive::Type GetComponentType() const {
5180 // The Dex format does not type floating point index operations. Since the
5181 // `expected_component_type_` is set during building and can therefore not
5182 // be correct, we also check what is the value type. If it is a floating
5183 // point type, we must use that type.
5184 Primitive::Type value_type = GetValue()->GetType();
5185 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5186 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005187 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005188 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005189
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005190 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005191 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005192 }
5193
Aart Bik18b36ab2016-04-13 16:41:35 -07005194 void ComputeSideEffects() {
5195 Primitive::Type type = GetComponentType();
5196 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5197 SideEffectsForArchRuntimeCalls(type)));
5198 }
5199
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005200 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5201 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5202 }
5203
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005204 DECLARE_INSTRUCTION(ArraySet);
5205
5206 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005207 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5208 static constexpr size_t kFieldExpectedComponentTypeSize =
5209 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5210 static constexpr size_t kFlagNeedsTypeCheck =
5211 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5212 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005213 // Cached information for the reference_type_info_ so that codegen
5214 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005215 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5216 static constexpr size_t kNumberOfArraySetPackedBits =
5217 kFlagStaticTypeOfArrayIsObjectArray + 1;
5218 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5219 using ExpectedComponentTypeField =
5220 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005221
5222 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5223};
5224
Vladimir Markofcb503c2016-05-18 12:48:17 +01005225class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005226 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005227 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005228 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005229 // Note that arrays do not change length, so the instruction does not
5230 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005231 SetRawInputAt(0, array);
5232 }
5233
Calin Juravle77520bc2015-01-12 18:45:46 +00005234 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005235 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005236 return true;
5237 }
Calin Juravle641547a2015-04-21 22:08:51 +01005238 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5239 return obj == InputAt(0);
5240 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005241
Vladimir Markodce016e2016-04-28 13:10:02 +01005242 void MarkAsStringLength() { SetPackedFlag<kFlagIsStringLength>(); }
5243 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5244
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005245 DECLARE_INSTRUCTION(ArrayLength);
5246
5247 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005248 // We treat a String as an array, creating the HArrayLength from String.length()
5249 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5250 // determine whether a particular HArrayLength is actually a String.length() by
5251 // looking at the type of the input but that requires holding the mutator lock, so
5252 // we prefer to use a flag, so that code generators don't need to do the locking.
5253 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5254 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5255 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5256 "Too many packed fields.");
5257
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005258 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5259};
5260
Vladimir Markofcb503c2016-05-18 12:48:17 +01005261class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005262 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005263 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5264 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005265 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005266 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005267 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005268 SetRawInputAt(0, index);
5269 SetRawInputAt(1, length);
5270 }
5271
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005272 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005273 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005274 return true;
5275 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005276
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005277 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005278
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005279 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005280
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005281 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005282
5283 DECLARE_INSTRUCTION(BoundsCheck);
5284
5285 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005286 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5287};
5288
Vladimir Markofcb503c2016-05-18 12:48:17 +01005289class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005290 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005291 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005292 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005293
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005294 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005295 return true;
5296 }
5297
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005298 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5299 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005300
5301 DECLARE_INSTRUCTION(SuspendCheck);
5302
5303 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005304 // Only used for code generation, in order to share the same slow path between back edges
5305 // of a same loop.
5306 SlowPathCode* slow_path_;
5307
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005308 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5309};
5310
David Srbecky0cf44932015-12-09 14:09:59 +00005311// Pseudo-instruction which provides the native debugger with mapping information.
5312// It ensures that we can generate line number and local variables at this point.
5313class HNativeDebugInfo : public HTemplateInstruction<0> {
5314 public:
5315 explicit HNativeDebugInfo(uint32_t dex_pc)
5316 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5317
5318 bool NeedsEnvironment() const OVERRIDE {
5319 return true;
5320 }
5321
5322 DECLARE_INSTRUCTION(NativeDebugInfo);
5323
5324 private:
5325 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5326};
5327
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005328/**
5329 * Instruction to load a Class object.
5330 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005331class HLoadClass FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005332 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005333 HLoadClass(HCurrentMethod* current_method,
5334 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005335 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005336 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005337 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005338 bool needs_access_check,
5339 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005340 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005341 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005342 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005343 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005344 // Referrers class should not need access check. We never inline unverified
5345 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005346 DCHECK(!is_referrers_class || !needs_access_check);
5347
5348 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5349 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5350 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5351 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005352 SetRawInputAt(0, current_method);
5353 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005354
5355 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005356
Vladimir Marko372f10e2016-05-17 16:30:10 +01005357 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005358 // Note that we don't need to test for generate_clinit_check_.
5359 // Whether or not we need to generate the clinit check is processed in
5360 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005361 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005362 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005363 }
5364
5365 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5366
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005367 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005368 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005369
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005370 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005371 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005372 }
5373
Calin Juravle0ba218d2015-05-19 18:46:01 +01005374 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005375 // The entrypoint the code generator is going to call does not do
5376 // clinit of the class.
5377 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005378 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005379 }
5380
5381 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005382 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005383 (!IsReferrersClass() && !IsInDexCache()) ||
5384 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005385 }
5386
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005387
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005388 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005389 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005390 }
5391
Calin Juravleacf735c2015-02-12 15:25:22 +00005392 ReferenceTypeInfo GetLoadedClassRTI() {
5393 return loaded_class_rti_;
5394 }
5395
5396 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5397 // Make sure we only set exact types (the loaded class should never be merged).
5398 DCHECK(rti.IsExact());
5399 loaded_class_rti_ = rti;
5400 }
5401
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005402 const DexFile& GetDexFile() { return dex_file_; }
5403
Vladimir Markoa1de9182016-02-25 11:37:38 +00005404 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005405
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005406 static SideEffects SideEffectsForArchRuntimeCalls() {
5407 return SideEffects::CanTriggerGC();
5408 }
5409
Vladimir Markoa1de9182016-02-25 11:37:38 +00005410 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5411 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5412 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5413 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005414
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005415 DECLARE_INSTRUCTION(LoadClass);
5416
5417 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005418 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5419 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5420 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005421 // Whether this instruction must generate the initialization check.
5422 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005423 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5424 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5425 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5426
5427 const uint16_t type_index_;
5428 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005429
Calin Juravleacf735c2015-02-12 15:25:22 +00005430 ReferenceTypeInfo loaded_class_rti_;
5431
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005432 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5433};
5434
Vladimir Marko372f10e2016-05-17 16:30:10 +01005435class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005436 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005437 // Determines how to load the String.
5438 enum class LoadKind {
5439 // Use boot image String* address that will be known at link time.
5440 // Used for boot image strings referenced by boot image code in non-PIC mode.
5441 kBootImageLinkTimeAddress,
5442
5443 // Use PC-relative boot image String* address that will be known at link time.
5444 // Used for boot image strings referenced by boot image code in PIC mode.
5445 kBootImageLinkTimePcRelative,
5446
5447 // Use a known boot image String* address, embedded in the code by the codegen.
5448 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5449 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5450 // GetIncludePatchInformation().
5451 kBootImageAddress,
5452
5453 // Load from the resolved strings array at an absolute address.
5454 // Used for strings outside the boot image referenced by JIT-compiled code.
5455 kDexCacheAddress,
5456
5457 // Load from resolved strings array in the dex cache using a PC-relative load.
5458 // Used for strings outside boot image when we know that we can access
5459 // the dex cache arrays using a PC-relative load.
5460 kDexCachePcRelative,
5461
5462 // Load from resolved strings array accessed through the class loaded from
5463 // the compiled method's own ArtMethod*. This is the default access type when
5464 // all other types are unavailable.
5465 kDexCacheViaMethod,
5466
5467 kLast = kDexCacheViaMethod
5468 };
5469
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005470 HLoadString(HCurrentMethod* current_method,
5471 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005472 const DexFile& dex_file,
5473 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005474 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5475 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005476 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005477 SetPackedFlag<kFlagIsInDexCache>(false);
5478 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5479 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005480 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005481
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005482 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5483 DCHECK(HasAddress(load_kind));
5484 load_data_.address = address;
5485 SetLoadKindInternal(load_kind);
5486 }
5487
5488 void SetLoadKindWithStringReference(LoadKind load_kind,
5489 const DexFile& dex_file,
5490 uint32_t string_index) {
5491 DCHECK(HasStringReference(load_kind));
5492 load_data_.ref.dex_file = &dex_file;
5493 string_index_ = string_index;
5494 SetLoadKindInternal(load_kind);
5495 }
5496
5497 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5498 const DexFile& dex_file,
5499 uint32_t element_index) {
5500 DCHECK(HasDexCacheReference(load_kind));
5501 load_data_.ref.dex_file = &dex_file;
5502 load_data_.ref.dex_cache_element_index = element_index;
5503 SetLoadKindInternal(load_kind);
5504 }
5505
5506 LoadKind GetLoadKind() const {
5507 return GetPackedField<LoadKindField>();
5508 }
5509
5510 const DexFile& GetDexFile() const;
5511
5512 uint32_t GetStringIndex() const {
5513 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5514 return string_index_;
5515 }
5516
5517 uint32_t GetDexCacheElementOffset() const;
5518
5519 uint64_t GetAddress() const {
5520 DCHECK(HasAddress(GetLoadKind()));
5521 return load_data_.address;
5522 }
5523
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005524 bool CanBeMoved() const OVERRIDE { return true; }
5525
Vladimir Marko372f10e2016-05-17 16:30:10 +01005526 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005527
5528 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5529
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005530 // Will call the runtime if we need to load the string through
5531 // the dex cache and the string is not guaranteed to be there yet.
5532 bool NeedsEnvironment() const OVERRIDE {
5533 LoadKind load_kind = GetLoadKind();
5534 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5535 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5536 load_kind == LoadKind::kBootImageAddress) {
5537 return false;
5538 }
5539 return !IsInDexCache();
5540 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005541
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005542 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5543 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5544 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005545
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005546 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005547 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005548
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005549 static SideEffects SideEffectsForArchRuntimeCalls() {
5550 return SideEffects::CanTriggerGC();
5551 }
5552
Vladimir Markoa1de9182016-02-25 11:37:38 +00005553 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5554
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005555 void MarkInDexCache() {
5556 SetPackedFlag<kFlagIsInDexCache>(true);
5557 DCHECK(!NeedsEnvironment());
5558 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005559 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005560 }
5561
Vladimir Marko372f10e2016-05-17 16:30:10 +01005562 void AddSpecialInput(HInstruction* special_input);
5563
5564 using HInstruction::GetInputRecords; // Keep the const version visible.
5565 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5566 return ArrayRef<HUserRecord<HInstruction*>>(
5567 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005568 }
5569
Vladimir Marko372f10e2016-05-17 16:30:10 +01005570 Primitive::Type GetType() const OVERRIDE {
5571 return Primitive::kPrimNot;
5572 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005573
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005574 DECLARE_INSTRUCTION(LoadString);
5575
5576 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005577 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005578 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5579 static constexpr size_t kFieldLoadKindSize =
5580 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5581 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005582 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005583 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005584
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005585 static bool HasStringReference(LoadKind load_kind) {
5586 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5587 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5588 load_kind == LoadKind::kDexCacheViaMethod;
5589 }
5590
5591 static bool HasAddress(LoadKind load_kind) {
5592 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5593 }
5594
5595 static bool HasDexCacheReference(LoadKind load_kind) {
5596 return load_kind == LoadKind::kDexCachePcRelative;
5597 }
5598
5599 void SetLoadKindInternal(LoadKind load_kind);
5600
Vladimir Marko372f10e2016-05-17 16:30:10 +01005601 HUserRecord<HInstruction*> special_input_;
5602
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005603 // String index serves also as the hash code and it's also needed for slow-paths,
5604 // so it must not be overwritten with other load data.
5605 uint32_t string_index_;
5606
5607 union {
5608 struct {
5609 const DexFile* dex_file; // For string reference and dex cache reference.
5610 uint32_t dex_cache_element_index; // Only for dex cache reference.
5611 } ref;
5612 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5613 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005614
5615 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5616};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005617std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5618
5619// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5620inline const DexFile& HLoadString::GetDexFile() const {
5621 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5622 << GetLoadKind();
5623 return *load_data_.ref.dex_file;
5624}
5625
5626// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5627inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5628 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5629 return load_data_.ref.dex_cache_element_index;
5630}
5631
5632// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5633inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5634 // The special input is used for PC-relative loads on some architectures.
5635 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5636 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005637 // HLoadString::GetInputRecords() returns an empty array at this point,
5638 // so use the GetInputRecords() from the base class to set the input record.
5639 DCHECK(special_input_.GetInstruction() == nullptr);
5640 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005641 special_input->AddUseAt(this, 0);
5642}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005643
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005644/**
5645 * Performs an initialization check on its Class object input.
5646 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005647class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005648 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005649 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005650 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005651 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005652 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5653 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005654 SetRawInputAt(0, constant);
5655 }
5656
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005657 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005658 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005659 return true;
5660 }
5661
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005662 bool NeedsEnvironment() const OVERRIDE {
5663 // May call runtime to initialize the class.
5664 return true;
5665 }
5666
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005667 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005668
5669 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5670
5671 DECLARE_INSTRUCTION(ClinitCheck);
5672
5673 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005674 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5675};
5676
Vladimir Markofcb503c2016-05-18 12:48:17 +01005677class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005678 public:
5679 HStaticFieldGet(HInstruction* cls,
5680 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005681 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005682 bool is_volatile,
5683 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005684 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005685 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005686 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005687 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005688 : HExpression(field_type,
5689 SideEffects::FieldReadOfType(field_type, is_volatile),
5690 dex_pc),
5691 field_info_(field_offset,
5692 field_type,
5693 is_volatile,
5694 field_idx,
5695 declaring_class_def_index,
5696 dex_file,
5697 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005698 SetRawInputAt(0, cls);
5699 }
5700
Calin Juravle52c48962014-12-16 17:02:57 +00005701
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005702 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005703
Vladimir Marko372f10e2016-05-17 16:30:10 +01005704 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5705 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005706 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005707 }
5708
5709 size_t ComputeHashCode() const OVERRIDE {
5710 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5711 }
5712
Calin Juravle52c48962014-12-16 17:02:57 +00005713 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005714 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5715 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005716 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005717
5718 DECLARE_INSTRUCTION(StaticFieldGet);
5719
5720 private:
5721 const FieldInfo field_info_;
5722
5723 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5724};
5725
Vladimir Markofcb503c2016-05-18 12:48:17 +01005726class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005727 public:
5728 HStaticFieldSet(HInstruction* cls,
5729 HInstruction* value,
5730 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005731 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005732 bool is_volatile,
5733 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005734 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005735 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005736 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005737 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005738 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5739 dex_pc),
5740 field_info_(field_offset,
5741 field_type,
5742 is_volatile,
5743 field_idx,
5744 declaring_class_def_index,
5745 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005746 dex_cache) {
5747 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005748 SetRawInputAt(0, cls);
5749 SetRawInputAt(1, value);
5750 }
5751
Calin Juravle52c48962014-12-16 17:02:57 +00005752 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005753 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5754 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005755 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005756
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005757 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005758 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5759 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005760
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005761 DECLARE_INSTRUCTION(StaticFieldSet);
5762
5763 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005764 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5765 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5766 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5767 "Too many packed fields.");
5768
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005769 const FieldInfo field_info_;
5770
5771 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5772};
5773
Vladimir Markofcb503c2016-05-18 12:48:17 +01005774class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005775 public:
5776 HUnresolvedInstanceFieldGet(HInstruction* obj,
5777 Primitive::Type field_type,
5778 uint32_t field_index,
5779 uint32_t dex_pc)
5780 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5781 field_index_(field_index) {
5782 SetRawInputAt(0, obj);
5783 }
5784
5785 bool NeedsEnvironment() const OVERRIDE { return true; }
5786 bool CanThrow() const OVERRIDE { return true; }
5787
5788 Primitive::Type GetFieldType() const { return GetType(); }
5789 uint32_t GetFieldIndex() const { return field_index_; }
5790
5791 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5792
5793 private:
5794 const uint32_t field_index_;
5795
5796 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5797};
5798
Vladimir Markofcb503c2016-05-18 12:48:17 +01005799class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005800 public:
5801 HUnresolvedInstanceFieldSet(HInstruction* obj,
5802 HInstruction* value,
5803 Primitive::Type field_type,
5804 uint32_t field_index,
5805 uint32_t dex_pc)
5806 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005807 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005808 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005809 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005810 SetRawInputAt(0, obj);
5811 SetRawInputAt(1, value);
5812 }
5813
5814 bool NeedsEnvironment() const OVERRIDE { return true; }
5815 bool CanThrow() const OVERRIDE { return true; }
5816
Vladimir Markoa1de9182016-02-25 11:37:38 +00005817 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005818 uint32_t GetFieldIndex() const { return field_index_; }
5819
5820 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5821
5822 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005823 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5824 static constexpr size_t kFieldFieldTypeSize =
5825 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5826 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5827 kFieldFieldType + kFieldFieldTypeSize;
5828 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5829 "Too many packed fields.");
5830 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5831
Calin Juravlee460d1d2015-09-29 04:52:17 +01005832 const uint32_t field_index_;
5833
5834 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5835};
5836
Vladimir Markofcb503c2016-05-18 12:48:17 +01005837class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005838 public:
5839 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5840 uint32_t field_index,
5841 uint32_t dex_pc)
5842 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5843 field_index_(field_index) {
5844 }
5845
5846 bool NeedsEnvironment() const OVERRIDE { return true; }
5847 bool CanThrow() const OVERRIDE { return true; }
5848
5849 Primitive::Type GetFieldType() const { return GetType(); }
5850 uint32_t GetFieldIndex() const { return field_index_; }
5851
5852 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5853
5854 private:
5855 const uint32_t field_index_;
5856
5857 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5858};
5859
Vladimir Markofcb503c2016-05-18 12:48:17 +01005860class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005861 public:
5862 HUnresolvedStaticFieldSet(HInstruction* value,
5863 Primitive::Type field_type,
5864 uint32_t field_index,
5865 uint32_t dex_pc)
5866 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005867 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005868 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005869 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005870 SetRawInputAt(0, value);
5871 }
5872
5873 bool NeedsEnvironment() const OVERRIDE { return true; }
5874 bool CanThrow() const OVERRIDE { return true; }
5875
Vladimir Markoa1de9182016-02-25 11:37:38 +00005876 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005877 uint32_t GetFieldIndex() const { return field_index_; }
5878
5879 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5880
5881 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005882 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5883 static constexpr size_t kFieldFieldTypeSize =
5884 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5885 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5886 kFieldFieldType + kFieldFieldTypeSize;
5887 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5888 "Too many packed fields.");
5889 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5890
Calin Juravlee460d1d2015-09-29 04:52:17 +01005891 const uint32_t field_index_;
5892
5893 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5894};
5895
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005896// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005897class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005898 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005899 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5900 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005901
David Brazdilbbd733e2015-08-18 17:48:17 +01005902 bool CanBeNull() const OVERRIDE { return false; }
5903
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005904 DECLARE_INSTRUCTION(LoadException);
5905
5906 private:
5907 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5908};
5909
David Brazdilcb1c0552015-08-04 16:22:25 +01005910// Implicit part of move-exception which clears thread-local exception storage.
5911// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005912class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01005913 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005914 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5915 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005916
5917 DECLARE_INSTRUCTION(ClearException);
5918
5919 private:
5920 DISALLOW_COPY_AND_ASSIGN(HClearException);
5921};
5922
Vladimir Markofcb503c2016-05-18 12:48:17 +01005923class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005924 public:
5925 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005926 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005927 SetRawInputAt(0, exception);
5928 }
5929
5930 bool IsControlFlow() const OVERRIDE { return true; }
5931
5932 bool NeedsEnvironment() const OVERRIDE { return true; }
5933
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005934 bool CanThrow() const OVERRIDE { return true; }
5935
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005936
5937 DECLARE_INSTRUCTION(Throw);
5938
5939 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005940 DISALLOW_COPY_AND_ASSIGN(HThrow);
5941};
5942
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005943/**
5944 * Implementation strategies for the code generator of a HInstanceOf
5945 * or `HCheckCast`.
5946 */
5947enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005948 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005949 kExactCheck, // Can do a single class compare.
5950 kClassHierarchyCheck, // Can just walk the super class chain.
5951 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5952 kInterfaceCheck, // No optimization yet when checking against an interface.
5953 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005954 kArrayCheck, // No optimization yet when checking against a generic array.
5955 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005956};
5957
Roland Levillain86503782016-02-11 19:07:30 +00005958std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5959
Vladimir Markofcb503c2016-05-18 12:48:17 +01005960class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005961 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005962 HInstanceOf(HInstruction* object,
5963 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005964 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005965 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005966 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005967 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005968 dex_pc) {
5969 SetPackedField<TypeCheckKindField>(check_kind);
5970 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005971 SetRawInputAt(0, object);
5972 SetRawInputAt(1, constant);
5973 }
5974
5975 bool CanBeMoved() const OVERRIDE { return true; }
5976
Vladimir Marko372f10e2016-05-17 16:30:10 +01005977 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005978 return true;
5979 }
5980
5981 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005982 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005983 }
5984
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005985 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005986 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5987 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5988 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5989 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005990
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005991 static bool CanCallRuntime(TypeCheckKind check_kind) {
5992 // Mips currently does runtime calls for any other checks.
5993 return check_kind != TypeCheckKind::kExactCheck;
5994 }
5995
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005996 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005997 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005998 }
5999
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006000 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006001
6002 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006003 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6004 static constexpr size_t kFieldTypeCheckKindSize =
6005 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6006 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6007 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6008 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6009 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006010
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006011 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6012};
6013
Vladimir Markofcb503c2016-05-18 12:48:17 +01006014class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006015 public:
David Brazdilf5552582015-12-27 13:36:12 +00006016 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006017 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006018 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6019 SetPackedFlag<kFlagUpperCanBeNull>(true);
6020 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006021 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006022 SetRawInputAt(0, input);
6023 }
6024
David Brazdilf5552582015-12-27 13:36:12 +00006025 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006026 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006027 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006028 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006029
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006030 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006031 DCHECK(GetUpperCanBeNull() || !can_be_null);
6032 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006033 }
6034
Vladimir Markoa1de9182016-02-25 11:37:38 +00006035 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006036
Calin Juravleb1498f62015-02-16 13:13:29 +00006037 DECLARE_INSTRUCTION(BoundType);
6038
6039 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006040 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6041 // is false then CanBeNull() cannot be true).
6042 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6043 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6044 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6045 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6046
Calin Juravleb1498f62015-02-16 13:13:29 +00006047 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006048 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6049 // It is used to bound the type in cases like:
6050 // if (x instanceof ClassX) {
6051 // // uper_bound_ will be ClassX
6052 // }
David Brazdilf5552582015-12-27 13:36:12 +00006053 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006054
6055 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6056};
6057
Vladimir Markofcb503c2016-05-18 12:48:17 +01006058class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006059 public:
6060 HCheckCast(HInstruction* object,
6061 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006062 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006063 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006064 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6065 SetPackedField<TypeCheckKindField>(check_kind);
6066 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006067 SetRawInputAt(0, object);
6068 SetRawInputAt(1, constant);
6069 }
6070
6071 bool CanBeMoved() const OVERRIDE { return true; }
6072
Vladimir Marko372f10e2016-05-17 16:30:10 +01006073 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006074 return true;
6075 }
6076
6077 bool NeedsEnvironment() const OVERRIDE {
6078 // Instruction may throw a CheckCastError.
6079 return true;
6080 }
6081
6082 bool CanThrow() const OVERRIDE { return true; }
6083
Vladimir Markoa1de9182016-02-25 11:37:38 +00006084 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6085 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6086 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6087 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006088
6089 DECLARE_INSTRUCTION(CheckCast);
6090
6091 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006092 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6093 static constexpr size_t kFieldTypeCheckKindSize =
6094 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6095 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6096 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6097 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6098 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006099
6100 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006101};
6102
Vladimir Markofcb503c2016-05-18 12:48:17 +01006103class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006104 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006105 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006106 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006107 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6108 SetPackedField<BarrierKindField>(barrier_kind);
6109 }
Calin Juravle27df7582015-04-17 19:12:31 +01006110
Vladimir Markoa1de9182016-02-25 11:37:38 +00006111 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006112
6113 DECLARE_INSTRUCTION(MemoryBarrier);
6114
6115 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006116 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6117 static constexpr size_t kFieldBarrierKindSize =
6118 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6119 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6120 kFieldBarrierKind + kFieldBarrierKindSize;
6121 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6122 "Too many packed fields.");
6123 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006124
6125 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6126};
6127
Vladimir Markofcb503c2016-05-18 12:48:17 +01006128class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006129 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006130 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006131 kEnter,
6132 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006133 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006134 };
6135
6136 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006137 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006138 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6139 dex_pc) {
6140 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006141 SetRawInputAt(0, object);
6142 }
6143
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006144 // Instruction may go into runtime, so we need an environment.
6145 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006146
6147 bool CanThrow() const OVERRIDE {
6148 // Verifier guarantees that monitor-exit cannot throw.
6149 // This is important because it allows the HGraphBuilder to remove
6150 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6151 return IsEnter();
6152 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006153
Vladimir Markoa1de9182016-02-25 11:37:38 +00006154 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6155 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006156
6157 DECLARE_INSTRUCTION(MonitorOperation);
6158
Calin Juravle52c48962014-12-16 17:02:57 +00006159 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006160 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6161 static constexpr size_t kFieldOperationKindSize =
6162 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6163 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6164 kFieldOperationKind + kFieldOperationKindSize;
6165 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6166 "Too many packed fields.");
6167 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006168
6169 private:
6170 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6171};
6172
Vladimir Markofcb503c2016-05-18 12:48:17 +01006173class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006174 public:
6175 HSelect(HInstruction* condition,
6176 HInstruction* true_value,
6177 HInstruction* false_value,
6178 uint32_t dex_pc)
6179 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6180 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6181
6182 // First input must be `true_value` or `false_value` to allow codegens to
6183 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6184 // that architectures which implement HSelect as a conditional move also
6185 // will not need to invert the condition.
6186 SetRawInputAt(0, false_value);
6187 SetRawInputAt(1, true_value);
6188 SetRawInputAt(2, condition);
6189 }
6190
6191 HInstruction* GetFalseValue() const { return InputAt(0); }
6192 HInstruction* GetTrueValue() const { return InputAt(1); }
6193 HInstruction* GetCondition() const { return InputAt(2); }
6194
6195 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006196 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6197 return true;
6198 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006199
6200 bool CanBeNull() const OVERRIDE {
6201 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6202 }
6203
6204 DECLARE_INSTRUCTION(Select);
6205
6206 private:
6207 DISALLOW_COPY_AND_ASSIGN(HSelect);
6208};
6209
Vladimir Markof9f64412015-09-02 14:05:49 +01006210class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006211 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006212 MoveOperands(Location source,
6213 Location destination,
6214 Primitive::Type type,
6215 HInstruction* instruction)
6216 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006217
6218 Location GetSource() const { return source_; }
6219 Location GetDestination() const { return destination_; }
6220
6221 void SetSource(Location value) { source_ = value; }
6222 void SetDestination(Location value) { destination_ = value; }
6223
6224 // The parallel move resolver marks moves as "in-progress" by clearing the
6225 // destination (but not the source).
6226 Location MarkPending() {
6227 DCHECK(!IsPending());
6228 Location dest = destination_;
6229 destination_ = Location::NoLocation();
6230 return dest;
6231 }
6232
6233 void ClearPending(Location dest) {
6234 DCHECK(IsPending());
6235 destination_ = dest;
6236 }
6237
6238 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006239 DCHECK(source_.IsValid() || destination_.IsInvalid());
6240 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006241 }
6242
6243 // True if this blocks a move from the given location.
6244 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006245 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006246 }
6247
6248 // A move is redundant if it's been eliminated, if its source and
6249 // destination are the same, or if its destination is unneeded.
6250 bool IsRedundant() const {
6251 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6252 }
6253
6254 // We clear both operands to indicate move that's been eliminated.
6255 void Eliminate() {
6256 source_ = destination_ = Location::NoLocation();
6257 }
6258
6259 bool IsEliminated() const {
6260 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6261 return source_.IsInvalid();
6262 }
6263
Alexey Frunze4dda3372015-06-01 18:31:49 -07006264 Primitive::Type GetType() const { return type_; }
6265
Nicolas Geoffray90218252015-04-15 11:56:51 +01006266 bool Is64BitMove() const {
6267 return Primitive::Is64BitType(type_);
6268 }
6269
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006270 HInstruction* GetInstruction() const { return instruction_; }
6271
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006272 private:
6273 Location source_;
6274 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006275 // The type this move is for.
6276 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006277 // The instruction this move is assocatied with. Null when this move is
6278 // for moving an input in the expected locations of user (including a phi user).
6279 // This is only used in debug mode, to ensure we do not connect interval siblings
6280 // in the same parallel move.
6281 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006282};
6283
Roland Levillainc9285912015-12-18 10:38:42 +00006284std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6285
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006286static constexpr size_t kDefaultNumberOfMoves = 4;
6287
Vladimir Markofcb503c2016-05-18 12:48:17 +01006288class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006289 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006290 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006291 : HTemplateInstruction(SideEffects::None(), dex_pc),
6292 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6293 moves_.reserve(kDefaultNumberOfMoves);
6294 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006295
Nicolas Geoffray90218252015-04-15 11:56:51 +01006296 void AddMove(Location source,
6297 Location destination,
6298 Primitive::Type type,
6299 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006300 DCHECK(source.IsValid());
6301 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006302 if (kIsDebugBuild) {
6303 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006304 for (const MoveOperands& move : moves_) {
6305 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006306 // Special case the situation where the move is for the spill slot
6307 // of the instruction.
6308 if ((GetPrevious() == instruction)
6309 || ((GetPrevious() == nullptr)
6310 && instruction->IsPhi()
6311 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006312 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006313 << "Doing parallel moves for the same instruction.";
6314 } else {
6315 DCHECK(false) << "Doing parallel moves for the same instruction.";
6316 }
6317 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006318 }
6319 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006320 for (const MoveOperands& move : moves_) {
6321 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006322 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006323 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006324 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006325 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006326 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006327 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006328 }
6329
Vladimir Marko225b6462015-09-28 12:17:40 +01006330 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006331 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006332 }
6333
Vladimir Marko225b6462015-09-28 12:17:40 +01006334 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006335
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006336 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006337
6338 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006339 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006340
6341 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6342};
6343
Mark Mendell0616ae02015-04-17 12:49:27 -04006344} // namespace art
6345
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006346#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6347#include "nodes_shared.h"
6348#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006349#ifdef ART_ENABLE_CODEGEN_arm
6350#include "nodes_arm.h"
6351#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006352#ifdef ART_ENABLE_CODEGEN_arm64
6353#include "nodes_arm64.h"
6354#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006355#ifdef ART_ENABLE_CODEGEN_x86
6356#include "nodes_x86.h"
6357#endif
6358
6359namespace art {
6360
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006361class HGraphVisitor : public ValueObject {
6362 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006363 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6364 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006365
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006366 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006367 virtual void VisitBasicBlock(HBasicBlock* block);
6368
Roland Levillain633021e2014-10-01 14:12:25 +01006369 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006370 void VisitInsertionOrder();
6371
Roland Levillain633021e2014-10-01 14:12:25 +01006372 // Visit the graph following dominator tree reverse post-order.
6373 void VisitReversePostOrder();
6374
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006375 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006376
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006377 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006378#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006379 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6380
6381 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6382
6383#undef DECLARE_VISIT_INSTRUCTION
6384
6385 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006386 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006387
6388 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6389};
6390
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006391class HGraphDelegateVisitor : public HGraphVisitor {
6392 public:
6393 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6394 virtual ~HGraphDelegateVisitor() {}
6395
6396 // Visit functions that delegate to to super class.
6397#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006398 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006399
6400 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6401
6402#undef DECLARE_VISIT_INSTRUCTION
6403
6404 private:
6405 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6406};
6407
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006408class HInsertionOrderIterator : public ValueObject {
6409 public:
6410 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6411
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006412 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006413 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006414 void Advance() { ++index_; }
6415
6416 private:
6417 const HGraph& graph_;
6418 size_t index_;
6419
6420 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6421};
6422
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006423class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006424 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006425 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6426 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006427 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006428 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006429
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006430 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6431 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006432 void Advance() { ++index_; }
6433
6434 private:
6435 const HGraph& graph_;
6436 size_t index_;
6437
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006438 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006439};
6440
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006441class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006442 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006443 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006444 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006445 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006446 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006447 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006448
6449 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006450 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006451 void Advance() { --index_; }
6452
6453 private:
6454 const HGraph& graph_;
6455 size_t index_;
6456
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006457 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006458};
6459
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006460class HLinearPostOrderIterator : public ValueObject {
6461 public:
6462 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006463 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006464
6465 bool Done() const { return index_ == 0; }
6466
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006467 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006468
6469 void Advance() {
6470 --index_;
6471 DCHECK_GE(index_, 0U);
6472 }
6473
6474 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006475 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006476 size_t index_;
6477
6478 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6479};
6480
6481class HLinearOrderIterator : public ValueObject {
6482 public:
6483 explicit HLinearOrderIterator(const HGraph& graph)
6484 : order_(graph.GetLinearOrder()), index_(0) {}
6485
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006486 bool Done() const { return index_ == order_.size(); }
6487 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006488 void Advance() { ++index_; }
6489
6490 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006491 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006492 size_t index_;
6493
6494 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6495};
6496
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006497// Iterator over the blocks that art part of the loop. Includes blocks part
6498// of an inner loop. The order in which the blocks are iterated is on their
6499// block id.
6500class HBlocksInLoopIterator : public ValueObject {
6501 public:
6502 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6503 : blocks_in_loop_(info.GetBlocks()),
6504 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6505 index_(0) {
6506 if (!blocks_in_loop_.IsBitSet(index_)) {
6507 Advance();
6508 }
6509 }
6510
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006511 bool Done() const { return index_ == blocks_.size(); }
6512 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006513 void Advance() {
6514 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006515 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006516 if (blocks_in_loop_.IsBitSet(index_)) {
6517 break;
6518 }
6519 }
6520 }
6521
6522 private:
6523 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006524 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006525 size_t index_;
6526
6527 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6528};
6529
Mingyao Yang3584bce2015-05-19 16:01:59 -07006530// Iterator over the blocks that art part of the loop. Includes blocks part
6531// of an inner loop. The order in which the blocks are iterated is reverse
6532// post order.
6533class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6534 public:
6535 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6536 : blocks_in_loop_(info.GetBlocks()),
6537 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6538 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006539 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006540 Advance();
6541 }
6542 }
6543
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006544 bool Done() const { return index_ == blocks_.size(); }
6545 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006546 void Advance() {
6547 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006548 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6549 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006550 break;
6551 }
6552 }
6553 }
6554
6555 private:
6556 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006557 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006558 size_t index_;
6559
6560 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6561};
6562
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006563inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006564 if (constant->IsIntConstant()) {
6565 return constant->AsIntConstant()->GetValue();
6566 } else if (constant->IsLongConstant()) {
6567 return constant->AsLongConstant()->GetValue();
6568 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006569 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006570 return 0;
6571 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006572}
6573
Vladimir Marko58155012015-08-19 12:49:41 +00006574inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6575 // For the purposes of the compiler, the dex files must actually be the same object
6576 // if we want to safely treat them as the same. This is especially important for JIT
6577 // as custom class loaders can open the same underlying file (or memory) multiple
6578 // times and provide different class resolution but no two class loaders should ever
6579 // use the same DexFile object - doing so is an unsupported hack that can lead to
6580 // all sorts of weird failures.
6581 return &lhs == &rhs;
6582}
6583
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006584#define INSTRUCTION_TYPE_CHECK(type, super) \
6585 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6586 inline const H##type* HInstruction::As##type() const { \
6587 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6588 } \
6589 inline H##type* HInstruction::As##type() { \
6590 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6591 }
6592
6593 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6594#undef INSTRUCTION_TYPE_CHECK
6595
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006596// Create space in `blocks` for adding `number_of_new_blocks` entries
6597// starting at location `at`. Blocks after `at` are moved accordingly.
6598inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6599 size_t number_of_new_blocks,
6600 size_t after) {
6601 DCHECK_LT(after, blocks->size());
6602 size_t old_size = blocks->size();
6603 size_t new_size = old_size + number_of_new_blocks;
6604 blocks->resize(new_size);
6605 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6606}
6607
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006608} // namespace art
6609
6610#endif // ART_COMPILER_OPTIMIZING_NODES_H_