blob: 27cab3a1a3338379d7e06693ff9845a4aa9e42dd [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"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010028#include "dex_file.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
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010088inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
89 // For the purposes of the compiler, the dex files must actually be the same object
90 // if we want to safely treat them as the same. This is especially important for JIT
91 // as custom class loaders can open the same underlying file (or memory) multiple
92 // times and provide different class resolution but no two class loaders should ever
93 // use the same DexFile object - doing so is an unsupported hack that can lead to
94 // all sorts of weird failures.
95 return &lhs == &rhs;
96}
97
Dave Allison20dfc792014-06-16 20:44:29 -070098enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070099 // All types.
100 kCondEQ, // ==
101 kCondNE, // !=
102 // Signed integers and floating-point numbers.
103 kCondLT, // <
104 kCondLE, // <=
105 kCondGT, // >
106 kCondGE, // >=
107 // Unsigned integers.
108 kCondB, // <
109 kCondBE, // <=
110 kCondA, // >
111 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -0700112};
113
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000114enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000115 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000116 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000117 kAnalysisFailThrowCatchLoop,
118 kAnalysisFailAmbiguousArrayOp,
119 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000120};
121
Vladimir Markof9f64412015-09-02 14:05:49 +0100122class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100123 public:
124 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
125
126 void AddInstruction(HInstruction* instruction);
127 void RemoveInstruction(HInstruction* instruction);
128
David Brazdilc3d743f2015-04-22 13:40:50 +0100129 // Insert `instruction` before/after an existing instruction `cursor`.
130 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
131 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
132
Roland Levillain6b469232014-09-25 10:10:38 +0100133 // Return true if this list contains `instruction`.
134 bool Contains(HInstruction* instruction) const;
135
Roland Levillainccc07a92014-09-16 14:48:16 +0100136 // Return true if `instruction1` is found before `instruction2` in
137 // this instruction list and false otherwise. Abort if none
138 // of these instructions is found.
139 bool FoundBefore(const HInstruction* instruction1,
140 const HInstruction* instruction2) const;
141
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000142 bool IsEmpty() const { return first_instruction_ == nullptr; }
143 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
144
145 // Update the block of all instructions to be `block`.
146 void SetBlockOfInstructions(HBasicBlock* block) const;
147
148 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000149 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000150 void Add(const HInstructionList& instruction_list);
151
David Brazdil2d7352b2015-04-20 14:52:42 +0100152 // Return the number of instructions in the list. This is an expensive operation.
153 size_t CountSize() const;
154
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100155 private:
156 HInstruction* first_instruction_;
157 HInstruction* last_instruction_;
158
159 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000160 friend class HGraph;
161 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100162 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100163 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100164
165 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
166};
167
David Brazdil4833f5a2015-12-16 10:37:39 +0000168class ReferenceTypeInfo : ValueObject {
169 public:
170 typedef Handle<mirror::Class> TypeHandle;
171
Vladimir Markoa1de9182016-02-25 11:37:38 +0000172 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
173
David Brazdil94ab38f2016-06-21 17:48:19 +0100174 static ReferenceTypeInfo Create(TypeHandle type_handle) SHARED_REQUIRES(Locks::mutator_lock_) {
175 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
176 }
177
Vladimir Markoa1de9182016-02-25 11:37:38 +0000178 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000179 return ReferenceTypeInfo(type_handle, is_exact);
180 }
181
182 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
183
Vladimir Markof39745e2016-01-26 12:16:55 +0000184 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000185 return handle.GetReference() != nullptr;
186 }
187
Vladimir Marko456307a2016-04-19 14:12:13 +0000188 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000189 return IsValidHandle(type_handle_);
190 }
191
192 bool IsExact() const { return is_exact_; }
193
194 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
195 DCHECK(IsValid());
196 return GetTypeHandle()->IsObjectClass();
197 }
198
199 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
200 DCHECK(IsValid());
201 return GetTypeHandle()->IsStringClass();
202 }
203
204 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
205 DCHECK(IsValid());
206 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
207 }
208
209 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
210 DCHECK(IsValid());
211 return GetTypeHandle()->IsInterface();
212 }
213
214 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
215 DCHECK(IsValid());
216 return GetTypeHandle()->IsArrayClass();
217 }
218
219 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
220 DCHECK(IsValid());
221 return GetTypeHandle()->IsPrimitiveArray();
222 }
223
224 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
225 DCHECK(IsValid());
226 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
227 }
228
229 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
230 DCHECK(IsValid());
231 if (!IsExact()) return false;
232 if (!IsArrayClass()) return false;
233 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
234 }
235
236 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
237 DCHECK(IsValid());
238 if (!IsExact()) return false;
239 if (!IsArrayClass()) return false;
240 if (!rti.IsArrayClass()) return false;
241 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
242 rti.GetTypeHandle()->GetComponentType());
243 }
244
245 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
246
247 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
248 DCHECK(IsValid());
249 DCHECK(rti.IsValid());
250 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
251 }
252
253 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
254 DCHECK(IsValid());
255 DCHECK(rti.IsValid());
256 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
257 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
258 }
259
260 // Returns true if the type information provide the same amount of details.
261 // Note that it does not mean that the instructions have the same actual type
262 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000263 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000264 if (!IsValid() && !rti.IsValid()) {
265 // Invalid types are equal.
266 return true;
267 }
268 if (!IsValid() || !rti.IsValid()) {
269 // One is valid, the other not.
270 return false;
271 }
272 return IsExact() == rti.IsExact()
273 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
274 }
275
276 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000277 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
278 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
279 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000280
281 // The class of the object.
282 TypeHandle type_handle_;
283 // Whether or not the type is exact or a superclass of the actual type.
284 // Whether or not we have any information about this type.
285 bool is_exact_;
286};
287
288std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
289
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000290// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100291class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000292 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100293 HGraph(ArenaAllocator* arena,
294 const DexFile& dex_file,
295 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100296 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700297 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100298 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100299 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000300 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100301 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000302 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100303 blocks_(arena->Adapter(kArenaAllocBlockList)),
304 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
305 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700306 entry_block_(nullptr),
307 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100308 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100309 number_of_vregs_(0),
310 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000311 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400312 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000313 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000314 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000315 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000316 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100317 dex_file_(dex_file),
318 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100319 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100320 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100321 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700322 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000323 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100324 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
325 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
326 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
327 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000328 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000329 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
330 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100331 blocks_.reserve(kDefaultNumberOfBlocks);
332 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000333
David Brazdilbadd8262016-02-02 16:28:56 +0000334 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
335 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
336
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000337 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100338 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
339
David Brazdil69ba7b72015-06-23 18:27:30 +0100340 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000341 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100342
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000343 HBasicBlock* GetEntryBlock() const { return entry_block_; }
344 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100345 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000346
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000347 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
348 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000349
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000350 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100351
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100352 void ComputeDominanceInformation();
353 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000354 void ClearLoopInformation();
355 void FindBackEdges(ArenaBitVector* visited);
356 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100357 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100358 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000359
David Brazdil4833f5a2015-12-16 10:37:39 +0000360 // Analyze all natural loops in this graph. Returns a code specifying that it
361 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000362 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000363 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100364
David Brazdilffee3d32015-07-06 11:48:53 +0100365 // Iterate over blocks to compute try block membership. Needs reverse post
366 // order and loop information.
367 void ComputeTryBlockInformation();
368
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000369 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000370 // Returns the instruction to replace the invoke expression or null if the
371 // invoke is for a void method. Note that the caller is responsible for replacing
372 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000373 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000374
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000375 // Update the loop and try membership of `block`, which was spawned from `reference`.
376 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
377 // should be the new back edge.
378 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
379 HBasicBlock* reference,
380 bool replace_if_back_edge);
381
Mingyao Yang3584bce2015-05-19 16:01:59 -0700382 // Need to add a couple of blocks to test if the loop body is entered and
383 // put deoptimization instructions, etc.
384 void TransformLoopHeaderForBCE(HBasicBlock* header);
385
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000386 // Removes `block` from the graph. Assumes `block` has been disconnected from
387 // other blocks and has no instructions or phis.
388 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000389
David Brazdilfc6a86a2015-06-26 10:33:45 +0000390 // Splits the edge between `block` and `successor` while preserving the
391 // indices in the predecessor/successor lists. If there are multiple edges
392 // between the blocks, the lowest indices are used.
393 // Returns the new block which is empty and has the same dex pc as `successor`.
394 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
395
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100396 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
397 void SimplifyLoop(HBasicBlock* header);
398
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000399 int32_t GetNextInstructionId() {
400 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000401 return current_instruction_id_++;
402 }
403
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000404 int32_t GetCurrentInstructionId() const {
405 return current_instruction_id_;
406 }
407
408 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000409 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000410 current_instruction_id_ = id;
411 }
412
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100413 uint16_t GetMaximumNumberOfOutVRegs() const {
414 return maximum_number_of_out_vregs_;
415 }
416
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000417 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
418 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100419 }
420
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100421 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
422 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
423 }
424
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000425 void UpdateTemporariesVRegSlots(size_t slots) {
426 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100427 }
428
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000429 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100430 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000431 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100432 }
433
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100434 void SetNumberOfVRegs(uint16_t number_of_vregs) {
435 number_of_vregs_ = number_of_vregs;
436 }
437
438 uint16_t GetNumberOfVRegs() const {
439 return number_of_vregs_;
440 }
441
442 void SetNumberOfInVRegs(uint16_t value) {
443 number_of_in_vregs_ = value;
444 }
445
David Brazdildee58d62016-04-07 09:54:26 +0000446 uint16_t GetNumberOfInVRegs() const {
447 return number_of_in_vregs_;
448 }
449
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100450 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100451 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100452 return number_of_vregs_ - number_of_in_vregs_;
453 }
454
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100455 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100456 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100457 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100458
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100459 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100460 return linear_order_;
461 }
462
Mark Mendell1152c922015-04-24 17:06:35 -0400463 bool HasBoundsChecks() const {
464 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800465 }
466
Mark Mendell1152c922015-04-24 17:06:35 -0400467 void SetHasBoundsChecks(bool value) {
468 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800469 }
470
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100471 bool ShouldGenerateConstructorBarrier() const {
472 return should_generate_constructor_barrier_;
473 }
474
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000475 bool IsDebuggable() const { return debuggable_; }
476
David Brazdil8d5b8b22015-03-24 10:51:52 +0000477 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000478 // already, it is created and inserted into the graph. This method is only for
479 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600480 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000481
482 // TODO: This is problematic for the consistency of reference type propagation
483 // because it can be created anytime after the pass and thus it will be left
484 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600485 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000486
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600487 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
488 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000489 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600490 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
491 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000492 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600493 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
494 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000495 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600496 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
497 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000498 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000499
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100500 HCurrentMethod* GetCurrentMethod();
501
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100502 const DexFile& GetDexFile() const {
503 return dex_file_;
504 }
505
506 uint32_t GetMethodIdx() const {
507 return method_idx_;
508 }
509
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100510 InvokeType GetInvokeType() const {
511 return invoke_type_;
512 }
513
Mark Mendellc4701932015-04-10 13:18:51 -0400514 InstructionSet GetInstructionSet() const {
515 return instruction_set_;
516 }
517
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000518 bool IsCompilingOsr() const { return osr_; }
519
David Brazdil77a48ae2015-09-15 12:34:04 +0000520 bool HasTryCatch() const { return has_try_catch_; }
521 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100522
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000523 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
524 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
525
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100526 ArtMethod* GetArtMethod() const { return art_method_; }
527 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
528
Mark Mendellf6529172015-11-17 11:16:56 -0500529 // Returns an instruction with the opposite boolean value from 'cond'.
530 // The instruction has been inserted into the graph, either as a constant, or
531 // before cursor.
532 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
533
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000534 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
535
David Brazdil2d7352b2015-04-20 14:52:42 +0100536 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000537 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100538 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000539
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000540 template <class InstructionType, typename ValueType>
541 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600542 ArenaSafeMap<ValueType, InstructionType*>* cache,
543 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000544 // Try to find an existing constant of the given value.
545 InstructionType* constant = nullptr;
546 auto cached_constant = cache->find(value);
547 if (cached_constant != cache->end()) {
548 constant = cached_constant->second;
549 }
550
551 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100552 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000553 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600554 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000555 cache->Overwrite(value, constant);
556 InsertConstant(constant);
557 }
558 return constant;
559 }
560
David Brazdil8d5b8b22015-03-24 10:51:52 +0000561 void InsertConstant(HConstant* instruction);
562
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000563 // Cache a float constant into the graph. This method should only be
564 // called by the SsaBuilder when creating "equivalent" instructions.
565 void CacheFloatConstant(HFloatConstant* constant);
566
567 // See CacheFloatConstant comment.
568 void CacheDoubleConstant(HDoubleConstant* constant);
569
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000570 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000571
572 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100573 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000574
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100575 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100576 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000577
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100578 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100579 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100580
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000581 HBasicBlock* entry_block_;
582 HBasicBlock* exit_block_;
583
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100584 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100585 uint16_t maximum_number_of_out_vregs_;
586
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100587 // The number of virtual registers in this method. Contains the parameters.
588 uint16_t number_of_vregs_;
589
590 // The number of virtual registers used by parameters of this method.
591 uint16_t number_of_in_vregs_;
592
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000593 // Number of vreg size slots that the temporaries use (used in baseline compiler).
594 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100595
Mark Mendell1152c922015-04-24 17:06:35 -0400596 // Has bounds checks. We can totally skip BCE if it's false.
597 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800598
David Brazdil77a48ae2015-09-15 12:34:04 +0000599 // Flag whether there are any try/catch blocks in the graph. We will skip
600 // try/catch-related passes if false.
601 bool has_try_catch_;
602
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000603 // Flag whether there are any irreducible loops in the graph.
604 bool has_irreducible_loops_;
605
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000606 // Indicates whether the graph should be compiled in a way that
607 // ensures full debuggability. If false, we can apply more
608 // aggressive optimizations that may limit the level of debugging.
609 const bool debuggable_;
610
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000611 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000612 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000613
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100614 // The dex file from which the method is from.
615 const DexFile& dex_file_;
616
617 // The method index in the dex file.
618 const uint32_t method_idx_;
619
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100620 // If inlined, this encodes how the callee is being invoked.
621 const InvokeType invoke_type_;
622
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100623 // Whether the graph has been transformed to SSA form. Only used
624 // in debug mode to ensure we are not using properties only valid
625 // for non-SSA form (like the number of temporaries).
626 bool in_ssa_form_;
627
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100628 const bool should_generate_constructor_barrier_;
629
Mathieu Chartiere401d142015-04-22 13:56:20 -0700630 const InstructionSet instruction_set_;
631
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000632 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000633 HNullConstant* cached_null_constant_;
634 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000635 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000636 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000637 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000638
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100639 HCurrentMethod* cached_current_method_;
640
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100641 // The ArtMethod this graph is for. Note that for AOT, it may be null,
642 // for example for methods whose declaring class could not be resolved
643 // (such as when the superclass could not be found).
644 ArtMethod* art_method_;
645
David Brazdil4833f5a2015-12-16 10:37:39 +0000646 // Keep the RTI of inexact Object to avoid having to pass stack handle
647 // collection pointer to passes which may create NullConstant.
648 ReferenceTypeInfo inexact_object_rti_;
649
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000650 // Whether we are compiling this graph for on stack replacement: this will
651 // make all loops seen as irreducible and emit special stack maps to mark
652 // compiled code entries which the interpreter can directly jump to.
653 const bool osr_;
654
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000655 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100656 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000657 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000658 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000659 DISALLOW_COPY_AND_ASSIGN(HGraph);
660};
661
Vladimir Markof9f64412015-09-02 14:05:49 +0100662class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000663 public:
664 HLoopInformation(HBasicBlock* header, HGraph* graph)
665 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100666 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000667 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100668 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100669 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100670 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000671 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100672 back_edges_.reserve(kDefaultNumberOfBackEdges);
673 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100674
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000675 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100676 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000677
678 void Dump(std::ostream& os);
679
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100680 HBasicBlock* GetHeader() const {
681 return header_;
682 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000683
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000684 void SetHeader(HBasicBlock* block) {
685 header_ = block;
686 }
687
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100688 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
689 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
690 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
691
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000692 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100693 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000694 }
695
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100696 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100697 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100698 }
699
David Brazdil46e2a392015-03-16 17:31:52 +0000700 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100701 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100702 }
703
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000704 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100705 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000706 }
707
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100708 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100709
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100710 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100711 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100712 }
713
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100714 // Returns the lifetime position of the back edge that has the
715 // greatest lifetime position.
716 size_t GetLifetimeEnd() const;
717
718 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100719 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100720 }
721
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000722 // Finds blocks that are part of this loop.
723 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100724
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100725 // Returns whether this loop information contains `block`.
726 // Note that this loop information *must* be populated before entering this function.
727 bool Contains(const HBasicBlock& block) const;
728
729 // Returns whether this loop information is an inner loop of `other`.
730 // Note that `other` *must* be populated before entering this function.
731 bool IsIn(const HLoopInformation& other) const;
732
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800733 // Returns true if instruction is not defined within this loop.
734 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700735
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100736 const ArenaBitVector& GetBlocks() const { return blocks_; }
737
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000738 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000739 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000740
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000741 void ClearAllBlocks() {
742 blocks_.ClearAllBits();
743 }
744
David Brazdil3f4a5222016-05-06 12:46:21 +0100745 bool HasBackEdgeNotDominatedByHeader() const;
746
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100747 bool IsPopulated() const {
748 return blocks_.GetHighestBitSet() != -1;
749 }
750
Anton Shaminf89381f2016-05-16 16:44:13 +0600751 bool DominatesAllBackEdges(HBasicBlock* block);
752
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000753 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100754 // Internal recursive implementation of `Populate`.
755 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100756 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100757
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000758 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100759 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000760 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100761 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100762 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100763 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000764
765 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
766};
767
David Brazdilec16f792015-08-19 15:04:01 +0100768// Stores try/catch information for basic blocks.
769// Note that HGraph is constructed so that catch blocks cannot simultaneously
770// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100771class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100772 public:
773 // Try block information constructor.
774 explicit TryCatchInformation(const HTryBoundary& try_entry)
775 : try_entry_(&try_entry),
776 catch_dex_file_(nullptr),
777 catch_type_index_(DexFile::kDexNoIndex16) {
778 DCHECK(try_entry_ != nullptr);
779 }
780
781 // Catch block information constructor.
782 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
783 : try_entry_(nullptr),
784 catch_dex_file_(&dex_file),
785 catch_type_index_(catch_type_index) {}
786
787 bool IsTryBlock() const { return try_entry_ != nullptr; }
788
789 const HTryBoundary& GetTryEntry() const {
790 DCHECK(IsTryBlock());
791 return *try_entry_;
792 }
793
794 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
795
796 bool IsCatchAllTypeIndex() const {
797 DCHECK(IsCatchBlock());
798 return catch_type_index_ == DexFile::kDexNoIndex16;
799 }
800
801 uint16_t GetCatchTypeIndex() const {
802 DCHECK(IsCatchBlock());
803 return catch_type_index_;
804 }
805
806 const DexFile& GetCatchDexFile() const {
807 DCHECK(IsCatchBlock());
808 return *catch_dex_file_;
809 }
810
811 private:
812 // One of possibly several TryBoundary instructions entering the block's try.
813 // Only set for try blocks.
814 const HTryBoundary* try_entry_;
815
816 // Exception type information. Only set for catch blocks.
817 const DexFile* catch_dex_file_;
818 const uint16_t catch_type_index_;
819};
820
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100821static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100822static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100823
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000824// A block in a method. Contains the list of instructions represented
825// as a double linked list. Each block knows its predecessors and
826// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100827
Vladimir Markof9f64412015-09-02 14:05:49 +0100828class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000829 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600830 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000831 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000832 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
833 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000834 loop_information_(nullptr),
835 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000836 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100837 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100838 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100839 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000840 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000841 try_catch_information_(nullptr) {
842 predecessors_.reserve(kDefaultNumberOfPredecessors);
843 successors_.reserve(kDefaultNumberOfSuccessors);
844 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
845 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000846
Vladimir Marko60584552015-09-03 13:35:12 +0000847 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100848 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000849 }
850
Vladimir Marko60584552015-09-03 13:35:12 +0000851 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100852 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000853 }
854
David Brazdild26a4112015-11-10 11:07:31 +0000855 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
856 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
857
Vladimir Marko60584552015-09-03 13:35:12 +0000858 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
859 return ContainsElement(successors_, block, start_from);
860 }
861
862 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100863 return dominated_blocks_;
864 }
865
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100866 bool IsEntryBlock() const {
867 return graph_->GetEntryBlock() == this;
868 }
869
870 bool IsExitBlock() const {
871 return graph_->GetExitBlock() == this;
872 }
873
David Brazdil46e2a392015-03-16 17:31:52 +0000874 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000875 bool IsSingleTryBoundary() const;
876
877 // Returns true if this block emits nothing but a jump.
878 bool IsSingleJump() const {
879 HLoopInformation* loop_info = GetLoopInformation();
880 return (IsSingleGoto() || IsSingleTryBoundary())
881 // Back edges generate a suspend check.
882 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
883 }
David Brazdil46e2a392015-03-16 17:31:52 +0000884
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000885 void AddBackEdge(HBasicBlock* back_edge) {
886 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000887 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000888 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100889 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000890 loop_information_->AddBackEdge(back_edge);
891 }
892
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000893 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000894 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000895
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100896 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000897 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600898 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000899
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000900 HBasicBlock* GetDominator() const { return dominator_; }
901 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000902 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
903
904 void RemoveDominatedBlock(HBasicBlock* block) {
905 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100906 }
Vladimir Marko60584552015-09-03 13:35:12 +0000907
908 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
909 ReplaceElement(dominated_blocks_, existing, new_block);
910 }
911
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100912 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000913
914 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100915 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000916 }
917
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100918 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
919 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100920 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100921 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100922 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
923 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000924
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000925 HInstruction* GetFirstInstructionDisregardMoves() const;
926
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000927 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000928 successors_.push_back(block);
929 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000930 }
931
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100932 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
933 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100934 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000935 new_block->predecessors_.push_back(this);
936 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000937 }
938
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000939 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
940 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000941 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000942 new_block->successors_.push_back(this);
943 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000944 }
945
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100946 // Insert `this` between `predecessor` and `successor. This method
947 // preserves the indicies, and will update the first edge found between
948 // `predecessor` and `successor`.
949 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
950 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100951 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000952 successor->predecessors_[predecessor_index] = this;
953 predecessor->successors_[successor_index] = this;
954 successors_.push_back(successor);
955 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100956 }
957
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100958 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000959 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100960 }
961
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000962 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000963 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000964 }
965
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100966 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000967 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100968 }
969
970 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000971 predecessors_.push_back(block);
972 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100973 }
974
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100975 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000976 DCHECK_EQ(predecessors_.size(), 2u);
977 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100978 }
979
David Brazdil769c9e52015-04-27 13:54:09 +0100980 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000981 DCHECK_EQ(successors_.size(), 2u);
982 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100983 }
984
David Brazdilfc6a86a2015-06-26 10:33:45 +0000985 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000986 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100987 }
988
David Brazdilfc6a86a2015-06-26 10:33:45 +0000989 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000990 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100991 }
992
David Brazdilfc6a86a2015-06-26 10:33:45 +0000993 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000994 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100995 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000996 }
997
998 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000999 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001000 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001001 }
1002
1003 // Returns whether the first occurrence of `predecessor` in the list of
1004 // predecessors is at index `idx`.
1005 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001006 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001007 return GetPredecessorIndexOf(predecessor) == idx;
1008 }
1009
David Brazdild7558da2015-09-22 13:04:14 +01001010 // Create a new block between this block and its predecessors. The new block
1011 // is added to the graph, all predecessor edges are relinked to it and an edge
1012 // is created to `this`. Returns the new empty block. Reverse post order or
1013 // loop and try/catch information are not updated.
1014 HBasicBlock* CreateImmediateDominator();
1015
David Brazdilfc6a86a2015-06-26 10:33:45 +00001016 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001017 // created, latter block. Note that this method will add the block to the
1018 // graph, create a Goto at the end of the former block and will create an edge
1019 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001020 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001021 HBasicBlock* SplitBefore(HInstruction* cursor);
1022
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001023 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001024 // created block. Note that this method just updates raw block information,
1025 // like predecessors, successors, dominators, and instruction list. It does not
1026 // update the graph, reverse post order, loop information, nor make sure the
1027 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001028 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1029
1030 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1031 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001032
1033 // Merge `other` at the end of `this`. Successors and dominated blocks of
1034 // `other` are changed to be successors and dominated blocks of `this`. Note
1035 // that this method does not update the graph, reverse post order, loop
1036 // information, nor make sure the blocks are consistent (for example ending
1037 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001038 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001039
1040 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1041 // of `this` are moved to `other`.
1042 // Note that this method does not update the graph, reverse post order, loop
1043 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001044 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001045 void ReplaceWith(HBasicBlock* other);
1046
David Brazdil2d7352b2015-04-20 14:52:42 +01001047 // Merge `other` at the end of `this`. This method updates loops, reverse post
1048 // order, links to predecessors, successors, dominators and deletes the block
1049 // from the graph. The two blocks must be successive, i.e. `this` the only
1050 // predecessor of `other` and vice versa.
1051 void MergeWith(HBasicBlock* other);
1052
1053 // Disconnects `this` from all its predecessors, successors and dominator,
1054 // removes it from all loops it is included in and eventually from the graph.
1055 // The block must not dominate any other block. Predecessors and successors
1056 // are safely updated.
1057 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001058
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001059 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001060 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001061 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001062 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001063 // Replace instruction `initial` with `replacement` within this block.
1064 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1065 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001066 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001067 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001068 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1069 // instruction list. With 'ensure_safety' set to true, it verifies that the
1070 // instruction is not in use and removes it from the use lists of its inputs.
1071 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1072 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001073 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001074
1075 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001076 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001077 }
1078
Roland Levillain6b879dd2014-09-22 17:13:44 +01001079 bool IsLoopPreHeaderFirstPredecessor() const {
1080 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001081 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001082 }
1083
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001084 bool IsFirstPredecessorBackEdge() const {
1085 DCHECK(IsLoopHeader());
1086 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1087 }
1088
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001089 HLoopInformation* GetLoopInformation() const {
1090 return loop_information_;
1091 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001092
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001093 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001094 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001095 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001096 void SetInLoop(HLoopInformation* info) {
1097 if (IsLoopHeader()) {
1098 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001099 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001100 loop_information_ = info;
1101 } else if (loop_information_->Contains(*info->GetHeader())) {
1102 // Block is currently part of an outer loop. Make it part of this inner loop.
1103 // Note that a non loop header having a loop information means this loop information
1104 // has already been populated
1105 loop_information_ = info;
1106 } else {
1107 // Block is part of an inner loop. Do not update the loop information.
1108 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1109 // at this point, because this method is being called while populating `info`.
1110 }
1111 }
1112
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001113 // Raw update of the loop information.
1114 void SetLoopInformation(HLoopInformation* info) {
1115 loop_information_ = info;
1116 }
1117
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001118 bool IsInLoop() const { return loop_information_ != nullptr; }
1119
David Brazdilec16f792015-08-19 15:04:01 +01001120 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1121
1122 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1123 try_catch_information_ = try_catch_information;
1124 }
1125
1126 bool IsTryBlock() const {
1127 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1128 }
1129
1130 bool IsCatchBlock() const {
1131 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1132 }
David Brazdilffee3d32015-07-06 11:48:53 +01001133
1134 // Returns the try entry that this block's successors should have. They will
1135 // be in the same try, unless the block ends in a try boundary. In that case,
1136 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001137 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001138
David Brazdild7558da2015-09-22 13:04:14 +01001139 bool HasThrowingInstructions() const;
1140
David Brazdila4b8c212015-05-07 09:59:30 +01001141 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001142 bool Dominates(HBasicBlock* block) const;
1143
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001144 size_t GetLifetimeStart() const { return lifetime_start_; }
1145 size_t GetLifetimeEnd() const { return lifetime_end_; }
1146
1147 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1148 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1149
David Brazdil8d5b8b22015-03-24 10:51:52 +00001150 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001151 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001152 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001153 bool HasSinglePhi() const;
1154
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001155 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001156 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001157 ArenaVector<HBasicBlock*> predecessors_;
1158 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001159 HInstructionList instructions_;
1160 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001161 HLoopInformation* loop_information_;
1162 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001163 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001164 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001165 // The dex program counter of the first instruction of this block.
1166 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001167 size_t lifetime_start_;
1168 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001169 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001170
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001171 friend class HGraph;
1172 friend class HInstruction;
1173
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001174 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1175};
1176
David Brazdilb2bd1c52015-03-25 11:17:37 +00001177// Iterates over the LoopInformation of all loops which contain 'block'
1178// from the innermost to the outermost.
1179class HLoopInformationOutwardIterator : public ValueObject {
1180 public:
1181 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1182 : current_(block.GetLoopInformation()) {}
1183
1184 bool Done() const { return current_ == nullptr; }
1185
1186 void Advance() {
1187 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001188 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001189 }
1190
1191 HLoopInformation* Current() const {
1192 DCHECK(!Done());
1193 return current_;
1194 }
1195
1196 private:
1197 HLoopInformation* current_;
1198
1199 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1200};
1201
Alexandre Ramesef20f712015-06-09 10:29:30 +01001202#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001203 M(Above, Condition) \
1204 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001205 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001206 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001207 M(ArrayGet, Instruction) \
1208 M(ArrayLength, Instruction) \
1209 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001210 M(Below, Condition) \
1211 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001212 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001213 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001214 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001215 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001216 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001217 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001218 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001219 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001220 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001221 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001222 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001223 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001224 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001225 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001226 M(Exit, Instruction) \
1227 M(FloatConstant, Constant) \
1228 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001229 M(GreaterThan, Condition) \
1230 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001231 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001232 M(InstanceFieldGet, Instruction) \
1233 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001234 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001235 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001236 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001237 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001238 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001239 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001240 M(LessThan, Condition) \
1241 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001242 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001243 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001244 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001245 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001246 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001247 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001248 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001249 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001250 M(Neg, UnaryOperation) \
1251 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001252 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001253 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001254 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001255 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001256 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001257 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001258 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001259 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001260 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001261 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001262 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001263 M(Return, Instruction) \
1264 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001265 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001266 M(Shl, BinaryOperation) \
1267 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001268 M(StaticFieldGet, Instruction) \
1269 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001270 M(UnresolvedInstanceFieldGet, Instruction) \
1271 M(UnresolvedInstanceFieldSet, Instruction) \
1272 M(UnresolvedStaticFieldGet, Instruction) \
1273 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001274 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001275 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001276 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001277 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001278 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001279 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001280 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001281 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001282
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001283/*
1284 * Instructions, shared across several (not all) architectures.
1285 */
1286#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1287#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1288#else
1289#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001290 M(BitwiseNegatedRight, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001291 M(MultiplyAccumulate, Instruction) \
1292 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001293#endif
1294
Vladimir Markob4536b72015-11-24 13:45:23 +00001295#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001296#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001297#else
1298#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1299 M(ArmDexCacheArraysBase, Instruction)
1300#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001301
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001302#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001303#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001304#else
1305#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Artem Serov328429f2016-07-06 16:23:04 +01001306 M(Arm64DataProcWithShifterOp, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001307#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001308
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001309#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001310#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001311#else
1312#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1313 M(MipsComputeBaseMethodAddress, Instruction) \
1314 M(MipsDexCacheArraysBase, Instruction)
1315#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001316
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001317#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1318
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001319#ifndef ART_ENABLE_CODEGEN_x86
1320#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1321#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001322#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1323 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001324 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001325 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001326 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001327#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001328
1329#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1330
1331#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1332 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001333 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001334 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1335 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001336 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001337 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001338 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1339 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1340
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001341#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1342 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001343 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001344 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001345 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001346 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001347
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001348#define FOR_EACH_INSTRUCTION(M) \
1349 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1350 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1351
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001352#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001353FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1354#undef FORWARD_DECLARATION
1355
Vladimir Marko372f10e2016-05-17 16:30:10 +01001356#define DECLARE_INSTRUCTION(type) \
1357 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1358 const char* DebugName() const OVERRIDE { return #type; } \
1359 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1360 return other->Is##type(); \
1361 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001362 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001363
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001364#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1365 bool Is##type() const { return As##type() != nullptr; } \
1366 const H##type* As##type() const { return this; } \
1367 H##type* As##type() { return this; }
1368
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001369template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001370class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001371 public:
David Brazdiled596192015-01-23 10:39:45 +00001372 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001373 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001374 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001375
Vladimir Marko46817b82016-03-29 12:21:58 +01001376 // Hook for the IntrusiveForwardList<>.
1377 // TODO: Hide this better.
1378 IntrusiveForwardListHook hook;
1379
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001380 private:
David Brazdiled596192015-01-23 10:39:45 +00001381 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001382 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001383
1384 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001385 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001386
Vladimir Marko46817b82016-03-29 12:21:58 +01001387 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001388
1389 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1390};
1391
David Brazdiled596192015-01-23 10:39:45 +00001392template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001393using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001394
David Brazdil1abb4192015-02-17 18:33:36 +00001395// This class is used by HEnvironment and HInstruction classes to record the
1396// instructions they use and pointers to the corresponding HUseListNodes kept
1397// by the used instructions.
1398template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001399class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001400 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001401 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1402 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001403
Vladimir Marko46817b82016-03-29 12:21:58 +01001404 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1405 : HUserRecord(old_record.instruction_, before_use_node) {}
1406 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1407 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001408 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001409 }
1410
1411 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001412 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1413 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001414
1415 private:
1416 // Instruction used by the user.
1417 HInstruction* instruction_;
1418
Vladimir Marko46817b82016-03-29 12:21:58 +01001419 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1420 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001421};
1422
Vladimir Markoe9004912016-06-16 16:50:52 +01001423// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1424// This is used for HInstruction::GetInputs() to return a container wrapper providing
1425// HInstruction* values even though the underlying container has HUserRecord<>s.
1426struct HInputExtractor {
1427 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1428 return record.GetInstruction();
1429 }
1430 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1431 return record.GetInstruction();
1432 }
1433};
1434
1435using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1436using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1437
Aart Bik854a02b2015-07-14 16:07:00 -07001438/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001439 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001440 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001441 * For write/read dependences on fields/arrays, the dependence analysis uses
1442 * type disambiguation (e.g. a float field write cannot modify the value of an
1443 * integer field read) and the access type (e.g. a reference array write cannot
1444 * modify the value of a reference field read [although it may modify the
1445 * reference fetch prior to reading the field, which is represented by its own
1446 * write/read dependence]). The analysis makes conservative points-to
1447 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1448 * the same, and any reference read depends on any reference read without
1449 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001450 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001451 * The internal representation uses 38-bit and is described in the table below.
1452 * The first line indicates the side effect, and for field/array accesses the
1453 * second line indicates the type of the access (in the order of the
1454 * Primitive::Type enum).
1455 * The two numbered lines below indicate the bit position in the bitfield (read
1456 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001457 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001458 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1459 * +-------------+---------+---------+--------------+---------+---------+
1460 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1461 * | 3 |333333322|222222221| 1 |111111110|000000000|
1462 * | 7 |654321098|765432109| 8 |765432109|876543210|
1463 *
1464 * Note that, to ease the implementation, 'changes' bits are least significant
1465 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001466 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001467class SideEffects : public ValueObject {
1468 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001469 SideEffects() : flags_(0) {}
1470
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001471 static SideEffects None() {
1472 return SideEffects(0);
1473 }
1474
1475 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001476 return SideEffects(kAllChangeBits | kAllDependOnBits);
1477 }
1478
1479 static SideEffects AllChanges() {
1480 return SideEffects(kAllChangeBits);
1481 }
1482
1483 static SideEffects AllDependencies() {
1484 return SideEffects(kAllDependOnBits);
1485 }
1486
1487 static SideEffects AllExceptGCDependency() {
1488 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1489 }
1490
1491 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001492 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001493 }
1494
Aart Bik34c3ba92015-07-20 14:08:59 -07001495 static SideEffects AllWrites() {
1496 return SideEffects(kAllWrites);
1497 }
1498
1499 static SideEffects AllReads() {
1500 return SideEffects(kAllReads);
1501 }
1502
1503 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1504 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001505 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001506 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001507 }
1508
Aart Bik854a02b2015-07-14 16:07:00 -07001509 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001510 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001511 }
1512
Aart Bik34c3ba92015-07-20 14:08:59 -07001513 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1514 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001515 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001516 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001517 }
1518
1519 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001520 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001521 }
1522
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001523 static SideEffects CanTriggerGC() {
1524 return SideEffects(1ULL << kCanTriggerGCBit);
1525 }
1526
1527 static SideEffects DependsOnGC() {
1528 return SideEffects(1ULL << kDependsOnGCBit);
1529 }
1530
Aart Bik854a02b2015-07-14 16:07:00 -07001531 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001532 SideEffects Union(SideEffects other) const {
1533 return SideEffects(flags_ | other.flags_);
1534 }
1535
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001536 SideEffects Exclusion(SideEffects other) const {
1537 return SideEffects(flags_ & ~other.flags_);
1538 }
1539
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001540 void Add(SideEffects other) {
1541 flags_ |= other.flags_;
1542 }
1543
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001544 bool Includes(SideEffects other) const {
1545 return (other.flags_ & flags_) == other.flags_;
1546 }
1547
1548 bool HasSideEffects() const {
1549 return (flags_ & kAllChangeBits);
1550 }
1551
1552 bool HasDependencies() const {
1553 return (flags_ & kAllDependOnBits);
1554 }
1555
1556 // Returns true if there are no side effects or dependencies.
1557 bool DoesNothing() const {
1558 return flags_ == 0;
1559 }
1560
Aart Bik854a02b2015-07-14 16:07:00 -07001561 // Returns true if something is written.
1562 bool DoesAnyWrite() const {
1563 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001564 }
1565
Aart Bik854a02b2015-07-14 16:07:00 -07001566 // Returns true if something is read.
1567 bool DoesAnyRead() const {
1568 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001569 }
1570
Aart Bik854a02b2015-07-14 16:07:00 -07001571 // Returns true if potentially everything is written and read
1572 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001573 bool DoesAllReadWrite() const {
1574 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1575 }
1576
Aart Bik854a02b2015-07-14 16:07:00 -07001577 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001578 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001579 }
1580
Roland Levillain0d5a2812015-11-13 10:07:31 +00001581 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001582 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001583 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1584 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001585 }
1586
1587 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001588 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001589 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001590 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001591 for (int s = kLastBit; s >= 0; s--) {
1592 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1593 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1594 // This is a bit for the GC side effect.
1595 if (current_bit_is_set) {
1596 flags += "GC";
1597 }
Aart Bik854a02b2015-07-14 16:07:00 -07001598 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001599 } else {
1600 // This is a bit for the array/field analysis.
1601 // The underscore character stands for the 'can trigger GC' bit.
1602 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1603 if (current_bit_is_set) {
1604 flags += kDebug[s];
1605 }
1606 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1607 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1608 flags += "|";
1609 }
1610 }
Aart Bik854a02b2015-07-14 16:07:00 -07001611 }
1612 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001613 }
1614
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001615 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001616
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001617 private:
1618 static constexpr int kFieldArrayAnalysisBits = 9;
1619
1620 static constexpr int kFieldWriteOffset = 0;
1621 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1622 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1623 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1624
1625 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1626
1627 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1628 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1629 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1630 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1631
1632 static constexpr int kLastBit = kDependsOnGCBit;
1633 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1634
1635 // Aliases.
1636
1637 static_assert(kChangeBits == kDependOnBits,
1638 "the 'change' bits should match the 'depend on' bits.");
1639
1640 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1641 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1642 static constexpr uint64_t kAllWrites =
1643 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1644 static constexpr uint64_t kAllReads =
1645 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001646
Aart Bik854a02b2015-07-14 16:07:00 -07001647 // Translates type to bit flag.
1648 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1649 CHECK_NE(type, Primitive::kPrimVoid);
1650 const uint64_t one = 1;
1651 const int shift = type; // 0-based consecutive enum
1652 DCHECK_LE(kFieldWriteOffset, shift);
1653 DCHECK_LT(shift, kArrayWriteOffset);
1654 return one << (type + offset);
1655 }
1656
1657 // Private constructor on direct flags value.
1658 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1659
1660 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001661};
1662
David Brazdiled596192015-01-23 10:39:45 +00001663// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001664class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001665 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001666 HEnvironment(ArenaAllocator* arena,
1667 size_t number_of_vregs,
1668 const DexFile& dex_file,
1669 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001670 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001671 InvokeType invoke_type,
1672 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001673 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1674 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001675 parent_(nullptr),
1676 dex_file_(dex_file),
1677 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001678 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001679 invoke_type_(invoke_type),
1680 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001681 }
1682
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001683 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001684 : HEnvironment(arena,
1685 to_copy.Size(),
1686 to_copy.GetDexFile(),
1687 to_copy.GetMethodIdx(),
1688 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001689 to_copy.GetInvokeType(),
1690 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001691
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001692 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001693 if (parent_ != nullptr) {
1694 parent_->SetAndCopyParentChain(allocator, parent);
1695 } else {
1696 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1697 parent_->CopyFrom(parent);
1698 if (parent->GetParent() != nullptr) {
1699 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1700 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001701 }
David Brazdiled596192015-01-23 10:39:45 +00001702 }
1703
Vladimir Marko71bf8092015-09-15 15:33:14 +01001704 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001705 void CopyFrom(HEnvironment* environment);
1706
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001707 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1708 // input to the loop phi instead. This is for inserting instructions that
1709 // require an environment (like HDeoptimization) in the loop pre-header.
1710 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001711
1712 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001713 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001714 }
1715
1716 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001717 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001718 }
1719
David Brazdil1abb4192015-02-17 18:33:36 +00001720 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001721
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001722 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001723
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001724 HEnvironment* GetParent() const { return parent_; }
1725
1726 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001727 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001728 }
1729
1730 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001731 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001732 }
1733
1734 uint32_t GetDexPc() const {
1735 return dex_pc_;
1736 }
1737
1738 uint32_t GetMethodIdx() const {
1739 return method_idx_;
1740 }
1741
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001742 InvokeType GetInvokeType() const {
1743 return invoke_type_;
1744 }
1745
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001746 const DexFile& GetDexFile() const {
1747 return dex_file_;
1748 }
1749
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001750 HInstruction* GetHolder() const {
1751 return holder_;
1752 }
1753
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001754
1755 bool IsFromInlinedInvoke() const {
1756 return GetParent() != nullptr;
1757 }
1758
David Brazdiled596192015-01-23 10:39:45 +00001759 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001760 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1761 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001762 HEnvironment* parent_;
1763 const DexFile& dex_file_;
1764 const uint32_t method_idx_;
1765 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001766 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001767
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001768 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001769 HInstruction* const holder_;
1770
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001771 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001772
1773 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1774};
1775
Vladimir Markof9f64412015-09-02 14:05:49 +01001776class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001777 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001778 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001779 : previous_(nullptr),
1780 next_(nullptr),
1781 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001782 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001783 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001784 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001785 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001786 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001787 locations_(nullptr),
1788 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001789 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001790 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001791 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1792 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1793 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001794
Dave Allison20dfc792014-06-16 20:44:29 -07001795 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001796
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001797#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001798 enum InstructionKind {
1799 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1800 };
1801#undef DECLARE_KIND
1802
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001803 HInstruction* GetNext() const { return next_; }
1804 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001805
Calin Juravle77520bc2015-01-12 18:45:46 +00001806 HInstruction* GetNextDisregardingMoves() const;
1807 HInstruction* GetPreviousDisregardingMoves() const;
1808
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001809 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001810 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001811 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001812 bool IsInBlock() const { return block_ != nullptr; }
1813 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001814 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1815 bool IsIrreducibleLoopHeaderPhi() const {
1816 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1817 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001818
Vladimir Marko372f10e2016-05-17 16:30:10 +01001819 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1820
1821 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1822 // One virtual method is enough, just const_cast<> and then re-add the const.
1823 return ArrayRef<const HUserRecord<HInstruction*>>(
1824 const_cast<HInstruction*>(this)->GetInputRecords());
1825 }
1826
Vladimir Markoe9004912016-06-16 16:50:52 +01001827 HInputsRef GetInputs() {
1828 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001829 }
1830
Vladimir Markoe9004912016-06-16 16:50:52 +01001831 HConstInputsRef GetInputs() const {
1832 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001833 }
1834
1835 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001836 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001837
Vladimir Marko372f10e2016-05-17 16:30:10 +01001838 void SetRawInputAt(size_t index, HInstruction* input) {
1839 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1840 }
1841
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001842 virtual void Accept(HGraphVisitor* visitor) = 0;
1843 virtual const char* DebugName() const = 0;
1844
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001845 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1846
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001847 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001848
1849 uint32_t GetDexPc() const { return dex_pc_; }
1850
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001851 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001852
Roland Levillaine161a2a2014-10-03 12:45:18 +01001853 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001854 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001855
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001856 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001857 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001858
Calin Juravle10e244f2015-01-26 18:54:32 +00001859 // Does not apply for all instructions, but having this at top level greatly
1860 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001861 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001862 virtual bool CanBeNull() const {
1863 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1864 return true;
1865 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001866
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001867 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001868 return false;
1869 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001870
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001871 virtual bool IsActualObject() const {
1872 return GetType() == Primitive::kPrimNot;
1873 }
1874
Calin Juravle2e768302015-07-28 14:41:11 +00001875 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001876
Calin Juravle61d544b2015-02-23 16:46:57 +00001877 ReferenceTypeInfo GetReferenceTypeInfo() const {
1878 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001879 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001880 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001881 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001882
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001883 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001884 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001885 // Note: fixup_end remains valid across push_front().
1886 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1887 HUseListNode<HInstruction*>* new_node =
1888 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1889 uses_.push_front(*new_node);
1890 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001891 }
1892
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001893 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001894 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001895 // Note: env_fixup_end remains valid across push_front().
1896 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1897 HUseListNode<HEnvironment*>* new_node =
1898 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1899 env_uses_.push_front(*new_node);
1900 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001901 }
1902
David Brazdil1abb4192015-02-17 18:33:36 +00001903 void RemoveAsUserOfInput(size_t input) {
1904 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001905 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1906 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1907 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001908 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001909
Vladimir Marko372f10e2016-05-17 16:30:10 +01001910 void RemoveAsUserOfAllInputs() {
1911 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1912 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1913 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1914 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1915 }
1916 }
1917
David Brazdil1abb4192015-02-17 18:33:36 +00001918 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1919 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001920
Vladimir Marko46817b82016-03-29 12:21:58 +01001921 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1922 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1923 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001924 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001925 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001926 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001927
Roland Levillain6c82d402014-10-13 16:10:27 +01001928 // Does this instruction strictly dominate `other_instruction`?
1929 // Returns false if this instruction and `other_instruction` are the same.
1930 // Aborts if this instruction and `other_instruction` are both phis.
1931 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001932
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001933 int GetId() const { return id_; }
1934 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001935
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001936 int GetSsaIndex() const { return ssa_index_; }
1937 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1938 bool HasSsaIndex() const { return ssa_index_ != -1; }
1939
1940 bool HasEnvironment() const { return environment_ != nullptr; }
1941 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001942 // Set the `environment_` field. Raw because this method does not
1943 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001944 void SetRawEnvironment(HEnvironment* environment) {
1945 DCHECK(environment_ == nullptr);
1946 DCHECK_EQ(environment->GetHolder(), this);
1947 environment_ = environment;
1948 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001949
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001950 void InsertRawEnvironment(HEnvironment* environment) {
1951 DCHECK(environment_ != nullptr);
1952 DCHECK_EQ(environment->GetHolder(), this);
1953 DCHECK(environment->GetParent() == nullptr);
1954 environment->parent_ = environment_;
1955 environment_ = environment;
1956 }
1957
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001958 void RemoveEnvironment();
1959
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001960 // Set the environment of this instruction, copying it from `environment`. While
1961 // copying, the uses lists are being updated.
1962 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001963 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001964 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001965 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001966 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001967 if (environment->GetParent() != nullptr) {
1968 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1969 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001970 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001971
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001972 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1973 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001974 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001975 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001976 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001977 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001978 if (environment->GetParent() != nullptr) {
1979 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1980 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001981 }
1982
Nicolas Geoffray39468442014-09-02 15:17:15 +01001983 // Returns the number of entries in the environment. Typically, that is the
1984 // number of dex registers in a method. It could be more in case of inlining.
1985 size_t EnvironmentSize() const;
1986
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001987 LocationSummary* GetLocations() const { return locations_; }
1988 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001989
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001990 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001991 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001992
Alexandre Rames188d4312015-04-09 18:30:21 +01001993 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1994 // uses of this instruction by `other` are *not* updated.
1995 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1996 ReplaceWith(other);
1997 other->ReplaceInput(this, use_index);
1998 }
1999
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002000 // Move `this` instruction before `cursor`.
2001 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002002
Vladimir Markofb337ea2015-11-25 15:25:10 +00002003 // Move `this` before its first user and out of any loops. If there is no
2004 // out-of-loop user that dominates all other users, move the instruction
2005 // to the end of the out-of-loop common dominator of the user's blocks.
2006 //
2007 // This can be used only on non-throwing instructions with no side effects that
2008 // have at least one use but no environment uses.
2009 void MoveBeforeFirstUserAndOutOfLoops();
2010
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002011#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002012 bool Is##type() const; \
2013 const H##type* As##type() const; \
2014 H##type* As##type();
2015
2016 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2017#undef INSTRUCTION_TYPE_CHECK
2018
2019#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002020 bool Is##type() const { return (As##type() != nullptr); } \
2021 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002022 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002023 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002024#undef INSTRUCTION_TYPE_CHECK
2025
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002026 // Returns whether the instruction can be moved within the graph.
2027 virtual bool CanBeMoved() const { return false; }
2028
2029 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002030 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002031 return false;
2032 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002033
2034 // Returns whether any data encoded in the two instructions is equal.
2035 // This method does not look at the inputs. Both instructions must be
2036 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002037 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002038 return false;
2039 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002040
2041 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002042 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002043 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002044 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002045
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002046 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2047 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2048 // the virtual function because the __attribute__((__pure__)) doesn't really
2049 // apply the strong requirement for virtual functions, preventing optimizations.
2050 InstructionKind GetKind() const PURE;
2051 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002052
2053 virtual size_t ComputeHashCode() const {
2054 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002055 for (const HInstruction* input : GetInputs()) {
2056 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002057 }
2058 return result;
2059 }
2060
2061 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002062 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002063 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002064
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002065 size_t GetLifetimePosition() const { return lifetime_position_; }
2066 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2067 LiveInterval* GetLiveInterval() const { return live_interval_; }
2068 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2069 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2070
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002071 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2072
2073 // Returns whether the code generation of the instruction will require to have access
2074 // to the current method. Such instructions are:
2075 // (1): Instructions that require an environment, as calling the runtime requires
2076 // to walk the stack and have the current method stored at a specific stack address.
2077 // (2): Object literals like classes and strings, that are loaded from the dex cache
2078 // fields of the current method.
2079 bool NeedsCurrentMethod() const {
2080 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2081 }
2082
Vladimir Markodc151b22015-10-15 18:02:30 +01002083 // Returns whether the code generation of the instruction will require to have access
2084 // to the dex cache of the current method's declaring class via the current method.
2085 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002086
Mark Mendellc4701932015-04-10 13:18:51 -04002087 // Does this instruction have any use in an environment before
2088 // control flow hits 'other'?
2089 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2090
2091 // Remove all references to environment uses of this instruction.
2092 // The caller must ensure that this is safe to do.
2093 void RemoveEnvironmentUsers();
2094
Vladimir Markoa1de9182016-02-25 11:37:38 +00002095 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2096 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002097
David Brazdil1abb4192015-02-17 18:33:36 +00002098 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002099 // If set, the machine code for this instruction is assumed to be generated by
2100 // its users. Used by liveness analysis to compute use positions accordingly.
2101 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2102 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2103 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2104 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2105
Vladimir Marko372f10e2016-05-17 16:30:10 +01002106 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2107 return GetInputRecords()[i];
2108 }
2109
2110 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2111 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2112 input_records[index] = input;
2113 }
David Brazdil1abb4192015-02-17 18:33:36 +00002114
Vladimir Markoa1de9182016-02-25 11:37:38 +00002115 uint32_t GetPackedFields() const {
2116 return packed_fields_;
2117 }
2118
2119 template <size_t flag>
2120 bool GetPackedFlag() const {
2121 return (packed_fields_ & (1u << flag)) != 0u;
2122 }
2123
2124 template <size_t flag>
2125 void SetPackedFlag(bool value = true) {
2126 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2127 }
2128
2129 template <typename BitFieldType>
2130 typename BitFieldType::value_type GetPackedField() const {
2131 return BitFieldType::Decode(packed_fields_);
2132 }
2133
2134 template <typename BitFieldType>
2135 void SetPackedField(typename BitFieldType::value_type value) {
2136 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2137 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2138 }
2139
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002140 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002141 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2142 auto before_use_node = uses_.before_begin();
2143 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2144 HInstruction* user = use_node->GetUser();
2145 size_t input_index = use_node->GetIndex();
2146 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2147 before_use_node = use_node;
2148 }
2149 }
2150
2151 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2152 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2153 if (next != uses_.end()) {
2154 HInstruction* next_user = next->GetUser();
2155 size_t next_index = next->GetIndex();
2156 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2157 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2158 }
2159 }
2160
2161 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2162 auto before_env_use_node = env_uses_.before_begin();
2163 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2164 HEnvironment* user = env_use_node->GetUser();
2165 size_t input_index = env_use_node->GetIndex();
2166 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2167 before_env_use_node = env_use_node;
2168 }
2169 }
2170
2171 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2172 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2173 if (next != env_uses_.end()) {
2174 HEnvironment* next_user = next->GetUser();
2175 size_t next_index = next->GetIndex();
2176 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2177 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2178 }
2179 }
David Brazdil1abb4192015-02-17 18:33:36 +00002180
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002181 HInstruction* previous_;
2182 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002183 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002184 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002185
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002186 // An instruction gets an id when it is added to the graph.
2187 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002188 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002189 int id_;
2190
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002191 // When doing liveness analysis, instructions that have uses get an SSA index.
2192 int ssa_index_;
2193
Vladimir Markoa1de9182016-02-25 11:37:38 +00002194 // Packed fields.
2195 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002196
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002197 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002198 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002199
2200 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002201 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002202
Nicolas Geoffray39468442014-09-02 15:17:15 +01002203 // The environment associated with this instruction. Not null if the instruction
2204 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002205 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002206
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002207 // Set by the code generator.
2208 LocationSummary* locations_;
2209
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002210 // Set by the liveness analysis.
2211 LiveInterval* live_interval_;
2212
2213 // Set by the liveness analysis, this is the position in a linear
2214 // order of blocks where this instruction's live interval start.
2215 size_t lifetime_position_;
2216
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002217 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002218
Vladimir Markoa1de9182016-02-25 11:37:38 +00002219 // The reference handle part of the reference type info.
2220 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002221 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002222 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002223
David Brazdil1abb4192015-02-17 18:33:36 +00002224 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002225 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002226 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002227 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002228 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002229
2230 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2231};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002232std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002233
2234class HInstructionIterator : public ValueObject {
2235 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002236 explicit HInstructionIterator(const HInstructionList& instructions)
2237 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002238 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002239 }
2240
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002241 bool Done() const { return instruction_ == nullptr; }
2242 HInstruction* Current() const { return instruction_; }
2243 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002244 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002245 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002246 }
2247
2248 private:
2249 HInstruction* instruction_;
2250 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002251
2252 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002253};
2254
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002255class HBackwardInstructionIterator : public ValueObject {
2256 public:
2257 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2258 : instruction_(instructions.last_instruction_) {
2259 next_ = Done() ? nullptr : instruction_->GetPrevious();
2260 }
2261
2262 bool Done() const { return instruction_ == nullptr; }
2263 HInstruction* Current() const { return instruction_; }
2264 void Advance() {
2265 instruction_ = next_;
2266 next_ = Done() ? nullptr : instruction_->GetPrevious();
2267 }
2268
2269 private:
2270 HInstruction* instruction_;
2271 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002272
2273 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002274};
2275
Vladimir Markof9f64412015-09-02 14:05:49 +01002276template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002277class HTemplateInstruction: public HInstruction {
2278 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002279 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002280 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002281 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002282
Vladimir Marko372f10e2016-05-17 16:30:10 +01002283 using HInstruction::GetInputRecords; // Keep the const version visible.
2284 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2285 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002286 }
2287
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002288 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002289 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002290
2291 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002292};
2293
Vladimir Markof9f64412015-09-02 14:05:49 +01002294// HTemplateInstruction specialization for N=0.
2295template<>
2296class HTemplateInstruction<0>: public HInstruction {
2297 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002298 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002299 : HInstruction(side_effects, dex_pc) {}
2300
Vladimir Markof9f64412015-09-02 14:05:49 +01002301 virtual ~HTemplateInstruction() {}
2302
Vladimir Marko372f10e2016-05-17 16:30:10 +01002303 using HInstruction::GetInputRecords; // Keep the const version visible.
2304 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2305 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002306 }
2307
2308 private:
2309 friend class SsaBuilder;
2310};
2311
Dave Allison20dfc792014-06-16 20:44:29 -07002312template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002313class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002314 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002315 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002316 : HTemplateInstruction<N>(side_effects, dex_pc) {
2317 this->template SetPackedField<TypeField>(type);
2318 }
Dave Allison20dfc792014-06-16 20:44:29 -07002319 virtual ~HExpression() {}
2320
Vladimir Markoa1de9182016-02-25 11:37:38 +00002321 Primitive::Type GetType() const OVERRIDE {
2322 return TypeField::Decode(this->GetPackedFields());
2323 }
Dave Allison20dfc792014-06-16 20:44:29 -07002324
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002325 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002326 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2327 static constexpr size_t kFieldTypeSize =
2328 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2329 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2330 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2331 "Too many packed fields.");
2332 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002333};
2334
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002335// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2336// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002337class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002338 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002339 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2340 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002341
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002342 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002343
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002344 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002345
2346 private:
2347 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2348};
2349
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002350// Represents dex's RETURN opcodes. A HReturn is a control flow
2351// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002352class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002353 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002354 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2355 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002356 SetRawInputAt(0, value);
2357 }
2358
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002359 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002360
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002361 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002362
2363 private:
2364 DISALLOW_COPY_AND_ASSIGN(HReturn);
2365};
2366
Vladimir Markofcb503c2016-05-18 12:48:17 +01002367class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002368 public:
2369 HPhi(ArenaAllocator* arena,
2370 uint32_t reg_number,
2371 size_t number_of_inputs,
2372 Primitive::Type type,
2373 uint32_t dex_pc = kNoDexPc)
2374 : HInstruction(SideEffects::None(), dex_pc),
2375 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2376 reg_number_(reg_number) {
2377 SetPackedField<TypeField>(ToPhiType(type));
2378 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2379 // Phis are constructed live and marked dead if conflicting or unused.
2380 // Individual steps of SsaBuilder should assume that if a phi has been
2381 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2382 SetPackedFlag<kFlagIsLive>(true);
2383 SetPackedFlag<kFlagCanBeNull>(true);
2384 }
2385
2386 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2387 static Primitive::Type ToPhiType(Primitive::Type type) {
2388 return Primitive::PrimitiveKind(type);
2389 }
2390
2391 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2392
Vladimir Marko372f10e2016-05-17 16:30:10 +01002393 using HInstruction::GetInputRecords; // Keep the const version visible.
2394 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2395 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2396 }
David Brazdildee58d62016-04-07 09:54:26 +00002397
2398 void AddInput(HInstruction* input);
2399 void RemoveInputAt(size_t index);
2400
2401 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2402 void SetType(Primitive::Type new_type) {
2403 // Make sure that only valid type changes occur. The following are allowed:
2404 // (1) int -> float/ref (primitive type propagation),
2405 // (2) long -> double (primitive type propagation).
2406 DCHECK(GetType() == new_type ||
2407 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2408 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2409 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2410 SetPackedField<TypeField>(new_type);
2411 }
2412
2413 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2414 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2415
2416 uint32_t GetRegNumber() const { return reg_number_; }
2417
2418 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2419 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2420 bool IsDead() const { return !IsLive(); }
2421 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2422
Vladimir Markoe9004912016-06-16 16:50:52 +01002423 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002424 return other != nullptr
2425 && other->IsPhi()
2426 && other->AsPhi()->GetBlock() == GetBlock()
2427 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2428 }
2429
2430 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2431 // An equivalent phi is a phi having the same dex register and type.
2432 // It assumes that phis with the same dex register are adjacent.
2433 HPhi* GetNextEquivalentPhiWithSameType() {
2434 HInstruction* next = GetNext();
2435 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2436 if (next->GetType() == GetType()) {
2437 return next->AsPhi();
2438 }
2439 next = next->GetNext();
2440 }
2441 return nullptr;
2442 }
2443
2444 DECLARE_INSTRUCTION(Phi);
2445
David Brazdildee58d62016-04-07 09:54:26 +00002446 private:
2447 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2448 static constexpr size_t kFieldTypeSize =
2449 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2450 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2451 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2452 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2453 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2454 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2455
Vladimir Marko372f10e2016-05-17 16:30:10 +01002456 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002457 const uint32_t reg_number_;
2458
2459 DISALLOW_COPY_AND_ASSIGN(HPhi);
2460};
2461
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002462// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002463// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002464// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002465class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002466 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002467 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002468
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002469 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002470
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002471 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002472
2473 private:
2474 DISALLOW_COPY_AND_ASSIGN(HExit);
2475};
2476
2477// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002478class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002479 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002480 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002481
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002482 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002483
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002484 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002485 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002486 }
2487
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002488 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002489
2490 private:
2491 DISALLOW_COPY_AND_ASSIGN(HGoto);
2492};
2493
Roland Levillain9867bc72015-08-05 10:21:34 +01002494class HConstant : public HExpression<0> {
2495 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002496 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2497 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002498
2499 bool CanBeMoved() const OVERRIDE { return true; }
2500
Roland Levillain1a653882016-03-18 18:05:57 +00002501 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002502 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002503 // Is this constant 0 in the arithmetic sense?
2504 virtual bool IsArithmeticZero() const { return false; }
2505 // Is this constant a 0-bit pattern?
2506 virtual bool IsZeroBitPattern() const { return false; }
2507 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002508 virtual bool IsOne() const { return false; }
2509
David Brazdil77a48ae2015-09-15 12:34:04 +00002510 virtual uint64_t GetValueAsUint64() const = 0;
2511
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002512 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002513
2514 private:
2515 DISALLOW_COPY_AND_ASSIGN(HConstant);
2516};
2517
Vladimir Markofcb503c2016-05-18 12:48:17 +01002518class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002519 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002520 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002521 return true;
2522 }
2523
David Brazdil77a48ae2015-09-15 12:34:04 +00002524 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2525
Roland Levillain9867bc72015-08-05 10:21:34 +01002526 size_t ComputeHashCode() const OVERRIDE { return 0; }
2527
Roland Levillain1a653882016-03-18 18:05:57 +00002528 // The null constant representation is a 0-bit pattern.
2529 virtual bool IsZeroBitPattern() const { return true; }
2530
Roland Levillain9867bc72015-08-05 10:21:34 +01002531 DECLARE_INSTRUCTION(NullConstant);
2532
2533 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002534 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002535
2536 friend class HGraph;
2537 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2538};
2539
2540// Constants of the type int. Those can be from Dex instructions, or
2541// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002542class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002543 public:
2544 int32_t GetValue() const { return value_; }
2545
David Brazdil9f389d42015-10-01 14:32:56 +01002546 uint64_t GetValueAsUint64() const OVERRIDE {
2547 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2548 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002549
Vladimir Marko372f10e2016-05-17 16:30:10 +01002550 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002551 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002552 return other->AsIntConstant()->value_ == value_;
2553 }
2554
2555 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2556
2557 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002558 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2559 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002560 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2561
Roland Levillain1a653882016-03-18 18:05:57 +00002562 // Integer constants are used to encode Boolean values as well,
2563 // where 1 means true and 0 means false.
2564 bool IsTrue() const { return GetValue() == 1; }
2565 bool IsFalse() const { return GetValue() == 0; }
2566
Roland Levillain9867bc72015-08-05 10:21:34 +01002567 DECLARE_INSTRUCTION(IntConstant);
2568
2569 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002570 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2571 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2572 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2573 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002574
2575 const int32_t value_;
2576
2577 friend class HGraph;
2578 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2579 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2580 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2581};
2582
Vladimir Markofcb503c2016-05-18 12:48:17 +01002583class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002584 public:
2585 int64_t GetValue() const { return value_; }
2586
David Brazdil77a48ae2015-09-15 12:34:04 +00002587 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2588
Vladimir Marko372f10e2016-05-17 16:30:10 +01002589 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002590 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002591 return other->AsLongConstant()->value_ == value_;
2592 }
2593
2594 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2595
2596 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002597 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2598 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002599 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2600
2601 DECLARE_INSTRUCTION(LongConstant);
2602
2603 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002604 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2605 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002606
2607 const int64_t value_;
2608
2609 friend class HGraph;
2610 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2611};
Dave Allison20dfc792014-06-16 20:44:29 -07002612
Vladimir Markofcb503c2016-05-18 12:48:17 +01002613class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002614 public:
2615 float GetValue() const { return value_; }
2616
2617 uint64_t GetValueAsUint64() const OVERRIDE {
2618 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2619 }
2620
Vladimir Marko372f10e2016-05-17 16:30:10 +01002621 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002622 DCHECK(other->IsFloatConstant()) << other->DebugName();
2623 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2624 }
2625
2626 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2627
2628 bool IsMinusOne() const OVERRIDE {
2629 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2630 }
Roland Levillain1a653882016-03-18 18:05:57 +00002631 bool IsArithmeticZero() const OVERRIDE {
2632 return std::fpclassify(value_) == FP_ZERO;
2633 }
2634 bool IsArithmeticPositiveZero() const {
2635 return IsArithmeticZero() && !std::signbit(value_);
2636 }
2637 bool IsArithmeticNegativeZero() const {
2638 return IsArithmeticZero() && std::signbit(value_);
2639 }
2640 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002641 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002642 }
2643 bool IsOne() const OVERRIDE {
2644 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2645 }
2646 bool IsNaN() const {
2647 return std::isnan(value_);
2648 }
2649
2650 DECLARE_INSTRUCTION(FloatConstant);
2651
2652 private:
2653 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2654 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2655 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2656 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2657
2658 const float value_;
2659
2660 // Only the SsaBuilder and HGraph can create floating-point constants.
2661 friend class SsaBuilder;
2662 friend class HGraph;
2663 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2664};
2665
Vladimir Markofcb503c2016-05-18 12:48:17 +01002666class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002667 public:
2668 double GetValue() const { return value_; }
2669
2670 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2671
Vladimir Marko372f10e2016-05-17 16:30:10 +01002672 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002673 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2674 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2675 }
2676
2677 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2678
2679 bool IsMinusOne() const OVERRIDE {
2680 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2681 }
Roland Levillain1a653882016-03-18 18:05:57 +00002682 bool IsArithmeticZero() const OVERRIDE {
2683 return std::fpclassify(value_) == FP_ZERO;
2684 }
2685 bool IsArithmeticPositiveZero() const {
2686 return IsArithmeticZero() && !std::signbit(value_);
2687 }
2688 bool IsArithmeticNegativeZero() const {
2689 return IsArithmeticZero() && std::signbit(value_);
2690 }
2691 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002692 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002693 }
2694 bool IsOne() const OVERRIDE {
2695 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2696 }
2697 bool IsNaN() const {
2698 return std::isnan(value_);
2699 }
2700
2701 DECLARE_INSTRUCTION(DoubleConstant);
2702
2703 private:
2704 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2705 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2706 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2707 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2708
2709 const double value_;
2710
2711 // Only the SsaBuilder and HGraph can create floating-point constants.
2712 friend class SsaBuilder;
2713 friend class HGraph;
2714 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2715};
2716
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002717// Conditional branch. A block ending with an HIf instruction must have
2718// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002719class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002720 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002721 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2722 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002723 SetRawInputAt(0, input);
2724 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002725
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002726 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002727
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002728 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002729 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002730 }
2731
2732 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002733 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002734 }
2735
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002736 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002737
2738 private:
2739 DISALLOW_COPY_AND_ASSIGN(HIf);
2740};
2741
David Brazdilfc6a86a2015-06-26 10:33:45 +00002742
2743// Abstract instruction which marks the beginning and/or end of a try block and
2744// links it to the respective exception handlers. Behaves the same as a Goto in
2745// non-exceptional control flow.
2746// Normal-flow successor is stored at index zero, exception handlers under
2747// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002748class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002749 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002750 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002751 kEntry,
2752 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002753 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002754 };
2755
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002756 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002757 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2758 SetPackedField<BoundaryKindField>(kind);
2759 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002760
2761 bool IsControlFlow() const OVERRIDE { return true; }
2762
2763 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002764 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002765
David Brazdild26a4112015-11-10 11:07:31 +00002766 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2767 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2768 }
2769
David Brazdilfc6a86a2015-06-26 10:33:45 +00002770 // Returns whether `handler` is among its exception handlers (non-zero index
2771 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002772 bool HasExceptionHandler(const HBasicBlock& handler) const {
2773 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002774 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002775 }
2776
2777 // If not present already, adds `handler` to its block's list of exception
2778 // handlers.
2779 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002780 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002781 GetBlock()->AddSuccessor(handler);
2782 }
2783 }
2784
Vladimir Markoa1de9182016-02-25 11:37:38 +00002785 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2786 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002787
David Brazdilffee3d32015-07-06 11:48:53 +01002788 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2789
David Brazdilfc6a86a2015-06-26 10:33:45 +00002790 DECLARE_INSTRUCTION(TryBoundary);
2791
2792 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002793 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2794 static constexpr size_t kFieldBoundaryKindSize =
2795 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2796 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2797 kFieldBoundaryKind + kFieldBoundaryKindSize;
2798 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2799 "Too many packed fields.");
2800 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002801
2802 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2803};
2804
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002805// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002806class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002807 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002808 // We set CanTriggerGC to prevent any intermediate address to be live
2809 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002810 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002811 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002812 SetRawInputAt(0, cond);
2813 }
2814
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002815 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002816 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002817 return true;
2818 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002819 bool NeedsEnvironment() const OVERRIDE { return true; }
2820 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002821
2822 DECLARE_INSTRUCTION(Deoptimize);
2823
2824 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002825 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2826};
2827
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002828// Represents the ArtMethod that was passed as a first argument to
2829// the method. It is used by instructions that depend on it, like
2830// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002831class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002832 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002833 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2834 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002835
2836 DECLARE_INSTRUCTION(CurrentMethod);
2837
2838 private:
2839 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2840};
2841
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002842// Fetches an ArtMethod from the virtual table or the interface method table
2843// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002844class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002845 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002846 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002847 kVTable,
2848 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002849 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002850 };
2851 HClassTableGet(HInstruction* cls,
2852 Primitive::Type type,
2853 TableKind kind,
2854 size_t index,
2855 uint32_t dex_pc)
2856 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002857 index_(index) {
2858 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002859 SetRawInputAt(0, cls);
2860 }
2861
2862 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002863 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002864 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002865 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002866 }
2867
Vladimir Markoa1de9182016-02-25 11:37:38 +00002868 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002869 size_t GetIndex() const { return index_; }
2870
2871 DECLARE_INSTRUCTION(ClassTableGet);
2872
2873 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002874 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2875 static constexpr size_t kFieldTableKindSize =
2876 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2877 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2878 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2879 "Too many packed fields.");
2880 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2881
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002882 // The index of the ArtMethod in the table.
2883 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002884
2885 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2886};
2887
Mark Mendellfe57faa2015-09-18 09:26:15 -04002888// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2889// have one successor for each entry in the switch table, and the final successor
2890// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002891class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002892 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002893 HPackedSwitch(int32_t start_value,
2894 uint32_t num_entries,
2895 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002896 uint32_t dex_pc = kNoDexPc)
2897 : HTemplateInstruction(SideEffects::None(), dex_pc),
2898 start_value_(start_value),
2899 num_entries_(num_entries) {
2900 SetRawInputAt(0, input);
2901 }
2902
2903 bool IsControlFlow() const OVERRIDE { return true; }
2904
2905 int32_t GetStartValue() const { return start_value_; }
2906
Vladimir Marko211c2112015-09-24 16:52:33 +01002907 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002908
2909 HBasicBlock* GetDefaultBlock() const {
2910 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002911 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002912 }
2913 DECLARE_INSTRUCTION(PackedSwitch);
2914
2915 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002916 const int32_t start_value_;
2917 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002918
2919 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2920};
2921
Roland Levillain88cb1752014-10-20 16:36:47 +01002922class HUnaryOperation : public HExpression<1> {
2923 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002924 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2925 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002926 SetRawInputAt(0, input);
2927 }
2928
2929 HInstruction* GetInput() const { return InputAt(0); }
2930 Primitive::Type GetResultType() const { return GetType(); }
2931
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002932 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002933 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002934 return true;
2935 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002936
Roland Levillain31dd3d62016-02-16 12:21:02 +00002937 // Try to statically evaluate `this` and return a HConstant
2938 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002939 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002940 HConstant* TryStaticEvaluation() const;
2941
2942 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002943 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2944 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002945 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2946 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002947
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002948 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002949
2950 private:
2951 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2952};
2953
Dave Allison20dfc792014-06-16 20:44:29 -07002954class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002955 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002956 HBinaryOperation(Primitive::Type result_type,
2957 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002958 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002959 SideEffects side_effects = SideEffects::None(),
2960 uint32_t dex_pc = kNoDexPc)
2961 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002962 SetRawInputAt(0, left);
2963 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002964 }
2965
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002966 HInstruction* GetLeft() const { return InputAt(0); }
2967 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002968 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002969
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002970 virtual bool IsCommutative() const { return false; }
2971
2972 // Put constant on the right.
2973 // Returns whether order is changed.
2974 bool OrderInputsWithConstantOnTheRight() {
2975 HInstruction* left = InputAt(0);
2976 HInstruction* right = InputAt(1);
2977 if (left->IsConstant() && !right->IsConstant()) {
2978 ReplaceInput(right, 0);
2979 ReplaceInput(left, 1);
2980 return true;
2981 }
2982 return false;
2983 }
2984
2985 // Order inputs by instruction id, but favor constant on the right side.
2986 // This helps GVN for commutative ops.
2987 void OrderInputs() {
2988 DCHECK(IsCommutative());
2989 HInstruction* left = InputAt(0);
2990 HInstruction* right = InputAt(1);
2991 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2992 return;
2993 }
2994 if (OrderInputsWithConstantOnTheRight()) {
2995 return;
2996 }
2997 // Order according to instruction id.
2998 if (left->GetId() > right->GetId()) {
2999 ReplaceInput(right, 0);
3000 ReplaceInput(left, 1);
3001 }
3002 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003003
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003004 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003005 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003006 return true;
3007 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003008
Roland Levillain31dd3d62016-02-16 12:21:02 +00003009 // Try to statically evaluate `this` and return a HConstant
3010 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003011 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003012 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003013
3014 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003015 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3016 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003017 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3018 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003019 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003020 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3021 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003022 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3023 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003024 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3025 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003026 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003027 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3028 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003029
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003030 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003031 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003032 HConstant* GetConstantRight() const;
3033
3034 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003035 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003036 HInstruction* GetLeastConstantLeft() const;
3037
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003038 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003039
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003040 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003041 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3042};
3043
Mark Mendellc4701932015-04-10 13:18:51 -04003044// The comparison bias applies for floating point operations and indicates how NaN
3045// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003046enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003047 kNoBias, // bias is not applicable (i.e. for long operation)
3048 kGtBias, // return 1 for NaN comparisons
3049 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003050 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003051};
3052
Roland Levillain31dd3d62016-02-16 12:21:02 +00003053std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3054
Dave Allison20dfc792014-06-16 20:44:29 -07003055class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003056 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003057 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003058 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3059 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3060 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003061
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003062 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003063 // `instruction`, and disregard moves in between.
3064 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003065
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003066 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003067
3068 virtual IfCondition GetCondition() const = 0;
3069
Mark Mendellc4701932015-04-10 13:18:51 -04003070 virtual IfCondition GetOppositeCondition() const = 0;
3071
Vladimir Markoa1de9182016-02-25 11:37:38 +00003072 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003073 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3074
Vladimir Markoa1de9182016-02-25 11:37:38 +00003075 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3076 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003077
Vladimir Marko372f10e2016-05-17 16:30:10 +01003078 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003079 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003080 }
3081
Roland Levillain4fa13f62015-07-06 18:11:54 +01003082 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003083 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003084 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003085 if (if_cond == kCondNE) {
3086 return true;
3087 } else if (if_cond == kCondEQ) {
3088 return false;
3089 }
3090 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003091 }
3092
3093 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003094 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003095 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003096 if (if_cond == kCondEQ) {
3097 return true;
3098 } else if (if_cond == kCondNE) {
3099 return false;
3100 }
3101 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003102 }
3103
Roland Levillain31dd3d62016-02-16 12:21:02 +00003104 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003105 // Needed if we merge a HCompare into a HCondition.
3106 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3107 static constexpr size_t kFieldComparisonBiasSize =
3108 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3109 static constexpr size_t kNumberOfConditionPackedBits =
3110 kFieldComparisonBias + kFieldComparisonBiasSize;
3111 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3112 using ComparisonBiasField =
3113 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3114
Roland Levillain31dd3d62016-02-16 12:21:02 +00003115 template <typename T>
3116 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3117
3118 template <typename T>
3119 int32_t CompareFP(T x, T y) const {
3120 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3121 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3122 // Handle the bias.
3123 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3124 }
3125
3126 // Return an integer constant containing the result of a condition evaluated at compile time.
3127 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3128 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3129 }
3130
Dave Allison20dfc792014-06-16 20:44:29 -07003131 private:
3132 DISALLOW_COPY_AND_ASSIGN(HCondition);
3133};
3134
3135// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003136class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003137 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003138 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3139 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003140
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003141 bool IsCommutative() const OVERRIDE { return true; }
3142
Vladimir Marko9e23df52015-11-10 17:14:35 +00003143 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3144 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003145 return MakeConstantCondition(true, GetDexPc());
3146 }
3147 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3148 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3149 }
3150 // In the following Evaluate methods, a HCompare instruction has
3151 // been merged into this HEqual instruction; evaluate it as
3152 // `Compare(x, y) == 0`.
3153 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3154 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3155 GetDexPc());
3156 }
3157 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3158 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3159 }
3160 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3161 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003162 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003163
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003164 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003165
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003166 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003167 return kCondEQ;
3168 }
3169
Mark Mendellc4701932015-04-10 13:18:51 -04003170 IfCondition GetOppositeCondition() const OVERRIDE {
3171 return kCondNE;
3172 }
3173
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003174 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003175 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003176
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003177 DISALLOW_COPY_AND_ASSIGN(HEqual);
3178};
3179
Vladimir Markofcb503c2016-05-18 12:48:17 +01003180class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003181 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003182 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3183 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003184
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003185 bool IsCommutative() const OVERRIDE { return true; }
3186
Vladimir Marko9e23df52015-11-10 17:14:35 +00003187 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3188 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003189 return MakeConstantCondition(false, GetDexPc());
3190 }
3191 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3192 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3193 }
3194 // In the following Evaluate methods, a HCompare instruction has
3195 // been merged into this HNotEqual instruction; evaluate it as
3196 // `Compare(x, y) != 0`.
3197 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3198 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3199 }
3200 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3201 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3202 }
3203 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3204 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003205 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003206
Dave Allison20dfc792014-06-16 20:44:29 -07003207 DECLARE_INSTRUCTION(NotEqual);
3208
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003209 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003210 return kCondNE;
3211 }
3212
Mark Mendellc4701932015-04-10 13:18:51 -04003213 IfCondition GetOppositeCondition() const OVERRIDE {
3214 return kCondEQ;
3215 }
3216
Dave Allison20dfc792014-06-16 20:44:29 -07003217 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003218 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003219
Dave Allison20dfc792014-06-16 20:44:29 -07003220 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3221};
3222
Vladimir Markofcb503c2016-05-18 12:48:17 +01003223class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003224 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003225 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3226 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003227
Roland Levillain9867bc72015-08-05 10:21:34 +01003228 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003229 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003230 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003231 // In the following Evaluate methods, a HCompare instruction has
3232 // been merged into this HLessThan instruction; evaluate it as
3233 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003234 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003235 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3236 }
3237 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3238 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3239 }
3240 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3241 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003242 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003243
Dave Allison20dfc792014-06-16 20:44:29 -07003244 DECLARE_INSTRUCTION(LessThan);
3245
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003246 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003247 return kCondLT;
3248 }
3249
Mark Mendellc4701932015-04-10 13:18:51 -04003250 IfCondition GetOppositeCondition() const OVERRIDE {
3251 return kCondGE;
3252 }
3253
Dave Allison20dfc792014-06-16 20:44:29 -07003254 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003255 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003256
Dave Allison20dfc792014-06-16 20:44:29 -07003257 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3258};
3259
Vladimir Markofcb503c2016-05-18 12:48:17 +01003260class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003261 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003262 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3263 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003264
Roland Levillain9867bc72015-08-05 10:21:34 +01003265 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003266 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003267 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003268 // In the following Evaluate methods, a HCompare instruction has
3269 // been merged into this HLessThanOrEqual instruction; evaluate it as
3270 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003271 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003272 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3273 }
3274 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3275 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3276 }
3277 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3278 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003279 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003280
Dave Allison20dfc792014-06-16 20:44:29 -07003281 DECLARE_INSTRUCTION(LessThanOrEqual);
3282
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003283 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003284 return kCondLE;
3285 }
3286
Mark Mendellc4701932015-04-10 13:18:51 -04003287 IfCondition GetOppositeCondition() const OVERRIDE {
3288 return kCondGT;
3289 }
3290
Dave Allison20dfc792014-06-16 20:44:29 -07003291 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003292 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003293
Dave Allison20dfc792014-06-16 20:44:29 -07003294 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3295};
3296
Vladimir Markofcb503c2016-05-18 12:48:17 +01003297class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003298 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003299 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3300 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003301
Roland Levillain9867bc72015-08-05 10:21:34 +01003302 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003303 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003304 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003305 // In the following Evaluate methods, a HCompare instruction has
3306 // been merged into this HGreaterThan instruction; evaluate it as
3307 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003308 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003309 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3310 }
3311 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3312 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3313 }
3314 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3315 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003316 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003317
Dave Allison20dfc792014-06-16 20:44:29 -07003318 DECLARE_INSTRUCTION(GreaterThan);
3319
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003320 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003321 return kCondGT;
3322 }
3323
Mark Mendellc4701932015-04-10 13:18:51 -04003324 IfCondition GetOppositeCondition() const OVERRIDE {
3325 return kCondLE;
3326 }
3327
Dave Allison20dfc792014-06-16 20:44:29 -07003328 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003329 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003330
Dave Allison20dfc792014-06-16 20:44:29 -07003331 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3332};
3333
Vladimir Markofcb503c2016-05-18 12:48:17 +01003334class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003335 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003336 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3337 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003338
Roland Levillain9867bc72015-08-05 10:21:34 +01003339 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003340 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003341 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003342 // In the following Evaluate methods, a HCompare instruction has
3343 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3344 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003345 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003346 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3347 }
3348 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3349 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3350 }
3351 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3352 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003353 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003354
Dave Allison20dfc792014-06-16 20:44:29 -07003355 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3356
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003357 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003358 return kCondGE;
3359 }
3360
Mark Mendellc4701932015-04-10 13:18:51 -04003361 IfCondition GetOppositeCondition() const OVERRIDE {
3362 return kCondLT;
3363 }
3364
Dave Allison20dfc792014-06-16 20:44:29 -07003365 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003366 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003367
Dave Allison20dfc792014-06-16 20:44:29 -07003368 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3369};
3370
Vladimir Markofcb503c2016-05-18 12:48:17 +01003371class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003372 public:
3373 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3374 : HCondition(first, second, dex_pc) {}
3375
3376 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003377 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003378 }
3379 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003380 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3381 }
3382 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3383 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3384 LOG(FATAL) << DebugName() << " is not defined for float values";
3385 UNREACHABLE();
3386 }
3387 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3388 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3389 LOG(FATAL) << DebugName() << " is not defined for double values";
3390 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003391 }
3392
3393 DECLARE_INSTRUCTION(Below);
3394
3395 IfCondition GetCondition() const OVERRIDE {
3396 return kCondB;
3397 }
3398
3399 IfCondition GetOppositeCondition() const OVERRIDE {
3400 return kCondAE;
3401 }
3402
3403 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003404 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003405 return MakeUnsigned(x) < MakeUnsigned(y);
3406 }
Aart Bike9f37602015-10-09 11:15:55 -07003407
3408 DISALLOW_COPY_AND_ASSIGN(HBelow);
3409};
3410
Vladimir Markofcb503c2016-05-18 12:48:17 +01003411class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003412 public:
3413 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3414 : HCondition(first, second, dex_pc) {}
3415
3416 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003417 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003418 }
3419 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003420 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3421 }
3422 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3423 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3424 LOG(FATAL) << DebugName() << " is not defined for float values";
3425 UNREACHABLE();
3426 }
3427 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3428 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3429 LOG(FATAL) << DebugName() << " is not defined for double values";
3430 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003431 }
3432
3433 DECLARE_INSTRUCTION(BelowOrEqual);
3434
3435 IfCondition GetCondition() const OVERRIDE {
3436 return kCondBE;
3437 }
3438
3439 IfCondition GetOppositeCondition() const OVERRIDE {
3440 return kCondA;
3441 }
3442
3443 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003444 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003445 return MakeUnsigned(x) <= MakeUnsigned(y);
3446 }
Aart Bike9f37602015-10-09 11:15:55 -07003447
3448 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3449};
3450
Vladimir Markofcb503c2016-05-18 12:48:17 +01003451class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003452 public:
3453 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3454 : HCondition(first, second, dex_pc) {}
3455
3456 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003457 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003458 }
3459 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003460 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3461 }
3462 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3463 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3464 LOG(FATAL) << DebugName() << " is not defined for float values";
3465 UNREACHABLE();
3466 }
3467 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3468 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3469 LOG(FATAL) << DebugName() << " is not defined for double values";
3470 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003471 }
3472
3473 DECLARE_INSTRUCTION(Above);
3474
3475 IfCondition GetCondition() const OVERRIDE {
3476 return kCondA;
3477 }
3478
3479 IfCondition GetOppositeCondition() const OVERRIDE {
3480 return kCondBE;
3481 }
3482
3483 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003484 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003485 return MakeUnsigned(x) > MakeUnsigned(y);
3486 }
Aart Bike9f37602015-10-09 11:15:55 -07003487
3488 DISALLOW_COPY_AND_ASSIGN(HAbove);
3489};
3490
Vladimir Markofcb503c2016-05-18 12:48:17 +01003491class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003492 public:
3493 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3494 : HCondition(first, second, dex_pc) {}
3495
3496 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003497 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003498 }
3499 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003500 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3501 }
3502 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3503 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3504 LOG(FATAL) << DebugName() << " is not defined for float values";
3505 UNREACHABLE();
3506 }
3507 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3508 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3509 LOG(FATAL) << DebugName() << " is not defined for double values";
3510 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003511 }
3512
3513 DECLARE_INSTRUCTION(AboveOrEqual);
3514
3515 IfCondition GetCondition() const OVERRIDE {
3516 return kCondAE;
3517 }
3518
3519 IfCondition GetOppositeCondition() const OVERRIDE {
3520 return kCondB;
3521 }
3522
3523 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003524 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003525 return MakeUnsigned(x) >= MakeUnsigned(y);
3526 }
Aart Bike9f37602015-10-09 11:15:55 -07003527
3528 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3529};
Dave Allison20dfc792014-06-16 20:44:29 -07003530
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003531// Instruction to check how two inputs compare to each other.
3532// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003533class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003534 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003535 // Note that `comparison_type` is the type of comparison performed
3536 // between the comparison's inputs, not the type of the instantiated
3537 // HCompare instruction (which is always Primitive::kPrimInt).
3538 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003539 HInstruction* first,
3540 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003541 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003542 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003543 : HBinaryOperation(Primitive::kPrimInt,
3544 first,
3545 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003546 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003547 dex_pc) {
3548 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003549 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3550 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003551 }
3552
Roland Levillain9867bc72015-08-05 10:21:34 +01003553 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003554 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3555
3556 template <typename T>
3557 int32_t ComputeFP(T x, T y) const {
3558 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3559 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3560 // Handle the bias.
3561 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3562 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003563
Roland Levillain9867bc72015-08-05 10:21:34 +01003564 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003565 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3566 // reach this code path when processing a freshly built HIR
3567 // graph. However HCompare integer instructions can be synthesized
3568 // by the instruction simplifier to implement IntegerCompare and
3569 // IntegerSignum intrinsics, so we have to handle this case.
3570 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003571 }
3572 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003573 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3574 }
3575 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3576 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3577 }
3578 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3579 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003580 }
3581
Vladimir Marko372f10e2016-05-17 16:30:10 +01003582 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003583 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003584 }
3585
Vladimir Markoa1de9182016-02-25 11:37:38 +00003586 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003587
Roland Levillain31dd3d62016-02-16 12:21:02 +00003588 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003589 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003590 bool IsGtBias() const {
3591 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003592 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003593 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003594
Roland Levillain1693a1f2016-03-15 14:57:31 +00003595 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3596 // Comparisons do not require a runtime call in any back end.
3597 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003598 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003599
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003600 DECLARE_INSTRUCTION(Compare);
3601
Roland Levillain31dd3d62016-02-16 12:21:02 +00003602 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003603 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3604 static constexpr size_t kFieldComparisonBiasSize =
3605 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3606 static constexpr size_t kNumberOfComparePackedBits =
3607 kFieldComparisonBias + kFieldComparisonBiasSize;
3608 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3609 using ComparisonBiasField =
3610 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3611
Roland Levillain31dd3d62016-02-16 12:21:02 +00003612 // Return an integer constant containing the result of a comparison evaluated at compile time.
3613 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3614 DCHECK(value == -1 || value == 0 || value == 1) << value;
3615 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3616 }
3617
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003618 private:
3619 DISALLOW_COPY_AND_ASSIGN(HCompare);
3620};
3621
Vladimir Markofcb503c2016-05-18 12:48:17 +01003622class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003623 public:
3624 HNewInstance(HInstruction* cls,
3625 HCurrentMethod* current_method,
3626 uint32_t dex_pc,
3627 uint16_t type_index,
3628 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003629 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003630 bool finalizable,
3631 QuickEntrypointEnum entrypoint)
3632 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3633 type_index_(type_index),
3634 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003635 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003636 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003637 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003638 SetRawInputAt(0, cls);
3639 SetRawInputAt(1, current_method);
3640 }
3641
3642 uint16_t GetTypeIndex() const { return type_index_; }
3643 const DexFile& GetDexFile() const { return dex_file_; }
3644
3645 // Calls runtime so needs an environment.
3646 bool NeedsEnvironment() const OVERRIDE { return true; }
3647
Mingyao Yang062157f2016-03-02 10:15:36 -08003648 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3649 bool CanThrow() const OVERRIDE { return true; }
3650
3651 // Needs to call into runtime to make sure it's instantiable/accessible.
3652 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003653
Vladimir Markoa1de9182016-02-25 11:37:38 +00003654 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003655
3656 bool CanBeNull() const OVERRIDE { return false; }
3657
3658 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3659
3660 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3661 entrypoint_ = entrypoint;
3662 }
3663
3664 bool IsStringAlloc() const;
3665
3666 DECLARE_INSTRUCTION(NewInstance);
3667
3668 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003669 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3670 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003671 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3672 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3673 "Too many packed fields.");
3674
David Brazdil6de19382016-01-08 17:37:10 +00003675 const uint16_t type_index_;
3676 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003677 QuickEntrypointEnum entrypoint_;
3678
3679 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3680};
3681
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003682enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003683#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3684 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003685#include "intrinsics_list.h"
3686 kNone,
3687 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3688#undef INTRINSICS_LIST
3689#undef OPTIMIZING_INTRINSICS
3690};
3691std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3692
Agi Csaki05f20562015-08-19 14:58:14 -07003693enum IntrinsicNeedsEnvironmentOrCache {
3694 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3695 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003696};
3697
Aart Bik5d75afe2015-12-14 11:57:01 -08003698enum IntrinsicSideEffects {
3699 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3700 kReadSideEffects, // Intrinsic may read heap memory.
3701 kWriteSideEffects, // Intrinsic may write heap memory.
3702 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3703};
3704
3705enum IntrinsicExceptions {
3706 kNoThrow, // Intrinsic does not throw any exceptions.
3707 kCanThrow // Intrinsic may throw exceptions.
3708};
3709
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003710class HInvoke : public HInstruction {
3711 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003712 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003713
Vladimir Marko372f10e2016-05-17 16:30:10 +01003714 using HInstruction::GetInputRecords; // Keep the const version visible.
3715 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3716 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3717 }
3718
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003719 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003720 SetRawInputAt(index, argument);
3721 }
3722
Roland Levillain3e3d7332015-04-28 11:00:54 +01003723 // Return the number of arguments. This number can be lower than
3724 // the number of inputs returned by InputCount(), as some invoke
3725 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3726 // inputs at the end of their list of inputs.
3727 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3728
Vladimir Markoa1de9182016-02-25 11:37:38 +00003729 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003730
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003731 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003732 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003733
Vladimir Markoa1de9182016-02-25 11:37:38 +00003734 InvokeType GetOriginalInvokeType() const {
3735 return GetPackedField<OriginalInvokeTypeField>();
3736 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003737
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003738 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003739 return intrinsic_;
3740 }
3741
Aart Bik5d75afe2015-12-14 11:57:01 -08003742 void SetIntrinsic(Intrinsics intrinsic,
3743 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3744 IntrinsicSideEffects side_effects,
3745 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003746
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003747 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003748 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003749 }
3750
Vladimir Markoa1de9182016-02-25 11:37:38 +00003751 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003752
3753 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3754
Vladimir Marko372f10e2016-05-17 16:30:10 +01003755 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003756 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3757 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003758
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003759 uint32_t* GetIntrinsicOptimizations() {
3760 return &intrinsic_optimizations_;
3761 }
3762
3763 const uint32_t* GetIntrinsicOptimizations() const {
3764 return &intrinsic_optimizations_;
3765 }
3766
3767 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3768
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003769 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003770
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003771 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003772 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3773 static constexpr size_t kFieldOriginalInvokeTypeSize =
3774 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3775 static constexpr size_t kFieldReturnType =
3776 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3777 static constexpr size_t kFieldReturnTypeSize =
3778 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3779 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3780 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3781 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3782 using OriginalInvokeTypeField =
3783 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3784 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3785
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003786 HInvoke(ArenaAllocator* arena,
3787 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003788 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003789 Primitive::Type return_type,
3790 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003791 uint32_t dex_method_index,
3792 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003793 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003794 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003795 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003796 inputs_(number_of_arguments + number_of_other_inputs,
3797 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003798 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003799 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003800 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003801 SetPackedField<ReturnTypeField>(return_type);
3802 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3803 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003804 }
3805
Vladimir Markoa1de9182016-02-25 11:37:38 +00003806 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003807
Roland Levillain3e3d7332015-04-28 11:00:54 +01003808 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003809 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003810 const uint32_t dex_method_index_;
3811 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003812
3813 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3814 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003815
3816 private:
3817 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3818};
3819
Vladimir Markofcb503c2016-05-18 12:48:17 +01003820class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003821 public:
3822 HInvokeUnresolved(ArenaAllocator* arena,
3823 uint32_t number_of_arguments,
3824 Primitive::Type return_type,
3825 uint32_t dex_pc,
3826 uint32_t dex_method_index,
3827 InvokeType invoke_type)
3828 : HInvoke(arena,
3829 number_of_arguments,
3830 0u /* number_of_other_inputs */,
3831 return_type,
3832 dex_pc,
3833 dex_method_index,
3834 invoke_type) {
3835 }
3836
3837 DECLARE_INSTRUCTION(InvokeUnresolved);
3838
3839 private:
3840 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3841};
3842
Vladimir Markofcb503c2016-05-18 12:48:17 +01003843class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003844 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003845 // Requirements of this method call regarding the class
3846 // initialization (clinit) check of its declaring class.
3847 enum class ClinitCheckRequirement {
3848 kNone, // Class already initialized.
3849 kExplicit, // Static call having explicit clinit check as last input.
3850 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003851 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003852 };
3853
Vladimir Marko58155012015-08-19 12:49:41 +00003854 // Determines how to load the target ArtMethod*.
3855 enum class MethodLoadKind {
3856 // Use a String init ArtMethod* loaded from Thread entrypoints.
3857 kStringInit,
3858
3859 // Use the method's own ArtMethod* loaded by the register allocator.
3860 kRecursive,
3861
3862 // Use ArtMethod* at a known address, embed the direct address in the code.
3863 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3864 kDirectAddress,
3865
3866 // Use ArtMethod* at an address that will be known at link time, embed the direct
3867 // address in the code. If the image is relocatable, emit .patch_oat entry.
3868 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3869 // the image relocatable or not.
3870 kDirectAddressWithFixup,
3871
Vladimir Markoa1de9182016-02-25 11:37:38 +00003872 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003873 // Used when we need to use the dex cache, for example for invoke-static that
3874 // may cause class initialization (the entry may point to a resolution method),
3875 // and we know that we can access the dex cache arrays using a PC-relative load.
3876 kDexCachePcRelative,
3877
3878 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3879 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3880 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3881 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3882 kDexCacheViaMethod,
3883 };
3884
3885 // Determines the location of the code pointer.
3886 enum class CodePtrLocation {
3887 // Recursive call, use local PC-relative call instruction.
3888 kCallSelf,
3889
3890 // Use PC-relative call instruction patched at link time.
3891 // Used for calls within an oat file, boot->boot or app->app.
3892 kCallPCRelative,
3893
3894 // Call to a known target address, embed the direct address in code.
3895 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3896 kCallDirect,
3897
3898 // Call to a target address that will be known at link time, embed the direct
3899 // address in code. If the image is relocatable, emit .patch_oat entry.
3900 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3901 // the image relocatable or not.
3902 kCallDirectWithFixup,
3903
3904 // Use code pointer from the ArtMethod*.
3905 // Used when we don't know the target code. This is also the last-resort-kind used when
3906 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3907 kCallArtMethod,
3908 };
3909
3910 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003911 MethodLoadKind method_load_kind;
3912 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003913 // The method load data holds
3914 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3915 // Note that there are multiple string init methods, each having its own offset.
3916 // - the method address for kDirectAddress
3917 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003918 uint64_t method_load_data;
3919 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003920 };
3921
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003922 HInvokeStaticOrDirect(ArenaAllocator* arena,
3923 uint32_t number_of_arguments,
3924 Primitive::Type return_type,
3925 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003926 uint32_t method_index,
3927 MethodReference target_method,
3928 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003929 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003930 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003931 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003932 : HInvoke(arena,
3933 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003934 // There is potentially one extra argument for the HCurrentMethod node, and
3935 // potentially one other if the clinit check is explicit, and potentially
3936 // one other if the method is a string factory.
3937 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003938 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003939 return_type,
3940 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003941 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003942 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003943 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003944 dispatch_info_(dispatch_info) {
3945 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3946 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3947 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003948
Vladimir Markodc151b22015-10-15 18:02:30 +01003949 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003950 bool had_current_method_input = HasCurrentMethodInput();
3951 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3952
3953 // Using the current method is the default and once we find a better
3954 // method load kind, we should not go back to using the current method.
3955 DCHECK(had_current_method_input || !needs_current_method_input);
3956
3957 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003958 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3959 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003960 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003961 dispatch_info_ = dispatch_info;
3962 }
3963
Vladimir Markoc53c0792015-11-19 15:48:33 +00003964 void AddSpecialInput(HInstruction* input) {
3965 // We allow only one special input.
3966 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3967 DCHECK(InputCount() == GetSpecialInputIndex() ||
3968 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3969 InsertInputAt(GetSpecialInputIndex(), input);
3970 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003971
Vladimir Marko372f10e2016-05-17 16:30:10 +01003972 using HInstruction::GetInputRecords; // Keep the const version visible.
3973 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3974 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
3975 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
3976 DCHECK(!input_records.empty());
3977 DCHECK_GT(input_records.size(), GetNumberOfArguments());
3978 HInstruction* last_input = input_records.back().GetInstruction();
3979 // Note: `last_input` may be null during arguments setup.
3980 if (last_input != nullptr) {
3981 // `last_input` is the last input of a static invoke marked as having
3982 // an explicit clinit check. It must either be:
3983 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3984 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3985 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
3986 }
3987 }
3988 return input_records;
3989 }
3990
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003991 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003992 // We access the method via the dex cache so we can't do an implicit null check.
3993 // TODO: for intrinsics we can generate implicit null checks.
3994 return false;
3995 }
3996
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003997 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003998 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003999 }
4000
Vladimir Markoc53c0792015-11-19 15:48:33 +00004001 // Get the index of the special input, if any.
4002 //
David Brazdil6de19382016-01-08 17:37:10 +00004003 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4004 // method pointer; otherwise there may be one platform-specific special input,
4005 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004006 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004007 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004008
Vladimir Markoa1de9182016-02-25 11:37:38 +00004009 InvokeType GetOptimizedInvokeType() const {
4010 return GetPackedField<OptimizedInvokeTypeField>();
4011 }
4012
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004013 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004014 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004015 }
4016
Vladimir Marko58155012015-08-19 12:49:41 +00004017 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4018 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4019 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004020 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004021 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004022 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004023 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004024 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4025 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004026 bool HasCurrentMethodInput() const {
4027 // This function can be called only after the invoke has been fully initialized by the builder.
4028 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004029 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004030 return true;
4031 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004032 DCHECK(InputCount() == GetSpecialInputIndex() ||
4033 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004034 return false;
4035 }
4036 }
Vladimir Marko58155012015-08-19 12:49:41 +00004037 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
4038 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004039 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00004040
4041 int32_t GetStringInitOffset() const {
4042 DCHECK(IsStringInit());
4043 return dispatch_info_.method_load_data;
4044 }
4045
4046 uint64_t GetMethodAddress() const {
4047 DCHECK(HasMethodAddress());
4048 return dispatch_info_.method_load_data;
4049 }
4050
4051 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004052 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004053 return dispatch_info_.method_load_data;
4054 }
4055
4056 uint64_t GetDirectCodePtr() const {
4057 DCHECK(HasDirectCodePtr());
4058 return dispatch_info_.direct_code_ptr;
4059 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004060
Vladimir Markoa1de9182016-02-25 11:37:38 +00004061 ClinitCheckRequirement GetClinitCheckRequirement() const {
4062 return GetPackedField<ClinitCheckRequirementField>();
4063 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004064
Roland Levillain4c0eb422015-04-24 16:43:49 +01004065 // Is this instruction a call to a static method?
4066 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004067 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004068 }
4069
Vladimir Markofbb184a2015-11-13 14:47:00 +00004070 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4071 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4072 // instruction; only relevant for static calls with explicit clinit check.
4073 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004074 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004075 size_t last_input_index = inputs_.size() - 1u;
4076 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004077 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004078 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004079 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004080 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004081 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004082 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004083 }
4084
4085 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004086 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004087 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004088 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004089 }
4090
4091 // Is this a call to a static method whose declaring class has an
4092 // implicit intialization check requirement?
4093 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004094 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004095 }
4096
Vladimir Markob554b5a2015-11-06 12:57:55 +00004097 // Does this method load kind need the current method as an input?
4098 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4099 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4100 }
4101
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004102 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004103
Roland Levillain4c0eb422015-04-24 16:43:49 +01004104 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004105 void InsertInputAt(size_t index, HInstruction* input);
4106 void RemoveInputAt(size_t index);
4107
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004108 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004109 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4110 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4111 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4112 static constexpr size_t kFieldClinitCheckRequirement =
4113 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4114 static constexpr size_t kFieldClinitCheckRequirementSize =
4115 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4116 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4117 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4118 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4119 "Too many packed fields.");
4120 using OptimizedInvokeTypeField =
4121 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4122 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4123 kFieldClinitCheckRequirement,
4124 kFieldClinitCheckRequirementSize>;
4125
Vladimir Marko58155012015-08-19 12:49:41 +00004126 // The target method may refer to different dex file or method index than the original
4127 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4128 // in derived class to increase visibility.
4129 MethodReference target_method_;
4130 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004131
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004132 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004133};
Vladimir Markof64242a2015-12-01 14:58:23 +00004134std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004135std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004136
Vladimir Markofcb503c2016-05-18 12:48:17 +01004137class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004138 public:
4139 HInvokeVirtual(ArenaAllocator* arena,
4140 uint32_t number_of_arguments,
4141 Primitive::Type return_type,
4142 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004143 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004144 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004145 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004146 vtable_index_(vtable_index) {}
4147
Calin Juravle641547a2015-04-21 22:08:51 +01004148 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004149 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004150 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004151 }
4152
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004153 uint32_t GetVTableIndex() const { return vtable_index_; }
4154
4155 DECLARE_INSTRUCTION(InvokeVirtual);
4156
4157 private:
4158 const uint32_t vtable_index_;
4159
4160 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4161};
4162
Vladimir Markofcb503c2016-05-18 12:48:17 +01004163class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004164 public:
4165 HInvokeInterface(ArenaAllocator* arena,
4166 uint32_t number_of_arguments,
4167 Primitive::Type return_type,
4168 uint32_t dex_pc,
4169 uint32_t dex_method_index,
4170 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004171 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004172 imt_index_(imt_index) {}
4173
Calin Juravle641547a2015-04-21 22:08:51 +01004174 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004175 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004176 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004177 }
4178
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004179 uint32_t GetImtIndex() const { return imt_index_; }
4180 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4181
4182 DECLARE_INSTRUCTION(InvokeInterface);
4183
4184 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004185 const uint32_t imt_index_;
4186
4187 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4188};
4189
Vladimir Markofcb503c2016-05-18 12:48:17 +01004190class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004191 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004192 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004193 : HUnaryOperation(result_type, input, dex_pc) {
4194 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4195 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004196
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004197 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004198
4199 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004200 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004201 }
4202 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004203 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004204 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004205 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4206 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4207 }
4208 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4209 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4210 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004211
Roland Levillain88cb1752014-10-20 16:36:47 +01004212 DECLARE_INSTRUCTION(Neg);
4213
4214 private:
4215 DISALLOW_COPY_AND_ASSIGN(HNeg);
4216};
4217
Vladimir Markofcb503c2016-05-18 12:48:17 +01004218class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004219 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004220 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004221 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004222 uint32_t dex_pc,
4223 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004224 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004225 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004226 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004227 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004228 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004229 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004230 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004231 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004232 }
4233
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004234 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004235 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004236
4237 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004238 bool NeedsEnvironment() const OVERRIDE { return true; }
4239
Mingyao Yang0c365e62015-03-31 15:09:29 -07004240 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4241 bool CanThrow() const OVERRIDE { return true; }
4242
Calin Juravle10e244f2015-01-26 18:54:32 +00004243 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004244
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004245 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4246
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004247 DECLARE_INSTRUCTION(NewArray);
4248
4249 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004250 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004251 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004252 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004253
4254 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4255};
4256
Vladimir Markofcb503c2016-05-18 12:48:17 +01004257class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004258 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004259 HAdd(Primitive::Type result_type,
4260 HInstruction* left,
4261 HInstruction* right,
4262 uint32_t dex_pc = kNoDexPc)
4263 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004264
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004265 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004266
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004267 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004268
4269 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004270 return GetBlock()->GetGraph()->GetIntConstant(
4271 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004272 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004273 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004274 return GetBlock()->GetGraph()->GetLongConstant(
4275 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004276 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004277 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4278 return GetBlock()->GetGraph()->GetFloatConstant(
4279 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4280 }
4281 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4282 return GetBlock()->GetGraph()->GetDoubleConstant(
4283 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4284 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004285
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004286 DECLARE_INSTRUCTION(Add);
4287
4288 private:
4289 DISALLOW_COPY_AND_ASSIGN(HAdd);
4290};
4291
Vladimir Markofcb503c2016-05-18 12:48:17 +01004292class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004293 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004294 HSub(Primitive::Type result_type,
4295 HInstruction* left,
4296 HInstruction* right,
4297 uint32_t dex_pc = kNoDexPc)
4298 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004299
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004300 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004301
4302 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004303 return GetBlock()->GetGraph()->GetIntConstant(
4304 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004305 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004306 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004307 return GetBlock()->GetGraph()->GetLongConstant(
4308 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004309 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004310 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4311 return GetBlock()->GetGraph()->GetFloatConstant(
4312 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4313 }
4314 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4315 return GetBlock()->GetGraph()->GetDoubleConstant(
4316 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4317 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004318
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004319 DECLARE_INSTRUCTION(Sub);
4320
4321 private:
4322 DISALLOW_COPY_AND_ASSIGN(HSub);
4323};
4324
Vladimir Markofcb503c2016-05-18 12:48:17 +01004325class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004326 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004327 HMul(Primitive::Type result_type,
4328 HInstruction* left,
4329 HInstruction* right,
4330 uint32_t dex_pc = kNoDexPc)
4331 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004332
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004333 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004334
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004335 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004336
4337 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004338 return GetBlock()->GetGraph()->GetIntConstant(
4339 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004340 }
4341 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004342 return GetBlock()->GetGraph()->GetLongConstant(
4343 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004344 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004345 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4346 return GetBlock()->GetGraph()->GetFloatConstant(
4347 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4348 }
4349 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4350 return GetBlock()->GetGraph()->GetDoubleConstant(
4351 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4352 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004353
4354 DECLARE_INSTRUCTION(Mul);
4355
4356 private:
4357 DISALLOW_COPY_AND_ASSIGN(HMul);
4358};
4359
Vladimir Markofcb503c2016-05-18 12:48:17 +01004360class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004361 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004362 HDiv(Primitive::Type result_type,
4363 HInstruction* left,
4364 HInstruction* right,
4365 uint32_t dex_pc)
4366 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004367
Roland Levillain9867bc72015-08-05 10:21:34 +01004368 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004369 T ComputeIntegral(T x, T y) const {
4370 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004371 // Our graph structure ensures we never have 0 for `y` during
4372 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004373 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004374 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004375 return (y == -1) ? -x : x / y;
4376 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004377
Roland Levillain31dd3d62016-02-16 12:21:02 +00004378 template <typename T>
4379 T ComputeFP(T x, T y) const {
4380 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4381 return x / y;
4382 }
4383
Roland Levillain9867bc72015-08-05 10:21:34 +01004384 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004385 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004386 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004387 }
4388 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004389 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004390 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4391 }
4392 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4393 return GetBlock()->GetGraph()->GetFloatConstant(
4394 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4395 }
4396 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4397 return GetBlock()->GetGraph()->GetDoubleConstant(
4398 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004399 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004400
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004401 static SideEffects SideEffectsForArchRuntimeCalls() {
4402 // The generated code can use a runtime call.
4403 return SideEffects::CanTriggerGC();
4404 }
4405
Calin Juravle7c4954d2014-10-28 16:57:40 +00004406 DECLARE_INSTRUCTION(Div);
4407
4408 private:
4409 DISALLOW_COPY_AND_ASSIGN(HDiv);
4410};
4411
Vladimir Markofcb503c2016-05-18 12:48:17 +01004412class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004413 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004414 HRem(Primitive::Type result_type,
4415 HInstruction* left,
4416 HInstruction* right,
4417 uint32_t dex_pc)
4418 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004419
Roland Levillain9867bc72015-08-05 10:21:34 +01004420 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004421 T ComputeIntegral(T x, T y) const {
4422 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004423 // Our graph structure ensures we never have 0 for `y` during
4424 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004425 DCHECK_NE(y, 0);
4426 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4427 return (y == -1) ? 0 : x % y;
4428 }
4429
Roland Levillain31dd3d62016-02-16 12:21:02 +00004430 template <typename T>
4431 T ComputeFP(T x, T y) const {
4432 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4433 return std::fmod(x, y);
4434 }
4435
Roland Levillain9867bc72015-08-05 10:21:34 +01004436 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004437 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004438 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004439 }
4440 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004441 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004442 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004443 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004444 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4445 return GetBlock()->GetGraph()->GetFloatConstant(
4446 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4447 }
4448 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4449 return GetBlock()->GetGraph()->GetDoubleConstant(
4450 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4451 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004452
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004453 static SideEffects SideEffectsForArchRuntimeCalls() {
4454 return SideEffects::CanTriggerGC();
4455 }
4456
Calin Juravlebacfec32014-11-14 15:54:36 +00004457 DECLARE_INSTRUCTION(Rem);
4458
4459 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004460 DISALLOW_COPY_AND_ASSIGN(HRem);
4461};
4462
Vladimir Markofcb503c2016-05-18 12:48:17 +01004463class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004464 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004465 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4466 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004467 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004468 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004469 SetRawInputAt(0, value);
4470 }
4471
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004472 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4473
Calin Juravled0d48522014-11-04 16:40:20 +00004474 bool CanBeMoved() const OVERRIDE { return true; }
4475
Vladimir Marko372f10e2016-05-17 16:30:10 +01004476 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004477 return true;
4478 }
4479
4480 bool NeedsEnvironment() const OVERRIDE { return true; }
4481 bool CanThrow() const OVERRIDE { return true; }
4482
Calin Juravled0d48522014-11-04 16:40:20 +00004483 DECLARE_INSTRUCTION(DivZeroCheck);
4484
4485 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004486 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4487};
4488
Vladimir Markofcb503c2016-05-18 12:48:17 +01004489class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004490 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004491 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004492 HInstruction* value,
4493 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004494 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004495 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4496 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4497 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004498 }
4499
Roland Levillain5b5b9312016-03-22 14:57:31 +00004500 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004501 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004502 return value << (distance & max_shift_distance);
4503 }
4504
4505 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004506 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004507 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004508 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004509 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004510 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004511 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004512 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004513 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4514 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4515 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4516 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004517 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004518 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4519 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004520 LOG(FATAL) << DebugName() << " is not defined for float values";
4521 UNREACHABLE();
4522 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004523 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4524 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004525 LOG(FATAL) << DebugName() << " is not defined for double values";
4526 UNREACHABLE();
4527 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004528
4529 DECLARE_INSTRUCTION(Shl);
4530
4531 private:
4532 DISALLOW_COPY_AND_ASSIGN(HShl);
4533};
4534
Vladimir Markofcb503c2016-05-18 12:48:17 +01004535class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004536 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004537 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004538 HInstruction* value,
4539 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004540 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004541 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4542 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4543 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004544 }
4545
Roland Levillain5b5b9312016-03-22 14:57:31 +00004546 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004547 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004548 return value >> (distance & max_shift_distance);
4549 }
4550
4551 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004552 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004553 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004554 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004555 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004556 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004557 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004558 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004559 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4560 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4561 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4562 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004563 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004564 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4565 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004566 LOG(FATAL) << DebugName() << " is not defined for float values";
4567 UNREACHABLE();
4568 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004569 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4570 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004571 LOG(FATAL) << DebugName() << " is not defined for double values";
4572 UNREACHABLE();
4573 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004574
4575 DECLARE_INSTRUCTION(Shr);
4576
4577 private:
4578 DISALLOW_COPY_AND_ASSIGN(HShr);
4579};
4580
Vladimir Markofcb503c2016-05-18 12:48:17 +01004581class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004582 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004583 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004584 HInstruction* value,
4585 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004586 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004587 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4588 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4589 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004590 }
4591
Roland Levillain5b5b9312016-03-22 14:57:31 +00004592 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004593 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004594 typedef typename std::make_unsigned<T>::type V;
4595 V ux = static_cast<V>(value);
4596 return static_cast<T>(ux >> (distance & max_shift_distance));
4597 }
4598
4599 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004600 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004601 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004602 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004603 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004604 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004605 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004606 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004607 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4608 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4609 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4610 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004611 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004612 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4613 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004614 LOG(FATAL) << DebugName() << " is not defined for float values";
4615 UNREACHABLE();
4616 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004617 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4618 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004619 LOG(FATAL) << DebugName() << " is not defined for double values";
4620 UNREACHABLE();
4621 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004622
4623 DECLARE_INSTRUCTION(UShr);
4624
4625 private:
4626 DISALLOW_COPY_AND_ASSIGN(HUShr);
4627};
4628
Vladimir Markofcb503c2016-05-18 12:48:17 +01004629class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004630 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004631 HAnd(Primitive::Type result_type,
4632 HInstruction* left,
4633 HInstruction* right,
4634 uint32_t dex_pc = kNoDexPc)
4635 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004636
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004637 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004638
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004639 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004640
4641 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004642 return GetBlock()->GetGraph()->GetIntConstant(
4643 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004644 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004645 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004646 return GetBlock()->GetGraph()->GetLongConstant(
4647 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004648 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004649 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4650 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4651 LOG(FATAL) << DebugName() << " is not defined for float values";
4652 UNREACHABLE();
4653 }
4654 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4655 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4656 LOG(FATAL) << DebugName() << " is not defined for double values";
4657 UNREACHABLE();
4658 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004659
4660 DECLARE_INSTRUCTION(And);
4661
4662 private:
4663 DISALLOW_COPY_AND_ASSIGN(HAnd);
4664};
4665
Vladimir Markofcb503c2016-05-18 12:48:17 +01004666class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004667 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004668 HOr(Primitive::Type result_type,
4669 HInstruction* left,
4670 HInstruction* right,
4671 uint32_t dex_pc = kNoDexPc)
4672 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004673
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004674 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004675
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004676 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004677
4678 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004679 return GetBlock()->GetGraph()->GetIntConstant(
4680 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004681 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004682 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004683 return GetBlock()->GetGraph()->GetLongConstant(
4684 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004685 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004686 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4687 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4688 LOG(FATAL) << DebugName() << " is not defined for float values";
4689 UNREACHABLE();
4690 }
4691 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4692 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4693 LOG(FATAL) << DebugName() << " is not defined for double values";
4694 UNREACHABLE();
4695 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004696
4697 DECLARE_INSTRUCTION(Or);
4698
4699 private:
4700 DISALLOW_COPY_AND_ASSIGN(HOr);
4701};
4702
Vladimir Markofcb503c2016-05-18 12:48:17 +01004703class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004704 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004705 HXor(Primitive::Type result_type,
4706 HInstruction* left,
4707 HInstruction* right,
4708 uint32_t dex_pc = kNoDexPc)
4709 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004710
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004711 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004712
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004713 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004714
4715 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004716 return GetBlock()->GetGraph()->GetIntConstant(
4717 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004718 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004719 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004720 return GetBlock()->GetGraph()->GetLongConstant(
4721 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004722 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004723 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4724 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4725 LOG(FATAL) << DebugName() << " is not defined for float values";
4726 UNREACHABLE();
4727 }
4728 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4729 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4730 LOG(FATAL) << DebugName() << " is not defined for double values";
4731 UNREACHABLE();
4732 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004733
4734 DECLARE_INSTRUCTION(Xor);
4735
4736 private:
4737 DISALLOW_COPY_AND_ASSIGN(HXor);
4738};
4739
Vladimir Markofcb503c2016-05-18 12:48:17 +01004740class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004741 public:
4742 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004743 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004744 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4745 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004746 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004747
Roland Levillain5b5b9312016-03-22 14:57:31 +00004748 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004749 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004750 typedef typename std::make_unsigned<T>::type V;
4751 V ux = static_cast<V>(value);
4752 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004753 return static_cast<T>(ux);
4754 } else {
4755 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004756 return static_cast<T>(ux >> (distance & max_shift_value)) |
4757 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004758 }
4759 }
4760
Roland Levillain5b5b9312016-03-22 14:57:31 +00004761 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004762 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004763 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004764 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004765 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004766 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004767 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004768 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004769 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4770 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4771 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4772 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004773 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004774 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4775 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004776 LOG(FATAL) << DebugName() << " is not defined for float values";
4777 UNREACHABLE();
4778 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004779 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4780 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004781 LOG(FATAL) << DebugName() << " is not defined for double values";
4782 UNREACHABLE();
4783 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004784
4785 DECLARE_INSTRUCTION(Ror);
4786
4787 private:
4788 DISALLOW_COPY_AND_ASSIGN(HRor);
4789};
4790
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004791// The value of a parameter in this method. Its location depends on
4792// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004793class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004794 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004795 HParameterValue(const DexFile& dex_file,
4796 uint16_t type_index,
4797 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004798 Primitive::Type parameter_type,
4799 bool is_this = false)
4800 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004801 dex_file_(dex_file),
4802 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004803 index_(index) {
4804 SetPackedFlag<kFlagIsThis>(is_this);
4805 SetPackedFlag<kFlagCanBeNull>(!is_this);
4806 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004807
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004808 const DexFile& GetDexFile() const { return dex_file_; }
4809 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004810 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004811 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004812
Vladimir Markoa1de9182016-02-25 11:37:38 +00004813 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4814 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004815
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004816 DECLARE_INSTRUCTION(ParameterValue);
4817
4818 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004819 // Whether or not the parameter value corresponds to 'this' argument.
4820 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4821 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4822 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4823 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4824 "Too many packed fields.");
4825
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004826 const DexFile& dex_file_;
4827 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004828 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004829 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004830 const uint8_t index_;
4831
4832 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4833};
4834
Vladimir Markofcb503c2016-05-18 12:48:17 +01004835class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004836 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004837 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4838 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004839
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004840 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004841 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004842 return true;
4843 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004844
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004845 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004846
4847 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) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004851 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004852 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004853 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4854 LOG(FATAL) << DebugName() << " is not defined for float values";
4855 UNREACHABLE();
4856 }
4857 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4858 LOG(FATAL) << DebugName() << " is not defined for double values";
4859 UNREACHABLE();
4860 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004861
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004862 DECLARE_INSTRUCTION(Not);
4863
4864 private:
4865 DISALLOW_COPY_AND_ASSIGN(HNot);
4866};
4867
Vladimir Markofcb503c2016-05-18 12:48:17 +01004868class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004869 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004870 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4871 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004872
4873 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004874 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004875 return true;
4876 }
4877
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004878 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004879 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004880 return !x;
4881 }
4882
Roland Levillain9867bc72015-08-05 10:21:34 +01004883 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004884 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004885 }
4886 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4887 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004888 UNREACHABLE();
4889 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004890 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4891 LOG(FATAL) << DebugName() << " is not defined for float values";
4892 UNREACHABLE();
4893 }
4894 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4895 LOG(FATAL) << DebugName() << " is not defined for double values";
4896 UNREACHABLE();
4897 }
David Brazdil66d126e2015-04-03 16:02:44 +01004898
4899 DECLARE_INSTRUCTION(BooleanNot);
4900
4901 private:
4902 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4903};
4904
Vladimir Markofcb503c2016-05-18 12:48:17 +01004905class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004906 public:
4907 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004908 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004909 : HExpression(result_type,
4910 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4911 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004912 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004913 // Invariant: We should never generate a conversion to a Boolean value.
4914 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004915 }
4916
4917 HInstruction* GetInput() const { return InputAt(0); }
4918 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4919 Primitive::Type GetResultType() const { return GetType(); }
4920
4921 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004922 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4923 return true;
4924 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004925
Mark Mendelle82549b2015-05-06 10:55:34 -04004926 // Try to statically evaluate the conversion and return a HConstant
4927 // containing the result. If the input cannot be converted, return nullptr.
4928 HConstant* TryStaticEvaluation() const;
4929
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004930 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4931 Primitive::Type result_type) {
4932 // Some architectures may not require the 'GC' side effects, but at this point
4933 // in the compilation process we do not know what architecture we will
4934 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004935 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4936 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004937 return SideEffects::CanTriggerGC();
4938 }
4939 return SideEffects::None();
4940 }
4941
Roland Levillaindff1f282014-11-05 14:15:05 +00004942 DECLARE_INSTRUCTION(TypeConversion);
4943
4944 private:
4945 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4946};
4947
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004948static constexpr uint32_t kNoRegNumber = -1;
4949
Vladimir Markofcb503c2016-05-18 12:48:17 +01004950class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004951 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004952 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4953 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004954 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004955 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004956 SetRawInputAt(0, value);
4957 }
4958
Calin Juravle10e244f2015-01-26 18:54:32 +00004959 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004960 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004961 return true;
4962 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004963
Calin Juravle10e244f2015-01-26 18:54:32 +00004964 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004965
Calin Juravle10e244f2015-01-26 18:54:32 +00004966 bool CanThrow() const OVERRIDE { return true; }
4967
4968 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004969
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004970
4971 DECLARE_INSTRUCTION(NullCheck);
4972
4973 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004974 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4975};
4976
4977class FieldInfo : public ValueObject {
4978 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004979 FieldInfo(MemberOffset field_offset,
4980 Primitive::Type field_type,
4981 bool is_volatile,
4982 uint32_t index,
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,
4985 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004986 : field_offset_(field_offset),
4987 field_type_(field_type),
4988 is_volatile_(is_volatile),
4989 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004990 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004991 dex_file_(dex_file),
4992 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004993
4994 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004995 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004996 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004997 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004998 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004999 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005000 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005001
5002 private:
5003 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005004 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005005 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005006 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005007 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005008 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005009 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005010};
5011
Vladimir Markofcb503c2016-05-18 12:48:17 +01005012class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005013 public:
5014 HInstanceFieldGet(HInstruction* value,
5015 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005016 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005017 bool is_volatile,
5018 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005019 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005020 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005021 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005022 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005023 : HExpression(field_type,
5024 SideEffects::FieldReadOfType(field_type, is_volatile),
5025 dex_pc),
5026 field_info_(field_offset,
5027 field_type,
5028 is_volatile,
5029 field_idx,
5030 declaring_class_def_index,
5031 dex_file,
5032 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005033 SetRawInputAt(0, value);
5034 }
5035
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005036 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005037
Vladimir Marko372f10e2016-05-17 16:30:10 +01005038 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5039 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005040 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005041 }
5042
Calin Juravle641547a2015-04-21 22:08:51 +01005043 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005044 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005045 }
5046
5047 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005048 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5049 }
5050
Calin Juravle52c48962014-12-16 17:02:57 +00005051 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005052 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005053 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005054 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005055
5056 DECLARE_INSTRUCTION(InstanceFieldGet);
5057
5058 private:
5059 const FieldInfo field_info_;
5060
5061 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5062};
5063
Vladimir Markofcb503c2016-05-18 12:48:17 +01005064class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005065 public:
5066 HInstanceFieldSet(HInstruction* object,
5067 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005068 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005069 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005070 bool is_volatile,
5071 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005072 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005073 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005074 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005075 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005076 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5077 dex_pc),
5078 field_info_(field_offset,
5079 field_type,
5080 is_volatile,
5081 field_idx,
5082 declaring_class_def_index,
5083 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005084 dex_cache) {
5085 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005086 SetRawInputAt(0, object);
5087 SetRawInputAt(1, value);
5088 }
5089
Calin Juravle641547a2015-04-21 22:08:51 +01005090 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005091 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005092 }
5093
Calin Juravle52c48962014-12-16 17:02:57 +00005094 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005095 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005096 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005097 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005098 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005099 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5100 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005101
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005102 DECLARE_INSTRUCTION(InstanceFieldSet);
5103
5104 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005105 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5106 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5107 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5108 "Too many packed fields.");
5109
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005110 const FieldInfo field_info_;
5111
5112 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5113};
5114
Vladimir Markofcb503c2016-05-18 12:48:17 +01005115class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005116 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005117 HArrayGet(HInstruction* array,
5118 HInstruction* index,
5119 Primitive::Type type,
5120 uint32_t dex_pc,
5121 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005122 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005123 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005124 SetRawInputAt(0, array);
5125 SetRawInputAt(1, index);
5126 }
5127
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005128 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005129 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005130 return true;
5131 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005132 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005133 // TODO: We can be smarter here.
5134 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5135 // which generates the implicit null check. There are cases when these can be removed
5136 // to produce better code. If we ever add optimizations to do so we should allow an
5137 // implicit check here (as long as the address falls in the first page).
5138 return false;
5139 }
5140
David Brazdil4833f5a2015-12-16 10:37:39 +00005141 bool IsEquivalentOf(HArrayGet* other) const {
5142 bool result = (GetDexPc() == other->GetDexPc());
5143 if (kIsDebugBuild && result) {
5144 DCHECK_EQ(GetBlock(), other->GetBlock());
5145 DCHECK_EQ(GetArray(), other->GetArray());
5146 DCHECK_EQ(GetIndex(), other->GetIndex());
5147 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005148 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005149 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005150 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5151 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005152 }
5153 }
5154 return result;
5155 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005156
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005157 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5158
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005159 HInstruction* GetArray() const { return InputAt(0); }
5160 HInstruction* GetIndex() const { return InputAt(1); }
5161
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005162 DECLARE_INSTRUCTION(ArrayGet);
5163
5164 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005165 // We treat a String as an array, creating the HArrayGet from String.charAt()
5166 // intrinsic in the instruction simplifier. We can always determine whether
5167 // a particular HArrayGet is actually a String.charAt() by looking at the type
5168 // of the input but that requires holding the mutator lock, so we prefer to use
5169 // a flag, so that code generators don't need to do the locking.
5170 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5171 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5172 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5173 "Too many packed fields.");
5174
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005175 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5176};
5177
Vladimir Markofcb503c2016-05-18 12:48:17 +01005178class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005179 public:
5180 HArraySet(HInstruction* array,
5181 HInstruction* index,
5182 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005183 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005184 uint32_t dex_pc)
5185 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005186 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5187 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5188 SetPackedFlag<kFlagValueCanBeNull>(true);
5189 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005190 SetRawInputAt(0, array);
5191 SetRawInputAt(1, index);
5192 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005193 // Make a best guess now, may be refined during SSA building.
5194 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005195 }
5196
Calin Juravle77520bc2015-01-12 18:45:46 +00005197 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005198 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005199 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005200 }
5201
Mingyao Yang81014cb2015-06-02 03:16:27 -07005202 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005203 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005204
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005205 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005206 // TODO: Same as for ArrayGet.
5207 return false;
5208 }
5209
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005210 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005211 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005212 }
5213
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005214 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005215 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005216 }
5217
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005218 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005219 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005220 }
5221
Vladimir Markoa1de9182016-02-25 11:37:38 +00005222 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5223 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5224 bool StaticTypeOfArrayIsObjectArray() const {
5225 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5226 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005227
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005228 HInstruction* GetArray() const { return InputAt(0); }
5229 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005230 HInstruction* GetValue() const { return InputAt(2); }
5231
5232 Primitive::Type GetComponentType() const {
5233 // The Dex format does not type floating point index operations. Since the
5234 // `expected_component_type_` is set during building and can therefore not
5235 // be correct, we also check what is the value type. If it is a floating
5236 // point type, we must use that type.
5237 Primitive::Type value_type = GetValue()->GetType();
5238 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5239 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005240 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005241 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005242
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005243 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005244 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005245 }
5246
Aart Bik18b36ab2016-04-13 16:41:35 -07005247 void ComputeSideEffects() {
5248 Primitive::Type type = GetComponentType();
5249 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5250 SideEffectsForArchRuntimeCalls(type)));
5251 }
5252
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005253 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5254 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5255 }
5256
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005257 DECLARE_INSTRUCTION(ArraySet);
5258
5259 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005260 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5261 static constexpr size_t kFieldExpectedComponentTypeSize =
5262 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5263 static constexpr size_t kFlagNeedsTypeCheck =
5264 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5265 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005266 // Cached information for the reference_type_info_ so that codegen
5267 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005268 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5269 static constexpr size_t kNumberOfArraySetPackedBits =
5270 kFlagStaticTypeOfArrayIsObjectArray + 1;
5271 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5272 using ExpectedComponentTypeField =
5273 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005274
5275 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5276};
5277
Vladimir Markofcb503c2016-05-18 12:48:17 +01005278class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005279 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005280 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005281 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005282 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005283 // Note that arrays do not change length, so the instruction does not
5284 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005285 SetRawInputAt(0, array);
5286 }
5287
Calin Juravle77520bc2015-01-12 18:45:46 +00005288 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005289 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005290 return true;
5291 }
Calin Juravle641547a2015-04-21 22:08:51 +01005292 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5293 return obj == InputAt(0);
5294 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005295
Vladimir Markodce016e2016-04-28 13:10:02 +01005296 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5297
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005298 DECLARE_INSTRUCTION(ArrayLength);
5299
5300 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005301 // We treat a String as an array, creating the HArrayLength from String.length()
5302 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5303 // determine whether a particular HArrayLength is actually a String.length() by
5304 // looking at the type of the input but that requires holding the mutator lock, so
5305 // we prefer to use a flag, so that code generators don't need to do the locking.
5306 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5307 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5308 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5309 "Too many packed fields.");
5310
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005311 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5312};
5313
Vladimir Markofcb503c2016-05-18 12:48:17 +01005314class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005315 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005316 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5317 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005318 HBoundsCheck(HInstruction* index,
5319 HInstruction* length,
5320 uint32_t dex_pc,
5321 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5322 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5323 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005324 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005325 SetRawInputAt(0, index);
5326 SetRawInputAt(1, length);
5327 }
5328
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005329 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005330 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005331 return true;
5332 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005333
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005334 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005335
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005336 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005337
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005338 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5339 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5340
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005341 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005342
5343 DECLARE_INSTRUCTION(BoundsCheck);
5344
5345 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005346 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5347 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5348 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5349 // so we need to create an HEnvironment which will be translated to an InlineInfo
5350 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5351 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5352 // from the invoke as we don't want to look again at the dex bytecode.
5353 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5354
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005355 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5356};
5357
Vladimir Markofcb503c2016-05-18 12:48:17 +01005358class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005359 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005360 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005361 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005362
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005363 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005364 return true;
5365 }
5366
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005367 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5368 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005369
5370 DECLARE_INSTRUCTION(SuspendCheck);
5371
5372 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005373 // Only used for code generation, in order to share the same slow path between back edges
5374 // of a same loop.
5375 SlowPathCode* slow_path_;
5376
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005377 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5378};
5379
David Srbecky0cf44932015-12-09 14:09:59 +00005380// Pseudo-instruction which provides the native debugger with mapping information.
5381// It ensures that we can generate line number and local variables at this point.
5382class HNativeDebugInfo : public HTemplateInstruction<0> {
5383 public:
5384 explicit HNativeDebugInfo(uint32_t dex_pc)
5385 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5386
5387 bool NeedsEnvironment() const OVERRIDE {
5388 return true;
5389 }
5390
5391 DECLARE_INSTRUCTION(NativeDebugInfo);
5392
5393 private:
5394 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5395};
5396
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005397/**
5398 * Instruction to load a Class object.
5399 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005400class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005401 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005402 // Determines how to load the Class.
5403 enum class LoadKind {
5404 // Use the Class* from the method's own ArtMethod*.
5405 kReferrersClass,
5406
5407 // Use boot image Class* address that will be known at link time.
5408 // Used for boot image classes referenced by boot image code in non-PIC mode.
5409 kBootImageLinkTimeAddress,
5410
5411 // Use PC-relative boot image Class* address that will be known at link time.
5412 // Used for boot image classes referenced by boot image code in PIC mode.
5413 kBootImageLinkTimePcRelative,
5414
5415 // Use a known boot image Class* address, embedded in the code by the codegen.
5416 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5417 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5418 // GetIncludePatchInformation().
5419 kBootImageAddress,
5420
5421 // Load from the resolved types array at an absolute address.
5422 // Used for classes outside the boot image referenced by JIT-compiled code.
5423 kDexCacheAddress,
5424
5425 // Load from resolved types array in the dex cache using a PC-relative load.
5426 // Used for classes outside boot image when we know that we can access
5427 // the dex cache arrays using a PC-relative load.
5428 kDexCachePcRelative,
5429
5430 // Load from resolved types array accessed through the class loaded from
5431 // the compiled method's own ArtMethod*. This is the default access type when
5432 // all other types are unavailable.
5433 kDexCacheViaMethod,
5434
5435 kLast = kDexCacheViaMethod
5436 };
5437
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005438 HLoadClass(HCurrentMethod* current_method,
5439 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005440 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005441 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005442 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005443 bool needs_access_check,
5444 bool is_in_dex_cache)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005445 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5446 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005447 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005448 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005449 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005450 // Referrers class should not need access check. We never inline unverified
5451 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005452 DCHECK(!is_referrers_class || !needs_access_check);
5453
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005454 SetPackedField<LoadKindField>(
5455 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005456 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5457 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5458 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005459 }
5460
5461 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5462 DCHECK(HasAddress(load_kind));
5463 load_data_.address = address;
5464 SetLoadKindInternal(load_kind);
5465 }
5466
5467 void SetLoadKindWithTypeReference(LoadKind load_kind,
5468 const DexFile& dex_file,
5469 uint32_t type_index) {
5470 DCHECK(HasTypeReference(load_kind));
5471 DCHECK(IsSameDexFile(dex_file_, dex_file));
5472 DCHECK_EQ(type_index_, type_index);
5473 SetLoadKindInternal(load_kind);
5474 }
5475
5476 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5477 const DexFile& dex_file,
5478 uint32_t element_index) {
5479 DCHECK(HasDexCacheReference(load_kind));
5480 DCHECK(IsSameDexFile(dex_file_, dex_file));
5481 load_data_.dex_cache_element_index = element_index;
5482 SetLoadKindInternal(load_kind);
5483 }
5484
5485 LoadKind GetLoadKind() const {
5486 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005487 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005488
5489 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005490
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005491 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005492
5493 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5494
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005495 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005496
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005497 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005498 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005499 }
5500
Calin Juravle0ba218d2015-05-19 18:46:01 +01005501 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005502 // The entrypoint the code generator is going to call does not do
5503 // clinit of the class.
5504 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005505 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005506 }
5507
5508 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005509 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005510 (!IsReferrersClass() && !IsInDexCache()) ||
5511 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005512 }
5513
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005514
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005515 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005516 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005517 }
5518
Calin Juravleacf735c2015-02-12 15:25:22 +00005519 ReferenceTypeInfo GetLoadedClassRTI() {
5520 return loaded_class_rti_;
5521 }
5522
5523 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5524 // Make sure we only set exact types (the loaded class should never be merged).
5525 DCHECK(rti.IsExact());
5526 loaded_class_rti_ = rti;
5527 }
5528
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005529 uint32_t GetTypeIndex() const { return type_index_; }
5530 const DexFile& GetDexFile() const { return dex_file_; }
5531
5532 uint32_t GetDexCacheElementOffset() const;
5533
5534 uint64_t GetAddress() const {
5535 DCHECK(HasAddress(GetLoadKind()));
5536 return load_data_.address;
5537 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005538
Vladimir Markoa1de9182016-02-25 11:37:38 +00005539 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005540
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005541 static SideEffects SideEffectsForArchRuntimeCalls() {
5542 return SideEffects::CanTriggerGC();
5543 }
5544
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005545 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005546 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5547 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5548 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005549
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005550 void MarkInDexCache() {
5551 SetPackedFlag<kFlagIsInDexCache>(true);
5552 DCHECK(!NeedsEnvironment());
5553 RemoveEnvironment();
5554 SetSideEffects(SideEffects::None());
5555 }
5556
5557 void AddSpecialInput(HInstruction* special_input);
5558
5559 using HInstruction::GetInputRecords; // Keep the const version visible.
5560 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5561 return ArrayRef<HUserRecord<HInstruction*>>(
5562 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5563 }
5564
5565 Primitive::Type GetType() const OVERRIDE {
5566 return Primitive::kPrimNot;
5567 }
5568
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005569 DECLARE_INSTRUCTION(LoadClass);
5570
5571 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005572 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005573 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005574 // Whether this instruction must generate the initialization check.
5575 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005576 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005577 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5578 static constexpr size_t kFieldLoadKindSize =
5579 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5580 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005581 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005582 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5583
5584 static bool HasTypeReference(LoadKind load_kind) {
5585 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5586 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5587 load_kind == LoadKind::kDexCacheViaMethod ||
5588 load_kind == LoadKind::kReferrersClass;
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
5601 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5602 // For other load kinds it's empty or possibly some architecture-specific instruction
5603 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5604 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005605
5606 const uint16_t type_index_;
5607 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005608
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005609 union {
5610 uint32_t dex_cache_element_index; // Only for dex cache reference.
5611 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5612 } load_data_;
5613
Calin Juravleacf735c2015-02-12 15:25:22 +00005614 ReferenceTypeInfo loaded_class_rti_;
5615
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005616 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5617};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005618std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5619
5620// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5621inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5622 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5623 return load_data_.dex_cache_element_index;
5624}
5625
5626// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5627inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
5628 // The special input is used for PC-relative loads on some architectures.
5629 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5630 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
5631 DCHECK(special_input_.GetInstruction() == nullptr);
5632 special_input_ = HUserRecord<HInstruction*>(special_input);
5633 special_input->AddUseAt(this, 0);
5634}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005635
Vladimir Marko372f10e2016-05-17 16:30:10 +01005636class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005637 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005638 // Determines how to load the String.
5639 enum class LoadKind {
5640 // Use boot image String* address that will be known at link time.
5641 // Used for boot image strings referenced by boot image code in non-PIC mode.
5642 kBootImageLinkTimeAddress,
5643
5644 // Use PC-relative boot image String* address that will be known at link time.
5645 // Used for boot image strings referenced by boot image code in PIC mode.
5646 kBootImageLinkTimePcRelative,
5647
5648 // Use a known boot image String* address, embedded in the code by the codegen.
5649 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5650 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5651 // GetIncludePatchInformation().
5652 kBootImageAddress,
5653
5654 // Load from the resolved strings array at an absolute address.
5655 // Used for strings outside the boot image referenced by JIT-compiled code.
5656 kDexCacheAddress,
5657
5658 // Load from resolved strings array in the dex cache using a PC-relative load.
5659 // Used for strings outside boot image when we know that we can access
5660 // the dex cache arrays using a PC-relative load.
5661 kDexCachePcRelative,
5662
5663 // Load from resolved strings array accessed through the class loaded from
5664 // the compiled method's own ArtMethod*. This is the default access type when
5665 // all other types are unavailable.
5666 kDexCacheViaMethod,
5667
5668 kLast = kDexCacheViaMethod
5669 };
5670
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005671 HLoadString(HCurrentMethod* current_method,
5672 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005673 const DexFile& dex_file,
5674 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005675 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5676 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005677 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005678 SetPackedFlag<kFlagIsInDexCache>(false);
5679 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5680 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005681 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005682
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005683 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5684 DCHECK(HasAddress(load_kind));
5685 load_data_.address = address;
5686 SetLoadKindInternal(load_kind);
5687 }
5688
5689 void SetLoadKindWithStringReference(LoadKind load_kind,
5690 const DexFile& dex_file,
5691 uint32_t string_index) {
5692 DCHECK(HasStringReference(load_kind));
5693 load_data_.ref.dex_file = &dex_file;
5694 string_index_ = string_index;
5695 SetLoadKindInternal(load_kind);
5696 }
5697
5698 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5699 const DexFile& dex_file,
5700 uint32_t element_index) {
5701 DCHECK(HasDexCacheReference(load_kind));
5702 load_data_.ref.dex_file = &dex_file;
5703 load_data_.ref.dex_cache_element_index = element_index;
5704 SetLoadKindInternal(load_kind);
5705 }
5706
5707 LoadKind GetLoadKind() const {
5708 return GetPackedField<LoadKindField>();
5709 }
5710
5711 const DexFile& GetDexFile() const;
5712
5713 uint32_t GetStringIndex() const {
5714 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5715 return string_index_;
5716 }
5717
5718 uint32_t GetDexCacheElementOffset() const;
5719
5720 uint64_t GetAddress() const {
5721 DCHECK(HasAddress(GetLoadKind()));
5722 return load_data_.address;
5723 }
5724
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005725 bool CanBeMoved() const OVERRIDE { return true; }
5726
Vladimir Marko372f10e2016-05-17 16:30:10 +01005727 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005728
5729 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5730
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005731 // Will call the runtime if we need to load the string through
5732 // the dex cache and the string is not guaranteed to be there yet.
5733 bool NeedsEnvironment() const OVERRIDE {
5734 LoadKind load_kind = GetLoadKind();
5735 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5736 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5737 load_kind == LoadKind::kBootImageAddress) {
5738 return false;
5739 }
5740 return !IsInDexCache();
5741 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005742
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005743 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5744 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5745 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005746
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005747 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005748 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005749
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005750 static SideEffects SideEffectsForArchRuntimeCalls() {
5751 return SideEffects::CanTriggerGC();
5752 }
5753
Vladimir Markoa1de9182016-02-25 11:37:38 +00005754 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5755
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005756 void MarkInDexCache() {
5757 SetPackedFlag<kFlagIsInDexCache>(true);
5758 DCHECK(!NeedsEnvironment());
5759 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005760 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005761 }
5762
Vladimir Marko372f10e2016-05-17 16:30:10 +01005763 void AddSpecialInput(HInstruction* special_input);
5764
5765 using HInstruction::GetInputRecords; // Keep the const version visible.
5766 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5767 return ArrayRef<HUserRecord<HInstruction*>>(
5768 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005769 }
5770
Vladimir Marko372f10e2016-05-17 16:30:10 +01005771 Primitive::Type GetType() const OVERRIDE {
5772 return Primitive::kPrimNot;
5773 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005774
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005775 DECLARE_INSTRUCTION(LoadString);
5776
5777 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005778 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005779 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5780 static constexpr size_t kFieldLoadKindSize =
5781 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5782 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005783 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005784 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005785
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005786 static bool HasStringReference(LoadKind load_kind) {
5787 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5788 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5789 load_kind == LoadKind::kDexCacheViaMethod;
5790 }
5791
5792 static bool HasAddress(LoadKind load_kind) {
5793 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5794 }
5795
5796 static bool HasDexCacheReference(LoadKind load_kind) {
5797 return load_kind == LoadKind::kDexCachePcRelative;
5798 }
5799
5800 void SetLoadKindInternal(LoadKind load_kind);
5801
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005802 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5803 // For other load kinds it's empty or possibly some architecture-specific instruction
5804 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005805 HUserRecord<HInstruction*> special_input_;
5806
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005807 // String index serves also as the hash code and it's also needed for slow-paths,
5808 // so it must not be overwritten with other load data.
5809 uint32_t string_index_;
5810
5811 union {
5812 struct {
5813 const DexFile* dex_file; // For string reference and dex cache reference.
5814 uint32_t dex_cache_element_index; // Only for dex cache reference.
5815 } ref;
5816 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5817 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005818
5819 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5820};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005821std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5822
5823// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5824inline const DexFile& HLoadString::GetDexFile() const {
5825 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5826 << GetLoadKind();
5827 return *load_data_.ref.dex_file;
5828}
5829
5830// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5831inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5832 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5833 return load_data_.ref.dex_cache_element_index;
5834}
5835
5836// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5837inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5838 // The special input is used for PC-relative loads on some architectures.
5839 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5840 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005841 // HLoadString::GetInputRecords() returns an empty array at this point,
5842 // so use the GetInputRecords() from the base class to set the input record.
5843 DCHECK(special_input_.GetInstruction() == nullptr);
5844 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005845 special_input->AddUseAt(this, 0);
5846}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005847
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005848/**
5849 * Performs an initialization check on its Class object input.
5850 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005851class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005852 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005853 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005854 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005855 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005856 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5857 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005858 SetRawInputAt(0, constant);
5859 }
5860
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005861 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005862 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005863 return true;
5864 }
5865
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005866 bool NeedsEnvironment() const OVERRIDE {
5867 // May call runtime to initialize the class.
5868 return true;
5869 }
5870
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005871 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005872
5873 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5874
5875 DECLARE_INSTRUCTION(ClinitCheck);
5876
5877 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005878 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5879};
5880
Vladimir Markofcb503c2016-05-18 12:48:17 +01005881class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005882 public:
5883 HStaticFieldGet(HInstruction* cls,
5884 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005885 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005886 bool is_volatile,
5887 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005888 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005889 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005890 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005891 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005892 : HExpression(field_type,
5893 SideEffects::FieldReadOfType(field_type, is_volatile),
5894 dex_pc),
5895 field_info_(field_offset,
5896 field_type,
5897 is_volatile,
5898 field_idx,
5899 declaring_class_def_index,
5900 dex_file,
5901 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005902 SetRawInputAt(0, cls);
5903 }
5904
Calin Juravle52c48962014-12-16 17:02:57 +00005905
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005906 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005907
Vladimir Marko372f10e2016-05-17 16:30:10 +01005908 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5909 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005910 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005911 }
5912
5913 size_t ComputeHashCode() const OVERRIDE {
5914 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5915 }
5916
Calin Juravle52c48962014-12-16 17:02:57 +00005917 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005918 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5919 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005920 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005921
5922 DECLARE_INSTRUCTION(StaticFieldGet);
5923
5924 private:
5925 const FieldInfo field_info_;
5926
5927 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5928};
5929
Vladimir Markofcb503c2016-05-18 12:48:17 +01005930class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005931 public:
5932 HStaticFieldSet(HInstruction* cls,
5933 HInstruction* value,
5934 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005935 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005936 bool is_volatile,
5937 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005938 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005939 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005940 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005941 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005942 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5943 dex_pc),
5944 field_info_(field_offset,
5945 field_type,
5946 is_volatile,
5947 field_idx,
5948 declaring_class_def_index,
5949 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005950 dex_cache) {
5951 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005952 SetRawInputAt(0, cls);
5953 SetRawInputAt(1, value);
5954 }
5955
Calin Juravle52c48962014-12-16 17:02:57 +00005956 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005957 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5958 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005959 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005960
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005961 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005962 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5963 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005964
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005965 DECLARE_INSTRUCTION(StaticFieldSet);
5966
5967 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005968 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5969 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5970 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5971 "Too many packed fields.");
5972
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005973 const FieldInfo field_info_;
5974
5975 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5976};
5977
Vladimir Markofcb503c2016-05-18 12:48:17 +01005978class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005979 public:
5980 HUnresolvedInstanceFieldGet(HInstruction* obj,
5981 Primitive::Type field_type,
5982 uint32_t field_index,
5983 uint32_t dex_pc)
5984 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5985 field_index_(field_index) {
5986 SetRawInputAt(0, obj);
5987 }
5988
5989 bool NeedsEnvironment() const OVERRIDE { return true; }
5990 bool CanThrow() const OVERRIDE { return true; }
5991
5992 Primitive::Type GetFieldType() const { return GetType(); }
5993 uint32_t GetFieldIndex() const { return field_index_; }
5994
5995 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5996
5997 private:
5998 const uint32_t field_index_;
5999
6000 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6001};
6002
Vladimir Markofcb503c2016-05-18 12:48:17 +01006003class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006004 public:
6005 HUnresolvedInstanceFieldSet(HInstruction* obj,
6006 HInstruction* value,
6007 Primitive::Type field_type,
6008 uint32_t field_index,
6009 uint32_t dex_pc)
6010 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006011 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006012 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006013 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006014 SetRawInputAt(0, obj);
6015 SetRawInputAt(1, value);
6016 }
6017
6018 bool NeedsEnvironment() const OVERRIDE { return true; }
6019 bool CanThrow() const OVERRIDE { return true; }
6020
Vladimir Markoa1de9182016-02-25 11:37:38 +00006021 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006022 uint32_t GetFieldIndex() const { return field_index_; }
6023
6024 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6025
6026 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006027 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6028 static constexpr size_t kFieldFieldTypeSize =
6029 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6030 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6031 kFieldFieldType + kFieldFieldTypeSize;
6032 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6033 "Too many packed fields.");
6034 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6035
Calin Juravlee460d1d2015-09-29 04:52:17 +01006036 const uint32_t field_index_;
6037
6038 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6039};
6040
Vladimir Markofcb503c2016-05-18 12:48:17 +01006041class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006042 public:
6043 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6044 uint32_t field_index,
6045 uint32_t dex_pc)
6046 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6047 field_index_(field_index) {
6048 }
6049
6050 bool NeedsEnvironment() const OVERRIDE { return true; }
6051 bool CanThrow() const OVERRIDE { return true; }
6052
6053 Primitive::Type GetFieldType() const { return GetType(); }
6054 uint32_t GetFieldIndex() const { return field_index_; }
6055
6056 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6057
6058 private:
6059 const uint32_t field_index_;
6060
6061 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6062};
6063
Vladimir Markofcb503c2016-05-18 12:48:17 +01006064class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006065 public:
6066 HUnresolvedStaticFieldSet(HInstruction* value,
6067 Primitive::Type field_type,
6068 uint32_t field_index,
6069 uint32_t dex_pc)
6070 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006071 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006072 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006073 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006074 SetRawInputAt(0, value);
6075 }
6076
6077 bool NeedsEnvironment() const OVERRIDE { return true; }
6078 bool CanThrow() const OVERRIDE { return true; }
6079
Vladimir Markoa1de9182016-02-25 11:37:38 +00006080 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006081 uint32_t GetFieldIndex() const { return field_index_; }
6082
6083 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6084
6085 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006086 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6087 static constexpr size_t kFieldFieldTypeSize =
6088 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6089 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6090 kFieldFieldType + kFieldFieldTypeSize;
6091 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6092 "Too many packed fields.");
6093 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6094
Calin Juravlee460d1d2015-09-29 04:52:17 +01006095 const uint32_t field_index_;
6096
6097 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6098};
6099
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006100// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006101class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006102 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006103 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6104 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006105
David Brazdilbbd733e2015-08-18 17:48:17 +01006106 bool CanBeNull() const OVERRIDE { return false; }
6107
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006108 DECLARE_INSTRUCTION(LoadException);
6109
6110 private:
6111 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6112};
6113
David Brazdilcb1c0552015-08-04 16:22:25 +01006114// Implicit part of move-exception which clears thread-local exception storage.
6115// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006116class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006117 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006118 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6119 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006120
6121 DECLARE_INSTRUCTION(ClearException);
6122
6123 private:
6124 DISALLOW_COPY_AND_ASSIGN(HClearException);
6125};
6126
Vladimir Markofcb503c2016-05-18 12:48:17 +01006127class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006128 public:
6129 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006130 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006131 SetRawInputAt(0, exception);
6132 }
6133
6134 bool IsControlFlow() const OVERRIDE { return true; }
6135
6136 bool NeedsEnvironment() const OVERRIDE { return true; }
6137
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006138 bool CanThrow() const OVERRIDE { return true; }
6139
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006140
6141 DECLARE_INSTRUCTION(Throw);
6142
6143 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006144 DISALLOW_COPY_AND_ASSIGN(HThrow);
6145};
6146
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006147/**
6148 * Implementation strategies for the code generator of a HInstanceOf
6149 * or `HCheckCast`.
6150 */
6151enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006152 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006153 kExactCheck, // Can do a single class compare.
6154 kClassHierarchyCheck, // Can just walk the super class chain.
6155 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6156 kInterfaceCheck, // No optimization yet when checking against an interface.
6157 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006158 kArrayCheck, // No optimization yet when checking against a generic array.
6159 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006160};
6161
Roland Levillain86503782016-02-11 19:07:30 +00006162std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6163
Vladimir Markofcb503c2016-05-18 12:48:17 +01006164class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006165 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006166 HInstanceOf(HInstruction* object,
6167 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006168 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006169 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006170 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006171 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006172 dex_pc) {
6173 SetPackedField<TypeCheckKindField>(check_kind);
6174 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006175 SetRawInputAt(0, object);
6176 SetRawInputAt(1, constant);
6177 }
6178
6179 bool CanBeMoved() const OVERRIDE { return true; }
6180
Vladimir Marko372f10e2016-05-17 16:30:10 +01006181 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006182 return true;
6183 }
6184
6185 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006186 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006187 }
6188
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006189 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006190 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6191 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6192 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6193 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006194
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006195 static bool CanCallRuntime(TypeCheckKind check_kind) {
6196 // Mips currently does runtime calls for any other checks.
6197 return check_kind != TypeCheckKind::kExactCheck;
6198 }
6199
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006200 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006201 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006202 }
6203
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006204 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006205
6206 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006207 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6208 static constexpr size_t kFieldTypeCheckKindSize =
6209 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6210 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6211 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6212 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6213 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006214
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006215 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6216};
6217
Vladimir Markofcb503c2016-05-18 12:48:17 +01006218class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006219 public:
David Brazdilf5552582015-12-27 13:36:12 +00006220 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006221 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006222 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6223 SetPackedFlag<kFlagUpperCanBeNull>(true);
6224 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006225 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006226 SetRawInputAt(0, input);
6227 }
6228
David Brazdilf5552582015-12-27 13:36:12 +00006229 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006230 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006231 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006232 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006233
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006234 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006235 DCHECK(GetUpperCanBeNull() || !can_be_null);
6236 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006237 }
6238
Vladimir Markoa1de9182016-02-25 11:37:38 +00006239 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006240
Calin Juravleb1498f62015-02-16 13:13:29 +00006241 DECLARE_INSTRUCTION(BoundType);
6242
6243 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006244 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6245 // is false then CanBeNull() cannot be true).
6246 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6247 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6248 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6249 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6250
Calin Juravleb1498f62015-02-16 13:13:29 +00006251 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006252 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6253 // It is used to bound the type in cases like:
6254 // if (x instanceof ClassX) {
6255 // // uper_bound_ will be ClassX
6256 // }
David Brazdilf5552582015-12-27 13:36:12 +00006257 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006258
6259 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6260};
6261
Vladimir Markofcb503c2016-05-18 12:48:17 +01006262class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006263 public:
6264 HCheckCast(HInstruction* object,
6265 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006266 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006267 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006268 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6269 SetPackedField<TypeCheckKindField>(check_kind);
6270 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006271 SetRawInputAt(0, object);
6272 SetRawInputAt(1, constant);
6273 }
6274
6275 bool CanBeMoved() const OVERRIDE { return true; }
6276
Vladimir Marko372f10e2016-05-17 16:30:10 +01006277 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006278 return true;
6279 }
6280
6281 bool NeedsEnvironment() const OVERRIDE {
6282 // Instruction may throw a CheckCastError.
6283 return true;
6284 }
6285
6286 bool CanThrow() const OVERRIDE { return true; }
6287
Vladimir Markoa1de9182016-02-25 11:37:38 +00006288 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6289 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6290 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6291 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006292
6293 DECLARE_INSTRUCTION(CheckCast);
6294
6295 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006296 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6297 static constexpr size_t kFieldTypeCheckKindSize =
6298 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6299 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6300 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6301 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6302 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006303
6304 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006305};
6306
Andreas Gampe26de38b2016-07-27 17:53:11 -07006307/**
6308 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6309 * @details We define the combined barrier types that are actually required
6310 * by the Java Memory Model, rather than using exactly the terminology from
6311 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6312 * primitives. Note that the JSR-133 cookbook generally does not deal with
6313 * store atomicity issues, and the recipes there are not always entirely sufficient.
6314 * The current recipe is as follows:
6315 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6316 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6317 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6318 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6319 * class has final fields.
6320 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6321 * store-to-memory instructions. Only generated together with non-temporal stores.
6322 */
6323enum MemBarrierKind {
6324 kAnyStore,
6325 kLoadAny,
6326 kStoreStore,
6327 kAnyAny,
6328 kNTStoreStore,
6329 kLastBarrierKind = kNTStoreStore
6330};
6331std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6332
Vladimir Markofcb503c2016-05-18 12:48:17 +01006333class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006334 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006335 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006336 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006337 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6338 SetPackedField<BarrierKindField>(barrier_kind);
6339 }
Calin Juravle27df7582015-04-17 19:12:31 +01006340
Vladimir Markoa1de9182016-02-25 11:37:38 +00006341 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006342
6343 DECLARE_INSTRUCTION(MemoryBarrier);
6344
6345 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006346 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6347 static constexpr size_t kFieldBarrierKindSize =
6348 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6349 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6350 kFieldBarrierKind + kFieldBarrierKindSize;
6351 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6352 "Too many packed fields.");
6353 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006354
6355 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6356};
6357
Vladimir Markofcb503c2016-05-18 12:48:17 +01006358class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006359 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006360 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006361 kEnter,
6362 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006363 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006364 };
6365
6366 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006367 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006368 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6369 dex_pc) {
6370 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006371 SetRawInputAt(0, object);
6372 }
6373
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006374 // Instruction may go into runtime, so we need an environment.
6375 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006376
6377 bool CanThrow() const OVERRIDE {
6378 // Verifier guarantees that monitor-exit cannot throw.
6379 // This is important because it allows the HGraphBuilder to remove
6380 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6381 return IsEnter();
6382 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006383
Vladimir Markoa1de9182016-02-25 11:37:38 +00006384 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6385 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006386
6387 DECLARE_INSTRUCTION(MonitorOperation);
6388
Calin Juravle52c48962014-12-16 17:02:57 +00006389 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006390 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6391 static constexpr size_t kFieldOperationKindSize =
6392 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6393 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6394 kFieldOperationKind + kFieldOperationKindSize;
6395 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6396 "Too many packed fields.");
6397 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006398
6399 private:
6400 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6401};
6402
Vladimir Markofcb503c2016-05-18 12:48:17 +01006403class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006404 public:
6405 HSelect(HInstruction* condition,
6406 HInstruction* true_value,
6407 HInstruction* false_value,
6408 uint32_t dex_pc)
6409 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6410 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6411
6412 // First input must be `true_value` or `false_value` to allow codegens to
6413 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6414 // that architectures which implement HSelect as a conditional move also
6415 // will not need to invert the condition.
6416 SetRawInputAt(0, false_value);
6417 SetRawInputAt(1, true_value);
6418 SetRawInputAt(2, condition);
6419 }
6420
6421 HInstruction* GetFalseValue() const { return InputAt(0); }
6422 HInstruction* GetTrueValue() const { return InputAt(1); }
6423 HInstruction* GetCondition() const { return InputAt(2); }
6424
6425 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006426 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6427 return true;
6428 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006429
6430 bool CanBeNull() const OVERRIDE {
6431 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6432 }
6433
6434 DECLARE_INSTRUCTION(Select);
6435
6436 private:
6437 DISALLOW_COPY_AND_ASSIGN(HSelect);
6438};
6439
Vladimir Markof9f64412015-09-02 14:05:49 +01006440class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006441 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006442 MoveOperands(Location source,
6443 Location destination,
6444 Primitive::Type type,
6445 HInstruction* instruction)
6446 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006447
6448 Location GetSource() const { return source_; }
6449 Location GetDestination() const { return destination_; }
6450
6451 void SetSource(Location value) { source_ = value; }
6452 void SetDestination(Location value) { destination_ = value; }
6453
6454 // The parallel move resolver marks moves as "in-progress" by clearing the
6455 // destination (but not the source).
6456 Location MarkPending() {
6457 DCHECK(!IsPending());
6458 Location dest = destination_;
6459 destination_ = Location::NoLocation();
6460 return dest;
6461 }
6462
6463 void ClearPending(Location dest) {
6464 DCHECK(IsPending());
6465 destination_ = dest;
6466 }
6467
6468 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006469 DCHECK(source_.IsValid() || destination_.IsInvalid());
6470 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006471 }
6472
6473 // True if this blocks a move from the given location.
6474 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006475 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006476 }
6477
6478 // A move is redundant if it's been eliminated, if its source and
6479 // destination are the same, or if its destination is unneeded.
6480 bool IsRedundant() const {
6481 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6482 }
6483
6484 // We clear both operands to indicate move that's been eliminated.
6485 void Eliminate() {
6486 source_ = destination_ = Location::NoLocation();
6487 }
6488
6489 bool IsEliminated() const {
6490 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6491 return source_.IsInvalid();
6492 }
6493
Alexey Frunze4dda3372015-06-01 18:31:49 -07006494 Primitive::Type GetType() const { return type_; }
6495
Nicolas Geoffray90218252015-04-15 11:56:51 +01006496 bool Is64BitMove() const {
6497 return Primitive::Is64BitType(type_);
6498 }
6499
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006500 HInstruction* GetInstruction() const { return instruction_; }
6501
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006502 private:
6503 Location source_;
6504 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006505 // The type this move is for.
6506 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006507 // The instruction this move is assocatied with. Null when this move is
6508 // for moving an input in the expected locations of user (including a phi user).
6509 // This is only used in debug mode, to ensure we do not connect interval siblings
6510 // in the same parallel move.
6511 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006512};
6513
Roland Levillainc9285912015-12-18 10:38:42 +00006514std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6515
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006516static constexpr size_t kDefaultNumberOfMoves = 4;
6517
Vladimir Markofcb503c2016-05-18 12:48:17 +01006518class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006519 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006520 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006521 : HTemplateInstruction(SideEffects::None(), dex_pc),
6522 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6523 moves_.reserve(kDefaultNumberOfMoves);
6524 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006525
Nicolas Geoffray90218252015-04-15 11:56:51 +01006526 void AddMove(Location source,
6527 Location destination,
6528 Primitive::Type type,
6529 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006530 DCHECK(source.IsValid());
6531 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006532 if (kIsDebugBuild) {
6533 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006534 for (const MoveOperands& move : moves_) {
6535 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006536 // Special case the situation where the move is for the spill slot
6537 // of the instruction.
6538 if ((GetPrevious() == instruction)
6539 || ((GetPrevious() == nullptr)
6540 && instruction->IsPhi()
6541 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006542 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006543 << "Doing parallel moves for the same instruction.";
6544 } else {
6545 DCHECK(false) << "Doing parallel moves for the same instruction.";
6546 }
6547 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006548 }
6549 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006550 for (const MoveOperands& move : moves_) {
6551 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006552 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006553 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006554 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006555 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006556 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006557 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006558 }
6559
Vladimir Marko225b6462015-09-28 12:17:40 +01006560 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006561 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006562 }
6563
Vladimir Marko225b6462015-09-28 12:17:40 +01006564 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006565
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006566 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006567
6568 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006569 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006570
6571 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6572};
6573
Mark Mendell0616ae02015-04-17 12:49:27 -04006574} // namespace art
6575
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006576#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6577#include "nodes_shared.h"
6578#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006579#ifdef ART_ENABLE_CODEGEN_arm
6580#include "nodes_arm.h"
6581#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006582#ifdef ART_ENABLE_CODEGEN_arm64
6583#include "nodes_arm64.h"
6584#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006585#ifdef ART_ENABLE_CODEGEN_mips
6586#include "nodes_mips.h"
6587#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006588#ifdef ART_ENABLE_CODEGEN_x86
6589#include "nodes_x86.h"
6590#endif
6591
6592namespace art {
6593
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006594class HGraphVisitor : public ValueObject {
6595 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006596 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6597 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006598
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006599 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006600 virtual void VisitBasicBlock(HBasicBlock* block);
6601
Roland Levillain633021e2014-10-01 14:12:25 +01006602 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006603 void VisitInsertionOrder();
6604
Roland Levillain633021e2014-10-01 14:12:25 +01006605 // Visit the graph following dominator tree reverse post-order.
6606 void VisitReversePostOrder();
6607
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006608 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006609
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006610 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006611#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006612 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6613
6614 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6615
6616#undef DECLARE_VISIT_INSTRUCTION
6617
6618 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006619 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006620
6621 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6622};
6623
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006624class HGraphDelegateVisitor : public HGraphVisitor {
6625 public:
6626 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6627 virtual ~HGraphDelegateVisitor() {}
6628
6629 // Visit functions that delegate to to super class.
6630#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006631 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006632
6633 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6634
6635#undef DECLARE_VISIT_INSTRUCTION
6636
6637 private:
6638 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6639};
6640
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006641class HInsertionOrderIterator : public ValueObject {
6642 public:
6643 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6644
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006645 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006646 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006647 void Advance() { ++index_; }
6648
6649 private:
6650 const HGraph& graph_;
6651 size_t index_;
6652
6653 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6654};
6655
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006656class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006657 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006658 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6659 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006660 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006661 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006662
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006663 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6664 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006665 void Advance() { ++index_; }
6666
6667 private:
6668 const HGraph& graph_;
6669 size_t index_;
6670
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006671 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006672};
6673
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006674class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006675 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006676 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006677 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006678 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006679 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006680 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006681
6682 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006683 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006684 void Advance() { --index_; }
6685
6686 private:
6687 const HGraph& graph_;
6688 size_t index_;
6689
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006690 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006691};
6692
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006693class HLinearPostOrderIterator : public ValueObject {
6694 public:
6695 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006696 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006697
6698 bool Done() const { return index_ == 0; }
6699
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006700 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006701
6702 void Advance() {
6703 --index_;
6704 DCHECK_GE(index_, 0U);
6705 }
6706
6707 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006708 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006709 size_t index_;
6710
6711 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6712};
6713
6714class HLinearOrderIterator : public ValueObject {
6715 public:
6716 explicit HLinearOrderIterator(const HGraph& graph)
6717 : order_(graph.GetLinearOrder()), index_(0) {}
6718
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006719 bool Done() const { return index_ == order_.size(); }
6720 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006721 void Advance() { ++index_; }
6722
6723 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006724 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006725 size_t index_;
6726
6727 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6728};
6729
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006730// Iterator over the blocks that art part of the loop. Includes blocks part
6731// of an inner loop. The order in which the blocks are iterated is on their
6732// block id.
6733class HBlocksInLoopIterator : public ValueObject {
6734 public:
6735 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6736 : blocks_in_loop_(info.GetBlocks()),
6737 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6738 index_(0) {
6739 if (!blocks_in_loop_.IsBitSet(index_)) {
6740 Advance();
6741 }
6742 }
6743
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006744 bool Done() const { return index_ == blocks_.size(); }
6745 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006746 void Advance() {
6747 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006748 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006749 if (blocks_in_loop_.IsBitSet(index_)) {
6750 break;
6751 }
6752 }
6753 }
6754
6755 private:
6756 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006757 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006758 size_t index_;
6759
6760 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6761};
6762
Mingyao Yang3584bce2015-05-19 16:01:59 -07006763// Iterator over the blocks that art part of the loop. Includes blocks part
6764// of an inner loop. The order in which the blocks are iterated is reverse
6765// post order.
6766class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6767 public:
6768 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6769 : blocks_in_loop_(info.GetBlocks()),
6770 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6771 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006772 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006773 Advance();
6774 }
6775 }
6776
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006777 bool Done() const { return index_ == blocks_.size(); }
6778 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006779 void Advance() {
6780 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006781 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6782 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006783 break;
6784 }
6785 }
6786 }
6787
6788 private:
6789 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006790 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006791 size_t index_;
6792
6793 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6794};
6795
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006796inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006797 if (constant->IsIntConstant()) {
6798 return constant->AsIntConstant()->GetValue();
6799 } else if (constant->IsLongConstant()) {
6800 return constant->AsLongConstant()->GetValue();
6801 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006802 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006803 return 0;
6804 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006805}
6806
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006807#define INSTRUCTION_TYPE_CHECK(type, super) \
6808 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6809 inline const H##type* HInstruction::As##type() const { \
6810 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6811 } \
6812 inline H##type* HInstruction::As##type() { \
6813 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6814 }
6815
6816 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6817#undef INSTRUCTION_TYPE_CHECK
6818
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006819// Create space in `blocks` for adding `number_of_new_blocks` entries
6820// starting at location `at`. Blocks after `at` are moved accordingly.
6821inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6822 size_t number_of_new_blocks,
6823 size_t after) {
6824 DCHECK_LT(after, blocks->size());
6825 size_t old_size = blocks->size();
6826 size_t new_size = old_size + number_of_new_blocks;
6827 blocks->resize(new_size);
6828 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6829}
6830
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006831} // namespace art
6832
6833#endif // ART_COMPILER_OPTIMIZING_NODES_H_