blob: 8774b9b1a9a5cb38376ab8ad2148ebc2a7f5c866 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010028#include "dex/compiler_enums.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010029#include "dex_file.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000030#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000031#include "handle.h"
32#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000033#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010034#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000035#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000036#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010037#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070038#include "primitive.h"
David Brazdild26a4112015-11-10 11:07:31 +000039#include "utils/array_ref.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010040#include "utils/intrusive_forward_list.h"
Vladimir Marko372f10e2016-05-17 16:30:10 +010041#include "utils/transform_array_ref.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042
43namespace art {
44
David Brazdil1abb4192015-02-17 18:33:36 +000045class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010047class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010049class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000051class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000053class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000054class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000055class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000056class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000057class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010058class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010059class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010060class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010061class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000062class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010063class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000064class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000065
Mathieu Chartier736b5602015-09-02 14:54:11 -070066namespace mirror {
67class DexCache;
68} // namespace mirror
69
Nicolas Geoffray818f2102014-02-18 16:43:35 +000070static const int kDefaultNumberOfBlocks = 8;
71static const int kDefaultNumberOfSuccessors = 2;
72static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010073static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010074static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000075static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000076
Roland Levillain5b5b9312016-03-22 14:57:31 +000077// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
78static constexpr int32_t kMaxIntShiftDistance = 0x1f;
79// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
80static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000081
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010082static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070083static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010084
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010085static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
86
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060087static constexpr uint32_t kNoDexPc = -1;
88
Dave Allison20dfc792014-06-16 20:44:29 -070089enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070090 // All types.
91 kCondEQ, // ==
92 kCondNE, // !=
93 // Signed integers and floating-point numbers.
94 kCondLT, // <
95 kCondLE, // <=
96 kCondGT, // >
97 kCondGE, // >=
98 // Unsigned integers.
99 kCondB, // <
100 kCondBE, // <=
101 kCondA, // >
102 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -0700103};
104
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000105enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000106 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000107 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000108 kAnalysisFailThrowCatchLoop,
109 kAnalysisFailAmbiguousArrayOp,
110 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000111};
112
Vladimir Markof9f64412015-09-02 14:05:49 +0100113class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100114 public:
115 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
116
117 void AddInstruction(HInstruction* instruction);
118 void RemoveInstruction(HInstruction* instruction);
119
David Brazdilc3d743f2015-04-22 13:40:50 +0100120 // Insert `instruction` before/after an existing instruction `cursor`.
121 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
122 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
123
Roland Levillain6b469232014-09-25 10:10:38 +0100124 // Return true if this list contains `instruction`.
125 bool Contains(HInstruction* instruction) const;
126
Roland Levillainccc07a92014-09-16 14:48:16 +0100127 // Return true if `instruction1` is found before `instruction2` in
128 // this instruction list and false otherwise. Abort if none
129 // of these instructions is found.
130 bool FoundBefore(const HInstruction* instruction1,
131 const HInstruction* instruction2) const;
132
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000133 bool IsEmpty() const { return first_instruction_ == nullptr; }
134 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
135
136 // Update the block of all instructions to be `block`.
137 void SetBlockOfInstructions(HBasicBlock* block) const;
138
139 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000140 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000141 void Add(const HInstructionList& instruction_list);
142
David Brazdil2d7352b2015-04-20 14:52:42 +0100143 // Return the number of instructions in the list. This is an expensive operation.
144 size_t CountSize() const;
145
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100146 private:
147 HInstruction* first_instruction_;
148 HInstruction* last_instruction_;
149
150 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000151 friend class HGraph;
152 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100153 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100154 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100155
156 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
157};
158
David Brazdil4833f5a2015-12-16 10:37:39 +0000159class ReferenceTypeInfo : ValueObject {
160 public:
161 typedef Handle<mirror::Class> TypeHandle;
162
Vladimir Markoa1de9182016-02-25 11:37:38 +0000163 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
164
165 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000166 return ReferenceTypeInfo(type_handle, is_exact);
167 }
168
169 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
170
Vladimir Markof39745e2016-01-26 12:16:55 +0000171 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000172 return handle.GetReference() != nullptr;
173 }
174
Vladimir Marko456307a2016-04-19 14:12:13 +0000175 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000176 return IsValidHandle(type_handle_);
177 }
178
179 bool IsExact() const { return is_exact_; }
180
181 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
182 DCHECK(IsValid());
183 return GetTypeHandle()->IsObjectClass();
184 }
185
186 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
187 DCHECK(IsValid());
188 return GetTypeHandle()->IsStringClass();
189 }
190
191 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
192 DCHECK(IsValid());
193 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
194 }
195
196 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
197 DCHECK(IsValid());
198 return GetTypeHandle()->IsInterface();
199 }
200
201 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
202 DCHECK(IsValid());
203 return GetTypeHandle()->IsArrayClass();
204 }
205
206 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
207 DCHECK(IsValid());
208 return GetTypeHandle()->IsPrimitiveArray();
209 }
210
211 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
212 DCHECK(IsValid());
213 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
214 }
215
216 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
217 DCHECK(IsValid());
218 if (!IsExact()) return false;
219 if (!IsArrayClass()) return false;
220 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
221 }
222
223 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
224 DCHECK(IsValid());
225 if (!IsExact()) return false;
226 if (!IsArrayClass()) return false;
227 if (!rti.IsArrayClass()) return false;
228 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
229 rti.GetTypeHandle()->GetComponentType());
230 }
231
232 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
233
234 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
235 DCHECK(IsValid());
236 DCHECK(rti.IsValid());
237 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
238 }
239
240 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
241 DCHECK(IsValid());
242 DCHECK(rti.IsValid());
243 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
244 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
245 }
246
247 // Returns true if the type information provide the same amount of details.
248 // Note that it does not mean that the instructions have the same actual type
249 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000250 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000251 if (!IsValid() && !rti.IsValid()) {
252 // Invalid types are equal.
253 return true;
254 }
255 if (!IsValid() || !rti.IsValid()) {
256 // One is valid, the other not.
257 return false;
258 }
259 return IsExact() == rti.IsExact()
260 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
261 }
262
263 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000264 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
265 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
266 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000267
268 // The class of the object.
269 TypeHandle type_handle_;
270 // Whether or not the type is exact or a superclass of the actual type.
271 // Whether or not we have any information about this type.
272 bool is_exact_;
273};
274
275std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
276
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000277// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100278class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000279 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100280 HGraph(ArenaAllocator* arena,
281 const DexFile& dex_file,
282 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100283 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700284 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100285 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100286 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000287 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100288 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000289 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100290 blocks_(arena->Adapter(kArenaAllocBlockList)),
291 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
292 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700293 entry_block_(nullptr),
294 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100295 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100296 number_of_vregs_(0),
297 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000298 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400299 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000300 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000301 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000302 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000303 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100304 dex_file_(dex_file),
305 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100306 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100307 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100308 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700309 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000310 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100311 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
312 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
313 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
314 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000315 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000316 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
317 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100318 blocks_.reserve(kDefaultNumberOfBlocks);
319 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000320
David Brazdilbadd8262016-02-02 16:28:56 +0000321 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
322 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
323
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000324 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100325 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
326
David Brazdil69ba7b72015-06-23 18:27:30 +0100327 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000328 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100329
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000330 HBasicBlock* GetEntryBlock() const { return entry_block_; }
331 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100332 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000333
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000334 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
335 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000336
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000337 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100338
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100339 void ComputeDominanceInformation();
340 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000341 void ClearLoopInformation();
342 void FindBackEdges(ArenaBitVector* visited);
343 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100344 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100345 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000346
David Brazdil4833f5a2015-12-16 10:37:39 +0000347 // Analyze all natural loops in this graph. Returns a code specifying that it
348 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000349 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000350 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100351
David Brazdilffee3d32015-07-06 11:48:53 +0100352 // Iterate over blocks to compute try block membership. Needs reverse post
353 // order and loop information.
354 void ComputeTryBlockInformation();
355
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000356 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000357 // Returns the instruction to replace the invoke expression or null if the
358 // invoke is for a void method. Note that the caller is responsible for replacing
359 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000360 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000361
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000362 // Update the loop and try membership of `block`, which was spawned from `reference`.
363 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
364 // should be the new back edge.
365 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
366 HBasicBlock* reference,
367 bool replace_if_back_edge);
368
Mingyao Yang3584bce2015-05-19 16:01:59 -0700369 // Need to add a couple of blocks to test if the loop body is entered and
370 // put deoptimization instructions, etc.
371 void TransformLoopHeaderForBCE(HBasicBlock* header);
372
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000373 // Removes `block` from the graph. Assumes `block` has been disconnected from
374 // other blocks and has no instructions or phis.
375 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000376
David Brazdilfc6a86a2015-06-26 10:33:45 +0000377 // Splits the edge between `block` and `successor` while preserving the
378 // indices in the predecessor/successor lists. If there are multiple edges
379 // between the blocks, the lowest indices are used.
380 // Returns the new block which is empty and has the same dex pc as `successor`.
381 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
382
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100383 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
384 void SimplifyLoop(HBasicBlock* header);
385
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000386 int32_t GetNextInstructionId() {
387 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000388 return current_instruction_id_++;
389 }
390
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000391 int32_t GetCurrentInstructionId() const {
392 return current_instruction_id_;
393 }
394
395 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000396 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000397 current_instruction_id_ = id;
398 }
399
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100400 uint16_t GetMaximumNumberOfOutVRegs() const {
401 return maximum_number_of_out_vregs_;
402 }
403
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000404 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
405 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100406 }
407
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100408 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
409 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
410 }
411
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000412 void UpdateTemporariesVRegSlots(size_t slots) {
413 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100414 }
415
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000416 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100417 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000418 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100419 }
420
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100421 void SetNumberOfVRegs(uint16_t number_of_vregs) {
422 number_of_vregs_ = number_of_vregs;
423 }
424
425 uint16_t GetNumberOfVRegs() const {
426 return number_of_vregs_;
427 }
428
429 void SetNumberOfInVRegs(uint16_t value) {
430 number_of_in_vregs_ = value;
431 }
432
David Brazdildee58d62016-04-07 09:54:26 +0000433 uint16_t GetNumberOfInVRegs() const {
434 return number_of_in_vregs_;
435 }
436
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100437 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100438 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100439 return number_of_vregs_ - number_of_in_vregs_;
440 }
441
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100442 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100443 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100444 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100445
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100446 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100447 return linear_order_;
448 }
449
Mark Mendell1152c922015-04-24 17:06:35 -0400450 bool HasBoundsChecks() const {
451 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800452 }
453
Mark Mendell1152c922015-04-24 17:06:35 -0400454 void SetHasBoundsChecks(bool value) {
455 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800456 }
457
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100458 bool ShouldGenerateConstructorBarrier() const {
459 return should_generate_constructor_barrier_;
460 }
461
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000462 bool IsDebuggable() const { return debuggable_; }
463
David Brazdil8d5b8b22015-03-24 10:51:52 +0000464 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000465 // already, it is created and inserted into the graph. This method is only for
466 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600467 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000468
469 // TODO: This is problematic for the consistency of reference type propagation
470 // because it can be created anytime after the pass and thus it will be left
471 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600472 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000473
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600474 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
475 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000476 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600477 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
478 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000479 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600480 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
481 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000482 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600483 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
484 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000485 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000486
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100487 HCurrentMethod* GetCurrentMethod();
488
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100489 const DexFile& GetDexFile() const {
490 return dex_file_;
491 }
492
493 uint32_t GetMethodIdx() const {
494 return method_idx_;
495 }
496
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100497 InvokeType GetInvokeType() const {
498 return invoke_type_;
499 }
500
Mark Mendellc4701932015-04-10 13:18:51 -0400501 InstructionSet GetInstructionSet() const {
502 return instruction_set_;
503 }
504
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000505 bool IsCompilingOsr() const { return osr_; }
506
David Brazdil77a48ae2015-09-15 12:34:04 +0000507 bool HasTryCatch() const { return has_try_catch_; }
508 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100509
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000510 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
511 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
512
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100513 ArtMethod* GetArtMethod() const { return art_method_; }
514 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
515
Mark Mendellf6529172015-11-17 11:16:56 -0500516 // Returns an instruction with the opposite boolean value from 'cond'.
517 // The instruction has been inserted into the graph, either as a constant, or
518 // before cursor.
519 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
520
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000521 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
522
David Brazdil2d7352b2015-04-20 14:52:42 +0100523 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000524 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100525 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000526
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000527 template <class InstructionType, typename ValueType>
528 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600529 ArenaSafeMap<ValueType, InstructionType*>* cache,
530 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000531 // Try to find an existing constant of the given value.
532 InstructionType* constant = nullptr;
533 auto cached_constant = cache->find(value);
534 if (cached_constant != cache->end()) {
535 constant = cached_constant->second;
536 }
537
538 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100539 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000540 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600541 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000542 cache->Overwrite(value, constant);
543 InsertConstant(constant);
544 }
545 return constant;
546 }
547
David Brazdil8d5b8b22015-03-24 10:51:52 +0000548 void InsertConstant(HConstant* instruction);
549
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000550 // Cache a float constant into the graph. This method should only be
551 // called by the SsaBuilder when creating "equivalent" instructions.
552 void CacheFloatConstant(HFloatConstant* constant);
553
554 // See CacheFloatConstant comment.
555 void CacheDoubleConstant(HDoubleConstant* constant);
556
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000557 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000558
559 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100560 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000561
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100562 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100563 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000564
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100565 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100566 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100567
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000568 HBasicBlock* entry_block_;
569 HBasicBlock* exit_block_;
570
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100571 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100572 uint16_t maximum_number_of_out_vregs_;
573
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100574 // The number of virtual registers in this method. Contains the parameters.
575 uint16_t number_of_vregs_;
576
577 // The number of virtual registers used by parameters of this method.
578 uint16_t number_of_in_vregs_;
579
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000580 // Number of vreg size slots that the temporaries use (used in baseline compiler).
581 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100582
Mark Mendell1152c922015-04-24 17:06:35 -0400583 // Has bounds checks. We can totally skip BCE if it's false.
584 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800585
David Brazdil77a48ae2015-09-15 12:34:04 +0000586 // Flag whether there are any try/catch blocks in the graph. We will skip
587 // try/catch-related passes if false.
588 bool has_try_catch_;
589
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000590 // Flag whether there are any irreducible loops in the graph.
591 bool has_irreducible_loops_;
592
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000593 // Indicates whether the graph should be compiled in a way that
594 // ensures full debuggability. If false, we can apply more
595 // aggressive optimizations that may limit the level of debugging.
596 const bool debuggable_;
597
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000598 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000599 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000600
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100601 // The dex file from which the method is from.
602 const DexFile& dex_file_;
603
604 // The method index in the dex file.
605 const uint32_t method_idx_;
606
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100607 // If inlined, this encodes how the callee is being invoked.
608 const InvokeType invoke_type_;
609
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100610 // Whether the graph has been transformed to SSA form. Only used
611 // in debug mode to ensure we are not using properties only valid
612 // for non-SSA form (like the number of temporaries).
613 bool in_ssa_form_;
614
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100615 const bool should_generate_constructor_barrier_;
616
Mathieu Chartiere401d142015-04-22 13:56:20 -0700617 const InstructionSet instruction_set_;
618
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000619 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000620 HNullConstant* cached_null_constant_;
621 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000622 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000623 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000624 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000625
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100626 HCurrentMethod* cached_current_method_;
627
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100628 // The ArtMethod this graph is for. Note that for AOT, it may be null,
629 // for example for methods whose declaring class could not be resolved
630 // (such as when the superclass could not be found).
631 ArtMethod* art_method_;
632
David Brazdil4833f5a2015-12-16 10:37:39 +0000633 // Keep the RTI of inexact Object to avoid having to pass stack handle
634 // collection pointer to passes which may create NullConstant.
635 ReferenceTypeInfo inexact_object_rti_;
636
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000637 // Whether we are compiling this graph for on stack replacement: this will
638 // make all loops seen as irreducible and emit special stack maps to mark
639 // compiled code entries which the interpreter can directly jump to.
640 const bool osr_;
641
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000642 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100643 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000644 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000645 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000646 DISALLOW_COPY_AND_ASSIGN(HGraph);
647};
648
Vladimir Markof9f64412015-09-02 14:05:49 +0100649class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000650 public:
651 HLoopInformation(HBasicBlock* header, HGraph* graph)
652 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100653 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000654 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100655 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100656 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100657 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000658 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100659 back_edges_.reserve(kDefaultNumberOfBackEdges);
660 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100661
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000662 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100663 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000664
665 void Dump(std::ostream& os);
666
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100667 HBasicBlock* GetHeader() const {
668 return header_;
669 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000670
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000671 void SetHeader(HBasicBlock* block) {
672 header_ = block;
673 }
674
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100675 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
676 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
677 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
678
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000679 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100680 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000681 }
682
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100683 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100684 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100685 }
686
David Brazdil46e2a392015-03-16 17:31:52 +0000687 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100688 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100689 }
690
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000691 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100692 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000693 }
694
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100695 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100696
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100697 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100698 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100699 }
700
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100701 // Returns the lifetime position of the back edge that has the
702 // greatest lifetime position.
703 size_t GetLifetimeEnd() const;
704
705 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100706 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100707 }
708
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000709 // Finds blocks that are part of this loop.
710 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100711
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100712 // Returns whether this loop information contains `block`.
713 // Note that this loop information *must* be populated before entering this function.
714 bool Contains(const HBasicBlock& block) const;
715
716 // Returns whether this loop information is an inner loop of `other`.
717 // Note that `other` *must* be populated before entering this function.
718 bool IsIn(const HLoopInformation& other) const;
719
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800720 // Returns true if instruction is not defined within this loop.
721 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700722
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100723 const ArenaBitVector& GetBlocks() const { return blocks_; }
724
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000725 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000726 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000727
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000728 void ClearAllBlocks() {
729 blocks_.ClearAllBits();
730 }
731
David Brazdil3f4a5222016-05-06 12:46:21 +0100732 bool HasBackEdgeNotDominatedByHeader() const;
733
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100734 bool IsPopulated() const {
735 return blocks_.GetHighestBitSet() != -1;
736 }
737
Anton Shaminf89381f2016-05-16 16:44:13 +0600738 bool DominatesAllBackEdges(HBasicBlock* block);
739
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000740 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100741 // Internal recursive implementation of `Populate`.
742 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100743 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100744
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000745 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100746 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000747 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100748 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100749 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100750 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000751
752 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
753};
754
David Brazdilec16f792015-08-19 15:04:01 +0100755// Stores try/catch information for basic blocks.
756// Note that HGraph is constructed so that catch blocks cannot simultaneously
757// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100758class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100759 public:
760 // Try block information constructor.
761 explicit TryCatchInformation(const HTryBoundary& try_entry)
762 : try_entry_(&try_entry),
763 catch_dex_file_(nullptr),
764 catch_type_index_(DexFile::kDexNoIndex16) {
765 DCHECK(try_entry_ != nullptr);
766 }
767
768 // Catch block information constructor.
769 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
770 : try_entry_(nullptr),
771 catch_dex_file_(&dex_file),
772 catch_type_index_(catch_type_index) {}
773
774 bool IsTryBlock() const { return try_entry_ != nullptr; }
775
776 const HTryBoundary& GetTryEntry() const {
777 DCHECK(IsTryBlock());
778 return *try_entry_;
779 }
780
781 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
782
783 bool IsCatchAllTypeIndex() const {
784 DCHECK(IsCatchBlock());
785 return catch_type_index_ == DexFile::kDexNoIndex16;
786 }
787
788 uint16_t GetCatchTypeIndex() const {
789 DCHECK(IsCatchBlock());
790 return catch_type_index_;
791 }
792
793 const DexFile& GetCatchDexFile() const {
794 DCHECK(IsCatchBlock());
795 return *catch_dex_file_;
796 }
797
798 private:
799 // One of possibly several TryBoundary instructions entering the block's try.
800 // Only set for try blocks.
801 const HTryBoundary* try_entry_;
802
803 // Exception type information. Only set for catch blocks.
804 const DexFile* catch_dex_file_;
805 const uint16_t catch_type_index_;
806};
807
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100808static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100809static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100810
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000811// A block in a method. Contains the list of instructions represented
812// as a double linked list. Each block knows its predecessors and
813// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100814
Vladimir Markof9f64412015-09-02 14:05:49 +0100815class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000816 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600817 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000818 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000819 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
820 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000821 loop_information_(nullptr),
822 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000823 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100824 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100825 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100826 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000827 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000828 try_catch_information_(nullptr) {
829 predecessors_.reserve(kDefaultNumberOfPredecessors);
830 successors_.reserve(kDefaultNumberOfSuccessors);
831 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
832 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000833
Vladimir Marko60584552015-09-03 13:35:12 +0000834 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100835 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000836 }
837
Vladimir Marko60584552015-09-03 13:35:12 +0000838 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100839 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000840 }
841
David Brazdild26a4112015-11-10 11:07:31 +0000842 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
843 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
844
Vladimir Marko60584552015-09-03 13:35:12 +0000845 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
846 return ContainsElement(successors_, block, start_from);
847 }
848
849 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100850 return dominated_blocks_;
851 }
852
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100853 bool IsEntryBlock() const {
854 return graph_->GetEntryBlock() == this;
855 }
856
857 bool IsExitBlock() const {
858 return graph_->GetExitBlock() == this;
859 }
860
David Brazdil46e2a392015-03-16 17:31:52 +0000861 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000862 bool IsSingleTryBoundary() const;
863
864 // Returns true if this block emits nothing but a jump.
865 bool IsSingleJump() const {
866 HLoopInformation* loop_info = GetLoopInformation();
867 return (IsSingleGoto() || IsSingleTryBoundary())
868 // Back edges generate a suspend check.
869 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
870 }
David Brazdil46e2a392015-03-16 17:31:52 +0000871
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000872 void AddBackEdge(HBasicBlock* back_edge) {
873 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000874 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000875 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100876 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000877 loop_information_->AddBackEdge(back_edge);
878 }
879
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000880 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000881 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000882
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100883 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000884 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600885 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000886
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000887 HBasicBlock* GetDominator() const { return dominator_; }
888 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000889 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
890
891 void RemoveDominatedBlock(HBasicBlock* block) {
892 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100893 }
Vladimir Marko60584552015-09-03 13:35:12 +0000894
895 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
896 ReplaceElement(dominated_blocks_, existing, new_block);
897 }
898
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100899 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000900
901 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100902 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000903 }
904
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100905 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
906 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100907 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100908 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100909 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
910 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000911
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000912 HInstruction* GetFirstInstructionDisregardMoves() const;
913
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000914 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000915 successors_.push_back(block);
916 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000917 }
918
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100919 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
920 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100921 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000922 new_block->predecessors_.push_back(this);
923 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000924 }
925
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000926 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
927 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000928 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000929 new_block->successors_.push_back(this);
930 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000931 }
932
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100933 // Insert `this` between `predecessor` and `successor. This method
934 // preserves the indicies, and will update the first edge found between
935 // `predecessor` and `successor`.
936 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
937 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100938 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000939 successor->predecessors_[predecessor_index] = this;
940 predecessor->successors_[successor_index] = this;
941 successors_.push_back(successor);
942 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100943 }
944
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100945 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000946 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100947 }
948
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000949 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000950 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000951 }
952
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100953 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000954 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100955 }
956
957 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000958 predecessors_.push_back(block);
959 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100960 }
961
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100962 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000963 DCHECK_EQ(predecessors_.size(), 2u);
964 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100965 }
966
David Brazdil769c9e52015-04-27 13:54:09 +0100967 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000968 DCHECK_EQ(successors_.size(), 2u);
969 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100970 }
971
David Brazdilfc6a86a2015-06-26 10:33:45 +0000972 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000973 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100974 }
975
David Brazdilfc6a86a2015-06-26 10:33:45 +0000976 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000977 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100978 }
979
David Brazdilfc6a86a2015-06-26 10:33:45 +0000980 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000981 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100982 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000983 }
984
985 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000986 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100987 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000988 }
989
990 // Returns whether the first occurrence of `predecessor` in the list of
991 // predecessors is at index `idx`.
992 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100993 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000994 return GetPredecessorIndexOf(predecessor) == idx;
995 }
996
David Brazdild7558da2015-09-22 13:04:14 +0100997 // Create a new block between this block and its predecessors. The new block
998 // is added to the graph, all predecessor edges are relinked to it and an edge
999 // is created to `this`. Returns the new empty block. Reverse post order or
1000 // loop and try/catch information are not updated.
1001 HBasicBlock* CreateImmediateDominator();
1002
David Brazdilfc6a86a2015-06-26 10:33:45 +00001003 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001004 // created, latter block. Note that this method will add the block to the
1005 // graph, create a Goto at the end of the former block and will create an edge
1006 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001007 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001008 HBasicBlock* SplitBefore(HInstruction* cursor);
1009
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001010 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001011 // created block. Note that this method just updates raw block information,
1012 // like predecessors, successors, dominators, and instruction list. It does not
1013 // update the graph, reverse post order, loop information, nor make sure the
1014 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001015 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1016
1017 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1018 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001019
1020 // Merge `other` at the end of `this`. Successors and dominated blocks of
1021 // `other` are changed to be successors and dominated blocks of `this`. Note
1022 // that this method does not update the graph, reverse post order, loop
1023 // information, nor make sure the blocks are consistent (for example ending
1024 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001025 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001026
1027 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1028 // of `this` are moved to `other`.
1029 // Note that this method does not update the graph, reverse post order, loop
1030 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001031 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001032 void ReplaceWith(HBasicBlock* other);
1033
David Brazdil2d7352b2015-04-20 14:52:42 +01001034 // Merge `other` at the end of `this`. This method updates loops, reverse post
1035 // order, links to predecessors, successors, dominators and deletes the block
1036 // from the graph. The two blocks must be successive, i.e. `this` the only
1037 // predecessor of `other` and vice versa.
1038 void MergeWith(HBasicBlock* other);
1039
1040 // Disconnects `this` from all its predecessors, successors and dominator,
1041 // removes it from all loops it is included in and eventually from the graph.
1042 // The block must not dominate any other block. Predecessors and successors
1043 // are safely updated.
1044 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001045
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001046 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001047 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001048 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001049 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001050 // Replace instruction `initial` with `replacement` within this block.
1051 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1052 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001053 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001054 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001055 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1056 // instruction list. With 'ensure_safety' set to true, it verifies that the
1057 // instruction is not in use and removes it from the use lists of its inputs.
1058 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1059 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001060 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001061
1062 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001063 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001064 }
1065
Roland Levillain6b879dd2014-09-22 17:13:44 +01001066 bool IsLoopPreHeaderFirstPredecessor() const {
1067 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001068 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001069 }
1070
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001071 bool IsFirstPredecessorBackEdge() const {
1072 DCHECK(IsLoopHeader());
1073 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1074 }
1075
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001076 HLoopInformation* GetLoopInformation() const {
1077 return loop_information_;
1078 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001079
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001080 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001081 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001082 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001083 void SetInLoop(HLoopInformation* info) {
1084 if (IsLoopHeader()) {
1085 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001086 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001087 loop_information_ = info;
1088 } else if (loop_information_->Contains(*info->GetHeader())) {
1089 // Block is currently part of an outer loop. Make it part of this inner loop.
1090 // Note that a non loop header having a loop information means this loop information
1091 // has already been populated
1092 loop_information_ = info;
1093 } else {
1094 // Block is part of an inner loop. Do not update the loop information.
1095 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1096 // at this point, because this method is being called while populating `info`.
1097 }
1098 }
1099
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001100 // Raw update of the loop information.
1101 void SetLoopInformation(HLoopInformation* info) {
1102 loop_information_ = info;
1103 }
1104
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001105 bool IsInLoop() const { return loop_information_ != nullptr; }
1106
David Brazdilec16f792015-08-19 15:04:01 +01001107 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1108
1109 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1110 try_catch_information_ = try_catch_information;
1111 }
1112
1113 bool IsTryBlock() const {
1114 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1115 }
1116
1117 bool IsCatchBlock() const {
1118 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1119 }
David Brazdilffee3d32015-07-06 11:48:53 +01001120
1121 // Returns the try entry that this block's successors should have. They will
1122 // be in the same try, unless the block ends in a try boundary. In that case,
1123 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001124 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001125
David Brazdild7558da2015-09-22 13:04:14 +01001126 bool HasThrowingInstructions() const;
1127
David Brazdila4b8c212015-05-07 09:59:30 +01001128 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001129 bool Dominates(HBasicBlock* block) const;
1130
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001131 size_t GetLifetimeStart() const { return lifetime_start_; }
1132 size_t GetLifetimeEnd() const { return lifetime_end_; }
1133
1134 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1135 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1136
David Brazdil8d5b8b22015-03-24 10:51:52 +00001137 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001138 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001139 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001140 bool HasSinglePhi() const;
1141
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001142 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001143 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001144 ArenaVector<HBasicBlock*> predecessors_;
1145 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001146 HInstructionList instructions_;
1147 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001148 HLoopInformation* loop_information_;
1149 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001150 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001151 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001152 // The dex program counter of the first instruction of this block.
1153 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001154 size_t lifetime_start_;
1155 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001156 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001157
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001158 friend class HGraph;
1159 friend class HInstruction;
1160
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001161 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1162};
1163
David Brazdilb2bd1c52015-03-25 11:17:37 +00001164// Iterates over the LoopInformation of all loops which contain 'block'
1165// from the innermost to the outermost.
1166class HLoopInformationOutwardIterator : public ValueObject {
1167 public:
1168 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1169 : current_(block.GetLoopInformation()) {}
1170
1171 bool Done() const { return current_ == nullptr; }
1172
1173 void Advance() {
1174 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001175 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001176 }
1177
1178 HLoopInformation* Current() const {
1179 DCHECK(!Done());
1180 return current_;
1181 }
1182
1183 private:
1184 HLoopInformation* current_;
1185
1186 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1187};
1188
Alexandre Ramesef20f712015-06-09 10:29:30 +01001189#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001190 M(Above, Condition) \
1191 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001192 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001193 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001194 M(ArrayGet, Instruction) \
1195 M(ArrayLength, Instruction) \
1196 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001197 M(Below, Condition) \
1198 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001199 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001200 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001201 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001202 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001203 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001204 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001205 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001206 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001207 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001208 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001209 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001210 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001211 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001212 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001213 M(Exit, Instruction) \
1214 M(FloatConstant, Constant) \
1215 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001216 M(GreaterThan, Condition) \
1217 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001218 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001219 M(InstanceFieldGet, Instruction) \
1220 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001221 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001222 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001223 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001224 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001225 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001226 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001227 M(LessThan, Condition) \
1228 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001229 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001230 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001231 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001232 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001233 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001234 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001235 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001236 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001237 M(Neg, UnaryOperation) \
1238 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001239 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001240 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001241 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001242 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001243 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001244 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001245 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001246 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001247 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001248 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001249 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001250 M(Return, Instruction) \
1251 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001252 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001253 M(Shl, BinaryOperation) \
1254 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001255 M(StaticFieldGet, Instruction) \
1256 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001257 M(UnresolvedInstanceFieldGet, Instruction) \
1258 M(UnresolvedInstanceFieldSet, Instruction) \
1259 M(UnresolvedStaticFieldGet, Instruction) \
1260 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001261 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001262 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001263 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001264 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001265 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001266 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001267 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001268 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001269
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001270/*
1271 * Instructions, shared across several (not all) architectures.
1272 */
1273#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1274#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1275#else
1276#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001277 M(BitwiseNegatedRight, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001278 M(MultiplyAccumulate, Instruction)
1279#endif
1280
Vladimir Markob4536b72015-11-24 13:45:23 +00001281#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001282#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001283#else
1284#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1285 M(ArmDexCacheArraysBase, Instruction)
1286#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001287
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001288#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001289#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001290#else
1291#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001292 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001293 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001294#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001295
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001296#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1297
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001298#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1299
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001300#ifndef ART_ENABLE_CODEGEN_x86
1301#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1302#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001303#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1304 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001305 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001306 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001307 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001308#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001309
1310#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1311
1312#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1313 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001314 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001315 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1316 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001317 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001318 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001319 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1320 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1321
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001322#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1323 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001324 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001325 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001326 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001327 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001328
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001329#define FOR_EACH_INSTRUCTION(M) \
1330 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1331 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1332
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001333#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001334FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1335#undef FORWARD_DECLARATION
1336
Vladimir Marko372f10e2016-05-17 16:30:10 +01001337#define DECLARE_INSTRUCTION(type) \
1338 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1339 const char* DebugName() const OVERRIDE { return #type; } \
1340 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1341 return other->Is##type(); \
1342 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001343 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001344
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001345#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1346 bool Is##type() const { return As##type() != nullptr; } \
1347 const H##type* As##type() const { return this; } \
1348 H##type* As##type() { return this; }
1349
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001350template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001351class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001352 public:
David Brazdiled596192015-01-23 10:39:45 +00001353 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001354 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001355 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001356
Vladimir Marko46817b82016-03-29 12:21:58 +01001357 // Hook for the IntrusiveForwardList<>.
1358 // TODO: Hide this better.
1359 IntrusiveForwardListHook hook;
1360
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001361 private:
David Brazdiled596192015-01-23 10:39:45 +00001362 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001363 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001364
1365 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001366 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001367
Vladimir Marko46817b82016-03-29 12:21:58 +01001368 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001369
1370 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1371};
1372
David Brazdiled596192015-01-23 10:39:45 +00001373template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001374using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001375
David Brazdil1abb4192015-02-17 18:33:36 +00001376// This class is used by HEnvironment and HInstruction classes to record the
1377// instructions they use and pointers to the corresponding HUseListNodes kept
1378// by the used instructions.
1379template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001380class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001381 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001382 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1383 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001384
Vladimir Marko46817b82016-03-29 12:21:58 +01001385 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1386 : HUserRecord(old_record.instruction_, before_use_node) {}
1387 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1388 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001389 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001390 }
1391
1392 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001393 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1394 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001395
1396 private:
1397 // Instruction used by the user.
1398 HInstruction* instruction_;
1399
Vladimir Marko46817b82016-03-29 12:21:58 +01001400 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1401 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001402};
1403
Aart Bik854a02b2015-07-14 16:07:00 -07001404/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001405 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001406 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001407 * For write/read dependences on fields/arrays, the dependence analysis uses
1408 * type disambiguation (e.g. a float field write cannot modify the value of an
1409 * integer field read) and the access type (e.g. a reference array write cannot
1410 * modify the value of a reference field read [although it may modify the
1411 * reference fetch prior to reading the field, which is represented by its own
1412 * write/read dependence]). The analysis makes conservative points-to
1413 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1414 * the same, and any reference read depends on any reference read without
1415 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001416 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001417 * The internal representation uses 38-bit and is described in the table below.
1418 * The first line indicates the side effect, and for field/array accesses the
1419 * second line indicates the type of the access (in the order of the
1420 * Primitive::Type enum).
1421 * The two numbered lines below indicate the bit position in the bitfield (read
1422 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001423 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001424 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1425 * +-------------+---------+---------+--------------+---------+---------+
1426 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1427 * | 3 |333333322|222222221| 1 |111111110|000000000|
1428 * | 7 |654321098|765432109| 8 |765432109|876543210|
1429 *
1430 * Note that, to ease the implementation, 'changes' bits are least significant
1431 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001432 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001433class SideEffects : public ValueObject {
1434 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001435 SideEffects() : flags_(0) {}
1436
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001437 static SideEffects None() {
1438 return SideEffects(0);
1439 }
1440
1441 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001442 return SideEffects(kAllChangeBits | kAllDependOnBits);
1443 }
1444
1445 static SideEffects AllChanges() {
1446 return SideEffects(kAllChangeBits);
1447 }
1448
1449 static SideEffects AllDependencies() {
1450 return SideEffects(kAllDependOnBits);
1451 }
1452
1453 static SideEffects AllExceptGCDependency() {
1454 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1455 }
1456
1457 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001458 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001459 }
1460
Aart Bik34c3ba92015-07-20 14:08:59 -07001461 static SideEffects AllWrites() {
1462 return SideEffects(kAllWrites);
1463 }
1464
1465 static SideEffects AllReads() {
1466 return SideEffects(kAllReads);
1467 }
1468
1469 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1470 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001471 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001472 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001473 }
1474
Aart Bik854a02b2015-07-14 16:07:00 -07001475 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001476 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001477 }
1478
Aart Bik34c3ba92015-07-20 14:08:59 -07001479 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1480 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001481 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001482 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001483 }
1484
1485 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001486 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001487 }
1488
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001489 static SideEffects CanTriggerGC() {
1490 return SideEffects(1ULL << kCanTriggerGCBit);
1491 }
1492
1493 static SideEffects DependsOnGC() {
1494 return SideEffects(1ULL << kDependsOnGCBit);
1495 }
1496
Aart Bik854a02b2015-07-14 16:07:00 -07001497 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001498 SideEffects Union(SideEffects other) const {
1499 return SideEffects(flags_ | other.flags_);
1500 }
1501
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001502 SideEffects Exclusion(SideEffects other) const {
1503 return SideEffects(flags_ & ~other.flags_);
1504 }
1505
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001506 void Add(SideEffects other) {
1507 flags_ |= other.flags_;
1508 }
1509
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001510 bool Includes(SideEffects other) const {
1511 return (other.flags_ & flags_) == other.flags_;
1512 }
1513
1514 bool HasSideEffects() const {
1515 return (flags_ & kAllChangeBits);
1516 }
1517
1518 bool HasDependencies() const {
1519 return (flags_ & kAllDependOnBits);
1520 }
1521
1522 // Returns true if there are no side effects or dependencies.
1523 bool DoesNothing() const {
1524 return flags_ == 0;
1525 }
1526
Aart Bik854a02b2015-07-14 16:07:00 -07001527 // Returns true if something is written.
1528 bool DoesAnyWrite() const {
1529 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001530 }
1531
Aart Bik854a02b2015-07-14 16:07:00 -07001532 // Returns true if something is read.
1533 bool DoesAnyRead() const {
1534 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001535 }
1536
Aart Bik854a02b2015-07-14 16:07:00 -07001537 // Returns true if potentially everything is written and read
1538 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001539 bool DoesAllReadWrite() const {
1540 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1541 }
1542
Aart Bik854a02b2015-07-14 16:07:00 -07001543 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001544 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001545 }
1546
Roland Levillain0d5a2812015-11-13 10:07:31 +00001547 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001548 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001549 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1550 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001551 }
1552
1553 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001554 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001555 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001556 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001557 for (int s = kLastBit; s >= 0; s--) {
1558 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1559 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1560 // This is a bit for the GC side effect.
1561 if (current_bit_is_set) {
1562 flags += "GC";
1563 }
Aart Bik854a02b2015-07-14 16:07:00 -07001564 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001565 } else {
1566 // This is a bit for the array/field analysis.
1567 // The underscore character stands for the 'can trigger GC' bit.
1568 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1569 if (current_bit_is_set) {
1570 flags += kDebug[s];
1571 }
1572 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1573 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1574 flags += "|";
1575 }
1576 }
Aart Bik854a02b2015-07-14 16:07:00 -07001577 }
1578 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001579 }
1580
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001581 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001582
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001583 private:
1584 static constexpr int kFieldArrayAnalysisBits = 9;
1585
1586 static constexpr int kFieldWriteOffset = 0;
1587 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1588 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1589 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1590
1591 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1592
1593 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1594 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1595 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1596 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1597
1598 static constexpr int kLastBit = kDependsOnGCBit;
1599 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1600
1601 // Aliases.
1602
1603 static_assert(kChangeBits == kDependOnBits,
1604 "the 'change' bits should match the 'depend on' bits.");
1605
1606 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1607 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1608 static constexpr uint64_t kAllWrites =
1609 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1610 static constexpr uint64_t kAllReads =
1611 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001612
Aart Bik854a02b2015-07-14 16:07:00 -07001613 // Translates type to bit flag.
1614 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1615 CHECK_NE(type, Primitive::kPrimVoid);
1616 const uint64_t one = 1;
1617 const int shift = type; // 0-based consecutive enum
1618 DCHECK_LE(kFieldWriteOffset, shift);
1619 DCHECK_LT(shift, kArrayWriteOffset);
1620 return one << (type + offset);
1621 }
1622
1623 // Private constructor on direct flags value.
1624 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1625
1626 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001627};
1628
David Brazdiled596192015-01-23 10:39:45 +00001629// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001630class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001631 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001632 HEnvironment(ArenaAllocator* arena,
1633 size_t number_of_vregs,
1634 const DexFile& dex_file,
1635 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001636 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001637 InvokeType invoke_type,
1638 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001639 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1640 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001641 parent_(nullptr),
1642 dex_file_(dex_file),
1643 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001644 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001645 invoke_type_(invoke_type),
1646 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001647 }
1648
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001649 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001650 : HEnvironment(arena,
1651 to_copy.Size(),
1652 to_copy.GetDexFile(),
1653 to_copy.GetMethodIdx(),
1654 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001655 to_copy.GetInvokeType(),
1656 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001657
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001658 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001659 if (parent_ != nullptr) {
1660 parent_->SetAndCopyParentChain(allocator, parent);
1661 } else {
1662 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1663 parent_->CopyFrom(parent);
1664 if (parent->GetParent() != nullptr) {
1665 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1666 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001667 }
David Brazdiled596192015-01-23 10:39:45 +00001668 }
1669
Vladimir Marko71bf8092015-09-15 15:33:14 +01001670 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001671 void CopyFrom(HEnvironment* environment);
1672
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001673 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1674 // input to the loop phi instead. This is for inserting instructions that
1675 // require an environment (like HDeoptimization) in the loop pre-header.
1676 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001677
1678 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001679 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001680 }
1681
1682 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001683 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001684 }
1685
David Brazdil1abb4192015-02-17 18:33:36 +00001686 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001687
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001688 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001689
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001690 HEnvironment* GetParent() const { return parent_; }
1691
1692 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001693 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001694 }
1695
1696 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001697 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001698 }
1699
1700 uint32_t GetDexPc() const {
1701 return dex_pc_;
1702 }
1703
1704 uint32_t GetMethodIdx() const {
1705 return method_idx_;
1706 }
1707
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001708 InvokeType GetInvokeType() const {
1709 return invoke_type_;
1710 }
1711
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001712 const DexFile& GetDexFile() const {
1713 return dex_file_;
1714 }
1715
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001716 HInstruction* GetHolder() const {
1717 return holder_;
1718 }
1719
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001720
1721 bool IsFromInlinedInvoke() const {
1722 return GetParent() != nullptr;
1723 }
1724
David Brazdiled596192015-01-23 10:39:45 +00001725 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001726 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1727 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001728 HEnvironment* parent_;
1729 const DexFile& dex_file_;
1730 const uint32_t method_idx_;
1731 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001732 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001733
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001734 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001735 HInstruction* const holder_;
1736
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001737 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001738
1739 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1740};
1741
Vladimir Markof9f64412015-09-02 14:05:49 +01001742class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001743 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001744 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001745 : previous_(nullptr),
1746 next_(nullptr),
1747 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001748 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001749 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001750 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001751 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001752 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001753 locations_(nullptr),
1754 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001755 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001756 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001757 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1758 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1759 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001760
Dave Allison20dfc792014-06-16 20:44:29 -07001761 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001762
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001763#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001764 enum InstructionKind {
1765 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1766 };
1767#undef DECLARE_KIND
1768
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001769 HInstruction* GetNext() const { return next_; }
1770 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001771
Calin Juravle77520bc2015-01-12 18:45:46 +00001772 HInstruction* GetNextDisregardingMoves() const;
1773 HInstruction* GetPreviousDisregardingMoves() const;
1774
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001775 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001776 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001777 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001778 bool IsInBlock() const { return block_ != nullptr; }
1779 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001780 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1781 bool IsIrreducibleLoopHeaderPhi() const {
1782 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1783 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001784
Vladimir Marko372f10e2016-05-17 16:30:10 +01001785 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1786
1787 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1788 // One virtual method is enough, just const_cast<> and then re-add the const.
1789 return ArrayRef<const HUserRecord<HInstruction*>>(
1790 const_cast<HInstruction*>(this)->GetInputRecords());
1791 }
1792
1793 auto GetInputs() {
1794 return MakeTransformArrayRef(
1795 GetInputRecords(),
1796 [](HUserRecord<HInstruction*>& record) -> HInstruction* {
1797 return record.GetInstruction();
1798 });
1799 }
1800
1801 auto GetInputs() const {
1802 return MakeTransformArrayRef(
1803 GetInputRecords(),
1804 [](const HUserRecord<HInstruction*>& record) -> const HInstruction* {
1805 return record.GetInstruction();
1806 });
1807 }
1808
1809 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001810 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001811
Vladimir Marko372f10e2016-05-17 16:30:10 +01001812 void SetRawInputAt(size_t index, HInstruction* input) {
1813 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1814 }
1815
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001816 virtual void Accept(HGraphVisitor* visitor) = 0;
1817 virtual const char* DebugName() const = 0;
1818
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001819 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1820
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001821 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001822
1823 uint32_t GetDexPc() const { return dex_pc_; }
1824
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001825 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001826
Roland Levillaine161a2a2014-10-03 12:45:18 +01001827 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001828 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001829
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001830 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001831 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001832
Calin Juravle10e244f2015-01-26 18:54:32 +00001833 // Does not apply for all instructions, but having this at top level greatly
1834 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001835 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001836 virtual bool CanBeNull() const {
1837 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1838 return true;
1839 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001840
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001841 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001842 return false;
1843 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001844
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001845 virtual bool IsActualObject() const {
1846 return GetType() == Primitive::kPrimNot;
1847 }
1848
Calin Juravle2e768302015-07-28 14:41:11 +00001849 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001850
Calin Juravle61d544b2015-02-23 16:46:57 +00001851 ReferenceTypeInfo GetReferenceTypeInfo() const {
1852 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001853 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001854 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001855 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001856
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001857 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001858 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001859 // Note: fixup_end remains valid across push_front().
1860 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1861 HUseListNode<HInstruction*>* new_node =
1862 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1863 uses_.push_front(*new_node);
1864 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001865 }
1866
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001867 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001868 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001869 // Note: env_fixup_end remains valid across push_front().
1870 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1871 HUseListNode<HEnvironment*>* new_node =
1872 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1873 env_uses_.push_front(*new_node);
1874 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001875 }
1876
David Brazdil1abb4192015-02-17 18:33:36 +00001877 void RemoveAsUserOfInput(size_t input) {
1878 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001879 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1880 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1881 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001882 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001883
Vladimir Marko372f10e2016-05-17 16:30:10 +01001884 void RemoveAsUserOfAllInputs() {
1885 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1886 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1887 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1888 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1889 }
1890 }
1891
David Brazdil1abb4192015-02-17 18:33:36 +00001892 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1893 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001894
Vladimir Marko46817b82016-03-29 12:21:58 +01001895 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1896 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1897 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001898 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001899 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001900 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001901
Roland Levillain6c82d402014-10-13 16:10:27 +01001902 // Does this instruction strictly dominate `other_instruction`?
1903 // Returns false if this instruction and `other_instruction` are the same.
1904 // Aborts if this instruction and `other_instruction` are both phis.
1905 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001906
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001907 int GetId() const { return id_; }
1908 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001909
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001910 int GetSsaIndex() const { return ssa_index_; }
1911 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1912 bool HasSsaIndex() const { return ssa_index_ != -1; }
1913
1914 bool HasEnvironment() const { return environment_ != nullptr; }
1915 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001916 // Set the `environment_` field. Raw because this method does not
1917 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001918 void SetRawEnvironment(HEnvironment* environment) {
1919 DCHECK(environment_ == nullptr);
1920 DCHECK_EQ(environment->GetHolder(), this);
1921 environment_ = environment;
1922 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001923
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01001924 void InsertRawEnvironment(HEnvironment* environment) {
1925 DCHECK(environment_ != nullptr);
1926 DCHECK_EQ(environment->GetHolder(), this);
1927 DCHECK(environment->GetParent() == nullptr);
1928 environment->parent_ = environment_;
1929 environment_ = environment;
1930 }
1931
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001932 void RemoveEnvironment();
1933
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001934 // Set the environment of this instruction, copying it from `environment`. While
1935 // copying, the uses lists are being updated.
1936 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001937 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001938 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001939 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001940 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001941 if (environment->GetParent() != nullptr) {
1942 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1943 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001944 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001945
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001946 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1947 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001948 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001949 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001950 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001951 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001952 if (environment->GetParent() != nullptr) {
1953 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1954 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001955 }
1956
Nicolas Geoffray39468442014-09-02 15:17:15 +01001957 // Returns the number of entries in the environment. Typically, that is the
1958 // number of dex registers in a method. It could be more in case of inlining.
1959 size_t EnvironmentSize() const;
1960
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001961 LocationSummary* GetLocations() const { return locations_; }
1962 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001963
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001964 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001965 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001966
Alexandre Rames188d4312015-04-09 18:30:21 +01001967 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1968 // uses of this instruction by `other` are *not* updated.
1969 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1970 ReplaceWith(other);
1971 other->ReplaceInput(this, use_index);
1972 }
1973
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001974 // Move `this` instruction before `cursor`.
1975 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001976
Vladimir Markofb337ea2015-11-25 15:25:10 +00001977 // Move `this` before its first user and out of any loops. If there is no
1978 // out-of-loop user that dominates all other users, move the instruction
1979 // to the end of the out-of-loop common dominator of the user's blocks.
1980 //
1981 // This can be used only on non-throwing instructions with no side effects that
1982 // have at least one use but no environment uses.
1983 void MoveBeforeFirstUserAndOutOfLoops();
1984
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001985#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001986 bool Is##type() const; \
1987 const H##type* As##type() const; \
1988 H##type* As##type();
1989
1990 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1991#undef INSTRUCTION_TYPE_CHECK
1992
1993#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001994 bool Is##type() const { return (As##type() != nullptr); } \
1995 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001996 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001997 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001998#undef INSTRUCTION_TYPE_CHECK
1999
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002000 // Returns whether the instruction can be moved within the graph.
2001 virtual bool CanBeMoved() const { return false; }
2002
2003 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002004 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002005 return false;
2006 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002007
2008 // Returns whether any data encoded in the two instructions is equal.
2009 // This method does not look at the inputs. Both instructions must be
2010 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002011 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002012 return false;
2013 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002014
2015 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002016 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002017 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002018 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002019
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002020 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2021 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2022 // the virtual function because the __attribute__((__pure__)) doesn't really
2023 // apply the strong requirement for virtual functions, preventing optimizations.
2024 InstructionKind GetKind() const PURE;
2025 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002026
2027 virtual size_t ComputeHashCode() const {
2028 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002029 for (const HInstruction* input : GetInputs()) {
2030 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002031 }
2032 return result;
2033 }
2034
2035 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002036 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002037 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002038
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002039 size_t GetLifetimePosition() const { return lifetime_position_; }
2040 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2041 LiveInterval* GetLiveInterval() const { return live_interval_; }
2042 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2043 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2044
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002045 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2046
2047 // Returns whether the code generation of the instruction will require to have access
2048 // to the current method. Such instructions are:
2049 // (1): Instructions that require an environment, as calling the runtime requires
2050 // to walk the stack and have the current method stored at a specific stack address.
2051 // (2): Object literals like classes and strings, that are loaded from the dex cache
2052 // fields of the current method.
2053 bool NeedsCurrentMethod() const {
2054 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2055 }
2056
Vladimir Markodc151b22015-10-15 18:02:30 +01002057 // Returns whether the code generation of the instruction will require to have access
2058 // to the dex cache of the current method's declaring class via the current method.
2059 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002060
Mark Mendellc4701932015-04-10 13:18:51 -04002061 // Does this instruction have any use in an environment before
2062 // control flow hits 'other'?
2063 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2064
2065 // Remove all references to environment uses of this instruction.
2066 // The caller must ensure that this is safe to do.
2067 void RemoveEnvironmentUsers();
2068
Vladimir Markoa1de9182016-02-25 11:37:38 +00002069 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2070 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002071
David Brazdil1abb4192015-02-17 18:33:36 +00002072 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002073 // If set, the machine code for this instruction is assumed to be generated by
2074 // its users. Used by liveness analysis to compute use positions accordingly.
2075 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2076 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2077 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2078 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2079
Vladimir Marko372f10e2016-05-17 16:30:10 +01002080 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2081 return GetInputRecords()[i];
2082 }
2083
2084 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2085 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2086 input_records[index] = input;
2087 }
David Brazdil1abb4192015-02-17 18:33:36 +00002088
Vladimir Markoa1de9182016-02-25 11:37:38 +00002089 uint32_t GetPackedFields() const {
2090 return packed_fields_;
2091 }
2092
2093 template <size_t flag>
2094 bool GetPackedFlag() const {
2095 return (packed_fields_ & (1u << flag)) != 0u;
2096 }
2097
2098 template <size_t flag>
2099 void SetPackedFlag(bool value = true) {
2100 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2101 }
2102
2103 template <typename BitFieldType>
2104 typename BitFieldType::value_type GetPackedField() const {
2105 return BitFieldType::Decode(packed_fields_);
2106 }
2107
2108 template <typename BitFieldType>
2109 void SetPackedField(typename BitFieldType::value_type value) {
2110 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2111 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2112 }
2113
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002114 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002115 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2116 auto before_use_node = uses_.before_begin();
2117 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2118 HInstruction* user = use_node->GetUser();
2119 size_t input_index = use_node->GetIndex();
2120 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2121 before_use_node = use_node;
2122 }
2123 }
2124
2125 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2126 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2127 if (next != uses_.end()) {
2128 HInstruction* next_user = next->GetUser();
2129 size_t next_index = next->GetIndex();
2130 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2131 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2132 }
2133 }
2134
2135 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2136 auto before_env_use_node = env_uses_.before_begin();
2137 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2138 HEnvironment* user = env_use_node->GetUser();
2139 size_t input_index = env_use_node->GetIndex();
2140 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2141 before_env_use_node = env_use_node;
2142 }
2143 }
2144
2145 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2146 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2147 if (next != env_uses_.end()) {
2148 HEnvironment* next_user = next->GetUser();
2149 size_t next_index = next->GetIndex();
2150 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2151 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2152 }
2153 }
David Brazdil1abb4192015-02-17 18:33:36 +00002154
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002155 HInstruction* previous_;
2156 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002157 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002158 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002159
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002160 // An instruction gets an id when it is added to the graph.
2161 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002162 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002163 int id_;
2164
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002165 // When doing liveness analysis, instructions that have uses get an SSA index.
2166 int ssa_index_;
2167
Vladimir Markoa1de9182016-02-25 11:37:38 +00002168 // Packed fields.
2169 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002170
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002171 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002172 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002173
2174 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002175 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002176
Nicolas Geoffray39468442014-09-02 15:17:15 +01002177 // The environment associated with this instruction. Not null if the instruction
2178 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002179 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002180
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002181 // Set by the code generator.
2182 LocationSummary* locations_;
2183
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002184 // Set by the liveness analysis.
2185 LiveInterval* live_interval_;
2186
2187 // Set by the liveness analysis, this is the position in a linear
2188 // order of blocks where this instruction's live interval start.
2189 size_t lifetime_position_;
2190
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002191 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002192
Vladimir Markoa1de9182016-02-25 11:37:38 +00002193 // The reference handle part of the reference type info.
2194 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002195 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002196 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002197
David Brazdil1abb4192015-02-17 18:33:36 +00002198 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002199 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002200 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002201 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002202 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002203
2204 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2205};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002206std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002207
2208class HInstructionIterator : public ValueObject {
2209 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002210 explicit HInstructionIterator(const HInstructionList& instructions)
2211 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002212 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002213 }
2214
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002215 bool Done() const { return instruction_ == nullptr; }
2216 HInstruction* Current() const { return instruction_; }
2217 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002218 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002219 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002220 }
2221
2222 private:
2223 HInstruction* instruction_;
2224 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002225
2226 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002227};
2228
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002229class HBackwardInstructionIterator : public ValueObject {
2230 public:
2231 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2232 : instruction_(instructions.last_instruction_) {
2233 next_ = Done() ? nullptr : instruction_->GetPrevious();
2234 }
2235
2236 bool Done() const { return instruction_ == nullptr; }
2237 HInstruction* Current() const { return instruction_; }
2238 void Advance() {
2239 instruction_ = next_;
2240 next_ = Done() ? nullptr : instruction_->GetPrevious();
2241 }
2242
2243 private:
2244 HInstruction* instruction_;
2245 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002246
2247 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002248};
2249
Vladimir Markof9f64412015-09-02 14:05:49 +01002250template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002251class HTemplateInstruction: public HInstruction {
2252 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002253 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002254 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002255 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002256
Vladimir Marko372f10e2016-05-17 16:30:10 +01002257 using HInstruction::GetInputRecords; // Keep the const version visible.
2258 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2259 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002260 }
2261
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002262 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002263 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002264
2265 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002266};
2267
Vladimir Markof9f64412015-09-02 14:05:49 +01002268// HTemplateInstruction specialization for N=0.
2269template<>
2270class HTemplateInstruction<0>: public HInstruction {
2271 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002272 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002273 : HInstruction(side_effects, dex_pc) {}
2274
Vladimir Markof9f64412015-09-02 14:05:49 +01002275 virtual ~HTemplateInstruction() {}
2276
Vladimir Marko372f10e2016-05-17 16:30:10 +01002277 using HInstruction::GetInputRecords; // Keep the const version visible.
2278 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2279 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002280 }
2281
2282 private:
2283 friend class SsaBuilder;
2284};
2285
Dave Allison20dfc792014-06-16 20:44:29 -07002286template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002287class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002288 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002289 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002290 : HTemplateInstruction<N>(side_effects, dex_pc) {
2291 this->template SetPackedField<TypeField>(type);
2292 }
Dave Allison20dfc792014-06-16 20:44:29 -07002293 virtual ~HExpression() {}
2294
Vladimir Markoa1de9182016-02-25 11:37:38 +00002295 Primitive::Type GetType() const OVERRIDE {
2296 return TypeField::Decode(this->GetPackedFields());
2297 }
Dave Allison20dfc792014-06-16 20:44:29 -07002298
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002299 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002300 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2301 static constexpr size_t kFieldTypeSize =
2302 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2303 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2304 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2305 "Too many packed fields.");
2306 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002307};
2308
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002309// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2310// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002311class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002312 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002313 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2314 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002315
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002316 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002317
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002318 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002319
2320 private:
2321 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2322};
2323
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002324// Represents dex's RETURN opcodes. A HReturn is a control flow
2325// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002326class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002327 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002328 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2329 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002330 SetRawInputAt(0, value);
2331 }
2332
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002333 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002334
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002335 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002336
2337 private:
2338 DISALLOW_COPY_AND_ASSIGN(HReturn);
2339};
2340
Vladimir Markofcb503c2016-05-18 12:48:17 +01002341class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002342 public:
2343 HPhi(ArenaAllocator* arena,
2344 uint32_t reg_number,
2345 size_t number_of_inputs,
2346 Primitive::Type type,
2347 uint32_t dex_pc = kNoDexPc)
2348 : HInstruction(SideEffects::None(), dex_pc),
2349 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2350 reg_number_(reg_number) {
2351 SetPackedField<TypeField>(ToPhiType(type));
2352 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2353 // Phis are constructed live and marked dead if conflicting or unused.
2354 // Individual steps of SsaBuilder should assume that if a phi has been
2355 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2356 SetPackedFlag<kFlagIsLive>(true);
2357 SetPackedFlag<kFlagCanBeNull>(true);
2358 }
2359
2360 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2361 static Primitive::Type ToPhiType(Primitive::Type type) {
2362 return Primitive::PrimitiveKind(type);
2363 }
2364
2365 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2366
Vladimir Marko372f10e2016-05-17 16:30:10 +01002367 using HInstruction::GetInputRecords; // Keep the const version visible.
2368 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2369 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2370 }
David Brazdildee58d62016-04-07 09:54:26 +00002371
2372 void AddInput(HInstruction* input);
2373 void RemoveInputAt(size_t index);
2374
2375 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2376 void SetType(Primitive::Type new_type) {
2377 // Make sure that only valid type changes occur. The following are allowed:
2378 // (1) int -> float/ref (primitive type propagation),
2379 // (2) long -> double (primitive type propagation).
2380 DCHECK(GetType() == new_type ||
2381 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2382 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2383 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2384 SetPackedField<TypeField>(new_type);
2385 }
2386
2387 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2388 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2389
2390 uint32_t GetRegNumber() const { return reg_number_; }
2391
2392 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2393 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2394 bool IsDead() const { return !IsLive(); }
2395 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2396
2397 bool IsVRegEquivalentOf(HInstruction* other) const {
2398 return other != nullptr
2399 && other->IsPhi()
2400 && other->AsPhi()->GetBlock() == GetBlock()
2401 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2402 }
2403
2404 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2405 // An equivalent phi is a phi having the same dex register and type.
2406 // It assumes that phis with the same dex register are adjacent.
2407 HPhi* GetNextEquivalentPhiWithSameType() {
2408 HInstruction* next = GetNext();
2409 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2410 if (next->GetType() == GetType()) {
2411 return next->AsPhi();
2412 }
2413 next = next->GetNext();
2414 }
2415 return nullptr;
2416 }
2417
2418 DECLARE_INSTRUCTION(Phi);
2419
David Brazdildee58d62016-04-07 09:54:26 +00002420 private:
2421 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2422 static constexpr size_t kFieldTypeSize =
2423 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2424 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2425 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2426 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2427 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2428 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2429
Vladimir Marko372f10e2016-05-17 16:30:10 +01002430 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002431 const uint32_t reg_number_;
2432
2433 DISALLOW_COPY_AND_ASSIGN(HPhi);
2434};
2435
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002436// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002437// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002438// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002439class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002440 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002441 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002442
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002443 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002444
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002445 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002446
2447 private:
2448 DISALLOW_COPY_AND_ASSIGN(HExit);
2449};
2450
2451// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002452class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002453 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002454 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002455
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002456 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002457
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002458 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002459 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002460 }
2461
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002462 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002463
2464 private:
2465 DISALLOW_COPY_AND_ASSIGN(HGoto);
2466};
2467
Roland Levillain9867bc72015-08-05 10:21:34 +01002468class HConstant : public HExpression<0> {
2469 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002470 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2471 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002472
2473 bool CanBeMoved() const OVERRIDE { return true; }
2474
Roland Levillain1a653882016-03-18 18:05:57 +00002475 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002476 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002477 // Is this constant 0 in the arithmetic sense?
2478 virtual bool IsArithmeticZero() const { return false; }
2479 // Is this constant a 0-bit pattern?
2480 virtual bool IsZeroBitPattern() const { return false; }
2481 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002482 virtual bool IsOne() const { return false; }
2483
David Brazdil77a48ae2015-09-15 12:34:04 +00002484 virtual uint64_t GetValueAsUint64() const = 0;
2485
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002486 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002487
2488 private:
2489 DISALLOW_COPY_AND_ASSIGN(HConstant);
2490};
2491
Vladimir Markofcb503c2016-05-18 12:48:17 +01002492class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002493 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002494 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002495 return true;
2496 }
2497
David Brazdil77a48ae2015-09-15 12:34:04 +00002498 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2499
Roland Levillain9867bc72015-08-05 10:21:34 +01002500 size_t ComputeHashCode() const OVERRIDE { return 0; }
2501
Roland Levillain1a653882016-03-18 18:05:57 +00002502 // The null constant representation is a 0-bit pattern.
2503 virtual bool IsZeroBitPattern() const { return true; }
2504
Roland Levillain9867bc72015-08-05 10:21:34 +01002505 DECLARE_INSTRUCTION(NullConstant);
2506
2507 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002508 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002509
2510 friend class HGraph;
2511 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2512};
2513
2514// Constants of the type int. Those can be from Dex instructions, or
2515// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002516class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002517 public:
2518 int32_t GetValue() const { return value_; }
2519
David Brazdil9f389d42015-10-01 14:32:56 +01002520 uint64_t GetValueAsUint64() const OVERRIDE {
2521 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2522 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002523
Vladimir Marko372f10e2016-05-17 16:30:10 +01002524 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002525 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002526 return other->AsIntConstant()->value_ == value_;
2527 }
2528
2529 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2530
2531 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002532 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2533 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002534 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2535
Roland Levillain1a653882016-03-18 18:05:57 +00002536 // Integer constants are used to encode Boolean values as well,
2537 // where 1 means true and 0 means false.
2538 bool IsTrue() const { return GetValue() == 1; }
2539 bool IsFalse() const { return GetValue() == 0; }
2540
Roland Levillain9867bc72015-08-05 10:21:34 +01002541 DECLARE_INSTRUCTION(IntConstant);
2542
2543 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002544 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2545 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2546 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2547 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002548
2549 const int32_t value_;
2550
2551 friend class HGraph;
2552 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2553 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2554 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2555};
2556
Vladimir Markofcb503c2016-05-18 12:48:17 +01002557class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002558 public:
2559 int64_t GetValue() const { return value_; }
2560
David Brazdil77a48ae2015-09-15 12:34:04 +00002561 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2562
Vladimir Marko372f10e2016-05-17 16:30:10 +01002563 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002564 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002565 return other->AsLongConstant()->value_ == value_;
2566 }
2567
2568 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2569
2570 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002571 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2572 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002573 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2574
2575 DECLARE_INSTRUCTION(LongConstant);
2576
2577 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002578 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2579 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002580
2581 const int64_t value_;
2582
2583 friend class HGraph;
2584 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2585};
Dave Allison20dfc792014-06-16 20:44:29 -07002586
Vladimir Markofcb503c2016-05-18 12:48:17 +01002587class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002588 public:
2589 float GetValue() const { return value_; }
2590
2591 uint64_t GetValueAsUint64() const OVERRIDE {
2592 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2593 }
2594
Vladimir Marko372f10e2016-05-17 16:30:10 +01002595 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002596 DCHECK(other->IsFloatConstant()) << other->DebugName();
2597 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2598 }
2599
2600 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2601
2602 bool IsMinusOne() const OVERRIDE {
2603 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2604 }
Roland Levillain1a653882016-03-18 18:05:57 +00002605 bool IsArithmeticZero() const OVERRIDE {
2606 return std::fpclassify(value_) == FP_ZERO;
2607 }
2608 bool IsArithmeticPositiveZero() const {
2609 return IsArithmeticZero() && !std::signbit(value_);
2610 }
2611 bool IsArithmeticNegativeZero() const {
2612 return IsArithmeticZero() && std::signbit(value_);
2613 }
2614 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002615 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002616 }
2617 bool IsOne() const OVERRIDE {
2618 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2619 }
2620 bool IsNaN() const {
2621 return std::isnan(value_);
2622 }
2623
2624 DECLARE_INSTRUCTION(FloatConstant);
2625
2626 private:
2627 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2628 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2629 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2630 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2631
2632 const float value_;
2633
2634 // Only the SsaBuilder and HGraph can create floating-point constants.
2635 friend class SsaBuilder;
2636 friend class HGraph;
2637 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2638};
2639
Vladimir Markofcb503c2016-05-18 12:48:17 +01002640class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002641 public:
2642 double GetValue() const { return value_; }
2643
2644 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2645
Vladimir Marko372f10e2016-05-17 16:30:10 +01002646 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002647 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2648 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2649 }
2650
2651 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2652
2653 bool IsMinusOne() const OVERRIDE {
2654 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2655 }
Roland Levillain1a653882016-03-18 18:05:57 +00002656 bool IsArithmeticZero() const OVERRIDE {
2657 return std::fpclassify(value_) == FP_ZERO;
2658 }
2659 bool IsArithmeticPositiveZero() const {
2660 return IsArithmeticZero() && !std::signbit(value_);
2661 }
2662 bool IsArithmeticNegativeZero() const {
2663 return IsArithmeticZero() && std::signbit(value_);
2664 }
2665 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002666 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002667 }
2668 bool IsOne() const OVERRIDE {
2669 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2670 }
2671 bool IsNaN() const {
2672 return std::isnan(value_);
2673 }
2674
2675 DECLARE_INSTRUCTION(DoubleConstant);
2676
2677 private:
2678 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2679 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2680 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2681 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2682
2683 const double value_;
2684
2685 // Only the SsaBuilder and HGraph can create floating-point constants.
2686 friend class SsaBuilder;
2687 friend class HGraph;
2688 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2689};
2690
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002691// Conditional branch. A block ending with an HIf instruction must have
2692// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002693class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002694 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002695 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2696 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002697 SetRawInputAt(0, input);
2698 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002699
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002700 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002701
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002702 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002703 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002704 }
2705
2706 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002707 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002708 }
2709
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002710 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002711
2712 private:
2713 DISALLOW_COPY_AND_ASSIGN(HIf);
2714};
2715
David Brazdilfc6a86a2015-06-26 10:33:45 +00002716
2717// Abstract instruction which marks the beginning and/or end of a try block and
2718// links it to the respective exception handlers. Behaves the same as a Goto in
2719// non-exceptional control flow.
2720// Normal-flow successor is stored at index zero, exception handlers under
2721// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002722class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002723 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002724 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002725 kEntry,
2726 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002727 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002728 };
2729
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002730 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002731 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2732 SetPackedField<BoundaryKindField>(kind);
2733 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002734
2735 bool IsControlFlow() const OVERRIDE { return true; }
2736
2737 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002738 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002739
David Brazdild26a4112015-11-10 11:07:31 +00002740 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2741 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2742 }
2743
David Brazdilfc6a86a2015-06-26 10:33:45 +00002744 // Returns whether `handler` is among its exception handlers (non-zero index
2745 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002746 bool HasExceptionHandler(const HBasicBlock& handler) const {
2747 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002748 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002749 }
2750
2751 // If not present already, adds `handler` to its block's list of exception
2752 // handlers.
2753 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002754 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002755 GetBlock()->AddSuccessor(handler);
2756 }
2757 }
2758
Vladimir Markoa1de9182016-02-25 11:37:38 +00002759 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2760 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002761
David Brazdilffee3d32015-07-06 11:48:53 +01002762 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2763
David Brazdilfc6a86a2015-06-26 10:33:45 +00002764 DECLARE_INSTRUCTION(TryBoundary);
2765
2766 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002767 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2768 static constexpr size_t kFieldBoundaryKindSize =
2769 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2770 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2771 kFieldBoundaryKind + kFieldBoundaryKindSize;
2772 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2773 "Too many packed fields.");
2774 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002775
2776 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2777};
2778
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002779// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002780class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002781 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002782 // We set CanTriggerGC to prevent any intermediate address to be live
2783 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002784 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002785 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002786 SetRawInputAt(0, cond);
2787 }
2788
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002789 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002790 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002791 return true;
2792 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002793 bool NeedsEnvironment() const OVERRIDE { return true; }
2794 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002795
2796 DECLARE_INSTRUCTION(Deoptimize);
2797
2798 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002799 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2800};
2801
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002802// Represents the ArtMethod that was passed as a first argument to
2803// the method. It is used by instructions that depend on it, like
2804// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002805class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002806 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002807 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2808 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002809
2810 DECLARE_INSTRUCTION(CurrentMethod);
2811
2812 private:
2813 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2814};
2815
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002816// Fetches an ArtMethod from the virtual table or the interface method table
2817// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002818class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002819 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002820 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002821 kVTable,
2822 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002823 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002824 };
2825 HClassTableGet(HInstruction* cls,
2826 Primitive::Type type,
2827 TableKind kind,
2828 size_t index,
2829 uint32_t dex_pc)
2830 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002831 index_(index) {
2832 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002833 SetRawInputAt(0, cls);
2834 }
2835
2836 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002837 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002838 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002839 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002840 }
2841
Vladimir Markoa1de9182016-02-25 11:37:38 +00002842 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002843 size_t GetIndex() const { return index_; }
2844
2845 DECLARE_INSTRUCTION(ClassTableGet);
2846
2847 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002848 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2849 static constexpr size_t kFieldTableKindSize =
2850 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2851 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2852 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2853 "Too many packed fields.");
2854 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2855
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002856 // The index of the ArtMethod in the table.
2857 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002858
2859 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2860};
2861
Mark Mendellfe57faa2015-09-18 09:26:15 -04002862// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2863// have one successor for each entry in the switch table, and the final successor
2864// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002865class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002866 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002867 HPackedSwitch(int32_t start_value,
2868 uint32_t num_entries,
2869 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002870 uint32_t dex_pc = kNoDexPc)
2871 : HTemplateInstruction(SideEffects::None(), dex_pc),
2872 start_value_(start_value),
2873 num_entries_(num_entries) {
2874 SetRawInputAt(0, input);
2875 }
2876
2877 bool IsControlFlow() const OVERRIDE { return true; }
2878
2879 int32_t GetStartValue() const { return start_value_; }
2880
Vladimir Marko211c2112015-09-24 16:52:33 +01002881 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002882
2883 HBasicBlock* GetDefaultBlock() const {
2884 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002885 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002886 }
2887 DECLARE_INSTRUCTION(PackedSwitch);
2888
2889 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002890 const int32_t start_value_;
2891 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002892
2893 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2894};
2895
Roland Levillain88cb1752014-10-20 16:36:47 +01002896class HUnaryOperation : public HExpression<1> {
2897 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002898 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2899 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002900 SetRawInputAt(0, input);
2901 }
2902
2903 HInstruction* GetInput() const { return InputAt(0); }
2904 Primitive::Type GetResultType() const { return GetType(); }
2905
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002906 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002907 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002908 return true;
2909 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002910
Roland Levillain31dd3d62016-02-16 12:21:02 +00002911 // Try to statically evaluate `this` and return a HConstant
2912 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002913 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002914 HConstant* TryStaticEvaluation() const;
2915
2916 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002917 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2918 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002919 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2920 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002921
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002922 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002923
2924 private:
2925 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2926};
2927
Dave Allison20dfc792014-06-16 20:44:29 -07002928class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002929 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002930 HBinaryOperation(Primitive::Type result_type,
2931 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002932 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002933 SideEffects side_effects = SideEffects::None(),
2934 uint32_t dex_pc = kNoDexPc)
2935 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002936 SetRawInputAt(0, left);
2937 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002938 }
2939
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002940 HInstruction* GetLeft() const { return InputAt(0); }
2941 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002942 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002943
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002944 virtual bool IsCommutative() const { return false; }
2945
2946 // Put constant on the right.
2947 // Returns whether order is changed.
2948 bool OrderInputsWithConstantOnTheRight() {
2949 HInstruction* left = InputAt(0);
2950 HInstruction* right = InputAt(1);
2951 if (left->IsConstant() && !right->IsConstant()) {
2952 ReplaceInput(right, 0);
2953 ReplaceInput(left, 1);
2954 return true;
2955 }
2956 return false;
2957 }
2958
2959 // Order inputs by instruction id, but favor constant on the right side.
2960 // This helps GVN for commutative ops.
2961 void OrderInputs() {
2962 DCHECK(IsCommutative());
2963 HInstruction* left = InputAt(0);
2964 HInstruction* right = InputAt(1);
2965 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2966 return;
2967 }
2968 if (OrderInputsWithConstantOnTheRight()) {
2969 return;
2970 }
2971 // Order according to instruction id.
2972 if (left->GetId() > right->GetId()) {
2973 ReplaceInput(right, 0);
2974 ReplaceInput(left, 1);
2975 }
2976 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002977
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002978 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002979 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002980 return true;
2981 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002982
Roland Levillain31dd3d62016-02-16 12:21:02 +00002983 // Try to statically evaluate `this` and return a HConstant
2984 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002985 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002986 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002987
2988 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002989 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2990 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002991 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2992 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002993 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002994 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2995 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002996 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2997 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002998 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2999 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003000 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003001 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3002 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003003
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003004 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003005 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003006 HConstant* GetConstantRight() const;
3007
3008 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003009 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003010 HInstruction* GetLeastConstantLeft() const;
3011
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003012 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003013
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003014 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003015 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3016};
3017
Mark Mendellc4701932015-04-10 13:18:51 -04003018// The comparison bias applies for floating point operations and indicates how NaN
3019// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003020enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003021 kNoBias, // bias is not applicable (i.e. for long operation)
3022 kGtBias, // return 1 for NaN comparisons
3023 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003024 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003025};
3026
Roland Levillain31dd3d62016-02-16 12:21:02 +00003027std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3028
Dave Allison20dfc792014-06-16 20:44:29 -07003029class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003030 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003031 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003032 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3033 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3034 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003035
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003036 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003037 // `instruction`, and disregard moves in between.
3038 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003039
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003040 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003041
3042 virtual IfCondition GetCondition() const = 0;
3043
Mark Mendellc4701932015-04-10 13:18:51 -04003044 virtual IfCondition GetOppositeCondition() const = 0;
3045
Vladimir Markoa1de9182016-02-25 11:37:38 +00003046 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003047 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3048
Vladimir Markoa1de9182016-02-25 11:37:38 +00003049 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3050 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003051
Vladimir Marko372f10e2016-05-17 16:30:10 +01003052 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003053 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003054 }
3055
Roland Levillain4fa13f62015-07-06 18:11:54 +01003056 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003057 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003058 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003059 if (if_cond == kCondNE) {
3060 return true;
3061 } else if (if_cond == kCondEQ) {
3062 return false;
3063 }
3064 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003065 }
3066
3067 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003068 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003069 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003070 if (if_cond == kCondEQ) {
3071 return true;
3072 } else if (if_cond == kCondNE) {
3073 return false;
3074 }
3075 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003076 }
3077
Roland Levillain31dd3d62016-02-16 12:21:02 +00003078 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003079 // Needed if we merge a HCompare into a HCondition.
3080 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3081 static constexpr size_t kFieldComparisonBiasSize =
3082 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3083 static constexpr size_t kNumberOfConditionPackedBits =
3084 kFieldComparisonBias + kFieldComparisonBiasSize;
3085 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3086 using ComparisonBiasField =
3087 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3088
Roland Levillain31dd3d62016-02-16 12:21:02 +00003089 template <typename T>
3090 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3091
3092 template <typename T>
3093 int32_t CompareFP(T x, T y) const {
3094 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3095 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3096 // Handle the bias.
3097 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3098 }
3099
3100 // Return an integer constant containing the result of a condition evaluated at compile time.
3101 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3102 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3103 }
3104
Dave Allison20dfc792014-06-16 20:44:29 -07003105 private:
3106 DISALLOW_COPY_AND_ASSIGN(HCondition);
3107};
3108
3109// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003110class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003111 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003112 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3113 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003114
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003115 bool IsCommutative() const OVERRIDE { return true; }
3116
Vladimir Marko9e23df52015-11-10 17:14:35 +00003117 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3118 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003119 return MakeConstantCondition(true, GetDexPc());
3120 }
3121 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3122 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3123 }
3124 // In the following Evaluate methods, a HCompare instruction has
3125 // been merged into this HEqual instruction; evaluate it as
3126 // `Compare(x, y) == 0`.
3127 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3128 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3129 GetDexPc());
3130 }
3131 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3132 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3133 }
3134 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3135 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003136 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003137
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003138 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003139
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003140 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003141 return kCondEQ;
3142 }
3143
Mark Mendellc4701932015-04-10 13:18:51 -04003144 IfCondition GetOppositeCondition() const OVERRIDE {
3145 return kCondNE;
3146 }
3147
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003148 private:
Aart Bike9f37602015-10-09 11:15:55 -07003149 template <typename T> bool Compute(T x, T y) const { return x == y; }
3150
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003151 DISALLOW_COPY_AND_ASSIGN(HEqual);
3152};
3153
Vladimir Markofcb503c2016-05-18 12:48:17 +01003154class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003155 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003156 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3157 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003158
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003159 bool IsCommutative() const OVERRIDE { return true; }
3160
Vladimir Marko9e23df52015-11-10 17:14:35 +00003161 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3162 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003163 return MakeConstantCondition(false, GetDexPc());
3164 }
3165 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3166 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3167 }
3168 // In the following Evaluate methods, a HCompare instruction has
3169 // been merged into this HNotEqual instruction; evaluate it as
3170 // `Compare(x, y) != 0`.
3171 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3172 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3173 }
3174 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3175 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3176 }
3177 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3178 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003179 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003180
Dave Allison20dfc792014-06-16 20:44:29 -07003181 DECLARE_INSTRUCTION(NotEqual);
3182
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003183 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003184 return kCondNE;
3185 }
3186
Mark Mendellc4701932015-04-10 13:18:51 -04003187 IfCondition GetOppositeCondition() const OVERRIDE {
3188 return kCondEQ;
3189 }
3190
Dave Allison20dfc792014-06-16 20:44:29 -07003191 private:
Aart Bike9f37602015-10-09 11:15:55 -07003192 template <typename T> bool Compute(T x, T y) const { return x != y; }
3193
Dave Allison20dfc792014-06-16 20:44:29 -07003194 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3195};
3196
Vladimir Markofcb503c2016-05-18 12:48:17 +01003197class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003198 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003199 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3200 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003201
Roland Levillain9867bc72015-08-05 10:21:34 +01003202 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003203 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003204 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003205 // In the following Evaluate methods, a HCompare instruction has
3206 // been merged into this HLessThan instruction; evaluate it as
3207 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003208 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003209 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3210 }
3211 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3212 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3213 }
3214 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3215 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003216 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003217
Dave Allison20dfc792014-06-16 20:44:29 -07003218 DECLARE_INSTRUCTION(LessThan);
3219
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003220 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003221 return kCondLT;
3222 }
3223
Mark Mendellc4701932015-04-10 13:18:51 -04003224 IfCondition GetOppositeCondition() const OVERRIDE {
3225 return kCondGE;
3226 }
3227
Dave Allison20dfc792014-06-16 20:44:29 -07003228 private:
Aart Bike9f37602015-10-09 11:15:55 -07003229 template <typename T> bool Compute(T x, T y) const { return x < y; }
3230
Dave Allison20dfc792014-06-16 20:44:29 -07003231 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3232};
3233
Vladimir Markofcb503c2016-05-18 12:48:17 +01003234class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003235 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003236 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3237 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003238
Roland Levillain9867bc72015-08-05 10:21:34 +01003239 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003240 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003241 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003242 // In the following Evaluate methods, a HCompare instruction has
3243 // been merged into this HLessThanOrEqual instruction; evaluate it as
3244 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003245 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003246 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3247 }
3248 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3249 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3250 }
3251 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3252 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003253 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003254
Dave Allison20dfc792014-06-16 20:44:29 -07003255 DECLARE_INSTRUCTION(LessThanOrEqual);
3256
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003257 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003258 return kCondLE;
3259 }
3260
Mark Mendellc4701932015-04-10 13:18:51 -04003261 IfCondition GetOppositeCondition() const OVERRIDE {
3262 return kCondGT;
3263 }
3264
Dave Allison20dfc792014-06-16 20:44:29 -07003265 private:
Aart Bike9f37602015-10-09 11:15:55 -07003266 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3267
Dave Allison20dfc792014-06-16 20:44:29 -07003268 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3269};
3270
Vladimir Markofcb503c2016-05-18 12:48:17 +01003271class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003272 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003273 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3274 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003275
Roland Levillain9867bc72015-08-05 10:21:34 +01003276 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003277 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003278 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003279 // In the following Evaluate methods, a HCompare instruction has
3280 // been merged into this HGreaterThan instruction; evaluate it as
3281 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003282 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003283 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3284 }
3285 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3286 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3287 }
3288 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3289 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003290 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003291
Dave Allison20dfc792014-06-16 20:44:29 -07003292 DECLARE_INSTRUCTION(GreaterThan);
3293
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003294 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003295 return kCondGT;
3296 }
3297
Mark Mendellc4701932015-04-10 13:18:51 -04003298 IfCondition GetOppositeCondition() const OVERRIDE {
3299 return kCondLE;
3300 }
3301
Dave Allison20dfc792014-06-16 20:44:29 -07003302 private:
Aart Bike9f37602015-10-09 11:15:55 -07003303 template <typename T> bool Compute(T x, T y) const { return x > y; }
3304
Dave Allison20dfc792014-06-16 20:44:29 -07003305 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3306};
3307
Vladimir Markofcb503c2016-05-18 12:48:17 +01003308class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003309 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003310 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3311 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003312
Roland Levillain9867bc72015-08-05 10:21:34 +01003313 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003314 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003315 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003316 // In the following Evaluate methods, a HCompare instruction has
3317 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3318 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003319 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003320 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3321 }
3322 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3323 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3324 }
3325 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3326 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003327 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003328
Dave Allison20dfc792014-06-16 20:44:29 -07003329 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3330
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003331 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003332 return kCondGE;
3333 }
3334
Mark Mendellc4701932015-04-10 13:18:51 -04003335 IfCondition GetOppositeCondition() const OVERRIDE {
3336 return kCondLT;
3337 }
3338
Dave Allison20dfc792014-06-16 20:44:29 -07003339 private:
Aart Bike9f37602015-10-09 11:15:55 -07003340 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3341
Dave Allison20dfc792014-06-16 20:44:29 -07003342 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3343};
3344
Vladimir Markofcb503c2016-05-18 12:48:17 +01003345class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003346 public:
3347 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3348 : HCondition(first, second, dex_pc) {}
3349
3350 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003351 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003352 }
3353 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003354 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3355 }
3356 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3357 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3358 LOG(FATAL) << DebugName() << " is not defined for float values";
3359 UNREACHABLE();
3360 }
3361 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3362 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3363 LOG(FATAL) << DebugName() << " is not defined for double values";
3364 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003365 }
3366
3367 DECLARE_INSTRUCTION(Below);
3368
3369 IfCondition GetCondition() const OVERRIDE {
3370 return kCondB;
3371 }
3372
3373 IfCondition GetOppositeCondition() const OVERRIDE {
3374 return kCondAE;
3375 }
3376
3377 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003378 template <typename T> bool Compute(T x, T y) const {
3379 return MakeUnsigned(x) < MakeUnsigned(y);
3380 }
Aart Bike9f37602015-10-09 11:15:55 -07003381
3382 DISALLOW_COPY_AND_ASSIGN(HBelow);
3383};
3384
Vladimir Markofcb503c2016-05-18 12:48:17 +01003385class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003386 public:
3387 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3388 : HCondition(first, second, dex_pc) {}
3389
3390 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003391 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003392 }
3393 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003394 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3395 }
3396 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3397 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3398 LOG(FATAL) << DebugName() << " is not defined for float values";
3399 UNREACHABLE();
3400 }
3401 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3402 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3403 LOG(FATAL) << DebugName() << " is not defined for double values";
3404 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003405 }
3406
3407 DECLARE_INSTRUCTION(BelowOrEqual);
3408
3409 IfCondition GetCondition() const OVERRIDE {
3410 return kCondBE;
3411 }
3412
3413 IfCondition GetOppositeCondition() const OVERRIDE {
3414 return kCondA;
3415 }
3416
3417 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003418 template <typename T> bool Compute(T x, T y) const {
3419 return MakeUnsigned(x) <= MakeUnsigned(y);
3420 }
Aart Bike9f37602015-10-09 11:15:55 -07003421
3422 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3423};
3424
Vladimir Markofcb503c2016-05-18 12:48:17 +01003425class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003426 public:
3427 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3428 : HCondition(first, second, dex_pc) {}
3429
3430 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003431 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003432 }
3433 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003434 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3435 }
3436 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3437 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3438 LOG(FATAL) << DebugName() << " is not defined for float values";
3439 UNREACHABLE();
3440 }
3441 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3442 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3443 LOG(FATAL) << DebugName() << " is not defined for double values";
3444 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003445 }
3446
3447 DECLARE_INSTRUCTION(Above);
3448
3449 IfCondition GetCondition() const OVERRIDE {
3450 return kCondA;
3451 }
3452
3453 IfCondition GetOppositeCondition() const OVERRIDE {
3454 return kCondBE;
3455 }
3456
3457 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003458 template <typename T> bool Compute(T x, T y) const {
3459 return MakeUnsigned(x) > MakeUnsigned(y);
3460 }
Aart Bike9f37602015-10-09 11:15:55 -07003461
3462 DISALLOW_COPY_AND_ASSIGN(HAbove);
3463};
3464
Vladimir Markofcb503c2016-05-18 12:48:17 +01003465class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003466 public:
3467 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3468 : HCondition(first, second, dex_pc) {}
3469
3470 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003471 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003472 }
3473 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003474 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3475 }
3476 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3477 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3478 LOG(FATAL) << DebugName() << " is not defined for float values";
3479 UNREACHABLE();
3480 }
3481 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3482 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3483 LOG(FATAL) << DebugName() << " is not defined for double values";
3484 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003485 }
3486
3487 DECLARE_INSTRUCTION(AboveOrEqual);
3488
3489 IfCondition GetCondition() const OVERRIDE {
3490 return kCondAE;
3491 }
3492
3493 IfCondition GetOppositeCondition() const OVERRIDE {
3494 return kCondB;
3495 }
3496
3497 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003498 template <typename T> bool Compute(T x, T y) const {
3499 return MakeUnsigned(x) >= MakeUnsigned(y);
3500 }
Aart Bike9f37602015-10-09 11:15:55 -07003501
3502 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3503};
Dave Allison20dfc792014-06-16 20:44:29 -07003504
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003505// Instruction to check how two inputs compare to each other.
3506// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003507class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003508 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003509 // Note that `comparison_type` is the type of comparison performed
3510 // between the comparison's inputs, not the type of the instantiated
3511 // HCompare instruction (which is always Primitive::kPrimInt).
3512 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003513 HInstruction* first,
3514 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003515 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003516 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003517 : HBinaryOperation(Primitive::kPrimInt,
3518 first,
3519 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003520 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003521 dex_pc) {
3522 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003523 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3524 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003525 }
3526
Roland Levillain9867bc72015-08-05 10:21:34 +01003527 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003528 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3529
3530 template <typename T>
3531 int32_t ComputeFP(T x, T y) const {
3532 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3533 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3534 // Handle the bias.
3535 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3536 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003537
Roland Levillain9867bc72015-08-05 10:21:34 +01003538 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003539 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3540 // reach this code path when processing a freshly built HIR
3541 // graph. However HCompare integer instructions can be synthesized
3542 // by the instruction simplifier to implement IntegerCompare and
3543 // IntegerSignum intrinsics, so we have to handle this case.
3544 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003545 }
3546 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003547 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3548 }
3549 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3550 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3551 }
3552 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3553 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003554 }
3555
Vladimir Marko372f10e2016-05-17 16:30:10 +01003556 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003557 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003558 }
3559
Vladimir Markoa1de9182016-02-25 11:37:38 +00003560 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003561
Roland Levillain31dd3d62016-02-16 12:21:02 +00003562 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003563 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003564 bool IsGtBias() const {
3565 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003566 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003567 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003568
Roland Levillain1693a1f2016-03-15 14:57:31 +00003569 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3570 // Comparisons do not require a runtime call in any back end.
3571 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003572 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003573
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003574 DECLARE_INSTRUCTION(Compare);
3575
Roland Levillain31dd3d62016-02-16 12:21:02 +00003576 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003577 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3578 static constexpr size_t kFieldComparisonBiasSize =
3579 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3580 static constexpr size_t kNumberOfComparePackedBits =
3581 kFieldComparisonBias + kFieldComparisonBiasSize;
3582 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3583 using ComparisonBiasField =
3584 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3585
Roland Levillain31dd3d62016-02-16 12:21:02 +00003586 // Return an integer constant containing the result of a comparison evaluated at compile time.
3587 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3588 DCHECK(value == -1 || value == 0 || value == 1) << value;
3589 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3590 }
3591
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003592 private:
3593 DISALLOW_COPY_AND_ASSIGN(HCompare);
3594};
3595
Vladimir Markofcb503c2016-05-18 12:48:17 +01003596class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003597 public:
3598 HNewInstance(HInstruction* cls,
3599 HCurrentMethod* current_method,
3600 uint32_t dex_pc,
3601 uint16_t type_index,
3602 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003603 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003604 bool finalizable,
3605 QuickEntrypointEnum entrypoint)
3606 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3607 type_index_(type_index),
3608 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003609 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003610 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003611 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003612 SetRawInputAt(0, cls);
3613 SetRawInputAt(1, current_method);
3614 }
3615
3616 uint16_t GetTypeIndex() const { return type_index_; }
3617 const DexFile& GetDexFile() const { return dex_file_; }
3618
3619 // Calls runtime so needs an environment.
3620 bool NeedsEnvironment() const OVERRIDE { return true; }
3621
Mingyao Yang062157f2016-03-02 10:15:36 -08003622 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3623 bool CanThrow() const OVERRIDE { return true; }
3624
3625 // Needs to call into runtime to make sure it's instantiable/accessible.
3626 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003627
Vladimir Markoa1de9182016-02-25 11:37:38 +00003628 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003629
3630 bool CanBeNull() const OVERRIDE { return false; }
3631
3632 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3633
3634 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3635 entrypoint_ = entrypoint;
3636 }
3637
3638 bool IsStringAlloc() const;
3639
3640 DECLARE_INSTRUCTION(NewInstance);
3641
3642 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003643 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3644 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003645 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3646 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3647 "Too many packed fields.");
3648
David Brazdil6de19382016-01-08 17:37:10 +00003649 const uint16_t type_index_;
3650 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003651 QuickEntrypointEnum entrypoint_;
3652
3653 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3654};
3655
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003656enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003657#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3658 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003659#include "intrinsics_list.h"
3660 kNone,
3661 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3662#undef INTRINSICS_LIST
3663#undef OPTIMIZING_INTRINSICS
3664};
3665std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3666
Agi Csaki05f20562015-08-19 14:58:14 -07003667enum IntrinsicNeedsEnvironmentOrCache {
3668 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3669 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003670};
3671
Aart Bik5d75afe2015-12-14 11:57:01 -08003672enum IntrinsicSideEffects {
3673 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3674 kReadSideEffects, // Intrinsic may read heap memory.
3675 kWriteSideEffects, // Intrinsic may write heap memory.
3676 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3677};
3678
3679enum IntrinsicExceptions {
3680 kNoThrow, // Intrinsic does not throw any exceptions.
3681 kCanThrow // Intrinsic may throw exceptions.
3682};
3683
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003684class HInvoke : public HInstruction {
3685 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003686 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003687
Vladimir Marko372f10e2016-05-17 16:30:10 +01003688 using HInstruction::GetInputRecords; // Keep the const version visible.
3689 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3690 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3691 }
3692
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003693 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003694 SetRawInputAt(index, argument);
3695 }
3696
Roland Levillain3e3d7332015-04-28 11:00:54 +01003697 // Return the number of arguments. This number can be lower than
3698 // the number of inputs returned by InputCount(), as some invoke
3699 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3700 // inputs at the end of their list of inputs.
3701 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3702
Vladimir Markoa1de9182016-02-25 11:37:38 +00003703 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003704
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003705 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003706 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003707
Vladimir Markoa1de9182016-02-25 11:37:38 +00003708 InvokeType GetOriginalInvokeType() const {
3709 return GetPackedField<OriginalInvokeTypeField>();
3710 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003711
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003712 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003713 return intrinsic_;
3714 }
3715
Aart Bik5d75afe2015-12-14 11:57:01 -08003716 void SetIntrinsic(Intrinsics intrinsic,
3717 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3718 IntrinsicSideEffects side_effects,
3719 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003720
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003721 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003722 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003723 }
3724
Vladimir Markoa1de9182016-02-25 11:37:38 +00003725 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003726
3727 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3728
Vladimir Marko372f10e2016-05-17 16:30:10 +01003729 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003730 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3731 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003732
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003733 uint32_t* GetIntrinsicOptimizations() {
3734 return &intrinsic_optimizations_;
3735 }
3736
3737 const uint32_t* GetIntrinsicOptimizations() const {
3738 return &intrinsic_optimizations_;
3739 }
3740
3741 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3742
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003743 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003744
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003745 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003746 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3747 static constexpr size_t kFieldOriginalInvokeTypeSize =
3748 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3749 static constexpr size_t kFieldReturnType =
3750 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3751 static constexpr size_t kFieldReturnTypeSize =
3752 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3753 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3754 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3755 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3756 using OriginalInvokeTypeField =
3757 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3758 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3759
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003760 HInvoke(ArenaAllocator* arena,
3761 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003762 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003763 Primitive::Type return_type,
3764 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003765 uint32_t dex_method_index,
3766 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003767 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003768 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003769 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003770 inputs_(number_of_arguments + number_of_other_inputs,
3771 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003772 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003773 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003774 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003775 SetPackedField<ReturnTypeField>(return_type);
3776 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3777 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003778 }
3779
Vladimir Markoa1de9182016-02-25 11:37:38 +00003780 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003781
Roland Levillain3e3d7332015-04-28 11:00:54 +01003782 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003783 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003784 const uint32_t dex_method_index_;
3785 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003786
3787 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3788 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003789
3790 private:
3791 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3792};
3793
Vladimir Markofcb503c2016-05-18 12:48:17 +01003794class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003795 public:
3796 HInvokeUnresolved(ArenaAllocator* arena,
3797 uint32_t number_of_arguments,
3798 Primitive::Type return_type,
3799 uint32_t dex_pc,
3800 uint32_t dex_method_index,
3801 InvokeType invoke_type)
3802 : HInvoke(arena,
3803 number_of_arguments,
3804 0u /* number_of_other_inputs */,
3805 return_type,
3806 dex_pc,
3807 dex_method_index,
3808 invoke_type) {
3809 }
3810
3811 DECLARE_INSTRUCTION(InvokeUnresolved);
3812
3813 private:
3814 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3815};
3816
Vladimir Markofcb503c2016-05-18 12:48:17 +01003817class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003818 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003819 // Requirements of this method call regarding the class
3820 // initialization (clinit) check of its declaring class.
3821 enum class ClinitCheckRequirement {
3822 kNone, // Class already initialized.
3823 kExplicit, // Static call having explicit clinit check as last input.
3824 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003825 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003826 };
3827
Vladimir Marko58155012015-08-19 12:49:41 +00003828 // Determines how to load the target ArtMethod*.
3829 enum class MethodLoadKind {
3830 // Use a String init ArtMethod* loaded from Thread entrypoints.
3831 kStringInit,
3832
3833 // Use the method's own ArtMethod* loaded by the register allocator.
3834 kRecursive,
3835
3836 // Use ArtMethod* at a known address, embed the direct address in the code.
3837 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3838 kDirectAddress,
3839
3840 // Use ArtMethod* at an address that will be known at link time, embed the direct
3841 // address in the code. If the image is relocatable, emit .patch_oat entry.
3842 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3843 // the image relocatable or not.
3844 kDirectAddressWithFixup,
3845
Vladimir Markoa1de9182016-02-25 11:37:38 +00003846 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003847 // Used when we need to use the dex cache, for example for invoke-static that
3848 // may cause class initialization (the entry may point to a resolution method),
3849 // and we know that we can access the dex cache arrays using a PC-relative load.
3850 kDexCachePcRelative,
3851
3852 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3853 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3854 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3855 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3856 kDexCacheViaMethod,
3857 };
3858
3859 // Determines the location of the code pointer.
3860 enum class CodePtrLocation {
3861 // Recursive call, use local PC-relative call instruction.
3862 kCallSelf,
3863
3864 // Use PC-relative call instruction patched at link time.
3865 // Used for calls within an oat file, boot->boot or app->app.
3866 kCallPCRelative,
3867
3868 // Call to a known target address, embed the direct address in code.
3869 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3870 kCallDirect,
3871
3872 // Call to a target address that will be known at link time, embed the direct
3873 // address in code. If the image is relocatable, emit .patch_oat entry.
3874 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3875 // the image relocatable or not.
3876 kCallDirectWithFixup,
3877
3878 // Use code pointer from the ArtMethod*.
3879 // Used when we don't know the target code. This is also the last-resort-kind used when
3880 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3881 kCallArtMethod,
3882 };
3883
3884 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003885 MethodLoadKind method_load_kind;
3886 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003887 // The method load data holds
3888 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3889 // Note that there are multiple string init methods, each having its own offset.
3890 // - the method address for kDirectAddress
3891 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003892 uint64_t method_load_data;
3893 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003894 };
3895
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003896 HInvokeStaticOrDirect(ArenaAllocator* arena,
3897 uint32_t number_of_arguments,
3898 Primitive::Type return_type,
3899 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003900 uint32_t method_index,
3901 MethodReference target_method,
3902 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003903 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003904 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003905 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003906 : HInvoke(arena,
3907 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003908 // There is potentially one extra argument for the HCurrentMethod node, and
3909 // potentially one other if the clinit check is explicit, and potentially
3910 // one other if the method is a string factory.
3911 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00003912 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003913 return_type,
3914 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003915 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003916 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003917 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003918 dispatch_info_(dispatch_info) {
3919 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3920 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3921 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003922
Vladimir Markodc151b22015-10-15 18:02:30 +01003923 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003924 bool had_current_method_input = HasCurrentMethodInput();
3925 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3926
3927 // Using the current method is the default and once we find a better
3928 // method load kind, we should not go back to using the current method.
3929 DCHECK(had_current_method_input || !needs_current_method_input);
3930
3931 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003932 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3933 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003934 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003935 dispatch_info_ = dispatch_info;
3936 }
3937
Vladimir Markoc53c0792015-11-19 15:48:33 +00003938 void AddSpecialInput(HInstruction* input) {
3939 // We allow only one special input.
3940 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3941 DCHECK(InputCount() == GetSpecialInputIndex() ||
3942 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3943 InsertInputAt(GetSpecialInputIndex(), input);
3944 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003945
Vladimir Marko372f10e2016-05-17 16:30:10 +01003946 using HInstruction::GetInputRecords; // Keep the const version visible.
3947 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3948 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
3949 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
3950 DCHECK(!input_records.empty());
3951 DCHECK_GT(input_records.size(), GetNumberOfArguments());
3952 HInstruction* last_input = input_records.back().GetInstruction();
3953 // Note: `last_input` may be null during arguments setup.
3954 if (last_input != nullptr) {
3955 // `last_input` is the last input of a static invoke marked as having
3956 // an explicit clinit check. It must either be:
3957 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3958 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3959 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
3960 }
3961 }
3962 return input_records;
3963 }
3964
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003965 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003966 // We access the method via the dex cache so we can't do an implicit null check.
3967 // TODO: for intrinsics we can generate implicit null checks.
3968 return false;
3969 }
3970
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003971 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003972 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003973 }
3974
Vladimir Markoc53c0792015-11-19 15:48:33 +00003975 // Get the index of the special input, if any.
3976 //
David Brazdil6de19382016-01-08 17:37:10 +00003977 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3978 // method pointer; otherwise there may be one platform-specific special input,
3979 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003980 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003981 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003982
Vladimir Markoa1de9182016-02-25 11:37:38 +00003983 InvokeType GetOptimizedInvokeType() const {
3984 return GetPackedField<OptimizedInvokeTypeField>();
3985 }
3986
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003987 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003988 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003989 }
3990
Vladimir Marko58155012015-08-19 12:49:41 +00003991 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3992 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3993 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003994 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003995 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003996 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003997 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003998 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3999 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004000 bool HasCurrentMethodInput() const {
4001 // This function can be called only after the invoke has been fully initialized by the builder.
4002 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004003 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004004 return true;
4005 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004006 DCHECK(InputCount() == GetSpecialInputIndex() ||
4007 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004008 return false;
4009 }
4010 }
Vladimir Marko58155012015-08-19 12:49:41 +00004011 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
4012 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004013 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00004014
4015 int32_t GetStringInitOffset() const {
4016 DCHECK(IsStringInit());
4017 return dispatch_info_.method_load_data;
4018 }
4019
4020 uint64_t GetMethodAddress() const {
4021 DCHECK(HasMethodAddress());
4022 return dispatch_info_.method_load_data;
4023 }
4024
4025 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004026 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004027 return dispatch_info_.method_load_data;
4028 }
4029
4030 uint64_t GetDirectCodePtr() const {
4031 DCHECK(HasDirectCodePtr());
4032 return dispatch_info_.direct_code_ptr;
4033 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004034
Vladimir Markoa1de9182016-02-25 11:37:38 +00004035 ClinitCheckRequirement GetClinitCheckRequirement() const {
4036 return GetPackedField<ClinitCheckRequirementField>();
4037 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004038
Roland Levillain4c0eb422015-04-24 16:43:49 +01004039 // Is this instruction a call to a static method?
4040 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004041 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004042 }
4043
Vladimir Markofbb184a2015-11-13 14:47:00 +00004044 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4045 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4046 // instruction; only relevant for static calls with explicit clinit check.
4047 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004048 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004049 size_t last_input_index = inputs_.size() - 1u;
4050 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004051 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004052 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004053 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004054 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004055 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004056 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004057 }
4058
4059 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004060 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004061 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004062 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004063 }
4064
4065 // Is this a call to a static method whose declaring class has an
4066 // implicit intialization check requirement?
4067 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004068 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004069 }
4070
Vladimir Markob554b5a2015-11-06 12:57:55 +00004071 // Does this method load kind need the current method as an input?
4072 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4073 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4074 }
4075
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004076 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004077
Roland Levillain4c0eb422015-04-24 16:43:49 +01004078 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004079 void InsertInputAt(size_t index, HInstruction* input);
4080 void RemoveInputAt(size_t index);
4081
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004082 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004083 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4084 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4085 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4086 static constexpr size_t kFieldClinitCheckRequirement =
4087 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4088 static constexpr size_t kFieldClinitCheckRequirementSize =
4089 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4090 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4091 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4092 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4093 "Too many packed fields.");
4094 using OptimizedInvokeTypeField =
4095 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4096 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4097 kFieldClinitCheckRequirement,
4098 kFieldClinitCheckRequirementSize>;
4099
Vladimir Marko58155012015-08-19 12:49:41 +00004100 // The target method may refer to different dex file or method index than the original
4101 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4102 // in derived class to increase visibility.
4103 MethodReference target_method_;
4104 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004105
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004106 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004107};
Vladimir Markof64242a2015-12-01 14:58:23 +00004108std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004109std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004110
Vladimir Markofcb503c2016-05-18 12:48:17 +01004111class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004112 public:
4113 HInvokeVirtual(ArenaAllocator* arena,
4114 uint32_t number_of_arguments,
4115 Primitive::Type return_type,
4116 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004117 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004118 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004119 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004120 vtable_index_(vtable_index) {}
4121
Calin Juravle641547a2015-04-21 22:08:51 +01004122 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004123 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004124 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004125 }
4126
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004127 uint32_t GetVTableIndex() const { return vtable_index_; }
4128
4129 DECLARE_INSTRUCTION(InvokeVirtual);
4130
4131 private:
4132 const uint32_t vtable_index_;
4133
4134 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4135};
4136
Vladimir Markofcb503c2016-05-18 12:48:17 +01004137class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004138 public:
4139 HInvokeInterface(ArenaAllocator* arena,
4140 uint32_t number_of_arguments,
4141 Primitive::Type return_type,
4142 uint32_t dex_pc,
4143 uint32_t dex_method_index,
4144 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004145 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004146 imt_index_(imt_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 Geoffray52839d12014-11-07 17:47:25 +00004153 uint32_t GetImtIndex() const { return imt_index_; }
4154 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4155
4156 DECLARE_INSTRUCTION(InvokeInterface);
4157
4158 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004159 const uint32_t imt_index_;
4160
4161 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4162};
4163
Vladimir Markofcb503c2016-05-18 12:48:17 +01004164class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004165 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004166 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004167 : HUnaryOperation(result_type, input, dex_pc) {
4168 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4169 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004170
Roland Levillain9867bc72015-08-05 10:21:34 +01004171 template <typename T> T Compute(T x) const { return -x; }
4172
4173 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004174 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004175 }
4176 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004177 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004178 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004179 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4180 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4181 }
4182 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4183 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4184 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004185
Roland Levillain88cb1752014-10-20 16:36:47 +01004186 DECLARE_INSTRUCTION(Neg);
4187
4188 private:
4189 DISALLOW_COPY_AND_ASSIGN(HNeg);
4190};
4191
Vladimir Markofcb503c2016-05-18 12:48:17 +01004192class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004193 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004194 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004195 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004196 uint32_t dex_pc,
4197 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004198 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004199 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004200 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004201 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004202 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004203 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004204 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004205 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004206 }
4207
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004208 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004209 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004210
4211 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004212 bool NeedsEnvironment() const OVERRIDE { return true; }
4213
Mingyao Yang0c365e62015-03-31 15:09:29 -07004214 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4215 bool CanThrow() const OVERRIDE { return true; }
4216
Calin Juravle10e244f2015-01-26 18:54:32 +00004217 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004218
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004219 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4220
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004221 DECLARE_INSTRUCTION(NewArray);
4222
4223 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004224 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004225 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004226 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004227
4228 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4229};
4230
Vladimir Markofcb503c2016-05-18 12:48:17 +01004231class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004232 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004233 HAdd(Primitive::Type result_type,
4234 HInstruction* left,
4235 HInstruction* right,
4236 uint32_t dex_pc = kNoDexPc)
4237 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004238
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004239 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004240
Roland Levillain9867bc72015-08-05 10:21:34 +01004241 template <typename T> T Compute(T x, T y) const { return x + y; }
4242
4243 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004244 return GetBlock()->GetGraph()->GetIntConstant(
4245 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004246 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004247 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004248 return GetBlock()->GetGraph()->GetLongConstant(
4249 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004250 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004251 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4252 return GetBlock()->GetGraph()->GetFloatConstant(
4253 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4254 }
4255 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4256 return GetBlock()->GetGraph()->GetDoubleConstant(
4257 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4258 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004259
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004260 DECLARE_INSTRUCTION(Add);
4261
4262 private:
4263 DISALLOW_COPY_AND_ASSIGN(HAdd);
4264};
4265
Vladimir Markofcb503c2016-05-18 12:48:17 +01004266class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004267 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004268 HSub(Primitive::Type result_type,
4269 HInstruction* left,
4270 HInstruction* right,
4271 uint32_t dex_pc = kNoDexPc)
4272 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004273
Roland Levillain9867bc72015-08-05 10:21:34 +01004274 template <typename T> T Compute(T x, T y) const { return x - y; }
4275
4276 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004277 return GetBlock()->GetGraph()->GetIntConstant(
4278 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004279 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004280 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004281 return GetBlock()->GetGraph()->GetLongConstant(
4282 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004283 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004284 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4285 return GetBlock()->GetGraph()->GetFloatConstant(
4286 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4287 }
4288 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4289 return GetBlock()->GetGraph()->GetDoubleConstant(
4290 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4291 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004292
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004293 DECLARE_INSTRUCTION(Sub);
4294
4295 private:
4296 DISALLOW_COPY_AND_ASSIGN(HSub);
4297};
4298
Vladimir Markofcb503c2016-05-18 12:48:17 +01004299class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004300 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004301 HMul(Primitive::Type result_type,
4302 HInstruction* left,
4303 HInstruction* right,
4304 uint32_t dex_pc = kNoDexPc)
4305 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004306
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004307 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004308
Roland Levillain9867bc72015-08-05 10:21:34 +01004309 template <typename T> T Compute(T x, T y) const { return x * y; }
4310
4311 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004312 return GetBlock()->GetGraph()->GetIntConstant(
4313 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004314 }
4315 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004316 return GetBlock()->GetGraph()->GetLongConstant(
4317 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004318 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004319 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4320 return GetBlock()->GetGraph()->GetFloatConstant(
4321 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4322 }
4323 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4324 return GetBlock()->GetGraph()->GetDoubleConstant(
4325 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4326 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004327
4328 DECLARE_INSTRUCTION(Mul);
4329
4330 private:
4331 DISALLOW_COPY_AND_ASSIGN(HMul);
4332};
4333
Vladimir Markofcb503c2016-05-18 12:48:17 +01004334class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004335 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004336 HDiv(Primitive::Type result_type,
4337 HInstruction* left,
4338 HInstruction* right,
4339 uint32_t dex_pc)
4340 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004341
Roland Levillain9867bc72015-08-05 10:21:34 +01004342 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004343 T ComputeIntegral(T x, T y) const {
4344 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004345 // Our graph structure ensures we never have 0 for `y` during
4346 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004347 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004348 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004349 return (y == -1) ? -x : x / y;
4350 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004351
Roland Levillain31dd3d62016-02-16 12:21:02 +00004352 template <typename T>
4353 T ComputeFP(T x, T y) const {
4354 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4355 return x / y;
4356 }
4357
Roland Levillain9867bc72015-08-05 10:21:34 +01004358 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004359 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004360 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004361 }
4362 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004363 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004364 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4365 }
4366 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4367 return GetBlock()->GetGraph()->GetFloatConstant(
4368 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4369 }
4370 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4371 return GetBlock()->GetGraph()->GetDoubleConstant(
4372 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004373 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004374
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004375 static SideEffects SideEffectsForArchRuntimeCalls() {
4376 // The generated code can use a runtime call.
4377 return SideEffects::CanTriggerGC();
4378 }
4379
Calin Juravle7c4954d2014-10-28 16:57:40 +00004380 DECLARE_INSTRUCTION(Div);
4381
4382 private:
4383 DISALLOW_COPY_AND_ASSIGN(HDiv);
4384};
4385
Vladimir Markofcb503c2016-05-18 12:48:17 +01004386class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004387 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004388 HRem(Primitive::Type result_type,
4389 HInstruction* left,
4390 HInstruction* right,
4391 uint32_t dex_pc)
4392 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004393
Roland Levillain9867bc72015-08-05 10:21:34 +01004394 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004395 T ComputeIntegral(T x, T y) const {
4396 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004397 // Our graph structure ensures we never have 0 for `y` during
4398 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004399 DCHECK_NE(y, 0);
4400 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4401 return (y == -1) ? 0 : x % y;
4402 }
4403
Roland Levillain31dd3d62016-02-16 12:21:02 +00004404 template <typename T>
4405 T ComputeFP(T x, T y) const {
4406 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4407 return std::fmod(x, y);
4408 }
4409
Roland Levillain9867bc72015-08-05 10:21:34 +01004410 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004411 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004412 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004413 }
4414 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004415 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004416 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004417 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004418 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4419 return GetBlock()->GetGraph()->GetFloatConstant(
4420 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4421 }
4422 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4423 return GetBlock()->GetGraph()->GetDoubleConstant(
4424 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4425 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004426
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004427 static SideEffects SideEffectsForArchRuntimeCalls() {
4428 return SideEffects::CanTriggerGC();
4429 }
4430
Calin Juravlebacfec32014-11-14 15:54:36 +00004431 DECLARE_INSTRUCTION(Rem);
4432
4433 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004434 DISALLOW_COPY_AND_ASSIGN(HRem);
4435};
4436
Vladimir Markofcb503c2016-05-18 12:48:17 +01004437class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004438 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004439 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4440 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004441 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004442 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004443 SetRawInputAt(0, value);
4444 }
4445
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004446 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4447
Calin Juravled0d48522014-11-04 16:40:20 +00004448 bool CanBeMoved() const OVERRIDE { return true; }
4449
Vladimir Marko372f10e2016-05-17 16:30:10 +01004450 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004451 return true;
4452 }
4453
4454 bool NeedsEnvironment() const OVERRIDE { return true; }
4455 bool CanThrow() const OVERRIDE { return true; }
4456
Calin Juravled0d48522014-11-04 16:40:20 +00004457 DECLARE_INSTRUCTION(DivZeroCheck);
4458
4459 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004460 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4461};
4462
Vladimir Markofcb503c2016-05-18 12:48:17 +01004463class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004464 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004465 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004466 HInstruction* value,
4467 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004468 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004469 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4470 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4471 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004472 }
4473
Roland Levillain5b5b9312016-03-22 14:57:31 +00004474 template <typename T>
4475 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4476 return value << (distance & max_shift_distance);
4477 }
4478
4479 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004480 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004481 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004482 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004483 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004484 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004485 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004486 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004487 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4488 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4489 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4490 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004491 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004492 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4493 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004494 LOG(FATAL) << DebugName() << " is not defined for float values";
4495 UNREACHABLE();
4496 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004497 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4498 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004499 LOG(FATAL) << DebugName() << " is not defined for double values";
4500 UNREACHABLE();
4501 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004502
4503 DECLARE_INSTRUCTION(Shl);
4504
4505 private:
4506 DISALLOW_COPY_AND_ASSIGN(HShl);
4507};
4508
Vladimir Markofcb503c2016-05-18 12:48:17 +01004509class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004510 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004511 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004512 HInstruction* value,
4513 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004514 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004515 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4516 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4517 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004518 }
4519
Roland Levillain5b5b9312016-03-22 14:57:31 +00004520 template <typename T>
4521 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4522 return value >> (distance & max_shift_distance);
4523 }
4524
4525 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004526 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004527 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004528 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004529 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004530 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004531 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004532 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004533 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4534 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4535 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4536 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004537 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004538 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4539 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004540 LOG(FATAL) << DebugName() << " is not defined for float values";
4541 UNREACHABLE();
4542 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004543 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4544 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004545 LOG(FATAL) << DebugName() << " is not defined for double values";
4546 UNREACHABLE();
4547 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004548
4549 DECLARE_INSTRUCTION(Shr);
4550
4551 private:
4552 DISALLOW_COPY_AND_ASSIGN(HShr);
4553};
4554
Vladimir Markofcb503c2016-05-18 12:48:17 +01004555class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004556 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004557 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004558 HInstruction* value,
4559 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004560 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004561 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4562 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4563 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004564 }
4565
Roland Levillain5b5b9312016-03-22 14:57:31 +00004566 template <typename T>
4567 T Compute(T value, int32_t distance, int32_t max_shift_distance) const {
4568 typedef typename std::make_unsigned<T>::type V;
4569 V ux = static_cast<V>(value);
4570 return static_cast<T>(ux >> (distance & max_shift_distance));
4571 }
4572
4573 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004574 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004575 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004576 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004577 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004578 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004579 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004580 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004581 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4582 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4583 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4584 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004585 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004586 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4587 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004588 LOG(FATAL) << DebugName() << " is not defined for float values";
4589 UNREACHABLE();
4590 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004591 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4592 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004593 LOG(FATAL) << DebugName() << " is not defined for double values";
4594 UNREACHABLE();
4595 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004596
4597 DECLARE_INSTRUCTION(UShr);
4598
4599 private:
4600 DISALLOW_COPY_AND_ASSIGN(HUShr);
4601};
4602
Vladimir Markofcb503c2016-05-18 12:48:17 +01004603class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004604 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004605 HAnd(Primitive::Type result_type,
4606 HInstruction* left,
4607 HInstruction* right,
4608 uint32_t dex_pc = kNoDexPc)
4609 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004610
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004611 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004612
Roland Levillaine53bd812016-02-24 14:54:18 +00004613 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004614
4615 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004616 return GetBlock()->GetGraph()->GetIntConstant(
4617 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004618 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004619 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004620 return GetBlock()->GetGraph()->GetLongConstant(
4621 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004622 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004623 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4624 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4625 LOG(FATAL) << DebugName() << " is not defined for float values";
4626 UNREACHABLE();
4627 }
4628 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4629 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4630 LOG(FATAL) << DebugName() << " is not defined for double values";
4631 UNREACHABLE();
4632 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004633
4634 DECLARE_INSTRUCTION(And);
4635
4636 private:
4637 DISALLOW_COPY_AND_ASSIGN(HAnd);
4638};
4639
Vladimir Markofcb503c2016-05-18 12:48:17 +01004640class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004641 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004642 HOr(Primitive::Type result_type,
4643 HInstruction* left,
4644 HInstruction* right,
4645 uint32_t dex_pc = kNoDexPc)
4646 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004647
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004648 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004649
Roland Levillaine53bd812016-02-24 14:54:18 +00004650 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004651
4652 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004653 return GetBlock()->GetGraph()->GetIntConstant(
4654 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004655 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004656 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004657 return GetBlock()->GetGraph()->GetLongConstant(
4658 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004659 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004660 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4661 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4662 LOG(FATAL) << DebugName() << " is not defined for float values";
4663 UNREACHABLE();
4664 }
4665 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4666 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4667 LOG(FATAL) << DebugName() << " is not defined for double values";
4668 UNREACHABLE();
4669 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004670
4671 DECLARE_INSTRUCTION(Or);
4672
4673 private:
4674 DISALLOW_COPY_AND_ASSIGN(HOr);
4675};
4676
Vladimir Markofcb503c2016-05-18 12:48:17 +01004677class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004678 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004679 HXor(Primitive::Type result_type,
4680 HInstruction* left,
4681 HInstruction* right,
4682 uint32_t dex_pc = kNoDexPc)
4683 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004684
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004685 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004686
Roland Levillaine53bd812016-02-24 14:54:18 +00004687 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004688
4689 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004690 return GetBlock()->GetGraph()->GetIntConstant(
4691 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004692 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004693 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004694 return GetBlock()->GetGraph()->GetLongConstant(
4695 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004696 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004697 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4698 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4699 LOG(FATAL) << DebugName() << " is not defined for float values";
4700 UNREACHABLE();
4701 }
4702 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4703 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4704 LOG(FATAL) << DebugName() << " is not defined for double values";
4705 UNREACHABLE();
4706 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004707
4708 DECLARE_INSTRUCTION(Xor);
4709
4710 private:
4711 DISALLOW_COPY_AND_ASSIGN(HXor);
4712};
4713
Vladimir Markofcb503c2016-05-18 12:48:17 +01004714class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004715 public:
4716 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004717 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004718 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4719 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004720 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004721
Roland Levillain5b5b9312016-03-22 14:57:31 +00004722 template <typename T>
4723 T Compute(T value, int32_t distance, int32_t max_shift_value) const {
4724 typedef typename std::make_unsigned<T>::type V;
4725 V ux = static_cast<V>(value);
4726 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004727 return static_cast<T>(ux);
4728 } else {
4729 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004730 return static_cast<T>(ux >> (distance & max_shift_value)) |
4731 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004732 }
4733 }
4734
Roland Levillain5b5b9312016-03-22 14:57:31 +00004735 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004736 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004737 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004738 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004739 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004740 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004741 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004742 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004743 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4744 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4745 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4746 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004747 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004748 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4749 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004750 LOG(FATAL) << DebugName() << " is not defined for float values";
4751 UNREACHABLE();
4752 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004753 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4754 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004755 LOG(FATAL) << DebugName() << " is not defined for double values";
4756 UNREACHABLE();
4757 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004758
4759 DECLARE_INSTRUCTION(Ror);
4760
4761 private:
4762 DISALLOW_COPY_AND_ASSIGN(HRor);
4763};
4764
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004765// The value of a parameter in this method. Its location depends on
4766// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004767class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004768 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004769 HParameterValue(const DexFile& dex_file,
4770 uint16_t type_index,
4771 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004772 Primitive::Type parameter_type,
4773 bool is_this = false)
4774 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004775 dex_file_(dex_file),
4776 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004777 index_(index) {
4778 SetPackedFlag<kFlagIsThis>(is_this);
4779 SetPackedFlag<kFlagCanBeNull>(!is_this);
4780 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004781
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004782 const DexFile& GetDexFile() const { return dex_file_; }
4783 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004784 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004785 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004786
Vladimir Markoa1de9182016-02-25 11:37:38 +00004787 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4788 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004789
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004790 DECLARE_INSTRUCTION(ParameterValue);
4791
4792 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004793 // Whether or not the parameter value corresponds to 'this' argument.
4794 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4795 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4796 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4797 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4798 "Too many packed fields.");
4799
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004800 const DexFile& dex_file_;
4801 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004802 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004803 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004804 const uint8_t index_;
4805
4806 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4807};
4808
Vladimir Markofcb503c2016-05-18 12:48:17 +01004809class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004810 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004811 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4812 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004813
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004814 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004815 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004816 return true;
4817 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004818
Roland Levillain9867bc72015-08-05 10:21:34 +01004819 template <typename T> T Compute(T x) const { return ~x; }
4820
4821 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004822 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004823 }
4824 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004825 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004826 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004827 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4828 LOG(FATAL) << DebugName() << " is not defined for float values";
4829 UNREACHABLE();
4830 }
4831 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4832 LOG(FATAL) << DebugName() << " is not defined for double values";
4833 UNREACHABLE();
4834 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004835
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004836 DECLARE_INSTRUCTION(Not);
4837
4838 private:
4839 DISALLOW_COPY_AND_ASSIGN(HNot);
4840};
4841
Vladimir Markofcb503c2016-05-18 12:48:17 +01004842class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004843 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004844 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4845 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004846
4847 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004848 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004849 return true;
4850 }
4851
Roland Levillain9867bc72015-08-05 10:21:34 +01004852 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004853 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004854 return !x;
4855 }
4856
Roland Levillain9867bc72015-08-05 10:21:34 +01004857 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004858 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004859 }
4860 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4861 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004862 UNREACHABLE();
4863 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004864 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4865 LOG(FATAL) << DebugName() << " is not defined for float values";
4866 UNREACHABLE();
4867 }
4868 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4869 LOG(FATAL) << DebugName() << " is not defined for double values";
4870 UNREACHABLE();
4871 }
David Brazdil66d126e2015-04-03 16:02:44 +01004872
4873 DECLARE_INSTRUCTION(BooleanNot);
4874
4875 private:
4876 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4877};
4878
Vladimir Markofcb503c2016-05-18 12:48:17 +01004879class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00004880 public:
4881 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004882 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004883 : HExpression(result_type,
4884 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4885 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004886 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01004887 // Invariant: We should never generate a conversion to a Boolean value.
4888 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00004889 }
4890
4891 HInstruction* GetInput() const { return InputAt(0); }
4892 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4893 Primitive::Type GetResultType() const { return GetType(); }
4894
4895 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004896 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
4897 return true;
4898 }
Roland Levillaindff1f282014-11-05 14:15:05 +00004899
Mark Mendelle82549b2015-05-06 10:55:34 -04004900 // Try to statically evaluate the conversion and return a HConstant
4901 // containing the result. If the input cannot be converted, return nullptr.
4902 HConstant* TryStaticEvaluation() const;
4903
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004904 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4905 Primitive::Type result_type) {
4906 // Some architectures may not require the 'GC' side effects, but at this point
4907 // in the compilation process we do not know what architecture we will
4908 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004909 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4910 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004911 return SideEffects::CanTriggerGC();
4912 }
4913 return SideEffects::None();
4914 }
4915
Roland Levillaindff1f282014-11-05 14:15:05 +00004916 DECLARE_INSTRUCTION(TypeConversion);
4917
4918 private:
4919 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4920};
4921
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004922static constexpr uint32_t kNoRegNumber = -1;
4923
Vladimir Markofcb503c2016-05-18 12:48:17 +01004924class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004925 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004926 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4927 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004928 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004929 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004930 SetRawInputAt(0, value);
4931 }
4932
Calin Juravle10e244f2015-01-26 18:54:32 +00004933 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004934 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004935 return true;
4936 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004937
Calin Juravle10e244f2015-01-26 18:54:32 +00004938 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004939
Calin Juravle10e244f2015-01-26 18:54:32 +00004940 bool CanThrow() const OVERRIDE { return true; }
4941
4942 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004943
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004944
4945 DECLARE_INSTRUCTION(NullCheck);
4946
4947 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004948 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4949};
4950
4951class FieldInfo : public ValueObject {
4952 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004953 FieldInfo(MemberOffset field_offset,
4954 Primitive::Type field_type,
4955 bool is_volatile,
4956 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004957 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004958 const DexFile& dex_file,
4959 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004960 : field_offset_(field_offset),
4961 field_type_(field_type),
4962 is_volatile_(is_volatile),
4963 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004964 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004965 dex_file_(dex_file),
4966 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004967
4968 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004969 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004970 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004971 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004972 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004973 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004974 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004975
4976 private:
4977 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004978 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004979 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004980 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004981 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004982 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004983 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004984};
4985
Vladimir Markofcb503c2016-05-18 12:48:17 +01004986class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004987 public:
4988 HInstanceFieldGet(HInstruction* value,
4989 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004990 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004991 bool is_volatile,
4992 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004993 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004994 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004995 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004996 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004997 : HExpression(field_type,
4998 SideEffects::FieldReadOfType(field_type, is_volatile),
4999 dex_pc),
5000 field_info_(field_offset,
5001 field_type,
5002 is_volatile,
5003 field_idx,
5004 declaring_class_def_index,
5005 dex_file,
5006 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005007 SetRawInputAt(0, value);
5008 }
5009
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005010 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005011
Vladimir Marko372f10e2016-05-17 16:30:10 +01005012 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5013 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005014 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005015 }
5016
Calin Juravle641547a2015-04-21 22:08:51 +01005017 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5018 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005019 }
5020
5021 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005022 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5023 }
5024
Calin Juravle52c48962014-12-16 17:02:57 +00005025 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005026 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005027 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005028 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005029
5030 DECLARE_INSTRUCTION(InstanceFieldGet);
5031
5032 private:
5033 const FieldInfo field_info_;
5034
5035 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5036};
5037
Vladimir Markofcb503c2016-05-18 12:48:17 +01005038class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005039 public:
5040 HInstanceFieldSet(HInstruction* object,
5041 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005042 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005043 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005044 bool is_volatile,
5045 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005046 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005047 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005048 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005049 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005050 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5051 dex_pc),
5052 field_info_(field_offset,
5053 field_type,
5054 is_volatile,
5055 field_idx,
5056 declaring_class_def_index,
5057 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005058 dex_cache) {
5059 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005060 SetRawInputAt(0, object);
5061 SetRawInputAt(1, value);
5062 }
5063
Calin Juravle641547a2015-04-21 22:08:51 +01005064 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5065 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005066 }
5067
Calin Juravle52c48962014-12-16 17:02:57 +00005068 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005069 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005070 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005071 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005072 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005073 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5074 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005075
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005076 DECLARE_INSTRUCTION(InstanceFieldSet);
5077
5078 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005079 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5080 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5081 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5082 "Too many packed fields.");
5083
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005084 const FieldInfo field_info_;
5085
5086 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5087};
5088
Vladimir Markofcb503c2016-05-18 12:48:17 +01005089class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005090 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005091 HArrayGet(HInstruction* array,
5092 HInstruction* index,
5093 Primitive::Type type,
5094 uint32_t dex_pc,
5095 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005096 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005097 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005098 SetRawInputAt(0, array);
5099 SetRawInputAt(1, index);
5100 }
5101
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005102 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005103 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005104 return true;
5105 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005106 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005107 // TODO: We can be smarter here.
5108 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5109 // which generates the implicit null check. There are cases when these can be removed
5110 // to produce better code. If we ever add optimizations to do so we should allow an
5111 // implicit check here (as long as the address falls in the first page).
5112 return false;
5113 }
5114
David Brazdil4833f5a2015-12-16 10:37:39 +00005115 bool IsEquivalentOf(HArrayGet* other) const {
5116 bool result = (GetDexPc() == other->GetDexPc());
5117 if (kIsDebugBuild && result) {
5118 DCHECK_EQ(GetBlock(), other->GetBlock());
5119 DCHECK_EQ(GetArray(), other->GetArray());
5120 DCHECK_EQ(GetIndex(), other->GetIndex());
5121 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005122 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005123 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005124 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5125 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005126 }
5127 }
5128 return result;
5129 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005130
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005131 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5132
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005133 HInstruction* GetArray() const { return InputAt(0); }
5134 HInstruction* GetIndex() const { return InputAt(1); }
5135
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005136 DECLARE_INSTRUCTION(ArrayGet);
5137
5138 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005139 // We treat a String as an array, creating the HArrayGet from String.charAt()
5140 // intrinsic in the instruction simplifier. We can always determine whether
5141 // a particular HArrayGet is actually a String.charAt() by looking at the type
5142 // of the input but that requires holding the mutator lock, so we prefer to use
5143 // a flag, so that code generators don't need to do the locking.
5144 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5145 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5146 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5147 "Too many packed fields.");
5148
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005149 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5150};
5151
Vladimir Markofcb503c2016-05-18 12:48:17 +01005152class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005153 public:
5154 HArraySet(HInstruction* array,
5155 HInstruction* index,
5156 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005157 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005158 uint32_t dex_pc)
5159 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005160 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5161 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5162 SetPackedFlag<kFlagValueCanBeNull>(true);
5163 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005164 SetRawInputAt(0, array);
5165 SetRawInputAt(1, index);
5166 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005167 // Make a best guess now, may be refined during SSA building.
5168 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005169 }
5170
Calin Juravle77520bc2015-01-12 18:45:46 +00005171 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005172 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005173 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005174 }
5175
Mingyao Yang81014cb2015-06-02 03:16:27 -07005176 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005177 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005178
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005179 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005180 // TODO: Same as for ArrayGet.
5181 return false;
5182 }
5183
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005184 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005185 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005186 }
5187
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005188 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005189 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005190 }
5191
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005192 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005193 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005194 }
5195
Vladimir Markoa1de9182016-02-25 11:37:38 +00005196 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5197 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5198 bool StaticTypeOfArrayIsObjectArray() const {
5199 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5200 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005201
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005202 HInstruction* GetArray() const { return InputAt(0); }
5203 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005204 HInstruction* GetValue() const { return InputAt(2); }
5205
5206 Primitive::Type GetComponentType() const {
5207 // The Dex format does not type floating point index operations. Since the
5208 // `expected_component_type_` is set during building and can therefore not
5209 // be correct, we also check what is the value type. If it is a floating
5210 // point type, we must use that type.
5211 Primitive::Type value_type = GetValue()->GetType();
5212 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5213 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005214 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005215 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005216
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005217 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005218 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005219 }
5220
Aart Bik18b36ab2016-04-13 16:41:35 -07005221 void ComputeSideEffects() {
5222 Primitive::Type type = GetComponentType();
5223 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5224 SideEffectsForArchRuntimeCalls(type)));
5225 }
5226
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005227 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5228 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5229 }
5230
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005231 DECLARE_INSTRUCTION(ArraySet);
5232
5233 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005234 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5235 static constexpr size_t kFieldExpectedComponentTypeSize =
5236 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5237 static constexpr size_t kFlagNeedsTypeCheck =
5238 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5239 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005240 // Cached information for the reference_type_info_ so that codegen
5241 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005242 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5243 static constexpr size_t kNumberOfArraySetPackedBits =
5244 kFlagStaticTypeOfArrayIsObjectArray + 1;
5245 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5246 using ExpectedComponentTypeField =
5247 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005248
5249 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5250};
5251
Vladimir Markofcb503c2016-05-18 12:48:17 +01005252class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005253 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005254 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005255 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005256 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005257 // Note that arrays do not change length, so the instruction does not
5258 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005259 SetRawInputAt(0, array);
5260 }
5261
Calin Juravle77520bc2015-01-12 18:45:46 +00005262 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005263 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005264 return true;
5265 }
Calin Juravle641547a2015-04-21 22:08:51 +01005266 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5267 return obj == InputAt(0);
5268 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005269
Vladimir Markodce016e2016-04-28 13:10:02 +01005270 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5271
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005272 DECLARE_INSTRUCTION(ArrayLength);
5273
5274 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005275 // We treat a String as an array, creating the HArrayLength from String.length()
5276 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5277 // determine whether a particular HArrayLength is actually a String.length() by
5278 // looking at the type of the input but that requires holding the mutator lock, so
5279 // we prefer to use a flag, so that code generators don't need to do the locking.
5280 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5281 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5282 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5283 "Too many packed fields.");
5284
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005285 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5286};
5287
Vladimir Markofcb503c2016-05-18 12:48:17 +01005288class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005289 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005290 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5291 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005292 HBoundsCheck(HInstruction* index,
5293 HInstruction* length,
5294 uint32_t dex_pc,
5295 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5296 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5297 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005298 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005299 SetRawInputAt(0, index);
5300 SetRawInputAt(1, length);
5301 }
5302
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005303 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005304 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005305 return true;
5306 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005307
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005308 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005309
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005310 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005311
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005312 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5313 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5314
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005315 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005316
5317 DECLARE_INSTRUCTION(BoundsCheck);
5318
5319 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005320 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5321 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5322 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5323 // so we need to create an HEnvironment which will be translated to an InlineInfo
5324 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5325 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5326 // from the invoke as we don't want to look again at the dex bytecode.
5327 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5328
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005329 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5330};
5331
Vladimir Markofcb503c2016-05-18 12:48:17 +01005332class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005333 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005334 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005335 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005336
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005337 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005338 return true;
5339 }
5340
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005341 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5342 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005343
5344 DECLARE_INSTRUCTION(SuspendCheck);
5345
5346 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005347 // Only used for code generation, in order to share the same slow path between back edges
5348 // of a same loop.
5349 SlowPathCode* slow_path_;
5350
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005351 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5352};
5353
David Srbecky0cf44932015-12-09 14:09:59 +00005354// Pseudo-instruction which provides the native debugger with mapping information.
5355// It ensures that we can generate line number and local variables at this point.
5356class HNativeDebugInfo : public HTemplateInstruction<0> {
5357 public:
5358 explicit HNativeDebugInfo(uint32_t dex_pc)
5359 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5360
5361 bool NeedsEnvironment() const OVERRIDE {
5362 return true;
5363 }
5364
5365 DECLARE_INSTRUCTION(NativeDebugInfo);
5366
5367 private:
5368 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5369};
5370
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005371/**
5372 * Instruction to load a Class object.
5373 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005374class HLoadClass FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005375 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005376 HLoadClass(HCurrentMethod* current_method,
5377 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005378 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005379 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005380 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005381 bool needs_access_check,
5382 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005383 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005384 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005385 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005386 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005387 // Referrers class should not need access check. We never inline unverified
5388 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005389 DCHECK(!is_referrers_class || !needs_access_check);
5390
5391 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5392 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5393 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5394 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005395 SetRawInputAt(0, current_method);
5396 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005397
5398 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005399
Vladimir Marko372f10e2016-05-17 16:30:10 +01005400 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005401 // Note that we don't need to test for generate_clinit_check_.
5402 // Whether or not we need to generate the clinit check is processed in
5403 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005404 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005405 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005406 }
5407
5408 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5409
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005410 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005411 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005412
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005413 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005414 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005415 }
5416
Calin Juravle0ba218d2015-05-19 18:46:01 +01005417 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005418 // The entrypoint the code generator is going to call does not do
5419 // clinit of the class.
5420 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005421 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005422 }
5423
5424 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005425 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005426 (!IsReferrersClass() && !IsInDexCache()) ||
5427 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005428 }
5429
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005430
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005431 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005432 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005433 }
5434
Calin Juravleacf735c2015-02-12 15:25:22 +00005435 ReferenceTypeInfo GetLoadedClassRTI() {
5436 return loaded_class_rti_;
5437 }
5438
5439 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5440 // Make sure we only set exact types (the loaded class should never be merged).
5441 DCHECK(rti.IsExact());
5442 loaded_class_rti_ = rti;
5443 }
5444
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005445 const DexFile& GetDexFile() { return dex_file_; }
5446
Vladimir Markoa1de9182016-02-25 11:37:38 +00005447 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005448
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005449 static SideEffects SideEffectsForArchRuntimeCalls() {
5450 return SideEffects::CanTriggerGC();
5451 }
5452
Vladimir Markoa1de9182016-02-25 11:37:38 +00005453 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5454 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5455 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5456 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005457
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005458 DECLARE_INSTRUCTION(LoadClass);
5459
5460 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005461 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5462 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5463 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005464 // Whether this instruction must generate the initialization check.
5465 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005466 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5467 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5468 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5469
5470 const uint16_t type_index_;
5471 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005472
Calin Juravleacf735c2015-02-12 15:25:22 +00005473 ReferenceTypeInfo loaded_class_rti_;
5474
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005475 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5476};
5477
Vladimir Marko372f10e2016-05-17 16:30:10 +01005478class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005479 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005480 // Determines how to load the String.
5481 enum class LoadKind {
5482 // Use boot image String* address that will be known at link time.
5483 // Used for boot image strings referenced by boot image code in non-PIC mode.
5484 kBootImageLinkTimeAddress,
5485
5486 // Use PC-relative boot image String* address that will be known at link time.
5487 // Used for boot image strings referenced by boot image code in PIC mode.
5488 kBootImageLinkTimePcRelative,
5489
5490 // Use a known boot image String* address, embedded in the code by the codegen.
5491 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5492 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5493 // GetIncludePatchInformation().
5494 kBootImageAddress,
5495
5496 // Load from the resolved strings array at an absolute address.
5497 // Used for strings outside the boot image referenced by JIT-compiled code.
5498 kDexCacheAddress,
5499
5500 // Load from resolved strings array in the dex cache using a PC-relative load.
5501 // Used for strings outside boot image when we know that we can access
5502 // the dex cache arrays using a PC-relative load.
5503 kDexCachePcRelative,
5504
5505 // Load from resolved strings array accessed through the class loaded from
5506 // the compiled method's own ArtMethod*. This is the default access type when
5507 // all other types are unavailable.
5508 kDexCacheViaMethod,
5509
5510 kLast = kDexCacheViaMethod
5511 };
5512
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005513 HLoadString(HCurrentMethod* current_method,
5514 uint32_t string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005515 const DexFile& dex_file,
5516 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005517 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5518 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005519 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005520 SetPackedFlag<kFlagIsInDexCache>(false);
5521 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
5522 load_data_.ref.dex_file = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005523 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005524
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005525 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5526 DCHECK(HasAddress(load_kind));
5527 load_data_.address = address;
5528 SetLoadKindInternal(load_kind);
5529 }
5530
5531 void SetLoadKindWithStringReference(LoadKind load_kind,
5532 const DexFile& dex_file,
5533 uint32_t string_index) {
5534 DCHECK(HasStringReference(load_kind));
5535 load_data_.ref.dex_file = &dex_file;
5536 string_index_ = string_index;
5537 SetLoadKindInternal(load_kind);
5538 }
5539
5540 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5541 const DexFile& dex_file,
5542 uint32_t element_index) {
5543 DCHECK(HasDexCacheReference(load_kind));
5544 load_data_.ref.dex_file = &dex_file;
5545 load_data_.ref.dex_cache_element_index = element_index;
5546 SetLoadKindInternal(load_kind);
5547 }
5548
5549 LoadKind GetLoadKind() const {
5550 return GetPackedField<LoadKindField>();
5551 }
5552
5553 const DexFile& GetDexFile() const;
5554
5555 uint32_t GetStringIndex() const {
5556 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5557 return string_index_;
5558 }
5559
5560 uint32_t GetDexCacheElementOffset() const;
5561
5562 uint64_t GetAddress() const {
5563 DCHECK(HasAddress(GetLoadKind()));
5564 return load_data_.address;
5565 }
5566
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005567 bool CanBeMoved() const OVERRIDE { return true; }
5568
Vladimir Marko372f10e2016-05-17 16:30:10 +01005569 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005570
5571 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5572
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005573 // Will call the runtime if we need to load the string through
5574 // the dex cache and the string is not guaranteed to be there yet.
5575 bool NeedsEnvironment() const OVERRIDE {
5576 LoadKind load_kind = GetLoadKind();
5577 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5578 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5579 load_kind == LoadKind::kBootImageAddress) {
5580 return false;
5581 }
5582 return !IsInDexCache();
5583 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005584
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005585 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5586 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5587 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005588
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005589 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005590 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005591
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005592 static SideEffects SideEffectsForArchRuntimeCalls() {
5593 return SideEffects::CanTriggerGC();
5594 }
5595
Vladimir Markoa1de9182016-02-25 11:37:38 +00005596 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5597
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005598 void MarkInDexCache() {
5599 SetPackedFlag<kFlagIsInDexCache>(true);
5600 DCHECK(!NeedsEnvironment());
5601 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005602 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005603 }
5604
Vladimir Marko372f10e2016-05-17 16:30:10 +01005605 void AddSpecialInput(HInstruction* special_input);
5606
5607 using HInstruction::GetInputRecords; // Keep the const version visible.
5608 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5609 return ArrayRef<HUserRecord<HInstruction*>>(
5610 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005611 }
5612
Vladimir Marko372f10e2016-05-17 16:30:10 +01005613 Primitive::Type GetType() const OVERRIDE {
5614 return Primitive::kPrimNot;
5615 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005616
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005617 DECLARE_INSTRUCTION(LoadString);
5618
5619 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005620 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005621 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5622 static constexpr size_t kFieldLoadKindSize =
5623 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5624 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005625 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005626 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005627
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005628 static bool HasStringReference(LoadKind load_kind) {
5629 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5630 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5631 load_kind == LoadKind::kDexCacheViaMethod;
5632 }
5633
5634 static bool HasAddress(LoadKind load_kind) {
5635 return load_kind == LoadKind::kBootImageAddress || load_kind == LoadKind::kDexCacheAddress;
5636 }
5637
5638 static bool HasDexCacheReference(LoadKind load_kind) {
5639 return load_kind == LoadKind::kDexCachePcRelative;
5640 }
5641
5642 void SetLoadKindInternal(LoadKind load_kind);
5643
Vladimir Marko372f10e2016-05-17 16:30:10 +01005644 HUserRecord<HInstruction*> special_input_;
5645
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005646 // String index serves also as the hash code and it's also needed for slow-paths,
5647 // so it must not be overwritten with other load data.
5648 uint32_t string_index_;
5649
5650 union {
5651 struct {
5652 const DexFile* dex_file; // For string reference and dex cache reference.
5653 uint32_t dex_cache_element_index; // Only for dex cache reference.
5654 } ref;
5655 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5656 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005657
5658 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5659};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005660std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5661
5662// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5663inline const DexFile& HLoadString::GetDexFile() const {
5664 DCHECK(HasStringReference(GetLoadKind()) || HasDexCacheReference(GetLoadKind()))
5665 << GetLoadKind();
5666 return *load_data_.ref.dex_file;
5667}
5668
5669// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5670inline uint32_t HLoadString::GetDexCacheElementOffset() const {
5671 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5672 return load_data_.ref.dex_cache_element_index;
5673}
5674
5675// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5676inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
5677 // The special input is used for PC-relative loads on some architectures.
5678 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
5679 GetLoadKind() == LoadKind::kDexCachePcRelative) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005680 // HLoadString::GetInputRecords() returns an empty array at this point,
5681 // so use the GetInputRecords() from the base class to set the input record.
5682 DCHECK(special_input_.GetInstruction() == nullptr);
5683 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005684 special_input->AddUseAt(this, 0);
5685}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005686
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005687/**
5688 * Performs an initialization check on its Class object input.
5689 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005690class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005691 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005692 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005693 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005694 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005695 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5696 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005697 SetRawInputAt(0, constant);
5698 }
5699
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005700 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005701 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005702 return true;
5703 }
5704
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005705 bool NeedsEnvironment() const OVERRIDE {
5706 // May call runtime to initialize the class.
5707 return true;
5708 }
5709
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005710 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005711
5712 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5713
5714 DECLARE_INSTRUCTION(ClinitCheck);
5715
5716 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005717 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5718};
5719
Vladimir Markofcb503c2016-05-18 12:48:17 +01005720class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005721 public:
5722 HStaticFieldGet(HInstruction* cls,
5723 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005724 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005725 bool is_volatile,
5726 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005727 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005728 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005729 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005730 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005731 : HExpression(field_type,
5732 SideEffects::FieldReadOfType(field_type, is_volatile),
5733 dex_pc),
5734 field_info_(field_offset,
5735 field_type,
5736 is_volatile,
5737 field_idx,
5738 declaring_class_def_index,
5739 dex_file,
5740 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005741 SetRawInputAt(0, cls);
5742 }
5743
Calin Juravle52c48962014-12-16 17:02:57 +00005744
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005745 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005746
Vladimir Marko372f10e2016-05-17 16:30:10 +01005747 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5748 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005749 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005750 }
5751
5752 size_t ComputeHashCode() const OVERRIDE {
5753 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5754 }
5755
Calin Juravle52c48962014-12-16 17:02:57 +00005756 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005757 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5758 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005759 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005760
5761 DECLARE_INSTRUCTION(StaticFieldGet);
5762
5763 private:
5764 const FieldInfo field_info_;
5765
5766 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5767};
5768
Vladimir Markofcb503c2016-05-18 12:48:17 +01005769class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005770 public:
5771 HStaticFieldSet(HInstruction* cls,
5772 HInstruction* value,
5773 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005774 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005775 bool is_volatile,
5776 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005777 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005778 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005779 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005780 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005781 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5782 dex_pc),
5783 field_info_(field_offset,
5784 field_type,
5785 is_volatile,
5786 field_idx,
5787 declaring_class_def_index,
5788 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005789 dex_cache) {
5790 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005791 SetRawInputAt(0, cls);
5792 SetRawInputAt(1, value);
5793 }
5794
Calin Juravle52c48962014-12-16 17:02:57 +00005795 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005796 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5797 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005798 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005799
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005800 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005801 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5802 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005803
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005804 DECLARE_INSTRUCTION(StaticFieldSet);
5805
5806 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005807 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5808 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5809 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5810 "Too many packed fields.");
5811
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005812 const FieldInfo field_info_;
5813
5814 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5815};
5816
Vladimir Markofcb503c2016-05-18 12:48:17 +01005817class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005818 public:
5819 HUnresolvedInstanceFieldGet(HInstruction* obj,
5820 Primitive::Type field_type,
5821 uint32_t field_index,
5822 uint32_t dex_pc)
5823 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5824 field_index_(field_index) {
5825 SetRawInputAt(0, obj);
5826 }
5827
5828 bool NeedsEnvironment() const OVERRIDE { return true; }
5829 bool CanThrow() const OVERRIDE { return true; }
5830
5831 Primitive::Type GetFieldType() const { return GetType(); }
5832 uint32_t GetFieldIndex() const { return field_index_; }
5833
5834 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5835
5836 private:
5837 const uint32_t field_index_;
5838
5839 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5840};
5841
Vladimir Markofcb503c2016-05-18 12:48:17 +01005842class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005843 public:
5844 HUnresolvedInstanceFieldSet(HInstruction* obj,
5845 HInstruction* value,
5846 Primitive::Type field_type,
5847 uint32_t field_index,
5848 uint32_t dex_pc)
5849 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005850 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005851 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005852 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005853 SetRawInputAt(0, obj);
5854 SetRawInputAt(1, value);
5855 }
5856
5857 bool NeedsEnvironment() const OVERRIDE { return true; }
5858 bool CanThrow() const OVERRIDE { return true; }
5859
Vladimir Markoa1de9182016-02-25 11:37:38 +00005860 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005861 uint32_t GetFieldIndex() const { return field_index_; }
5862
5863 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5864
5865 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005866 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5867 static constexpr size_t kFieldFieldTypeSize =
5868 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5869 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5870 kFieldFieldType + kFieldFieldTypeSize;
5871 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5872 "Too many packed fields.");
5873 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5874
Calin Juravlee460d1d2015-09-29 04:52:17 +01005875 const uint32_t field_index_;
5876
5877 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5878};
5879
Vladimir Markofcb503c2016-05-18 12:48:17 +01005880class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005881 public:
5882 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5883 uint32_t field_index,
5884 uint32_t dex_pc)
5885 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5886 field_index_(field_index) {
5887 }
5888
5889 bool NeedsEnvironment() const OVERRIDE { return true; }
5890 bool CanThrow() const OVERRIDE { return true; }
5891
5892 Primitive::Type GetFieldType() const { return GetType(); }
5893 uint32_t GetFieldIndex() const { return field_index_; }
5894
5895 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5896
5897 private:
5898 const uint32_t field_index_;
5899
5900 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5901};
5902
Vladimir Markofcb503c2016-05-18 12:48:17 +01005903class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01005904 public:
5905 HUnresolvedStaticFieldSet(HInstruction* value,
5906 Primitive::Type field_type,
5907 uint32_t field_index,
5908 uint32_t dex_pc)
5909 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005910 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005911 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00005912 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01005913 SetRawInputAt(0, value);
5914 }
5915
5916 bool NeedsEnvironment() const OVERRIDE { return true; }
5917 bool CanThrow() const OVERRIDE { return true; }
5918
Vladimir Markoa1de9182016-02-25 11:37:38 +00005919 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005920 uint32_t GetFieldIndex() const { return field_index_; }
5921
5922 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5923
5924 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005925 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5926 static constexpr size_t kFieldFieldTypeSize =
5927 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5928 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5929 kFieldFieldType + kFieldFieldTypeSize;
5930 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5931 "Too many packed fields.");
5932 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5933
Calin Juravlee460d1d2015-09-29 04:52:17 +01005934 const uint32_t field_index_;
5935
5936 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5937};
5938
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005939// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005940class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005941 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005942 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5943 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005944
David Brazdilbbd733e2015-08-18 17:48:17 +01005945 bool CanBeNull() const OVERRIDE { return false; }
5946
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005947 DECLARE_INSTRUCTION(LoadException);
5948
5949 private:
5950 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5951};
5952
David Brazdilcb1c0552015-08-04 16:22:25 +01005953// Implicit part of move-exception which clears thread-local exception storage.
5954// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005955class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01005956 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005957 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5958 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005959
5960 DECLARE_INSTRUCTION(ClearException);
5961
5962 private:
5963 DISALLOW_COPY_AND_ASSIGN(HClearException);
5964};
5965
Vladimir Markofcb503c2016-05-18 12:48:17 +01005966class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005967 public:
5968 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005969 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005970 SetRawInputAt(0, exception);
5971 }
5972
5973 bool IsControlFlow() const OVERRIDE { return true; }
5974
5975 bool NeedsEnvironment() const OVERRIDE { return true; }
5976
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005977 bool CanThrow() const OVERRIDE { return true; }
5978
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005979
5980 DECLARE_INSTRUCTION(Throw);
5981
5982 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005983 DISALLOW_COPY_AND_ASSIGN(HThrow);
5984};
5985
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005986/**
5987 * Implementation strategies for the code generator of a HInstanceOf
5988 * or `HCheckCast`.
5989 */
5990enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005991 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005992 kExactCheck, // Can do a single class compare.
5993 kClassHierarchyCheck, // Can just walk the super class chain.
5994 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5995 kInterfaceCheck, // No optimization yet when checking against an interface.
5996 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005997 kArrayCheck, // No optimization yet when checking against a generic array.
5998 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005999};
6000
Roland Levillain86503782016-02-11 19:07:30 +00006001std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6002
Vladimir Markofcb503c2016-05-18 12:48:17 +01006003class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006004 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006005 HInstanceOf(HInstruction* object,
6006 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006007 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006008 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006009 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006010 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006011 dex_pc) {
6012 SetPackedField<TypeCheckKindField>(check_kind);
6013 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006014 SetRawInputAt(0, object);
6015 SetRawInputAt(1, constant);
6016 }
6017
6018 bool CanBeMoved() const OVERRIDE { return true; }
6019
Vladimir Marko372f10e2016-05-17 16:30:10 +01006020 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006021 return true;
6022 }
6023
6024 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006025 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006026 }
6027
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006028 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006029 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6030 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6031 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6032 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006033
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006034 static bool CanCallRuntime(TypeCheckKind check_kind) {
6035 // Mips currently does runtime calls for any other checks.
6036 return check_kind != TypeCheckKind::kExactCheck;
6037 }
6038
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006039 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006040 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006041 }
6042
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006043 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006044
6045 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006046 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6047 static constexpr size_t kFieldTypeCheckKindSize =
6048 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6049 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6050 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6051 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6052 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006053
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006054 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6055};
6056
Vladimir Markofcb503c2016-05-18 12:48:17 +01006057class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006058 public:
David Brazdilf5552582015-12-27 13:36:12 +00006059 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006060 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006061 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6062 SetPackedFlag<kFlagUpperCanBeNull>(true);
6063 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006064 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006065 SetRawInputAt(0, input);
6066 }
6067
David Brazdilf5552582015-12-27 13:36:12 +00006068 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006069 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006070 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006071 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006072
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006073 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006074 DCHECK(GetUpperCanBeNull() || !can_be_null);
6075 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006076 }
6077
Vladimir Markoa1de9182016-02-25 11:37:38 +00006078 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006079
Calin Juravleb1498f62015-02-16 13:13:29 +00006080 DECLARE_INSTRUCTION(BoundType);
6081
6082 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006083 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6084 // is false then CanBeNull() cannot be true).
6085 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6086 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6087 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6088 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6089
Calin Juravleb1498f62015-02-16 13:13:29 +00006090 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006091 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6092 // It is used to bound the type in cases like:
6093 // if (x instanceof ClassX) {
6094 // // uper_bound_ will be ClassX
6095 // }
David Brazdilf5552582015-12-27 13:36:12 +00006096 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006097
6098 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6099};
6100
Vladimir Markofcb503c2016-05-18 12:48:17 +01006101class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006102 public:
6103 HCheckCast(HInstruction* object,
6104 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006105 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006106 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006107 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6108 SetPackedField<TypeCheckKindField>(check_kind);
6109 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006110 SetRawInputAt(0, object);
6111 SetRawInputAt(1, constant);
6112 }
6113
6114 bool CanBeMoved() const OVERRIDE { return true; }
6115
Vladimir Marko372f10e2016-05-17 16:30:10 +01006116 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006117 return true;
6118 }
6119
6120 bool NeedsEnvironment() const OVERRIDE {
6121 // Instruction may throw a CheckCastError.
6122 return true;
6123 }
6124
6125 bool CanThrow() const OVERRIDE { return true; }
6126
Vladimir Markoa1de9182016-02-25 11:37:38 +00006127 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6128 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6129 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6130 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006131
6132 DECLARE_INSTRUCTION(CheckCast);
6133
6134 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006135 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6136 static constexpr size_t kFieldTypeCheckKindSize =
6137 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6138 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6139 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6140 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6141 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006142
6143 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006144};
6145
Vladimir Markofcb503c2016-05-18 12:48:17 +01006146class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006147 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006148 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006149 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006150 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6151 SetPackedField<BarrierKindField>(barrier_kind);
6152 }
Calin Juravle27df7582015-04-17 19:12:31 +01006153
Vladimir Markoa1de9182016-02-25 11:37:38 +00006154 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006155
6156 DECLARE_INSTRUCTION(MemoryBarrier);
6157
6158 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006159 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6160 static constexpr size_t kFieldBarrierKindSize =
6161 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6162 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6163 kFieldBarrierKind + kFieldBarrierKindSize;
6164 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6165 "Too many packed fields.");
6166 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006167
6168 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6169};
6170
Vladimir Markofcb503c2016-05-18 12:48:17 +01006171class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006172 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006173 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006174 kEnter,
6175 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006176 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006177 };
6178
6179 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006180 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006181 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6182 dex_pc) {
6183 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006184 SetRawInputAt(0, object);
6185 }
6186
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006187 // Instruction may go into runtime, so we need an environment.
6188 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006189
6190 bool CanThrow() const OVERRIDE {
6191 // Verifier guarantees that monitor-exit cannot throw.
6192 // This is important because it allows the HGraphBuilder to remove
6193 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6194 return IsEnter();
6195 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006196
Vladimir Markoa1de9182016-02-25 11:37:38 +00006197 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6198 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006199
6200 DECLARE_INSTRUCTION(MonitorOperation);
6201
Calin Juravle52c48962014-12-16 17:02:57 +00006202 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006203 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6204 static constexpr size_t kFieldOperationKindSize =
6205 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6206 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6207 kFieldOperationKind + kFieldOperationKindSize;
6208 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6209 "Too many packed fields.");
6210 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006211
6212 private:
6213 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6214};
6215
Vladimir Markofcb503c2016-05-18 12:48:17 +01006216class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006217 public:
6218 HSelect(HInstruction* condition,
6219 HInstruction* true_value,
6220 HInstruction* false_value,
6221 uint32_t dex_pc)
6222 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6223 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6224
6225 // First input must be `true_value` or `false_value` to allow codegens to
6226 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6227 // that architectures which implement HSelect as a conditional move also
6228 // will not need to invert the condition.
6229 SetRawInputAt(0, false_value);
6230 SetRawInputAt(1, true_value);
6231 SetRawInputAt(2, condition);
6232 }
6233
6234 HInstruction* GetFalseValue() const { return InputAt(0); }
6235 HInstruction* GetTrueValue() const { return InputAt(1); }
6236 HInstruction* GetCondition() const { return InputAt(2); }
6237
6238 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006239 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6240 return true;
6241 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006242
6243 bool CanBeNull() const OVERRIDE {
6244 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6245 }
6246
6247 DECLARE_INSTRUCTION(Select);
6248
6249 private:
6250 DISALLOW_COPY_AND_ASSIGN(HSelect);
6251};
6252
Vladimir Markof9f64412015-09-02 14:05:49 +01006253class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006254 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006255 MoveOperands(Location source,
6256 Location destination,
6257 Primitive::Type type,
6258 HInstruction* instruction)
6259 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006260
6261 Location GetSource() const { return source_; }
6262 Location GetDestination() const { return destination_; }
6263
6264 void SetSource(Location value) { source_ = value; }
6265 void SetDestination(Location value) { destination_ = value; }
6266
6267 // The parallel move resolver marks moves as "in-progress" by clearing the
6268 // destination (but not the source).
6269 Location MarkPending() {
6270 DCHECK(!IsPending());
6271 Location dest = destination_;
6272 destination_ = Location::NoLocation();
6273 return dest;
6274 }
6275
6276 void ClearPending(Location dest) {
6277 DCHECK(IsPending());
6278 destination_ = dest;
6279 }
6280
6281 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006282 DCHECK(source_.IsValid() || destination_.IsInvalid());
6283 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006284 }
6285
6286 // True if this blocks a move from the given location.
6287 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006288 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006289 }
6290
6291 // A move is redundant if it's been eliminated, if its source and
6292 // destination are the same, or if its destination is unneeded.
6293 bool IsRedundant() const {
6294 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6295 }
6296
6297 // We clear both operands to indicate move that's been eliminated.
6298 void Eliminate() {
6299 source_ = destination_ = Location::NoLocation();
6300 }
6301
6302 bool IsEliminated() const {
6303 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6304 return source_.IsInvalid();
6305 }
6306
Alexey Frunze4dda3372015-06-01 18:31:49 -07006307 Primitive::Type GetType() const { return type_; }
6308
Nicolas Geoffray90218252015-04-15 11:56:51 +01006309 bool Is64BitMove() const {
6310 return Primitive::Is64BitType(type_);
6311 }
6312
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006313 HInstruction* GetInstruction() const { return instruction_; }
6314
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006315 private:
6316 Location source_;
6317 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006318 // The type this move is for.
6319 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006320 // The instruction this move is assocatied with. Null when this move is
6321 // for moving an input in the expected locations of user (including a phi user).
6322 // This is only used in debug mode, to ensure we do not connect interval siblings
6323 // in the same parallel move.
6324 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006325};
6326
Roland Levillainc9285912015-12-18 10:38:42 +00006327std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6328
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006329static constexpr size_t kDefaultNumberOfMoves = 4;
6330
Vladimir Markofcb503c2016-05-18 12:48:17 +01006331class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006332 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006333 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006334 : HTemplateInstruction(SideEffects::None(), dex_pc),
6335 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6336 moves_.reserve(kDefaultNumberOfMoves);
6337 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006338
Nicolas Geoffray90218252015-04-15 11:56:51 +01006339 void AddMove(Location source,
6340 Location destination,
6341 Primitive::Type type,
6342 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006343 DCHECK(source.IsValid());
6344 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006345 if (kIsDebugBuild) {
6346 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006347 for (const MoveOperands& move : moves_) {
6348 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006349 // Special case the situation where the move is for the spill slot
6350 // of the instruction.
6351 if ((GetPrevious() == instruction)
6352 || ((GetPrevious() == nullptr)
6353 && instruction->IsPhi()
6354 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006355 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006356 << "Doing parallel moves for the same instruction.";
6357 } else {
6358 DCHECK(false) << "Doing parallel moves for the same instruction.";
6359 }
6360 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006361 }
6362 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006363 for (const MoveOperands& move : moves_) {
6364 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006365 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006366 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006367 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006368 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006369 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006370 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006371 }
6372
Vladimir Marko225b6462015-09-28 12:17:40 +01006373 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006374 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006375 }
6376
Vladimir Marko225b6462015-09-28 12:17:40 +01006377 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006378
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006379 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006380
6381 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006382 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006383
6384 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6385};
6386
Mark Mendell0616ae02015-04-17 12:49:27 -04006387} // namespace art
6388
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006389#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6390#include "nodes_shared.h"
6391#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006392#ifdef ART_ENABLE_CODEGEN_arm
6393#include "nodes_arm.h"
6394#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006395#ifdef ART_ENABLE_CODEGEN_arm64
6396#include "nodes_arm64.h"
6397#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006398#ifdef ART_ENABLE_CODEGEN_x86
6399#include "nodes_x86.h"
6400#endif
6401
6402namespace art {
6403
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006404class HGraphVisitor : public ValueObject {
6405 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006406 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6407 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006408
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006409 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006410 virtual void VisitBasicBlock(HBasicBlock* block);
6411
Roland Levillain633021e2014-10-01 14:12:25 +01006412 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006413 void VisitInsertionOrder();
6414
Roland Levillain633021e2014-10-01 14:12:25 +01006415 // Visit the graph following dominator tree reverse post-order.
6416 void VisitReversePostOrder();
6417
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006418 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006419
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006420 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006421#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006422 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6423
6424 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6425
6426#undef DECLARE_VISIT_INSTRUCTION
6427
6428 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006429 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006430
6431 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6432};
6433
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006434class HGraphDelegateVisitor : public HGraphVisitor {
6435 public:
6436 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6437 virtual ~HGraphDelegateVisitor() {}
6438
6439 // Visit functions that delegate to to super class.
6440#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006441 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006442
6443 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6444
6445#undef DECLARE_VISIT_INSTRUCTION
6446
6447 private:
6448 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6449};
6450
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006451class HInsertionOrderIterator : public ValueObject {
6452 public:
6453 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6454
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006455 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006456 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006457 void Advance() { ++index_; }
6458
6459 private:
6460 const HGraph& graph_;
6461 size_t index_;
6462
6463 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6464};
6465
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006466class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006467 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006468 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6469 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006470 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006471 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006472
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006473 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6474 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006475 void Advance() { ++index_; }
6476
6477 private:
6478 const HGraph& graph_;
6479 size_t index_;
6480
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006481 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006482};
6483
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006484class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006485 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006486 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006487 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006488 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006489 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006490 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006491
6492 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006493 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006494 void Advance() { --index_; }
6495
6496 private:
6497 const HGraph& graph_;
6498 size_t index_;
6499
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006500 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006501};
6502
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006503class HLinearPostOrderIterator : public ValueObject {
6504 public:
6505 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006506 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006507
6508 bool Done() const { return index_ == 0; }
6509
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006510 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006511
6512 void Advance() {
6513 --index_;
6514 DCHECK_GE(index_, 0U);
6515 }
6516
6517 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006518 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006519 size_t index_;
6520
6521 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6522};
6523
6524class HLinearOrderIterator : public ValueObject {
6525 public:
6526 explicit HLinearOrderIterator(const HGraph& graph)
6527 : order_(graph.GetLinearOrder()), index_(0) {}
6528
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006529 bool Done() const { return index_ == order_.size(); }
6530 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006531 void Advance() { ++index_; }
6532
6533 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006534 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006535 size_t index_;
6536
6537 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6538};
6539
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006540// Iterator over the blocks that art part of the loop. Includes blocks part
6541// of an inner loop. The order in which the blocks are iterated is on their
6542// block id.
6543class HBlocksInLoopIterator : public ValueObject {
6544 public:
6545 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6546 : blocks_in_loop_(info.GetBlocks()),
6547 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6548 index_(0) {
6549 if (!blocks_in_loop_.IsBitSet(index_)) {
6550 Advance();
6551 }
6552 }
6553
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006554 bool Done() const { return index_ == blocks_.size(); }
6555 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006556 void Advance() {
6557 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006558 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006559 if (blocks_in_loop_.IsBitSet(index_)) {
6560 break;
6561 }
6562 }
6563 }
6564
6565 private:
6566 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006567 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006568 size_t index_;
6569
6570 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6571};
6572
Mingyao Yang3584bce2015-05-19 16:01:59 -07006573// Iterator over the blocks that art part of the loop. Includes blocks part
6574// of an inner loop. The order in which the blocks are iterated is reverse
6575// post order.
6576class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6577 public:
6578 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6579 : blocks_in_loop_(info.GetBlocks()),
6580 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6581 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006582 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006583 Advance();
6584 }
6585 }
6586
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006587 bool Done() const { return index_ == blocks_.size(); }
6588 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006589 void Advance() {
6590 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006591 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6592 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006593 break;
6594 }
6595 }
6596 }
6597
6598 private:
6599 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006600 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006601 size_t index_;
6602
6603 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6604};
6605
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006606inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006607 if (constant->IsIntConstant()) {
6608 return constant->AsIntConstant()->GetValue();
6609 } else if (constant->IsLongConstant()) {
6610 return constant->AsLongConstant()->GetValue();
6611 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006612 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006613 return 0;
6614 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006615}
6616
Vladimir Marko58155012015-08-19 12:49:41 +00006617inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6618 // For the purposes of the compiler, the dex files must actually be the same object
6619 // if we want to safely treat them as the same. This is especially important for JIT
6620 // as custom class loaders can open the same underlying file (or memory) multiple
6621 // times and provide different class resolution but no two class loaders should ever
6622 // use the same DexFile object - doing so is an unsupported hack that can lead to
6623 // all sorts of weird failures.
6624 return &lhs == &rhs;
6625}
6626
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006627#define INSTRUCTION_TYPE_CHECK(type, super) \
6628 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6629 inline const H##type* HInstruction::As##type() const { \
6630 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6631 } \
6632 inline H##type* HInstruction::As##type() { \
6633 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6634 }
6635
6636 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6637#undef INSTRUCTION_TYPE_CHECK
6638
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006639// Create space in `blocks` for adding `number_of_new_blocks` entries
6640// starting at location `at`. Blocks after `at` are moved accordingly.
6641inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6642 size_t number_of_new_blocks,
6643 size_t after) {
6644 DCHECK_LT(after, blocks->size());
6645 size_t old_size = blocks->size();
6646 size_t new_size = old_size + number_of_new_blocks;
6647 blocks->resize(new_size);
6648 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6649}
6650
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006651} // namespace art
6652
6653#endif // ART_COMPILER_OPTIMIZING_NODES_H_