blob: b3c51a1dbd41c430417f3a1c4f21597fd9ab57d5 [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"
Andreas Gampe7c9c31c2016-03-08 16:00:41 -080029#include "dex_instruction-inl.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"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040
41namespace art {
42
David Brazdil1abb4192015-02-17 18:33:36 +000043class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010045class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000046class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010047class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000049class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000052class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000053class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000055class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010056class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010057class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010058class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010059class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000060class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010061class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000062class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000063
Mathieu Chartier736b5602015-09-02 14:54:11 -070064namespace mirror {
65class DexCache;
66} // namespace mirror
67
Nicolas Geoffray818f2102014-02-18 16:43:35 +000068static const int kDefaultNumberOfBlocks = 8;
69static const int kDefaultNumberOfSuccessors = 2;
70static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010071static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010072static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000073static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000074
Calin Juravle9aec02f2014-11-18 23:06:35 +000075static constexpr uint32_t kMaxIntShiftValue = 0x1f;
76static constexpr uint64_t kMaxLongShiftValue = 0x3f;
77
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010078static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070079static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010080
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010081static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
82
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060083static constexpr uint32_t kNoDexPc = -1;
84
Dave Allison20dfc792014-06-16 20:44:29 -070085enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070086 // All types.
87 kCondEQ, // ==
88 kCondNE, // !=
89 // Signed integers and floating-point numbers.
90 kCondLT, // <
91 kCondLE, // <=
92 kCondGT, // >
93 kCondGE, // >=
94 // Unsigned integers.
95 kCondB, // <
96 kCondBE, // <=
97 kCondA, // >
98 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -070099};
100
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000101enum GraphAnalysisResult {
David Brazdilbadd8262016-02-02 16:28:56 +0000102 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000103 kAnalysisFailThrowCatchLoop,
104 kAnalysisFailAmbiguousArrayOp,
105 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000106};
107
Vladimir Markof9f64412015-09-02 14:05:49 +0100108class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100109 public:
110 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
111
112 void AddInstruction(HInstruction* instruction);
113 void RemoveInstruction(HInstruction* instruction);
114
David Brazdilc3d743f2015-04-22 13:40:50 +0100115 // Insert `instruction` before/after an existing instruction `cursor`.
116 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
117 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
118
Roland Levillain6b469232014-09-25 10:10:38 +0100119 // Return true if this list contains `instruction`.
120 bool Contains(HInstruction* instruction) const;
121
Roland Levillainccc07a92014-09-16 14:48:16 +0100122 // Return true if `instruction1` is found before `instruction2` in
123 // this instruction list and false otherwise. Abort if none
124 // of these instructions is found.
125 bool FoundBefore(const HInstruction* instruction1,
126 const HInstruction* instruction2) const;
127
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000128 bool IsEmpty() const { return first_instruction_ == nullptr; }
129 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
130
131 // Update the block of all instructions to be `block`.
132 void SetBlockOfInstructions(HBasicBlock* block) const;
133
134 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000135 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000136 void Add(const HInstructionList& instruction_list);
137
David Brazdil2d7352b2015-04-20 14:52:42 +0100138 // Return the number of instructions in the list. This is an expensive operation.
139 size_t CountSize() const;
140
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100141 private:
142 HInstruction* first_instruction_;
143 HInstruction* last_instruction_;
144
145 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000146 friend class HGraph;
147 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100148 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100149 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100150
151 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
152};
153
David Brazdil4833f5a2015-12-16 10:37:39 +0000154class ReferenceTypeInfo : ValueObject {
155 public:
156 typedef Handle<mirror::Class> TypeHandle;
157
Vladimir Markoa1de9182016-02-25 11:37:38 +0000158 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
159
160 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000161 return ReferenceTypeInfo(type_handle, is_exact);
162 }
163
164 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
165
Vladimir Markof39745e2016-01-26 12:16:55 +0000166 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000167 return handle.GetReference() != nullptr;
168 }
169
170 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
171 return IsValidHandle(type_handle_);
172 }
173
174 bool IsExact() const { return is_exact_; }
175
176 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
177 DCHECK(IsValid());
178 return GetTypeHandle()->IsObjectClass();
179 }
180
181 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
182 DCHECK(IsValid());
183 return GetTypeHandle()->IsStringClass();
184 }
185
186 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
187 DCHECK(IsValid());
188 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
189 }
190
191 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
192 DCHECK(IsValid());
193 return GetTypeHandle()->IsInterface();
194 }
195
196 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
197 DCHECK(IsValid());
198 return GetTypeHandle()->IsArrayClass();
199 }
200
201 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
202 DCHECK(IsValid());
203 return GetTypeHandle()->IsPrimitiveArray();
204 }
205
206 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
207 DCHECK(IsValid());
208 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
209 }
210
211 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
212 DCHECK(IsValid());
213 if (!IsExact()) return false;
214 if (!IsArrayClass()) return false;
215 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
216 }
217
218 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
219 DCHECK(IsValid());
220 if (!IsExact()) return false;
221 if (!IsArrayClass()) return false;
222 if (!rti.IsArrayClass()) return false;
223 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
224 rti.GetTypeHandle()->GetComponentType());
225 }
226
227 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
228
229 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
230 DCHECK(IsValid());
231 DCHECK(rti.IsValid());
232 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
233 }
234
235 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
236 DCHECK(IsValid());
237 DCHECK(rti.IsValid());
238 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
239 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
240 }
241
242 // Returns true if the type information provide the same amount of details.
243 // Note that it does not mean that the instructions have the same actual type
244 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000245 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000246 if (!IsValid() && !rti.IsValid()) {
247 // Invalid types are equal.
248 return true;
249 }
250 if (!IsValid() || !rti.IsValid()) {
251 // One is valid, the other not.
252 return false;
253 }
254 return IsExact() == rti.IsExact()
255 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
256 }
257
258 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000259 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
260 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
261 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000262
263 // The class of the object.
264 TypeHandle type_handle_;
265 // Whether or not the type is exact or a superclass of the actual type.
266 // Whether or not we have any information about this type.
267 bool is_exact_;
268};
269
270std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
271
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000272// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100273class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000274 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100275 HGraph(ArenaAllocator* arena,
276 const DexFile& dex_file,
277 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100278 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700279 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100280 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100281 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000282 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100283 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000284 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100285 blocks_(arena->Adapter(kArenaAllocBlockList)),
286 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
287 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700288 entry_block_(nullptr),
289 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100290 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100291 number_of_vregs_(0),
292 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000293 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400294 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000295 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000296 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000297 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000298 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100299 dex_file_(dex_file),
300 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100301 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100302 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100303 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700304 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000305 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100306 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
307 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
308 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
309 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000310 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000311 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
312 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100313 blocks_.reserve(kDefaultNumberOfBlocks);
314 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000315
David Brazdilbadd8262016-02-02 16:28:56 +0000316 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
317 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
318
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000319 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100320 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
321
David Brazdil69ba7b72015-06-23 18:27:30 +0100322 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000323 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100324
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000325 HBasicBlock* GetEntryBlock() const { return entry_block_; }
326 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100327 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000328
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000329 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
330 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000331
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000332 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100333
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100334 void ComputeDominanceInformation();
335 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000336 void ClearLoopInformation();
337 void FindBackEdges(ArenaBitVector* visited);
338 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100339 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100340 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000341
David Brazdil4833f5a2015-12-16 10:37:39 +0000342 // Analyze all natural loops in this graph. Returns a code specifying that it
343 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000344 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000345 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100346
David Brazdilffee3d32015-07-06 11:48:53 +0100347 // Iterate over blocks to compute try block membership. Needs reverse post
348 // order and loop information.
349 void ComputeTryBlockInformation();
350
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000351 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000352 // Returns the instruction to replace the invoke expression or null if the
353 // invoke is for a void method. Note that the caller is responsible for replacing
354 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000355 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000356
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000357 // Update the loop and try membership of `block`, which was spawned from `reference`.
358 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
359 // should be the new back edge.
360 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
361 HBasicBlock* reference,
362 bool replace_if_back_edge);
363
Mingyao Yang3584bce2015-05-19 16:01:59 -0700364 // Need to add a couple of blocks to test if the loop body is entered and
365 // put deoptimization instructions, etc.
366 void TransformLoopHeaderForBCE(HBasicBlock* header);
367
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000368 // Removes `block` from the graph. Assumes `block` has been disconnected from
369 // other blocks and has no instructions or phis.
370 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000371
David Brazdilfc6a86a2015-06-26 10:33:45 +0000372 // Splits the edge between `block` and `successor` while preserving the
373 // indices in the predecessor/successor lists. If there are multiple edges
374 // between the blocks, the lowest indices are used.
375 // Returns the new block which is empty and has the same dex pc as `successor`.
376 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
377
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100378 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
379 void SimplifyLoop(HBasicBlock* header);
380
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000381 int32_t GetNextInstructionId() {
382 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000383 return current_instruction_id_++;
384 }
385
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000386 int32_t GetCurrentInstructionId() const {
387 return current_instruction_id_;
388 }
389
390 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000391 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000392 current_instruction_id_ = id;
393 }
394
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100395 uint16_t GetMaximumNumberOfOutVRegs() const {
396 return maximum_number_of_out_vregs_;
397 }
398
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000399 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
400 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100401 }
402
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100403 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
404 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
405 }
406
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000407 void UpdateTemporariesVRegSlots(size_t slots) {
408 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100409 }
410
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000411 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100412 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000413 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100414 }
415
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100416 void SetNumberOfVRegs(uint16_t number_of_vregs) {
417 number_of_vregs_ = number_of_vregs;
418 }
419
420 uint16_t GetNumberOfVRegs() const {
421 return number_of_vregs_;
422 }
423
424 void SetNumberOfInVRegs(uint16_t value) {
425 number_of_in_vregs_ = value;
426 }
427
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100428 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100429 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100430 return number_of_vregs_ - number_of_in_vregs_;
431 }
432
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100433 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100434 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100435 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100436
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100437 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100438 return linear_order_;
439 }
440
Mark Mendell1152c922015-04-24 17:06:35 -0400441 bool HasBoundsChecks() const {
442 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800443 }
444
Mark Mendell1152c922015-04-24 17:06:35 -0400445 void SetHasBoundsChecks(bool value) {
446 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800447 }
448
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100449 bool ShouldGenerateConstructorBarrier() const {
450 return should_generate_constructor_barrier_;
451 }
452
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000453 bool IsDebuggable() const { return debuggable_; }
454
David Brazdil8d5b8b22015-03-24 10:51:52 +0000455 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000456 // already, it is created and inserted into the graph. This method is only for
457 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600458 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000459
460 // TODO: This is problematic for the consistency of reference type propagation
461 // because it can be created anytime after the pass and thus it will be left
462 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600463 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000464
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600465 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
466 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000467 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600468 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
469 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000470 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600471 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
472 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000473 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600474 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
475 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000476 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000477
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100478 HCurrentMethod* GetCurrentMethod();
479
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100480 const DexFile& GetDexFile() const {
481 return dex_file_;
482 }
483
484 uint32_t GetMethodIdx() const {
485 return method_idx_;
486 }
487
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100488 InvokeType GetInvokeType() const {
489 return invoke_type_;
490 }
491
Mark Mendellc4701932015-04-10 13:18:51 -0400492 InstructionSet GetInstructionSet() const {
493 return instruction_set_;
494 }
495
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000496 bool IsCompilingOsr() const { return osr_; }
497
David Brazdil77a48ae2015-09-15 12:34:04 +0000498 bool HasTryCatch() const { return has_try_catch_; }
499 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100500
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000501 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
502 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
503
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100504 ArtMethod* GetArtMethod() const { return art_method_; }
505 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
506
Mark Mendellf6529172015-11-17 11:16:56 -0500507 // Returns an instruction with the opposite boolean value from 'cond'.
508 // The instruction has been inserted into the graph, either as a constant, or
509 // before cursor.
510 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
511
David Brazdil2d7352b2015-04-20 14:52:42 +0100512 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000513 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100514 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000515
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000516 template <class InstructionType, typename ValueType>
517 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600518 ArenaSafeMap<ValueType, InstructionType*>* cache,
519 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000520 // Try to find an existing constant of the given value.
521 InstructionType* constant = nullptr;
522 auto cached_constant = cache->find(value);
523 if (cached_constant != cache->end()) {
524 constant = cached_constant->second;
525 }
526
527 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100528 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000529 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600530 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000531 cache->Overwrite(value, constant);
532 InsertConstant(constant);
533 }
534 return constant;
535 }
536
David Brazdil8d5b8b22015-03-24 10:51:52 +0000537 void InsertConstant(HConstant* instruction);
538
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000539 // Cache a float constant into the graph. This method should only be
540 // called by the SsaBuilder when creating "equivalent" instructions.
541 void CacheFloatConstant(HFloatConstant* constant);
542
543 // See CacheFloatConstant comment.
544 void CacheDoubleConstant(HDoubleConstant* constant);
545
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000546 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000547
548 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100549 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000550
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100551 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100552 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000553
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100554 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100555 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100556
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000557 HBasicBlock* entry_block_;
558 HBasicBlock* exit_block_;
559
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100560 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100561 uint16_t maximum_number_of_out_vregs_;
562
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100563 // The number of virtual registers in this method. Contains the parameters.
564 uint16_t number_of_vregs_;
565
566 // The number of virtual registers used by parameters of this method.
567 uint16_t number_of_in_vregs_;
568
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000569 // Number of vreg size slots that the temporaries use (used in baseline compiler).
570 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100571
Mark Mendell1152c922015-04-24 17:06:35 -0400572 // Has bounds checks. We can totally skip BCE if it's false.
573 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800574
David Brazdil77a48ae2015-09-15 12:34:04 +0000575 // Flag whether there are any try/catch blocks in the graph. We will skip
576 // try/catch-related passes if false.
577 bool has_try_catch_;
578
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000579 // Flag whether there are any irreducible loops in the graph.
580 bool has_irreducible_loops_;
581
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000582 // Indicates whether the graph should be compiled in a way that
583 // ensures full debuggability. If false, we can apply more
584 // aggressive optimizations that may limit the level of debugging.
585 const bool debuggable_;
586
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000587 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000588 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000589
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100590 // The dex file from which the method is from.
591 const DexFile& dex_file_;
592
593 // The method index in the dex file.
594 const uint32_t method_idx_;
595
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100596 // If inlined, this encodes how the callee is being invoked.
597 const InvokeType invoke_type_;
598
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100599 // Whether the graph has been transformed to SSA form. Only used
600 // in debug mode to ensure we are not using properties only valid
601 // for non-SSA form (like the number of temporaries).
602 bool in_ssa_form_;
603
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100604 const bool should_generate_constructor_barrier_;
605
Mathieu Chartiere401d142015-04-22 13:56:20 -0700606 const InstructionSet instruction_set_;
607
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000608 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000609 HNullConstant* cached_null_constant_;
610 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000611 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000612 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000613 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000614
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100615 HCurrentMethod* cached_current_method_;
616
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100617 // The ArtMethod this graph is for. Note that for AOT, it may be null,
618 // for example for methods whose declaring class could not be resolved
619 // (such as when the superclass could not be found).
620 ArtMethod* art_method_;
621
David Brazdil4833f5a2015-12-16 10:37:39 +0000622 // Keep the RTI of inexact Object to avoid having to pass stack handle
623 // collection pointer to passes which may create NullConstant.
624 ReferenceTypeInfo inexact_object_rti_;
625
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000626 // Whether we are compiling this graph for on stack replacement: this will
627 // make all loops seen as irreducible and emit special stack maps to mark
628 // compiled code entries which the interpreter can directly jump to.
629 const bool osr_;
630
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000631 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100632 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000633 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000634 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000635 DISALLOW_COPY_AND_ASSIGN(HGraph);
636};
637
Vladimir Markof9f64412015-09-02 14:05:49 +0100638class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000639 public:
640 HLoopInformation(HBasicBlock* header, HGraph* graph)
641 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100642 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000643 irreducible_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100644 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100645 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100646 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
647 back_edges_.reserve(kDefaultNumberOfBackEdges);
648 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100649
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000650 bool IsIrreducible() const { return irreducible_; }
651
652 void Dump(std::ostream& os);
653
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100654 HBasicBlock* GetHeader() const {
655 return header_;
656 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000657
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000658 void SetHeader(HBasicBlock* block) {
659 header_ = block;
660 }
661
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100662 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
663 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
664 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
665
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000666 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100667 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000668 }
669
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100670 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100671 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100672 }
673
David Brazdil46e2a392015-03-16 17:31:52 +0000674 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100675 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100676 }
677
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000678 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100679 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000680 }
681
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100682 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100683
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100684 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100685 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100686 }
687
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100688 // Returns the lifetime position of the back edge that has the
689 // greatest lifetime position.
690 size_t GetLifetimeEnd() const;
691
692 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100693 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100694 }
695
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000696 // Finds blocks that are part of this loop.
697 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100698
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100699 // Returns whether this loop information contains `block`.
700 // Note that this loop information *must* be populated before entering this function.
701 bool Contains(const HBasicBlock& block) const;
702
703 // Returns whether this loop information is an inner loop of `other`.
704 // Note that `other` *must* be populated before entering this function.
705 bool IsIn(const HLoopInformation& other) const;
706
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800707 // Returns true if instruction is not defined within this loop.
708 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700709
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100710 const ArenaBitVector& GetBlocks() const { return blocks_; }
711
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000712 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000713 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000714
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000715 void ClearAllBlocks() {
716 blocks_.ClearAllBits();
717 }
718
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000719 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100720 // Internal recursive implementation of `Populate`.
721 void PopulateRecursive(HBasicBlock* block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000722 void PopulateIrreducibleRecursive(HBasicBlock* block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100723
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000724 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100725 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000726 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100727 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100728 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000729
730 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
731};
732
David Brazdilec16f792015-08-19 15:04:01 +0100733// Stores try/catch information for basic blocks.
734// Note that HGraph is constructed so that catch blocks cannot simultaneously
735// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100736class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100737 public:
738 // Try block information constructor.
739 explicit TryCatchInformation(const HTryBoundary& try_entry)
740 : try_entry_(&try_entry),
741 catch_dex_file_(nullptr),
742 catch_type_index_(DexFile::kDexNoIndex16) {
743 DCHECK(try_entry_ != nullptr);
744 }
745
746 // Catch block information constructor.
747 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
748 : try_entry_(nullptr),
749 catch_dex_file_(&dex_file),
750 catch_type_index_(catch_type_index) {}
751
752 bool IsTryBlock() const { return try_entry_ != nullptr; }
753
754 const HTryBoundary& GetTryEntry() const {
755 DCHECK(IsTryBlock());
756 return *try_entry_;
757 }
758
759 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
760
761 bool IsCatchAllTypeIndex() const {
762 DCHECK(IsCatchBlock());
763 return catch_type_index_ == DexFile::kDexNoIndex16;
764 }
765
766 uint16_t GetCatchTypeIndex() const {
767 DCHECK(IsCatchBlock());
768 return catch_type_index_;
769 }
770
771 const DexFile& GetCatchDexFile() const {
772 DCHECK(IsCatchBlock());
773 return *catch_dex_file_;
774 }
775
776 private:
777 // One of possibly several TryBoundary instructions entering the block's try.
778 // Only set for try blocks.
779 const HTryBoundary* try_entry_;
780
781 // Exception type information. Only set for catch blocks.
782 const DexFile* catch_dex_file_;
783 const uint16_t catch_type_index_;
784};
785
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100786static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100787static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100788
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000789// A block in a method. Contains the list of instructions represented
790// as a double linked list. Each block knows its predecessors and
791// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100792
Vladimir Markof9f64412015-09-02 14:05:49 +0100793class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000794 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600795 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000796 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000797 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
798 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000799 loop_information_(nullptr),
800 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000801 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100802 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100803 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100804 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000805 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000806 try_catch_information_(nullptr) {
807 predecessors_.reserve(kDefaultNumberOfPredecessors);
808 successors_.reserve(kDefaultNumberOfSuccessors);
809 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
810 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000811
Vladimir Marko60584552015-09-03 13:35:12 +0000812 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100813 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000814 }
815
Vladimir Marko60584552015-09-03 13:35:12 +0000816 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100817 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000818 }
819
David Brazdild26a4112015-11-10 11:07:31 +0000820 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
821 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
822
Vladimir Marko60584552015-09-03 13:35:12 +0000823 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
824 return ContainsElement(successors_, block, start_from);
825 }
826
827 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100828 return dominated_blocks_;
829 }
830
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100831 bool IsEntryBlock() const {
832 return graph_->GetEntryBlock() == this;
833 }
834
835 bool IsExitBlock() const {
836 return graph_->GetExitBlock() == this;
837 }
838
David Brazdil46e2a392015-03-16 17:31:52 +0000839 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000840 bool IsSingleTryBoundary() const;
841
842 // Returns true if this block emits nothing but a jump.
843 bool IsSingleJump() const {
844 HLoopInformation* loop_info = GetLoopInformation();
845 return (IsSingleGoto() || IsSingleTryBoundary())
846 // Back edges generate a suspend check.
847 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
848 }
David Brazdil46e2a392015-03-16 17:31:52 +0000849
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000850 void AddBackEdge(HBasicBlock* back_edge) {
851 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000852 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000853 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100854 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000855 loop_information_->AddBackEdge(back_edge);
856 }
857
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000858 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000859 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000860
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100861 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000862 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600863 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000864
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000865 HBasicBlock* GetDominator() const { return dominator_; }
866 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000867 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
868
869 void RemoveDominatedBlock(HBasicBlock* block) {
870 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100871 }
Vladimir Marko60584552015-09-03 13:35:12 +0000872
873 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
874 ReplaceElement(dominated_blocks_, existing, new_block);
875 }
876
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100877 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000878
879 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100880 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000881 }
882
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100883 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
884 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100885 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100886 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100887 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
888 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000889
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000890 HInstruction* GetFirstInstructionDisregardMoves() const;
891
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000892 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000893 successors_.push_back(block);
894 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000895 }
896
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100897 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
898 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100899 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000900 new_block->predecessors_.push_back(this);
901 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000902 }
903
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000904 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
905 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000906 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000907 new_block->successors_.push_back(this);
908 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000909 }
910
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100911 // Insert `this` between `predecessor` and `successor. This method
912 // preserves the indicies, and will update the first edge found between
913 // `predecessor` and `successor`.
914 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
915 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100916 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000917 successor->predecessors_[predecessor_index] = this;
918 predecessor->successors_[successor_index] = this;
919 successors_.push_back(successor);
920 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100921 }
922
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100923 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000924 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100925 }
926
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000927 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000928 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000929 }
930
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100931 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000932 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100933 }
934
935 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000936 predecessors_.push_back(block);
937 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100938 }
939
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100940 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000941 DCHECK_EQ(predecessors_.size(), 2u);
942 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100943 }
944
David Brazdil769c9e52015-04-27 13:54:09 +0100945 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000946 DCHECK_EQ(successors_.size(), 2u);
947 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100948 }
949
David Brazdilfc6a86a2015-06-26 10:33:45 +0000950 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000951 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100952 }
953
David Brazdilfc6a86a2015-06-26 10:33:45 +0000954 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000955 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100956 }
957
David Brazdilfc6a86a2015-06-26 10:33:45 +0000958 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000959 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100960 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000961 }
962
963 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000964 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100965 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000966 }
967
968 // Returns whether the first occurrence of `predecessor` in the list of
969 // predecessors is at index `idx`.
970 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100971 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000972 return GetPredecessorIndexOf(predecessor) == idx;
973 }
974
David Brazdild7558da2015-09-22 13:04:14 +0100975 // Create a new block between this block and its predecessors. The new block
976 // is added to the graph, all predecessor edges are relinked to it and an edge
977 // is created to `this`. Returns the new empty block. Reverse post order or
978 // loop and try/catch information are not updated.
979 HBasicBlock* CreateImmediateDominator();
980
David Brazdilfc6a86a2015-06-26 10:33:45 +0000981 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100982 // created, latter block. Note that this method will add the block to the
983 // graph, create a Goto at the end of the former block and will create an edge
984 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100985 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000986 HBasicBlock* SplitBefore(HInstruction* cursor);
987
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000988 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000989 // created block. Note that this method just updates raw block information,
990 // like predecessors, successors, dominators, and instruction list. It does not
991 // update the graph, reverse post order, loop information, nor make sure the
992 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000993 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
994
995 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
996 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000997
David Brazdil9bc43612015-11-05 21:25:24 +0000998 // Split catch block into two blocks after the original move-exception bytecode
999 // instruction, or at the beginning if not present. Returns the newly created,
1000 // latter block, or nullptr if such block could not be created (must be dead
1001 // in that case). Note that this method just updates raw block information,
1002 // like predecessors, successors, dominators, and instruction list. It does not
1003 // update the graph, reverse post order, loop information, nor make sure the
1004 // blocks are consistent (for example ending with a control flow instruction).
1005 HBasicBlock* SplitCatchBlockAfterMoveException();
1006
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001007 // Merge `other` at the end of `this`. Successors and dominated blocks of
1008 // `other` are changed to be successors and dominated blocks of `this`. Note
1009 // that this method does not update the graph, reverse post order, loop
1010 // information, nor make sure the blocks are consistent (for example ending
1011 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001012 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001013
1014 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1015 // of `this` are moved to `other`.
1016 // Note that this method does not update the graph, reverse post order, loop
1017 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001018 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001019 void ReplaceWith(HBasicBlock* other);
1020
David Brazdil2d7352b2015-04-20 14:52:42 +01001021 // Merge `other` at the end of `this`. This method updates loops, reverse post
1022 // order, links to predecessors, successors, dominators and deletes the block
1023 // from the graph. The two blocks must be successive, i.e. `this` the only
1024 // predecessor of `other` and vice versa.
1025 void MergeWith(HBasicBlock* other);
1026
1027 // Disconnects `this` from all its predecessors, successors and dominator,
1028 // removes it from all loops it is included in and eventually from the graph.
1029 // The block must not dominate any other block. Predecessors and successors
1030 // are safely updated.
1031 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001032
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001033 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001034 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001035 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001036 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001037 // Replace instruction `initial` with `replacement` within this block.
1038 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1039 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001040 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001041 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001042 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001043 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1044 // instruction list. With 'ensure_safety' set to true, it verifies that the
1045 // instruction is not in use and removes it from the use lists of its inputs.
1046 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1047 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001048 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001049
1050 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001051 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001052 }
1053
Roland Levillain6b879dd2014-09-22 17:13:44 +01001054 bool IsLoopPreHeaderFirstPredecessor() const {
1055 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001056 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001057 }
1058
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001059 bool IsFirstPredecessorBackEdge() const {
1060 DCHECK(IsLoopHeader());
1061 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1062 }
1063
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001064 HLoopInformation* GetLoopInformation() const {
1065 return loop_information_;
1066 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001067
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001068 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001069 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001070 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001071 void SetInLoop(HLoopInformation* info) {
1072 if (IsLoopHeader()) {
1073 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001074 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001075 loop_information_ = info;
1076 } else if (loop_information_->Contains(*info->GetHeader())) {
1077 // Block is currently part of an outer loop. Make it part of this inner loop.
1078 // Note that a non loop header having a loop information means this loop information
1079 // has already been populated
1080 loop_information_ = info;
1081 } else {
1082 // Block is part of an inner loop. Do not update the loop information.
1083 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1084 // at this point, because this method is being called while populating `info`.
1085 }
1086 }
1087
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001088 // Raw update of the loop information.
1089 void SetLoopInformation(HLoopInformation* info) {
1090 loop_information_ = info;
1091 }
1092
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001093 bool IsInLoop() const { return loop_information_ != nullptr; }
1094
David Brazdilec16f792015-08-19 15:04:01 +01001095 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1096
1097 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1098 try_catch_information_ = try_catch_information;
1099 }
1100
1101 bool IsTryBlock() const {
1102 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1103 }
1104
1105 bool IsCatchBlock() const {
1106 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1107 }
David Brazdilffee3d32015-07-06 11:48:53 +01001108
1109 // Returns the try entry that this block's successors should have. They will
1110 // be in the same try, unless the block ends in a try boundary. In that case,
1111 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001112 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001113
David Brazdild7558da2015-09-22 13:04:14 +01001114 bool HasThrowingInstructions() const;
1115
David Brazdila4b8c212015-05-07 09:59:30 +01001116 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001117 bool Dominates(HBasicBlock* block) const;
1118
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001119 size_t GetLifetimeStart() const { return lifetime_start_; }
1120 size_t GetLifetimeEnd() const { return lifetime_end_; }
1121
1122 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1123 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1124
David Brazdil8d5b8b22015-03-24 10:51:52 +00001125 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001126 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001127 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001128 bool HasSinglePhi() const;
1129
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001130 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001131 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001132 ArenaVector<HBasicBlock*> predecessors_;
1133 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001134 HInstructionList instructions_;
1135 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001136 HLoopInformation* loop_information_;
1137 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001138 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001139 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001140 // The dex program counter of the first instruction of this block.
1141 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001142 size_t lifetime_start_;
1143 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001144 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001145
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001146 friend class HGraph;
1147 friend class HInstruction;
1148
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001149 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1150};
1151
David Brazdilb2bd1c52015-03-25 11:17:37 +00001152// Iterates over the LoopInformation of all loops which contain 'block'
1153// from the innermost to the outermost.
1154class HLoopInformationOutwardIterator : public ValueObject {
1155 public:
1156 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1157 : current_(block.GetLoopInformation()) {}
1158
1159 bool Done() const { return current_ == nullptr; }
1160
1161 void Advance() {
1162 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001163 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001164 }
1165
1166 HLoopInformation* Current() const {
1167 DCHECK(!Done());
1168 return current_;
1169 }
1170
1171 private:
1172 HLoopInformation* current_;
1173
1174 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1175};
1176
Alexandre Ramesef20f712015-06-09 10:29:30 +01001177#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001178 M(Above, Condition) \
1179 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001180 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001181 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001182 M(ArrayGet, Instruction) \
1183 M(ArrayLength, Instruction) \
1184 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001185 M(Below, Condition) \
1186 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001187 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001188 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001189 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001190 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001191 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001192 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001193 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001194 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001195 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001196 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001197 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001198 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001199 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001200 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001201 M(Exit, Instruction) \
1202 M(FloatConstant, Constant) \
1203 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001204 M(GreaterThan, Condition) \
1205 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001206 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001207 M(InstanceFieldGet, Instruction) \
1208 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001209 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001210 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001211 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001212 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001213 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001214 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001215 M(LessThan, Condition) \
1216 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001217 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001218 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001219 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001220 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001221 M(Local, Instruction) \
1222 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001223 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001224 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001225 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001226 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001227 M(Neg, UnaryOperation) \
1228 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001229 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001230 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001231 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001232 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001233 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001234 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001235 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001236 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001237 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001238 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001239 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001240 M(Return, Instruction) \
1241 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001242 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001243 M(Shl, BinaryOperation) \
1244 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001245 M(StaticFieldGet, Instruction) \
1246 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001247 M(UnresolvedInstanceFieldGet, Instruction) \
1248 M(UnresolvedInstanceFieldSet, Instruction) \
1249 M(UnresolvedStaticFieldGet, Instruction) \
1250 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001251 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001252 M(StoreLocal, Instruction) \
1253 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001254 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001255 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001256 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001257 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001258 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001259 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001260
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001261/*
1262 * Instructions, shared across several (not all) architectures.
1263 */
1264#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1265#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1266#else
1267#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
1268 M(MultiplyAccumulate, Instruction)
1269#endif
1270
Vladimir Markob4536b72015-11-24 13:45:23 +00001271#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001272#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001273#else
1274#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1275 M(ArmDexCacheArraysBase, Instruction)
1276#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001277
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001278#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001279#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001280#else
1281#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001282 M(Arm64BitwiseNegatedRight, Instruction) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001283 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001284 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001285#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001286
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001287#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1288
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001289#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1290
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001291#ifndef ART_ENABLE_CODEGEN_x86
1292#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1293#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001294#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1295 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001296 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001297 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001298 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001299#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001300
1301#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1302
1303#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1304 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001305 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001306 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1307 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001308 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001309 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001310 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1311 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1312
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001313#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1314 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001315 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001316 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001317 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001318 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001319
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001320#define FOR_EACH_INSTRUCTION(M) \
1321 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1322 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1323
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001324#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001325FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1326#undef FORWARD_DECLARATION
1327
Roland Levillainccc07a92014-09-16 14:48:16 +01001328#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001329 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001330 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001331 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001332 return other->Is##type(); \
1333 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001334 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001335
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001336#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1337 bool Is##type() const { return As##type() != nullptr; } \
1338 const H##type* As##type() const { return this; } \
1339 H##type* As##type() { return this; }
1340
David Brazdiled596192015-01-23 10:39:45 +00001341template <typename T> class HUseList;
1342
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001343template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001344class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001345 public:
David Brazdiled596192015-01-23 10:39:45 +00001346 HUseListNode* GetPrevious() const { return prev_; }
1347 HUseListNode* GetNext() const { return next_; }
1348 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001349 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001350 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001351
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001352 private:
David Brazdiled596192015-01-23 10:39:45 +00001353 HUseListNode(T user, size_t index)
1354 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1355
1356 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001357 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001358 HUseListNode<T>* prev_;
1359 HUseListNode<T>* next_;
1360
1361 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001362
1363 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1364};
1365
David Brazdiled596192015-01-23 10:39:45 +00001366template <typename T>
1367class HUseList : public ValueObject {
1368 public:
1369 HUseList() : first_(nullptr) {}
1370
1371 void Clear() {
1372 first_ = nullptr;
1373 }
1374
1375 // Adds a new entry at the beginning of the use list and returns
1376 // the newly created node.
1377 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001378 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001379 if (IsEmpty()) {
1380 first_ = new_node;
1381 } else {
1382 first_->prev_ = new_node;
1383 new_node->next_ = first_;
1384 first_ = new_node;
1385 }
1386 return new_node;
1387 }
1388
1389 HUseListNode<T>* GetFirst() const {
1390 return first_;
1391 }
1392
1393 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001394 DCHECK(node != nullptr);
1395 DCHECK(Contains(node));
1396
David Brazdiled596192015-01-23 10:39:45 +00001397 if (node->prev_ != nullptr) {
1398 node->prev_->next_ = node->next_;
1399 }
1400 if (node->next_ != nullptr) {
1401 node->next_->prev_ = node->prev_;
1402 }
1403 if (node == first_) {
1404 first_ = node->next_;
1405 }
1406 }
1407
David Brazdil1abb4192015-02-17 18:33:36 +00001408 bool Contains(const HUseListNode<T>* node) const {
1409 if (node == nullptr) {
1410 return false;
1411 }
1412 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1413 if (current == node) {
1414 return true;
1415 }
1416 }
1417 return false;
1418 }
1419
David Brazdiled596192015-01-23 10:39:45 +00001420 bool IsEmpty() const {
1421 return first_ == nullptr;
1422 }
1423
1424 bool HasOnlyOneUse() const {
1425 return first_ != nullptr && first_->next_ == nullptr;
1426 }
1427
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001428 size_t SizeSlow() const {
1429 size_t count = 0;
1430 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1431 ++count;
1432 }
1433 return count;
1434 }
1435
David Brazdiled596192015-01-23 10:39:45 +00001436 private:
1437 HUseListNode<T>* first_;
1438};
1439
1440template<typename T>
1441class HUseIterator : public ValueObject {
1442 public:
1443 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1444
1445 bool Done() const { return current_ == nullptr; }
1446
1447 void Advance() {
1448 DCHECK(!Done());
1449 current_ = current_->GetNext();
1450 }
1451
1452 HUseListNode<T>* Current() const {
1453 DCHECK(!Done());
1454 return current_;
1455 }
1456
1457 private:
1458 HUseListNode<T>* current_;
1459
1460 friend class HValue;
1461};
1462
David Brazdil1abb4192015-02-17 18:33:36 +00001463// This class is used by HEnvironment and HInstruction classes to record the
1464// instructions they use and pointers to the corresponding HUseListNodes kept
1465// by the used instructions.
1466template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001467class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001468 public:
1469 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1470 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1471
1472 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1473 : instruction_(old_record.instruction_), use_node_(use_node) {
1474 DCHECK(instruction_ != nullptr);
1475 DCHECK(use_node_ != nullptr);
1476 DCHECK(old_record.use_node_ == nullptr);
1477 }
1478
1479 HInstruction* GetInstruction() const { return instruction_; }
1480 HUseListNode<T>* GetUseNode() const { return use_node_; }
1481
1482 private:
1483 // Instruction used by the user.
1484 HInstruction* instruction_;
1485
1486 // Corresponding entry in the use list kept by 'instruction_'.
1487 HUseListNode<T>* use_node_;
1488};
1489
Aart Bik854a02b2015-07-14 16:07:00 -07001490/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001491 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001492 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001493 * For write/read dependences on fields/arrays, the dependence analysis uses
1494 * type disambiguation (e.g. a float field write cannot modify the value of an
1495 * integer field read) and the access type (e.g. a reference array write cannot
1496 * modify the value of a reference field read [although it may modify the
1497 * reference fetch prior to reading the field, which is represented by its own
1498 * write/read dependence]). The analysis makes conservative points-to
1499 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1500 * the same, and any reference read depends on any reference read without
1501 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001502 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001503 * The internal representation uses 38-bit and is described in the table below.
1504 * The first line indicates the side effect, and for field/array accesses the
1505 * second line indicates the type of the access (in the order of the
1506 * Primitive::Type enum).
1507 * The two numbered lines below indicate the bit position in the bitfield (read
1508 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001509 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001510 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1511 * +-------------+---------+---------+--------------+---------+---------+
1512 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1513 * | 3 |333333322|222222221| 1 |111111110|000000000|
1514 * | 7 |654321098|765432109| 8 |765432109|876543210|
1515 *
1516 * Note that, to ease the implementation, 'changes' bits are least significant
1517 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001518 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001519class SideEffects : public ValueObject {
1520 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001521 SideEffects() : flags_(0) {}
1522
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001523 static SideEffects None() {
1524 return SideEffects(0);
1525 }
1526
1527 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001528 return SideEffects(kAllChangeBits | kAllDependOnBits);
1529 }
1530
1531 static SideEffects AllChanges() {
1532 return SideEffects(kAllChangeBits);
1533 }
1534
1535 static SideEffects AllDependencies() {
1536 return SideEffects(kAllDependOnBits);
1537 }
1538
1539 static SideEffects AllExceptGCDependency() {
1540 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1541 }
1542
1543 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001544 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001545 }
1546
Aart Bik34c3ba92015-07-20 14:08:59 -07001547 static SideEffects AllWrites() {
1548 return SideEffects(kAllWrites);
1549 }
1550
1551 static SideEffects AllReads() {
1552 return SideEffects(kAllReads);
1553 }
1554
1555 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1556 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001557 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001558 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001559 }
1560
Aart Bik854a02b2015-07-14 16:07:00 -07001561 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1562 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001563 }
1564
Aart Bik34c3ba92015-07-20 14:08:59 -07001565 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1566 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001567 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001568 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001569 }
1570
1571 static SideEffects ArrayReadOfType(Primitive::Type type) {
1572 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1573 }
1574
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001575 static SideEffects CanTriggerGC() {
1576 return SideEffects(1ULL << kCanTriggerGCBit);
1577 }
1578
1579 static SideEffects DependsOnGC() {
1580 return SideEffects(1ULL << kDependsOnGCBit);
1581 }
1582
Aart Bik854a02b2015-07-14 16:07:00 -07001583 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001584 SideEffects Union(SideEffects other) const {
1585 return SideEffects(flags_ | other.flags_);
1586 }
1587
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001588 SideEffects Exclusion(SideEffects other) const {
1589 return SideEffects(flags_ & ~other.flags_);
1590 }
1591
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001592 void Add(SideEffects other) {
1593 flags_ |= other.flags_;
1594 }
1595
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001596 bool Includes(SideEffects other) const {
1597 return (other.flags_ & flags_) == other.flags_;
1598 }
1599
1600 bool HasSideEffects() const {
1601 return (flags_ & kAllChangeBits);
1602 }
1603
1604 bool HasDependencies() const {
1605 return (flags_ & kAllDependOnBits);
1606 }
1607
1608 // Returns true if there are no side effects or dependencies.
1609 bool DoesNothing() const {
1610 return flags_ == 0;
1611 }
1612
Aart Bik854a02b2015-07-14 16:07:00 -07001613 // Returns true if something is written.
1614 bool DoesAnyWrite() const {
1615 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001616 }
1617
Aart Bik854a02b2015-07-14 16:07:00 -07001618 // Returns true if something is read.
1619 bool DoesAnyRead() const {
1620 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001621 }
1622
Aart Bik854a02b2015-07-14 16:07:00 -07001623 // Returns true if potentially everything is written and read
1624 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001625 bool DoesAllReadWrite() const {
1626 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1627 }
1628
Aart Bik854a02b2015-07-14 16:07:00 -07001629 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001630 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001631 }
1632
Roland Levillain0d5a2812015-11-13 10:07:31 +00001633 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001634 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001635 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1636 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001637 }
1638
1639 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001640 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001641 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001642 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001643 for (int s = kLastBit; s >= 0; s--) {
1644 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1645 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1646 // This is a bit for the GC side effect.
1647 if (current_bit_is_set) {
1648 flags += "GC";
1649 }
Aart Bik854a02b2015-07-14 16:07:00 -07001650 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001651 } else {
1652 // This is a bit for the array/field analysis.
1653 // The underscore character stands for the 'can trigger GC' bit.
1654 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1655 if (current_bit_is_set) {
1656 flags += kDebug[s];
1657 }
1658 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1659 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1660 flags += "|";
1661 }
1662 }
Aart Bik854a02b2015-07-14 16:07:00 -07001663 }
1664 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001665 }
1666
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001667 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001668
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001669 private:
1670 static constexpr int kFieldArrayAnalysisBits = 9;
1671
1672 static constexpr int kFieldWriteOffset = 0;
1673 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1674 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1675 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1676
1677 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1678
1679 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1680 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1681 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1682 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1683
1684 static constexpr int kLastBit = kDependsOnGCBit;
1685 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1686
1687 // Aliases.
1688
1689 static_assert(kChangeBits == kDependOnBits,
1690 "the 'change' bits should match the 'depend on' bits.");
1691
1692 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1693 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1694 static constexpr uint64_t kAllWrites =
1695 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1696 static constexpr uint64_t kAllReads =
1697 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001698
Aart Bik854a02b2015-07-14 16:07:00 -07001699 // Work around the fact that HIR aliases I/F and J/D.
1700 // TODO: remove this interceptor once HIR types are clean
1701 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1702 switch (type) {
1703 case Primitive::kPrimInt:
1704 case Primitive::kPrimFloat:
1705 return TypeFlag(Primitive::kPrimInt, offset) |
1706 TypeFlag(Primitive::kPrimFloat, offset);
1707 case Primitive::kPrimLong:
1708 case Primitive::kPrimDouble:
1709 return TypeFlag(Primitive::kPrimLong, offset) |
1710 TypeFlag(Primitive::kPrimDouble, offset);
1711 default:
1712 return TypeFlag(type, offset);
1713 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001714 }
1715
Aart Bik854a02b2015-07-14 16:07:00 -07001716 // Translates type to bit flag.
1717 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1718 CHECK_NE(type, Primitive::kPrimVoid);
1719 const uint64_t one = 1;
1720 const int shift = type; // 0-based consecutive enum
1721 DCHECK_LE(kFieldWriteOffset, shift);
1722 DCHECK_LT(shift, kArrayWriteOffset);
1723 return one << (type + offset);
1724 }
1725
1726 // Private constructor on direct flags value.
1727 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1728
1729 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001730};
1731
David Brazdiled596192015-01-23 10:39:45 +00001732// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001733class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001734 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001735 HEnvironment(ArenaAllocator* arena,
1736 size_t number_of_vregs,
1737 const DexFile& dex_file,
1738 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001739 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001740 InvokeType invoke_type,
1741 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001742 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1743 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001744 parent_(nullptr),
1745 dex_file_(dex_file),
1746 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001747 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001748 invoke_type_(invoke_type),
1749 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001750 }
1751
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001752 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001753 : HEnvironment(arena,
1754 to_copy.Size(),
1755 to_copy.GetDexFile(),
1756 to_copy.GetMethodIdx(),
1757 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001758 to_copy.GetInvokeType(),
1759 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001760
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001761 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001762 if (parent_ != nullptr) {
1763 parent_->SetAndCopyParentChain(allocator, parent);
1764 } else {
1765 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1766 parent_->CopyFrom(parent);
1767 if (parent->GetParent() != nullptr) {
1768 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1769 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001770 }
David Brazdiled596192015-01-23 10:39:45 +00001771 }
1772
Vladimir Marko71bf8092015-09-15 15:33:14 +01001773 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001774 void CopyFrom(HEnvironment* environment);
1775
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001776 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1777 // input to the loop phi instead. This is for inserting instructions that
1778 // require an environment (like HDeoptimization) in the loop pre-header.
1779 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001780
1781 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001782 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001783 }
1784
1785 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001786 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001787 }
1788
David Brazdil1abb4192015-02-17 18:33:36 +00001789 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001790
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001791 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001792
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001793 HEnvironment* GetParent() const { return parent_; }
1794
1795 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001796 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001797 }
1798
1799 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001800 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001801 }
1802
1803 uint32_t GetDexPc() const {
1804 return dex_pc_;
1805 }
1806
1807 uint32_t GetMethodIdx() const {
1808 return method_idx_;
1809 }
1810
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001811 InvokeType GetInvokeType() const {
1812 return invoke_type_;
1813 }
1814
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001815 const DexFile& GetDexFile() const {
1816 return dex_file_;
1817 }
1818
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001819 HInstruction* GetHolder() const {
1820 return holder_;
1821 }
1822
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001823
1824 bool IsFromInlinedInvoke() const {
1825 return GetParent() != nullptr;
1826 }
1827
David Brazdiled596192015-01-23 10:39:45 +00001828 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001829 // Record instructions' use entries of this environment for constant-time removal.
1830 // It should only be called by HInstruction when a new environment use is added.
1831 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1832 DCHECK(env_use->GetUser() == this);
1833 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001834 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001835 }
David Brazdiled596192015-01-23 10:39:45 +00001836
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001837 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1838 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001839 HEnvironment* parent_;
1840 const DexFile& dex_file_;
1841 const uint32_t method_idx_;
1842 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001843 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001844
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001845 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001846 HInstruction* const holder_;
1847
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001848 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001849
1850 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1851};
1852
Vladimir Markof9f64412015-09-02 14:05:49 +01001853class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001854 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001855 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001856 : previous_(nullptr),
1857 next_(nullptr),
1858 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001859 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001860 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001861 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001862 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001863 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001864 locations_(nullptr),
1865 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001866 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001867 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001868 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1869 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1870 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001871
Dave Allison20dfc792014-06-16 20:44:29 -07001872 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001873
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001874#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001875 enum InstructionKind {
1876 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1877 };
1878#undef DECLARE_KIND
1879
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001880 HInstruction* GetNext() const { return next_; }
1881 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001882
Calin Juravle77520bc2015-01-12 18:45:46 +00001883 HInstruction* GetNextDisregardingMoves() const;
1884 HInstruction* GetPreviousDisregardingMoves() const;
1885
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001886 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001887 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001888 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001889 bool IsInBlock() const { return block_ != nullptr; }
1890 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001891 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1892 bool IsIrreducibleLoopHeaderPhi() const {
1893 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1894 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001895
Roland Levillain6b879dd2014-09-22 17:13:44 +01001896 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001897 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001898
1899 virtual void Accept(HGraphVisitor* visitor) = 0;
1900 virtual const char* DebugName() const = 0;
1901
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001902 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001903 void SetRawInputAt(size_t index, HInstruction* input) {
1904 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1905 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001906
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001907 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001908
1909 uint32_t GetDexPc() const { return dex_pc_; }
1910
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001911 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001912
Roland Levillaine161a2a2014-10-03 12:45:18 +01001913 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001914 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001915
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001916 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001917 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001918
Calin Juravle10e244f2015-01-26 18:54:32 +00001919 // Does not apply for all instructions, but having this at top level greatly
1920 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001921 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001922 virtual bool CanBeNull() const {
1923 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1924 return true;
1925 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001926
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001927 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001928 return false;
1929 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001930
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001931 virtual bool IsActualObject() const {
1932 return GetType() == Primitive::kPrimNot;
1933 }
1934
Calin Juravle2e768302015-07-28 14:41:11 +00001935 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001936
Calin Juravle61d544b2015-02-23 16:46:57 +00001937 ReferenceTypeInfo GetReferenceTypeInfo() const {
1938 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001939 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
1940 GetPackedFlag<kFlagReferenceTypeIsExact>());;
Calin Juravle61d544b2015-02-23 16:46:57 +00001941 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001942
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001943 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001944 DCHECK(user != nullptr);
1945 HUseListNode<HInstruction*>* use =
1946 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1947 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001948 }
1949
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001950 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001951 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001952 HUseListNode<HEnvironment*>* env_use =
1953 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1954 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001955 }
1956
David Brazdil1abb4192015-02-17 18:33:36 +00001957 void RemoveAsUserOfInput(size_t input) {
1958 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1959 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1960 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001961
David Brazdil1abb4192015-02-17 18:33:36 +00001962 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1963 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001964
David Brazdiled596192015-01-23 10:39:45 +00001965 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1966 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001967 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001968 bool HasOnlyOneNonEnvironmentUse() const {
1969 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1970 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001971
Roland Levillain6c82d402014-10-13 16:10:27 +01001972 // Does this instruction strictly dominate `other_instruction`?
1973 // Returns false if this instruction and `other_instruction` are the same.
1974 // Aborts if this instruction and `other_instruction` are both phis.
1975 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001976
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001977 int GetId() const { return id_; }
1978 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001979
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001980 int GetSsaIndex() const { return ssa_index_; }
1981 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1982 bool HasSsaIndex() const { return ssa_index_ != -1; }
1983
1984 bool HasEnvironment() const { return environment_ != nullptr; }
1985 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001986 // Set the `environment_` field. Raw because this method does not
1987 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001988 void SetRawEnvironment(HEnvironment* environment) {
1989 DCHECK(environment_ == nullptr);
1990 DCHECK_EQ(environment->GetHolder(), this);
1991 environment_ = environment;
1992 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001993
1994 // Set the environment of this instruction, copying it from `environment`. While
1995 // copying, the uses lists are being updated.
1996 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001997 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001998 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001999 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002000 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002001 if (environment->GetParent() != nullptr) {
2002 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2003 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002004 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002005
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002006 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2007 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002008 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002009 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002010 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002011 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002012 if (environment->GetParent() != nullptr) {
2013 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2014 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002015 }
2016
Nicolas Geoffray39468442014-09-02 15:17:15 +01002017 // Returns the number of entries in the environment. Typically, that is the
2018 // number of dex registers in a method. It could be more in case of inlining.
2019 size_t EnvironmentSize() const;
2020
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002021 LocationSummary* GetLocations() const { return locations_; }
2022 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002023
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002024 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002025 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002026
Alexandre Rames188d4312015-04-09 18:30:21 +01002027 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2028 // uses of this instruction by `other` are *not* updated.
2029 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2030 ReplaceWith(other);
2031 other->ReplaceInput(this, use_index);
2032 }
2033
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002034 // Move `this` instruction before `cursor`.
2035 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002036
Vladimir Markofb337ea2015-11-25 15:25:10 +00002037 // Move `this` before its first user and out of any loops. If there is no
2038 // out-of-loop user that dominates all other users, move the instruction
2039 // to the end of the out-of-loop common dominator of the user's blocks.
2040 //
2041 // This can be used only on non-throwing instructions with no side effects that
2042 // have at least one use but no environment uses.
2043 void MoveBeforeFirstUserAndOutOfLoops();
2044
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002045#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002046 bool Is##type() const; \
2047 const H##type* As##type() const; \
2048 H##type* As##type();
2049
2050 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2051#undef INSTRUCTION_TYPE_CHECK
2052
2053#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002054 bool Is##type() const { return (As##type() != nullptr); } \
2055 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002056 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002057 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002058#undef INSTRUCTION_TYPE_CHECK
2059
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002060 // Returns whether the instruction can be moved within the graph.
2061 virtual bool CanBeMoved() const { return false; }
2062
2063 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002064 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002065 return false;
2066 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002067
2068 // Returns whether any data encoded in the two instructions is equal.
2069 // This method does not look at the inputs. Both instructions must be
2070 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002071 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002072 return false;
2073 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002074
2075 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002076 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002077 // 2) Their inputs are identical.
2078 bool Equals(HInstruction* other) const;
2079
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002080 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2081 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2082 // the virtual function because the __attribute__((__pure__)) doesn't really
2083 // apply the strong requirement for virtual functions, preventing optimizations.
2084 InstructionKind GetKind() const PURE;
2085 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002086
2087 virtual size_t ComputeHashCode() const {
2088 size_t result = GetKind();
2089 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2090 result = (result * 31) + InputAt(i)->GetId();
2091 }
2092 return result;
2093 }
2094
2095 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002096 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002097 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002098
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002099 size_t GetLifetimePosition() const { return lifetime_position_; }
2100 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2101 LiveInterval* GetLiveInterval() const { return live_interval_; }
2102 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2103 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2104
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002105 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2106
2107 // Returns whether the code generation of the instruction will require to have access
2108 // to the current method. Such instructions are:
2109 // (1): Instructions that require an environment, as calling the runtime requires
2110 // to walk the stack and have the current method stored at a specific stack address.
2111 // (2): Object literals like classes and strings, that are loaded from the dex cache
2112 // fields of the current method.
2113 bool NeedsCurrentMethod() const {
2114 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2115 }
2116
Vladimir Markodc151b22015-10-15 18:02:30 +01002117 // Returns whether the code generation of the instruction will require to have access
2118 // to the dex cache of the current method's declaring class via the current method.
2119 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002120
Mark Mendellc4701932015-04-10 13:18:51 -04002121 // Does this instruction have any use in an environment before
2122 // control flow hits 'other'?
2123 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2124
2125 // Remove all references to environment uses of this instruction.
2126 // The caller must ensure that this is safe to do.
2127 void RemoveEnvironmentUsers();
2128
Vladimir Markoa1de9182016-02-25 11:37:38 +00002129 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2130 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002131
David Brazdil1abb4192015-02-17 18:33:36 +00002132 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002133 // If set, the machine code for this instruction is assumed to be generated by
2134 // its users. Used by liveness analysis to compute use positions accordingly.
2135 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2136 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2137 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2138 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2139
David Brazdil1abb4192015-02-17 18:33:36 +00002140 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2141 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2142
Vladimir Markoa1de9182016-02-25 11:37:38 +00002143 uint32_t GetPackedFields() const {
2144 return packed_fields_;
2145 }
2146
2147 template <size_t flag>
2148 bool GetPackedFlag() const {
2149 return (packed_fields_ & (1u << flag)) != 0u;
2150 }
2151
2152 template <size_t flag>
2153 void SetPackedFlag(bool value = true) {
2154 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2155 }
2156
2157 template <typename BitFieldType>
2158 typename BitFieldType::value_type GetPackedField() const {
2159 return BitFieldType::Decode(packed_fields_);
2160 }
2161
2162 template <typename BitFieldType>
2163 void SetPackedField(typename BitFieldType::value_type value) {
2164 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2165 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2166 }
2167
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002168 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002169 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2170
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002171 HInstruction* previous_;
2172 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002173 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002174 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002175
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002176 // An instruction gets an id when it is added to the graph.
2177 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002178 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002179 int id_;
2180
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002181 // When doing liveness analysis, instructions that have uses get an SSA index.
2182 int ssa_index_;
2183
Vladimir Markoa1de9182016-02-25 11:37:38 +00002184 // Packed fields.
2185 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002186
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002187 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002188 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002189
2190 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002191 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002192
Nicolas Geoffray39468442014-09-02 15:17:15 +01002193 // The environment associated with this instruction. Not null if the instruction
2194 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002195 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002196
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002197 // Set by the code generator.
2198 LocationSummary* locations_;
2199
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002200 // Set by the liveness analysis.
2201 LiveInterval* live_interval_;
2202
2203 // Set by the liveness analysis, this is the position in a linear
2204 // order of blocks where this instruction's live interval start.
2205 size_t lifetime_position_;
2206
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002207 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002208
Vladimir Markoa1de9182016-02-25 11:37:38 +00002209 // The reference handle part of the reference type info.
2210 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002211 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002212 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002213
David Brazdil1abb4192015-02-17 18:33:36 +00002214 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002215 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002216 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002217 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002218 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002219
2220 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2221};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002222std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002223
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002224class HInputIterator : public ValueObject {
2225 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002226 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002227
2228 bool Done() const { return index_ == instruction_->InputCount(); }
2229 HInstruction* Current() const { return instruction_->InputAt(index_); }
2230 void Advance() { index_++; }
2231
2232 private:
2233 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002234 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002235
2236 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2237};
2238
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002239class HInstructionIterator : public ValueObject {
2240 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002241 explicit HInstructionIterator(const HInstructionList& instructions)
2242 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002243 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002244 }
2245
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002246 bool Done() const { return instruction_ == nullptr; }
2247 HInstruction* Current() const { return instruction_; }
2248 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002249 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002250 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002251 }
2252
2253 private:
2254 HInstruction* instruction_;
2255 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002256
2257 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002258};
2259
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002260class HBackwardInstructionIterator : public ValueObject {
2261 public:
2262 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2263 : instruction_(instructions.last_instruction_) {
2264 next_ = Done() ? nullptr : instruction_->GetPrevious();
2265 }
2266
2267 bool Done() const { return instruction_ == nullptr; }
2268 HInstruction* Current() const { return instruction_; }
2269 void Advance() {
2270 instruction_ = next_;
2271 next_ = Done() ? nullptr : instruction_->GetPrevious();
2272 }
2273
2274 private:
2275 HInstruction* instruction_;
2276 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002277
2278 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002279};
2280
Vladimir Markof9f64412015-09-02 14:05:49 +01002281template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002282class HTemplateInstruction: public HInstruction {
2283 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002284 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002285 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002286 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002287
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002288 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002289
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002290 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002291 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2292 DCHECK_LT(i, N);
2293 return inputs_[i];
2294 }
David Brazdil1abb4192015-02-17 18:33:36 +00002295
2296 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002297 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002298 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002299 }
2300
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002301 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002302 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002303
2304 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002305};
2306
Vladimir Markof9f64412015-09-02 14:05:49 +01002307// HTemplateInstruction specialization for N=0.
2308template<>
2309class HTemplateInstruction<0>: public HInstruction {
2310 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002311 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002312 : HInstruction(side_effects, dex_pc) {}
2313
Vladimir Markof9f64412015-09-02 14:05:49 +01002314 virtual ~HTemplateInstruction() {}
2315
2316 size_t InputCount() const OVERRIDE { return 0; }
2317
2318 protected:
2319 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2320 LOG(FATAL) << "Unreachable";
2321 UNREACHABLE();
2322 }
2323
2324 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2325 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2326 LOG(FATAL) << "Unreachable";
2327 UNREACHABLE();
2328 }
2329
2330 private:
2331 friend class SsaBuilder;
2332};
2333
Dave Allison20dfc792014-06-16 20:44:29 -07002334template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002335class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002336 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002337 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002338 : HTemplateInstruction<N>(side_effects, dex_pc) {
2339 this->template SetPackedField<TypeField>(type);
2340 }
Dave Allison20dfc792014-06-16 20:44:29 -07002341 virtual ~HExpression() {}
2342
Vladimir Markoa1de9182016-02-25 11:37:38 +00002343 Primitive::Type GetType() const OVERRIDE {
2344 return TypeField::Decode(this->GetPackedFields());
2345 }
Dave Allison20dfc792014-06-16 20:44:29 -07002346
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002347 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002348 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2349 static constexpr size_t kFieldTypeSize =
2350 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2351 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2352 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2353 "Too many packed fields.");
2354 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002355};
2356
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002357// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2358// instruction that branches to the exit block.
2359class HReturnVoid : public HTemplateInstruction<0> {
2360 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002361 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2362 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002363
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002364 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002365
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002366 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002367
2368 private:
2369 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2370};
2371
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002372// Represents dex's RETURN opcodes. A HReturn is a control flow
2373// instruction that branches to the exit block.
2374class HReturn : public HTemplateInstruction<1> {
2375 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002376 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2377 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002378 SetRawInputAt(0, value);
2379 }
2380
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002381 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002382
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002383 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002384
2385 private:
2386 DISALLOW_COPY_AND_ASSIGN(HReturn);
2387};
2388
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002389// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002390// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002391// exit block.
2392class HExit : public HTemplateInstruction<0> {
2393 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002394 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002395
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002396 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002397
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002398 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002399
2400 private:
2401 DISALLOW_COPY_AND_ASSIGN(HExit);
2402};
2403
2404// Jumps from one block to another.
2405class HGoto : public HTemplateInstruction<0> {
2406 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002407 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002408
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002409 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002410
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002411 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002412 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002413 }
2414
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002415 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002416
2417 private:
2418 DISALLOW_COPY_AND_ASSIGN(HGoto);
2419};
2420
Roland Levillain9867bc72015-08-05 10:21:34 +01002421class HConstant : public HExpression<0> {
2422 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002423 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2424 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002425
2426 bool CanBeMoved() const OVERRIDE { return true; }
2427
2428 virtual bool IsMinusOne() const { return false; }
2429 virtual bool IsZero() const { return false; }
2430 virtual bool IsOne() const { return false; }
2431
David Brazdil77a48ae2015-09-15 12:34:04 +00002432 virtual uint64_t GetValueAsUint64() const = 0;
2433
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002434 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002435
2436 private:
2437 DISALLOW_COPY_AND_ASSIGN(HConstant);
2438};
2439
2440class HNullConstant : public HConstant {
2441 public:
2442 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2443 return true;
2444 }
2445
David Brazdil77a48ae2015-09-15 12:34:04 +00002446 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2447
Roland Levillain9867bc72015-08-05 10:21:34 +01002448 size_t ComputeHashCode() const OVERRIDE { return 0; }
2449
2450 DECLARE_INSTRUCTION(NullConstant);
2451
2452 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002453 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002454
2455 friend class HGraph;
2456 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2457};
2458
2459// Constants of the type int. Those can be from Dex instructions, or
2460// synthesized (for example with the if-eqz instruction).
2461class HIntConstant : public HConstant {
2462 public:
2463 int32_t GetValue() const { return value_; }
2464
David Brazdil9f389d42015-10-01 14:32:56 +01002465 uint64_t GetValueAsUint64() const OVERRIDE {
2466 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2467 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002468
Roland Levillain9867bc72015-08-05 10:21:34 +01002469 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002470 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002471 return other->AsIntConstant()->value_ == value_;
2472 }
2473
2474 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2475
2476 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2477 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2478 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2479
2480 DECLARE_INSTRUCTION(IntConstant);
2481
2482 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002483 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2484 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2485 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2486 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002487
2488 const int32_t value_;
2489
2490 friend class HGraph;
2491 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2492 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2493 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2494};
2495
2496class HLongConstant : public HConstant {
2497 public:
2498 int64_t GetValue() const { return value_; }
2499
David Brazdil77a48ae2015-09-15 12:34:04 +00002500 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2501
Roland Levillain9867bc72015-08-05 10:21:34 +01002502 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002503 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002504 return other->AsLongConstant()->value_ == value_;
2505 }
2506
2507 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2508
2509 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2510 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2511 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2512
2513 DECLARE_INSTRUCTION(LongConstant);
2514
2515 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002516 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2517 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002518
2519 const int64_t value_;
2520
2521 friend class HGraph;
2522 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2523};
Dave Allison20dfc792014-06-16 20:44:29 -07002524
Roland Levillain31dd3d62016-02-16 12:21:02 +00002525class HFloatConstant : public HConstant {
2526 public:
2527 float GetValue() const { return value_; }
2528
2529 uint64_t GetValueAsUint64() const OVERRIDE {
2530 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2531 }
2532
2533 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2534 DCHECK(other->IsFloatConstant()) << other->DebugName();
2535 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2536 }
2537
2538 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2539
2540 bool IsMinusOne() const OVERRIDE {
2541 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2542 }
2543 bool IsZero() const OVERRIDE {
2544 return value_ == 0.0f;
2545 }
2546 bool IsOne() const OVERRIDE {
2547 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2548 }
2549 bool IsNaN() const {
2550 return std::isnan(value_);
2551 }
2552
2553 DECLARE_INSTRUCTION(FloatConstant);
2554
2555 private:
2556 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2557 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2558 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2559 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2560
2561 const float value_;
2562
2563 // Only the SsaBuilder and HGraph can create floating-point constants.
2564 friend class SsaBuilder;
2565 friend class HGraph;
2566 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2567};
2568
2569class HDoubleConstant : public HConstant {
2570 public:
2571 double GetValue() const { return value_; }
2572
2573 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2574
2575 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2576 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2577 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2578 }
2579
2580 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2581
2582 bool IsMinusOne() const OVERRIDE {
2583 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2584 }
2585 bool IsZero() const OVERRIDE {
2586 return value_ == 0.0;
2587 }
2588 bool IsOne() const OVERRIDE {
2589 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2590 }
2591 bool IsNaN() const {
2592 return std::isnan(value_);
2593 }
2594
2595 DECLARE_INSTRUCTION(DoubleConstant);
2596
2597 private:
2598 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2599 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2600 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2601 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2602
2603 const double value_;
2604
2605 // Only the SsaBuilder and HGraph can create floating-point constants.
2606 friend class SsaBuilder;
2607 friend class HGraph;
2608 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2609};
2610
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002611// Conditional branch. A block ending with an HIf instruction must have
2612// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002613class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002614 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002615 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2616 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002617 SetRawInputAt(0, input);
2618 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002619
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002620 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002621
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002622 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002623 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002624 }
2625
2626 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002627 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002628 }
2629
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002630 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002631
2632 private:
2633 DISALLOW_COPY_AND_ASSIGN(HIf);
2634};
2635
David Brazdilfc6a86a2015-06-26 10:33:45 +00002636
2637// Abstract instruction which marks the beginning and/or end of a try block and
2638// links it to the respective exception handlers. Behaves the same as a Goto in
2639// non-exceptional control flow.
2640// Normal-flow successor is stored at index zero, exception handlers under
2641// higher indices in no particular order.
2642class HTryBoundary : public HTemplateInstruction<0> {
2643 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002644 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002645 kEntry,
2646 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002647 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002648 };
2649
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002650 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002651 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2652 SetPackedField<BoundaryKindField>(kind);
2653 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002654
2655 bool IsControlFlow() const OVERRIDE { return true; }
2656
2657 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002658 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002659
David Brazdild26a4112015-11-10 11:07:31 +00002660 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2661 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2662 }
2663
David Brazdilfc6a86a2015-06-26 10:33:45 +00002664 // Returns whether `handler` is among its exception handlers (non-zero index
2665 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002666 bool HasExceptionHandler(const HBasicBlock& handler) const {
2667 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002668 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002669 }
2670
2671 // If not present already, adds `handler` to its block's list of exception
2672 // handlers.
2673 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002674 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002675 GetBlock()->AddSuccessor(handler);
2676 }
2677 }
2678
Vladimir Markoa1de9182016-02-25 11:37:38 +00002679 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2680 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002681
David Brazdilffee3d32015-07-06 11:48:53 +01002682 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2683
David Brazdilfc6a86a2015-06-26 10:33:45 +00002684 DECLARE_INSTRUCTION(TryBoundary);
2685
2686 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002687 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2688 static constexpr size_t kFieldBoundaryKindSize =
2689 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2690 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2691 kFieldBoundaryKind + kFieldBoundaryKindSize;
2692 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2693 "Too many packed fields.");
2694 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002695
2696 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2697};
2698
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002699// Deoptimize to interpreter, upon checking a condition.
2700class HDeoptimize : public HTemplateInstruction<1> {
2701 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002702 // We set CanTriggerGC to prevent any intermediate address to be live
2703 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002704 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002705 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002706 SetRawInputAt(0, cond);
2707 }
2708
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002709 bool CanBeMoved() const OVERRIDE { return true; }
2710 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2711 return true;
2712 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002713 bool NeedsEnvironment() const OVERRIDE { return true; }
2714 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002715
2716 DECLARE_INSTRUCTION(Deoptimize);
2717
2718 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002719 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2720};
2721
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002722// Represents the ArtMethod that was passed as a first argument to
2723// the method. It is used by instructions that depend on it, like
2724// instructions that work with the dex cache.
2725class HCurrentMethod : public HExpression<0> {
2726 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002727 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2728 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002729
2730 DECLARE_INSTRUCTION(CurrentMethod);
2731
2732 private:
2733 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2734};
2735
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002736// Fetches an ArtMethod from the virtual table or the interface method table
2737// of a class.
2738class HClassTableGet : public HExpression<1> {
2739 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002740 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002741 kVTable,
2742 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002743 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002744 };
2745 HClassTableGet(HInstruction* cls,
2746 Primitive::Type type,
2747 TableKind kind,
2748 size_t index,
2749 uint32_t dex_pc)
2750 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002751 index_(index) {
2752 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002753 SetRawInputAt(0, cls);
2754 }
2755
2756 bool CanBeMoved() const OVERRIDE { return true; }
2757 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2758 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002759 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002760 }
2761
Vladimir Markoa1de9182016-02-25 11:37:38 +00002762 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002763 size_t GetIndex() const { return index_; }
2764
2765 DECLARE_INSTRUCTION(ClassTableGet);
2766
2767 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002768 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2769 static constexpr size_t kFieldTableKindSize =
2770 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2771 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2772 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2773 "Too many packed fields.");
2774 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2775
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002776 // The index of the ArtMethod in the table.
2777 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002778
2779 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2780};
2781
Mark Mendellfe57faa2015-09-18 09:26:15 -04002782// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2783// have one successor for each entry in the switch table, and the final successor
2784// will be the block containing the next Dex opcode.
2785class HPackedSwitch : public HTemplateInstruction<1> {
2786 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002787 HPackedSwitch(int32_t start_value,
2788 uint32_t num_entries,
2789 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002790 uint32_t dex_pc = kNoDexPc)
2791 : HTemplateInstruction(SideEffects::None(), dex_pc),
2792 start_value_(start_value),
2793 num_entries_(num_entries) {
2794 SetRawInputAt(0, input);
2795 }
2796
2797 bool IsControlFlow() const OVERRIDE { return true; }
2798
2799 int32_t GetStartValue() const { return start_value_; }
2800
Vladimir Marko211c2112015-09-24 16:52:33 +01002801 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002802
2803 HBasicBlock* GetDefaultBlock() const {
2804 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002805 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002806 }
2807 DECLARE_INSTRUCTION(PackedSwitch);
2808
2809 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002810 const int32_t start_value_;
2811 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002812
2813 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2814};
2815
Roland Levillain88cb1752014-10-20 16:36:47 +01002816class HUnaryOperation : public HExpression<1> {
2817 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002818 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2819 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002820 SetRawInputAt(0, input);
2821 }
2822
2823 HInstruction* GetInput() const { return InputAt(0); }
2824 Primitive::Type GetResultType() const { return GetType(); }
2825
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002826 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002827 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002828 return true;
2829 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002830
Roland Levillain31dd3d62016-02-16 12:21:02 +00002831 // Try to statically evaluate `this` and return a HConstant
2832 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002833 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002834 HConstant* TryStaticEvaluation() const;
2835
2836 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002837 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2838 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002839 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2840 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002841
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002842 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002843
2844 private:
2845 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2846};
2847
Dave Allison20dfc792014-06-16 20:44:29 -07002848class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002849 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002850 HBinaryOperation(Primitive::Type result_type,
2851 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002852 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002853 SideEffects side_effects = SideEffects::None(),
2854 uint32_t dex_pc = kNoDexPc)
2855 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002856 SetRawInputAt(0, left);
2857 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002858 }
2859
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002860 HInstruction* GetLeft() const { return InputAt(0); }
2861 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002862 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002863
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002864 virtual bool IsCommutative() const { return false; }
2865
2866 // Put constant on the right.
2867 // Returns whether order is changed.
2868 bool OrderInputsWithConstantOnTheRight() {
2869 HInstruction* left = InputAt(0);
2870 HInstruction* right = InputAt(1);
2871 if (left->IsConstant() && !right->IsConstant()) {
2872 ReplaceInput(right, 0);
2873 ReplaceInput(left, 1);
2874 return true;
2875 }
2876 return false;
2877 }
2878
2879 // Order inputs by instruction id, but favor constant on the right side.
2880 // This helps GVN for commutative ops.
2881 void OrderInputs() {
2882 DCHECK(IsCommutative());
2883 HInstruction* left = InputAt(0);
2884 HInstruction* right = InputAt(1);
2885 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2886 return;
2887 }
2888 if (OrderInputsWithConstantOnTheRight()) {
2889 return;
2890 }
2891 // Order according to instruction id.
2892 if (left->GetId() > right->GetId()) {
2893 ReplaceInput(right, 0);
2894 ReplaceInput(left, 1);
2895 }
2896 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002897
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002898 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002899 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002900 return true;
2901 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002902
Roland Levillain31dd3d62016-02-16 12:21:02 +00002903 // Try to statically evaluate `this` and return a HConstant
2904 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002905 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002906 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002907
2908 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002909 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2910 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002911 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2912 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002913 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002914 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2915 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002916 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2917 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002918 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2919 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01002920 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002921 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2922 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002923
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002924 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002925 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002926 HConstant* GetConstantRight() const;
2927
2928 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002929 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002930 HInstruction* GetLeastConstantLeft() const;
2931
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002932 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002933
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002934 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002935 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2936};
2937
Mark Mendellc4701932015-04-10 13:18:51 -04002938// The comparison bias applies for floating point operations and indicates how NaN
2939// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002940enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002941 kNoBias, // bias is not applicable (i.e. for long operation)
2942 kGtBias, // return 1 for NaN comparisons
2943 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00002944 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04002945};
2946
Roland Levillain31dd3d62016-02-16 12:21:02 +00002947std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
2948
Dave Allison20dfc792014-06-16 20:44:29 -07002949class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002950 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002951 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002952 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
2953 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
2954 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002955
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002956 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002957 // `instruction`, and disregard moves in between.
2958 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002959
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002960 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002961
2962 virtual IfCondition GetCondition() const = 0;
2963
Mark Mendellc4701932015-04-10 13:18:51 -04002964 virtual IfCondition GetOppositeCondition() const = 0;
2965
Vladimir Markoa1de9182016-02-25 11:37:38 +00002966 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06002967 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
2968
Vladimir Markoa1de9182016-02-25 11:37:38 +00002969 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
2970 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04002971
2972 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002973 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04002974 }
2975
Roland Levillain4fa13f62015-07-06 18:11:54 +01002976 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002977 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002978 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06002979 if (if_cond == kCondNE) {
2980 return true;
2981 } else if (if_cond == kCondEQ) {
2982 return false;
2983 }
2984 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002985 }
2986
2987 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002988 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002989 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06002990 if (if_cond == kCondEQ) {
2991 return true;
2992 } else if (if_cond == kCondNE) {
2993 return false;
2994 }
2995 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002996 }
2997
Roland Levillain31dd3d62016-02-16 12:21:02 +00002998 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002999 // Needed if we merge a HCompare into a HCondition.
3000 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3001 static constexpr size_t kFieldComparisonBiasSize =
3002 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3003 static constexpr size_t kNumberOfConditionPackedBits =
3004 kFieldComparisonBias + kFieldComparisonBiasSize;
3005 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3006 using ComparisonBiasField =
3007 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3008
Roland Levillain31dd3d62016-02-16 12:21:02 +00003009 template <typename T>
3010 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3011
3012 template <typename T>
3013 int32_t CompareFP(T x, T y) const {
3014 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3015 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3016 // Handle the bias.
3017 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3018 }
3019
3020 // Return an integer constant containing the result of a condition evaluated at compile time.
3021 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3022 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3023 }
3024
Dave Allison20dfc792014-06-16 20:44:29 -07003025 private:
3026 DISALLOW_COPY_AND_ASSIGN(HCondition);
3027};
3028
3029// Instruction to check if two inputs are equal to each other.
3030class HEqual : public HCondition {
3031 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003032 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3033 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003034
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003035 bool IsCommutative() const OVERRIDE { return true; }
3036
Vladimir Marko9e23df52015-11-10 17:14:35 +00003037 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3038 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003039 return MakeConstantCondition(true, GetDexPc());
3040 }
3041 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3042 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3043 }
3044 // In the following Evaluate methods, a HCompare instruction has
3045 // been merged into this HEqual instruction; evaluate it as
3046 // `Compare(x, y) == 0`.
3047 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3048 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3049 GetDexPc());
3050 }
3051 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3052 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3053 }
3054 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3055 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003056 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003057
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003058 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003059
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003060 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003061 return kCondEQ;
3062 }
3063
Mark Mendellc4701932015-04-10 13:18:51 -04003064 IfCondition GetOppositeCondition() const OVERRIDE {
3065 return kCondNE;
3066 }
3067
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003068 private:
Aart Bike9f37602015-10-09 11:15:55 -07003069 template <typename T> bool Compute(T x, T y) const { return x == y; }
3070
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003071 DISALLOW_COPY_AND_ASSIGN(HEqual);
3072};
3073
Dave Allison20dfc792014-06-16 20:44:29 -07003074class HNotEqual : public HCondition {
3075 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003076 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3077 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003078
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003079 bool IsCommutative() const OVERRIDE { return true; }
3080
Vladimir Marko9e23df52015-11-10 17:14:35 +00003081 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3082 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003083 return MakeConstantCondition(false, GetDexPc());
3084 }
3085 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3086 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3087 }
3088 // In the following Evaluate methods, a HCompare instruction has
3089 // been merged into this HNotEqual instruction; evaluate it as
3090 // `Compare(x, y) != 0`.
3091 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3092 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3093 }
3094 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3095 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3096 }
3097 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3098 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003099 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003100
Dave Allison20dfc792014-06-16 20:44:29 -07003101 DECLARE_INSTRUCTION(NotEqual);
3102
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003103 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003104 return kCondNE;
3105 }
3106
Mark Mendellc4701932015-04-10 13:18:51 -04003107 IfCondition GetOppositeCondition() const OVERRIDE {
3108 return kCondEQ;
3109 }
3110
Dave Allison20dfc792014-06-16 20:44:29 -07003111 private:
Aart Bike9f37602015-10-09 11:15:55 -07003112 template <typename T> bool Compute(T x, T y) const { return x != y; }
3113
Dave Allison20dfc792014-06-16 20:44:29 -07003114 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3115};
3116
3117class HLessThan : public HCondition {
3118 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003119 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3120 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003121
Roland Levillain9867bc72015-08-05 10:21:34 +01003122 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003123 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003124 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003125 // In the following Evaluate methods, a HCompare instruction has
3126 // been merged into this HLessThan instruction; evaluate it as
3127 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003128 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003129 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), 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());
Roland Levillain93445682014-10-06 19:24:02 +01003136 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003137
Dave Allison20dfc792014-06-16 20:44:29 -07003138 DECLARE_INSTRUCTION(LessThan);
3139
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003140 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003141 return kCondLT;
3142 }
3143
Mark Mendellc4701932015-04-10 13:18:51 -04003144 IfCondition GetOppositeCondition() const OVERRIDE {
3145 return kCondGE;
3146 }
3147
Dave Allison20dfc792014-06-16 20:44:29 -07003148 private:
Aart Bike9f37602015-10-09 11:15:55 -07003149 template <typename T> bool Compute(T x, T y) const { return x < y; }
3150
Dave Allison20dfc792014-06-16 20:44:29 -07003151 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3152};
3153
3154class HLessThanOrEqual : public HCondition {
3155 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003156 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3157 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003158
Roland Levillain9867bc72015-08-05 10:21:34 +01003159 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003160 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003161 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003162 // In the following Evaluate methods, a HCompare instruction has
3163 // been merged into this HLessThanOrEqual instruction; evaluate it as
3164 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003165 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003166 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3167 }
3168 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3169 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3170 }
3171 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3172 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003173 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003174
Dave Allison20dfc792014-06-16 20:44:29 -07003175 DECLARE_INSTRUCTION(LessThanOrEqual);
3176
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003177 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003178 return kCondLE;
3179 }
3180
Mark Mendellc4701932015-04-10 13:18:51 -04003181 IfCondition GetOppositeCondition() const OVERRIDE {
3182 return kCondGT;
3183 }
3184
Dave Allison20dfc792014-06-16 20:44:29 -07003185 private:
Aart Bike9f37602015-10-09 11:15:55 -07003186 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3187
Dave Allison20dfc792014-06-16 20:44:29 -07003188 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3189};
3190
3191class HGreaterThan : public HCondition {
3192 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003193 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3194 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003195
Roland Levillain9867bc72015-08-05 10:21:34 +01003196 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003197 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003198 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003199 // In the following Evaluate methods, a HCompare instruction has
3200 // been merged into this HGreaterThan instruction; evaluate it as
3201 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003202 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003203 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3204 }
3205 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3206 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3207 }
3208 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3209 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003210 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003211
Dave Allison20dfc792014-06-16 20:44:29 -07003212 DECLARE_INSTRUCTION(GreaterThan);
3213
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003214 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003215 return kCondGT;
3216 }
3217
Mark Mendellc4701932015-04-10 13:18:51 -04003218 IfCondition GetOppositeCondition() const OVERRIDE {
3219 return kCondLE;
3220 }
3221
Dave Allison20dfc792014-06-16 20:44:29 -07003222 private:
Aart Bike9f37602015-10-09 11:15:55 -07003223 template <typename T> bool Compute(T x, T y) const { return x > y; }
3224
Dave Allison20dfc792014-06-16 20:44:29 -07003225 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3226};
3227
3228class HGreaterThanOrEqual : public HCondition {
3229 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003230 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3231 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003232
Roland Levillain9867bc72015-08-05 10:21:34 +01003233 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003234 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003235 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003236 // In the following Evaluate methods, a HCompare instruction has
3237 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3238 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003239 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003240 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3241 }
3242 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3243 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3244 }
3245 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3246 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003247 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003248
Dave Allison20dfc792014-06-16 20:44:29 -07003249 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3250
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003251 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003252 return kCondGE;
3253 }
3254
Mark Mendellc4701932015-04-10 13:18:51 -04003255 IfCondition GetOppositeCondition() const OVERRIDE {
3256 return kCondLT;
3257 }
3258
Dave Allison20dfc792014-06-16 20:44:29 -07003259 private:
Aart Bike9f37602015-10-09 11:15:55 -07003260 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3261
Dave Allison20dfc792014-06-16 20:44:29 -07003262 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3263};
3264
Aart Bike9f37602015-10-09 11:15:55 -07003265class HBelow : public HCondition {
3266 public:
3267 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3268 : HCondition(first, second, dex_pc) {}
3269
3270 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003271 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003272 }
3273 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003274 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3275 }
3276 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3277 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3278 LOG(FATAL) << DebugName() << " is not defined for float values";
3279 UNREACHABLE();
3280 }
3281 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3282 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3283 LOG(FATAL) << DebugName() << " is not defined for double values";
3284 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003285 }
3286
3287 DECLARE_INSTRUCTION(Below);
3288
3289 IfCondition GetCondition() const OVERRIDE {
3290 return kCondB;
3291 }
3292
3293 IfCondition GetOppositeCondition() const OVERRIDE {
3294 return kCondAE;
3295 }
3296
3297 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003298 template <typename T> bool Compute(T x, T y) const {
3299 return MakeUnsigned(x) < MakeUnsigned(y);
3300 }
Aart Bike9f37602015-10-09 11:15:55 -07003301
3302 DISALLOW_COPY_AND_ASSIGN(HBelow);
3303};
3304
3305class HBelowOrEqual : public HCondition {
3306 public:
3307 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3308 : HCondition(first, second, dex_pc) {}
3309
3310 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003311 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003312 }
3313 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003314 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3315 }
3316 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3317 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3318 LOG(FATAL) << DebugName() << " is not defined for float values";
3319 UNREACHABLE();
3320 }
3321 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3322 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3323 LOG(FATAL) << DebugName() << " is not defined for double values";
3324 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003325 }
3326
3327 DECLARE_INSTRUCTION(BelowOrEqual);
3328
3329 IfCondition GetCondition() const OVERRIDE {
3330 return kCondBE;
3331 }
3332
3333 IfCondition GetOppositeCondition() const OVERRIDE {
3334 return kCondA;
3335 }
3336
3337 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003338 template <typename T> bool Compute(T x, T y) const {
3339 return MakeUnsigned(x) <= MakeUnsigned(y);
3340 }
Aart Bike9f37602015-10-09 11:15:55 -07003341
3342 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3343};
3344
3345class HAbove : public HCondition {
3346 public:
3347 HAbove(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(Above);
3368
3369 IfCondition GetCondition() const OVERRIDE {
3370 return kCondA;
3371 }
3372
3373 IfCondition GetOppositeCondition() const OVERRIDE {
3374 return kCondBE;
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(HAbove);
3383};
3384
3385class HAboveOrEqual : public HCondition {
3386 public:
3387 HAboveOrEqual(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(AboveOrEqual);
3408
3409 IfCondition GetCondition() const OVERRIDE {
3410 return kCondAE;
3411 }
3412
3413 IfCondition GetOppositeCondition() const OVERRIDE {
3414 return kCondB;
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(HAboveOrEqual);
3423};
Dave Allison20dfc792014-06-16 20:44:29 -07003424
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003425// Instruction to check how two inputs compare to each other.
3426// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3427class HCompare : public HBinaryOperation {
3428 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003429 HCompare(Primitive::Type type,
3430 HInstruction* first,
3431 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003432 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003433 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003434 : HBinaryOperation(Primitive::kPrimInt,
3435 first,
3436 second,
3437 SideEffectsForArchRuntimeCalls(type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003438 dex_pc) {
3439 SetPackedField<ComparisonBiasField>(bias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003440 DCHECK_EQ(type, first->GetType());
3441 DCHECK_EQ(type, second->GetType());
3442 }
3443
Roland Levillain9867bc72015-08-05 10:21:34 +01003444 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003445 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3446
3447 template <typename T>
3448 int32_t ComputeFP(T x, T y) const {
3449 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3450 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3451 // Handle the bias.
3452 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3453 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003454
Roland Levillain9867bc72015-08-05 10:21:34 +01003455 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003456 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3457 // reach this code path when processing a freshly built HIR
3458 // graph. However HCompare integer instructions can be synthesized
3459 // by the instruction simplifier to implement IntegerCompare and
3460 // IntegerSignum intrinsics, so we have to handle this case.
3461 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003462 }
3463 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003464 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3465 }
3466 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3467 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3468 }
3469 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3470 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003471 }
3472
Calin Juravleddb7df22014-11-25 20:56:51 +00003473 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003474 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003475 }
3476
Vladimir Markoa1de9182016-02-25 11:37:38 +00003477 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003478
Roland Levillain31dd3d62016-02-16 12:21:02 +00003479 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003480 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003481 bool IsGtBias() const {
3482 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003483 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003484 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003485
3486 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3487 // MIPS64 uses a runtime call for FP comparisons.
3488 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3489 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003490
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003491 DECLARE_INSTRUCTION(Compare);
3492
Roland Levillain31dd3d62016-02-16 12:21:02 +00003493 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003494 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3495 static constexpr size_t kFieldComparisonBiasSize =
3496 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3497 static constexpr size_t kNumberOfComparePackedBits =
3498 kFieldComparisonBias + kFieldComparisonBiasSize;
3499 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3500 using ComparisonBiasField =
3501 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3502
Roland Levillain31dd3d62016-02-16 12:21:02 +00003503 // Return an integer constant containing the result of a comparison evaluated at compile time.
3504 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3505 DCHECK(value == -1 || value == 0 || value == 1) << value;
3506 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3507 }
3508
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003509 private:
3510 DISALLOW_COPY_AND_ASSIGN(HCompare);
3511};
3512
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003513// A local in the graph. Corresponds to a Dex register.
3514class HLocal : public HTemplateInstruction<0> {
3515 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003516 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003517 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003518
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003519 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003520
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003521 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003522
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003523 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003524 // The Dex register number.
3525 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003526
3527 DISALLOW_COPY_AND_ASSIGN(HLocal);
3528};
3529
3530// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003531class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003532 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003533 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3534 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003535 SetRawInputAt(0, local);
3536 }
3537
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003538 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3539
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003540 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003541
3542 private:
3543 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3544};
3545
3546// Store a value in a given local. This instruction has two inputs: the value
3547// and the local.
3548class HStoreLocal : public HTemplateInstruction<2> {
3549 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003550 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3551 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003552 SetRawInputAt(0, local);
3553 SetRawInputAt(1, value);
3554 }
3555
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003556 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3557
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003558 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003559
3560 private:
3561 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3562};
3563
David Brazdil6de19382016-01-08 17:37:10 +00003564class HNewInstance : public HExpression<2> {
3565 public:
3566 HNewInstance(HInstruction* cls,
3567 HCurrentMethod* current_method,
3568 uint32_t dex_pc,
3569 uint16_t type_index,
3570 const DexFile& dex_file,
3571 bool can_throw,
3572 bool finalizable,
3573 QuickEntrypointEnum entrypoint)
3574 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3575 type_index_(type_index),
3576 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003577 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003578 SetPackedFlag<kFlagCanThrow>(can_throw);
3579 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003580 SetRawInputAt(0, cls);
3581 SetRawInputAt(1, current_method);
3582 }
3583
3584 uint16_t GetTypeIndex() const { return type_index_; }
3585 const DexFile& GetDexFile() const { return dex_file_; }
3586
3587 // Calls runtime so needs an environment.
3588 bool NeedsEnvironment() const OVERRIDE { return true; }
3589
3590 // It may throw when called on type that's not instantiable/accessible.
3591 // It can throw OOME.
3592 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003593 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>() || true; }
David Brazdil6de19382016-01-08 17:37:10 +00003594
Vladimir Markoa1de9182016-02-25 11:37:38 +00003595 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003596
3597 bool CanBeNull() const OVERRIDE { return false; }
3598
3599 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3600
3601 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3602 entrypoint_ = entrypoint;
3603 }
3604
3605 bool IsStringAlloc() const;
3606
3607 DECLARE_INSTRUCTION(NewInstance);
3608
3609 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003610 static constexpr size_t kFlagCanThrow = kNumberOfExpressionPackedBits;
3611 static constexpr size_t kFlagFinalizable = kFlagCanThrow + 1;
3612 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3613 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3614 "Too many packed fields.");
3615
David Brazdil6de19382016-01-08 17:37:10 +00003616 const uint16_t type_index_;
3617 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003618 QuickEntrypointEnum entrypoint_;
3619
3620 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3621};
3622
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003623enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003624#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3625 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003626#include "intrinsics_list.h"
3627 kNone,
3628 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3629#undef INTRINSICS_LIST
3630#undef OPTIMIZING_INTRINSICS
3631};
3632std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3633
Agi Csaki05f20562015-08-19 14:58:14 -07003634enum IntrinsicNeedsEnvironmentOrCache {
3635 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3636 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003637};
3638
Aart Bik5d75afe2015-12-14 11:57:01 -08003639enum IntrinsicSideEffects {
3640 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3641 kReadSideEffects, // Intrinsic may read heap memory.
3642 kWriteSideEffects, // Intrinsic may write heap memory.
3643 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3644};
3645
3646enum IntrinsicExceptions {
3647 kNoThrow, // Intrinsic does not throw any exceptions.
3648 kCanThrow // Intrinsic may throw exceptions.
3649};
3650
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003651class HInvoke : public HInstruction {
3652 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003653 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003654
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003655 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003656
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003657 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003658 SetRawInputAt(index, argument);
3659 }
3660
Roland Levillain3e3d7332015-04-28 11:00:54 +01003661 // Return the number of arguments. This number can be lower than
3662 // the number of inputs returned by InputCount(), as some invoke
3663 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3664 // inputs at the end of their list of inputs.
3665 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3666
Vladimir Markoa1de9182016-02-25 11:37:38 +00003667 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003668
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003669 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003670 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003671
Vladimir Markoa1de9182016-02-25 11:37:38 +00003672 InvokeType GetOriginalInvokeType() const {
3673 return GetPackedField<OriginalInvokeTypeField>();
3674 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003675
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003676 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003677 return intrinsic_;
3678 }
3679
Aart Bik5d75afe2015-12-14 11:57:01 -08003680 void SetIntrinsic(Intrinsics intrinsic,
3681 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3682 IntrinsicSideEffects side_effects,
3683 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003684
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003685 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003686 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003687 }
3688
Vladimir Markoa1de9182016-02-25 11:37:38 +00003689 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003690
3691 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3692
3693 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3694 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3695 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003696
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003697 uint32_t* GetIntrinsicOptimizations() {
3698 return &intrinsic_optimizations_;
3699 }
3700
3701 const uint32_t* GetIntrinsicOptimizations() const {
3702 return &intrinsic_optimizations_;
3703 }
3704
3705 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3706
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003707 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003708
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003709 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003710 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3711 static constexpr size_t kFieldOriginalInvokeTypeSize =
3712 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3713 static constexpr size_t kFieldReturnType =
3714 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3715 static constexpr size_t kFieldReturnTypeSize =
3716 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3717 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3718 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3719 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3720 using OriginalInvokeTypeField =
3721 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3722 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3723
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003724 HInvoke(ArenaAllocator* arena,
3725 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003726 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003727 Primitive::Type return_type,
3728 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003729 uint32_t dex_method_index,
3730 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003731 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003732 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003733 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003734 inputs_(number_of_arguments + number_of_other_inputs,
3735 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003736 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003737 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003738 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003739 SetPackedField<ReturnTypeField>(return_type);
3740 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3741 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003742 }
3743
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003744 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003745 return inputs_[index];
3746 }
3747
David Brazdil1abb4192015-02-17 18:33:36 +00003748 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003749 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003750 }
3751
Vladimir Markoa1de9182016-02-25 11:37:38 +00003752 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003753
Roland Levillain3e3d7332015-04-28 11:00:54 +01003754 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003755 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003756 const uint32_t dex_method_index_;
3757 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003758
3759 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3760 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003761
3762 private:
3763 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3764};
3765
Calin Juravle175dc732015-08-25 15:42:32 +01003766class HInvokeUnresolved : public HInvoke {
3767 public:
3768 HInvokeUnresolved(ArenaAllocator* arena,
3769 uint32_t number_of_arguments,
3770 Primitive::Type return_type,
3771 uint32_t dex_pc,
3772 uint32_t dex_method_index,
3773 InvokeType invoke_type)
3774 : HInvoke(arena,
3775 number_of_arguments,
3776 0u /* number_of_other_inputs */,
3777 return_type,
3778 dex_pc,
3779 dex_method_index,
3780 invoke_type) {
3781 }
3782
3783 DECLARE_INSTRUCTION(InvokeUnresolved);
3784
3785 private:
3786 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3787};
3788
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003789class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003790 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003791 // Requirements of this method call regarding the class
3792 // initialization (clinit) check of its declaring class.
3793 enum class ClinitCheckRequirement {
3794 kNone, // Class already initialized.
3795 kExplicit, // Static call having explicit clinit check as last input.
3796 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003797 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003798 };
3799
Vladimir Marko58155012015-08-19 12:49:41 +00003800 // Determines how to load the target ArtMethod*.
3801 enum class MethodLoadKind {
3802 // Use a String init ArtMethod* loaded from Thread entrypoints.
3803 kStringInit,
3804
3805 // Use the method's own ArtMethod* loaded by the register allocator.
3806 kRecursive,
3807
3808 // Use ArtMethod* at a known address, embed the direct address in the code.
3809 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3810 kDirectAddress,
3811
3812 // Use ArtMethod* at an address that will be known at link time, embed the direct
3813 // address in the code. If the image is relocatable, emit .patch_oat entry.
3814 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3815 // the image relocatable or not.
3816 kDirectAddressWithFixup,
3817
Vladimir Markoa1de9182016-02-25 11:37:38 +00003818 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003819 // Used when we need to use the dex cache, for example for invoke-static that
3820 // may cause class initialization (the entry may point to a resolution method),
3821 // and we know that we can access the dex cache arrays using a PC-relative load.
3822 kDexCachePcRelative,
3823
3824 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3825 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3826 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3827 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3828 kDexCacheViaMethod,
3829 };
3830
3831 // Determines the location of the code pointer.
3832 enum class CodePtrLocation {
3833 // Recursive call, use local PC-relative call instruction.
3834 kCallSelf,
3835
3836 // Use PC-relative call instruction patched at link time.
3837 // Used for calls within an oat file, boot->boot or app->app.
3838 kCallPCRelative,
3839
3840 // Call to a known target address, embed the direct address in code.
3841 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3842 kCallDirect,
3843
3844 // Call to a target address that will be known at link time, embed the direct
3845 // address in code. If the image is relocatable, emit .patch_oat entry.
3846 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3847 // the image relocatable or not.
3848 kCallDirectWithFixup,
3849
3850 // Use code pointer from the ArtMethod*.
3851 // Used when we don't know the target code. This is also the last-resort-kind used when
3852 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3853 kCallArtMethod,
3854 };
3855
3856 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003857 MethodLoadKind method_load_kind;
3858 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003859 // The method load data holds
3860 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3861 // Note that there are multiple string init methods, each having its own offset.
3862 // - the method address for kDirectAddress
3863 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003864 uint64_t method_load_data;
3865 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003866 };
3867
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003868 HInvokeStaticOrDirect(ArenaAllocator* arena,
3869 uint32_t number_of_arguments,
3870 Primitive::Type return_type,
3871 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003872 uint32_t method_index,
3873 MethodReference target_method,
3874 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003875 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003876 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003877 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003878 : HInvoke(arena,
3879 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003880 // There is potentially one extra argument for the HCurrentMethod node, and
3881 // potentially one other if the clinit check is explicit, and potentially
3882 // one other if the method is a string factory.
3883 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3884 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3885 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003886 return_type,
3887 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003888 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003889 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003890 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003891 dispatch_info_(dispatch_info) {
3892 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3893 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3894 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003895
Vladimir Markodc151b22015-10-15 18:02:30 +01003896 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003897 bool had_current_method_input = HasCurrentMethodInput();
3898 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3899
3900 // Using the current method is the default and once we find a better
3901 // method load kind, we should not go back to using the current method.
3902 DCHECK(had_current_method_input || !needs_current_method_input);
3903
3904 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003905 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3906 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003907 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003908 dispatch_info_ = dispatch_info;
3909 }
3910
Vladimir Markoc53c0792015-11-19 15:48:33 +00003911 void AddSpecialInput(HInstruction* input) {
3912 // We allow only one special input.
3913 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3914 DCHECK(InputCount() == GetSpecialInputIndex() ||
3915 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3916 InsertInputAt(GetSpecialInputIndex(), input);
3917 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003918
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003919 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003920 // We access the method via the dex cache so we can't do an implicit null check.
3921 // TODO: for intrinsics we can generate implicit null checks.
3922 return false;
3923 }
3924
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003925 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003926 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003927 }
3928
Vladimir Markoc53c0792015-11-19 15:48:33 +00003929 // Get the index of the special input, if any.
3930 //
David Brazdil6de19382016-01-08 17:37:10 +00003931 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3932 // method pointer; otherwise there may be one platform-specific special input,
3933 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003934 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003935 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003936
Vladimir Markoa1de9182016-02-25 11:37:38 +00003937 InvokeType GetOptimizedInvokeType() const {
3938 return GetPackedField<OptimizedInvokeTypeField>();
3939 }
3940
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003941 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003942 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003943 }
3944
Vladimir Marko58155012015-08-19 12:49:41 +00003945 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3946 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3947 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003948 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003949 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003950 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003951 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003952 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3953 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003954 bool HasCurrentMethodInput() const {
3955 // This function can be called only after the invoke has been fully initialized by the builder.
3956 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003957 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003958 return true;
3959 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003960 DCHECK(InputCount() == GetSpecialInputIndex() ||
3961 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003962 return false;
3963 }
3964 }
Vladimir Marko58155012015-08-19 12:49:41 +00003965 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3966 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003967 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003968
3969 int32_t GetStringInitOffset() const {
3970 DCHECK(IsStringInit());
3971 return dispatch_info_.method_load_data;
3972 }
3973
3974 uint64_t GetMethodAddress() const {
3975 DCHECK(HasMethodAddress());
3976 return dispatch_info_.method_load_data;
3977 }
3978
3979 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003980 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003981 return dispatch_info_.method_load_data;
3982 }
3983
3984 uint64_t GetDirectCodePtr() const {
3985 DCHECK(HasDirectCodePtr());
3986 return dispatch_info_.direct_code_ptr;
3987 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003988
Vladimir Markoa1de9182016-02-25 11:37:38 +00003989 ClinitCheckRequirement GetClinitCheckRequirement() const {
3990 return GetPackedField<ClinitCheckRequirementField>();
3991 }
Calin Juravle68ad6492015-08-18 17:08:12 +01003992
Roland Levillain4c0eb422015-04-24 16:43:49 +01003993 // Is this instruction a call to a static method?
3994 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003995 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003996 }
3997
Vladimir Markofbb184a2015-11-13 14:47:00 +00003998 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3999 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4000 // instruction; only relevant for static calls with explicit clinit check.
4001 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004002 DCHECK(IsStaticWithExplicitClinitCheck());
4003 size_t last_input_index = InputCount() - 1;
4004 HInstruction* last_input = InputAt(last_input_index);
4005 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004006 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004007 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004008 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004009 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004010 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004011 }
4012
David Brazdilbc9ab162016-01-20 14:50:19 +00004013 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00004014 DCHECK(IsStringInit());
4015 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00004016 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00004017 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004018 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00004019 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004020 }
4021
Roland Levillain4c0eb422015-04-24 16:43:49 +01004022 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004023 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004024 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004025 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004026 }
4027
4028 // Is this a call to a static method whose declaring class has an
4029 // implicit intialization check requirement?
4030 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004031 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004032 }
4033
Vladimir Markob554b5a2015-11-06 12:57:55 +00004034 // Does this method load kind need the current method as an input?
4035 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4036 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4037 }
4038
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004039 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004040
Roland Levillain4c0eb422015-04-24 16:43:49 +01004041 protected:
4042 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
4043 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
4044 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
4045 HInstruction* input = input_record.GetInstruction();
4046 // `input` is the last input of a static invoke marked as having
4047 // an explicit clinit check. It must either be:
4048 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4049 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4050 DCHECK(input != nullptr);
4051 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
4052 }
4053 return input_record;
4054 }
4055
Vladimir Markoc53c0792015-11-19 15:48:33 +00004056 void InsertInputAt(size_t index, HInstruction* input);
4057 void RemoveInputAt(size_t index);
4058
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004059 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004060 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4061 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4062 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4063 static constexpr size_t kFieldClinitCheckRequirement =
4064 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4065 static constexpr size_t kFieldClinitCheckRequirementSize =
4066 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4067 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4068 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4069 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4070 "Too many packed fields.");
4071 using OptimizedInvokeTypeField =
4072 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4073 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4074 kFieldClinitCheckRequirement,
4075 kFieldClinitCheckRequirementSize>;
4076
Vladimir Marko58155012015-08-19 12:49:41 +00004077 // The target method may refer to different dex file or method index than the original
4078 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4079 // in derived class to increase visibility.
4080 MethodReference target_method_;
4081 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004082
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004083 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004084};
Vladimir Markof64242a2015-12-01 14:58:23 +00004085std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004086std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004087
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004088class HInvokeVirtual : public HInvoke {
4089 public:
4090 HInvokeVirtual(ArenaAllocator* arena,
4091 uint32_t number_of_arguments,
4092 Primitive::Type return_type,
4093 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004094 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004095 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004096 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004097 vtable_index_(vtable_index) {}
4098
Calin Juravle641547a2015-04-21 22:08:51 +01004099 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004100 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004101 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004102 }
4103
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004104 uint32_t GetVTableIndex() const { return vtable_index_; }
4105
4106 DECLARE_INSTRUCTION(InvokeVirtual);
4107
4108 private:
4109 const uint32_t vtable_index_;
4110
4111 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4112};
4113
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004114class HInvokeInterface : public HInvoke {
4115 public:
4116 HInvokeInterface(ArenaAllocator* arena,
4117 uint32_t number_of_arguments,
4118 Primitive::Type return_type,
4119 uint32_t dex_pc,
4120 uint32_t dex_method_index,
4121 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004122 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004123 imt_index_(imt_index) {}
4124
Calin Juravle641547a2015-04-21 22:08:51 +01004125 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004126 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004127 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004128 }
4129
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004130 uint32_t GetImtIndex() const { return imt_index_; }
4131 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4132
4133 DECLARE_INSTRUCTION(InvokeInterface);
4134
4135 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004136 const uint32_t imt_index_;
4137
4138 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4139};
4140
Roland Levillain88cb1752014-10-20 16:36:47 +01004141class HNeg : public HUnaryOperation {
4142 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004143 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4144 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01004145
Roland Levillain9867bc72015-08-05 10:21:34 +01004146 template <typename T> T Compute(T x) const { return -x; }
4147
4148 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004149 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004150 }
4151 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004152 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004153 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004154 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4155 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4156 }
4157 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4158 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4159 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004160
Roland Levillain88cb1752014-10-20 16:36:47 +01004161 DECLARE_INSTRUCTION(Neg);
4162
4163 private:
4164 DISALLOW_COPY_AND_ASSIGN(HNeg);
4165};
4166
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004167class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004168 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004169 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004170 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004171 uint32_t dex_pc,
4172 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004173 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004174 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004175 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004176 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004177 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004178 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004179 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004180 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004181 }
4182
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004183 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004184 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004185
4186 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004187 bool NeedsEnvironment() const OVERRIDE { return true; }
4188
Mingyao Yang0c365e62015-03-31 15:09:29 -07004189 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4190 bool CanThrow() const OVERRIDE { return true; }
4191
Calin Juravle10e244f2015-01-26 18:54:32 +00004192 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004193
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004194 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4195
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004196 DECLARE_INSTRUCTION(NewArray);
4197
4198 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004199 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004200 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004201 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004202
4203 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4204};
4205
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004206class HAdd : public HBinaryOperation {
4207 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004208 HAdd(Primitive::Type result_type,
4209 HInstruction* left,
4210 HInstruction* right,
4211 uint32_t dex_pc = kNoDexPc)
4212 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004213
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004214 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004215
Roland Levillain9867bc72015-08-05 10:21:34 +01004216 template <typename T> T Compute(T x, T y) const { return x + y; }
4217
4218 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004219 return GetBlock()->GetGraph()->GetIntConstant(
4220 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004221 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004222 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004223 return GetBlock()->GetGraph()->GetLongConstant(
4224 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004225 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004226 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4227 return GetBlock()->GetGraph()->GetFloatConstant(
4228 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4229 }
4230 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4231 return GetBlock()->GetGraph()->GetDoubleConstant(
4232 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4233 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004234
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004235 DECLARE_INSTRUCTION(Add);
4236
4237 private:
4238 DISALLOW_COPY_AND_ASSIGN(HAdd);
4239};
4240
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004241class HSub : public HBinaryOperation {
4242 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004243 HSub(Primitive::Type result_type,
4244 HInstruction* left,
4245 HInstruction* right,
4246 uint32_t dex_pc = kNoDexPc)
4247 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004248
Roland Levillain9867bc72015-08-05 10:21:34 +01004249 template <typename T> T Compute(T x, T y) const { return x - y; }
4250
4251 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004252 return GetBlock()->GetGraph()->GetIntConstant(
4253 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004254 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004255 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004256 return GetBlock()->GetGraph()->GetLongConstant(
4257 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004258 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004259 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4260 return GetBlock()->GetGraph()->GetFloatConstant(
4261 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4262 }
4263 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4264 return GetBlock()->GetGraph()->GetDoubleConstant(
4265 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4266 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004267
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004268 DECLARE_INSTRUCTION(Sub);
4269
4270 private:
4271 DISALLOW_COPY_AND_ASSIGN(HSub);
4272};
4273
Calin Juravle34bacdf2014-10-07 20:23:36 +01004274class HMul : public HBinaryOperation {
4275 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004276 HMul(Primitive::Type result_type,
4277 HInstruction* left,
4278 HInstruction* right,
4279 uint32_t dex_pc = kNoDexPc)
4280 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004281
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004282 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004283
Roland Levillain9867bc72015-08-05 10:21:34 +01004284 template <typename T> T Compute(T x, T y) const { return x * y; }
4285
4286 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004287 return GetBlock()->GetGraph()->GetIntConstant(
4288 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004289 }
4290 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004291 return GetBlock()->GetGraph()->GetLongConstant(
4292 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004293 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004294 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4295 return GetBlock()->GetGraph()->GetFloatConstant(
4296 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4297 }
4298 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4299 return GetBlock()->GetGraph()->GetDoubleConstant(
4300 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4301 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004302
4303 DECLARE_INSTRUCTION(Mul);
4304
4305 private:
4306 DISALLOW_COPY_AND_ASSIGN(HMul);
4307};
4308
Calin Juravle7c4954d2014-10-28 16:57:40 +00004309class HDiv : public HBinaryOperation {
4310 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004311 HDiv(Primitive::Type result_type,
4312 HInstruction* left,
4313 HInstruction* right,
4314 uint32_t dex_pc)
4315 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004316
Roland Levillain9867bc72015-08-05 10:21:34 +01004317 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004318 T ComputeIntegral(T x, T y) const {
4319 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004320 // Our graph structure ensures we never have 0 for `y` during
4321 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004322 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004323 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004324 return (y == -1) ? -x : x / y;
4325 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004326
Roland Levillain31dd3d62016-02-16 12:21:02 +00004327 template <typename T>
4328 T ComputeFP(T x, T y) const {
4329 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4330 return x / y;
4331 }
4332
Roland Levillain9867bc72015-08-05 10:21:34 +01004333 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004334 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004335 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004336 }
4337 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004338 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004339 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4340 }
4341 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4342 return GetBlock()->GetGraph()->GetFloatConstant(
4343 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4344 }
4345 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4346 return GetBlock()->GetGraph()->GetDoubleConstant(
4347 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004348 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004349
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004350 static SideEffects SideEffectsForArchRuntimeCalls() {
4351 // The generated code can use a runtime call.
4352 return SideEffects::CanTriggerGC();
4353 }
4354
Calin Juravle7c4954d2014-10-28 16:57:40 +00004355 DECLARE_INSTRUCTION(Div);
4356
4357 private:
4358 DISALLOW_COPY_AND_ASSIGN(HDiv);
4359};
4360
Calin Juravlebacfec32014-11-14 15:54:36 +00004361class HRem : public HBinaryOperation {
4362 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004363 HRem(Primitive::Type result_type,
4364 HInstruction* left,
4365 HInstruction* right,
4366 uint32_t dex_pc)
4367 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004368
Roland Levillain9867bc72015-08-05 10:21:34 +01004369 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004370 T ComputeIntegral(T x, T y) const {
4371 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004372 // Our graph structure ensures we never have 0 for `y` during
4373 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004374 DCHECK_NE(y, 0);
4375 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4376 return (y == -1) ? 0 : x % y;
4377 }
4378
Roland Levillain31dd3d62016-02-16 12:21:02 +00004379 template <typename T>
4380 T ComputeFP(T x, T y) const {
4381 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4382 return std::fmod(x, y);
4383 }
4384
Roland Levillain9867bc72015-08-05 10:21:34 +01004385 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004386 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004387 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004388 }
4389 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004390 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004391 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004392 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004393 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4394 return GetBlock()->GetGraph()->GetFloatConstant(
4395 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4396 }
4397 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4398 return GetBlock()->GetGraph()->GetDoubleConstant(
4399 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4400 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004401
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004402 static SideEffects SideEffectsForArchRuntimeCalls() {
4403 return SideEffects::CanTriggerGC();
4404 }
4405
Calin Juravlebacfec32014-11-14 15:54:36 +00004406 DECLARE_INSTRUCTION(Rem);
4407
4408 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004409 DISALLOW_COPY_AND_ASSIGN(HRem);
4410};
4411
Calin Juravled0d48522014-11-04 16:40:20 +00004412class HDivZeroCheck : public HExpression<1> {
4413 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004414 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4415 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004416 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004417 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004418 SetRawInputAt(0, value);
4419 }
4420
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004421 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4422
Calin Juravled0d48522014-11-04 16:40:20 +00004423 bool CanBeMoved() const OVERRIDE { return true; }
4424
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004425 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004426 return true;
4427 }
4428
4429 bool NeedsEnvironment() const OVERRIDE { return true; }
4430 bool CanThrow() const OVERRIDE { return true; }
4431
Calin Juravled0d48522014-11-04 16:40:20 +00004432 DECLARE_INSTRUCTION(DivZeroCheck);
4433
4434 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004435 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4436};
4437
Calin Juravle9aec02f2014-11-18 23:06:35 +00004438class HShl : public HBinaryOperation {
4439 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004440 HShl(Primitive::Type result_type,
4441 HInstruction* left,
4442 HInstruction* right,
4443 uint32_t dex_pc = kNoDexPc)
4444 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004445
Roland Levillain9867bc72015-08-05 10:21:34 +01004446 template <typename T, typename U, typename V>
4447 T Compute(T x, U y, V max_shift_value) const {
4448 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4449 "V is not the unsigned integer type corresponding to T");
4450 return x << (y & max_shift_value);
4451 }
4452
4453 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4454 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004455 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004456 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004457 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4458 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004459 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004460 }
4461 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4462 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004463 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004464 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004465 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4466 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4467 LOG(FATAL) << DebugName() << " is not defined for float values";
4468 UNREACHABLE();
4469 }
4470 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4471 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4472 LOG(FATAL) << DebugName() << " is not defined for double values";
4473 UNREACHABLE();
4474 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004475
4476 DECLARE_INSTRUCTION(Shl);
4477
4478 private:
4479 DISALLOW_COPY_AND_ASSIGN(HShl);
4480};
4481
4482class HShr : public HBinaryOperation {
4483 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004484 HShr(Primitive::Type result_type,
4485 HInstruction* left,
4486 HInstruction* right,
4487 uint32_t dex_pc = kNoDexPc)
4488 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004489
Roland Levillain9867bc72015-08-05 10:21:34 +01004490 template <typename T, typename U, typename V>
4491 T Compute(T x, U y, V max_shift_value) const {
4492 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4493 "V is not the unsigned integer type corresponding to T");
4494 return x >> (y & max_shift_value);
4495 }
4496
4497 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4498 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004499 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004500 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004501 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4502 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004503 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004504 }
4505 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4506 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004507 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004508 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004509 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4510 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4511 LOG(FATAL) << DebugName() << " is not defined for float values";
4512 UNREACHABLE();
4513 }
4514 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4515 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4516 LOG(FATAL) << DebugName() << " is not defined for double values";
4517 UNREACHABLE();
4518 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004519
4520 DECLARE_INSTRUCTION(Shr);
4521
4522 private:
4523 DISALLOW_COPY_AND_ASSIGN(HShr);
4524};
4525
4526class HUShr : public HBinaryOperation {
4527 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004528 HUShr(Primitive::Type result_type,
4529 HInstruction* left,
4530 HInstruction* right,
4531 uint32_t dex_pc = kNoDexPc)
4532 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004533
Roland Levillain9867bc72015-08-05 10:21:34 +01004534 template <typename T, typename U, typename V>
4535 T Compute(T x, U y, V max_shift_value) const {
4536 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4537 "V is not the unsigned integer type corresponding to T");
4538 V ux = static_cast<V>(x);
4539 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004540 }
4541
Roland Levillain9867bc72015-08-05 10:21:34 +01004542 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4543 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004544 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004545 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004546 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4547 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004548 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004549 }
4550 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4551 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004552 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004553 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004554 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4555 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4556 LOG(FATAL) << DebugName() << " is not defined for float values";
4557 UNREACHABLE();
4558 }
4559 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4560 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4561 LOG(FATAL) << DebugName() << " is not defined for double values";
4562 UNREACHABLE();
4563 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004564
4565 DECLARE_INSTRUCTION(UShr);
4566
4567 private:
4568 DISALLOW_COPY_AND_ASSIGN(HUShr);
4569};
4570
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004571class HAnd : public HBinaryOperation {
4572 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004573 HAnd(Primitive::Type result_type,
4574 HInstruction* left,
4575 HInstruction* right,
4576 uint32_t dex_pc = kNoDexPc)
4577 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004578
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004579 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004580
Roland Levillaine53bd812016-02-24 14:54:18 +00004581 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004582
4583 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004584 return GetBlock()->GetGraph()->GetIntConstant(
4585 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004586 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004587 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004588 return GetBlock()->GetGraph()->GetLongConstant(
4589 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004590 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004591 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4592 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4593 LOG(FATAL) << DebugName() << " is not defined for float values";
4594 UNREACHABLE();
4595 }
4596 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4597 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4598 LOG(FATAL) << DebugName() << " is not defined for double values";
4599 UNREACHABLE();
4600 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004601
4602 DECLARE_INSTRUCTION(And);
4603
4604 private:
4605 DISALLOW_COPY_AND_ASSIGN(HAnd);
4606};
4607
4608class HOr : public HBinaryOperation {
4609 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004610 HOr(Primitive::Type result_type,
4611 HInstruction* left,
4612 HInstruction* right,
4613 uint32_t dex_pc = kNoDexPc)
4614 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004615
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004616 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004617
Roland Levillaine53bd812016-02-24 14:54:18 +00004618 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004619
4620 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004621 return GetBlock()->GetGraph()->GetIntConstant(
4622 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004623 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004624 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004625 return GetBlock()->GetGraph()->GetLongConstant(
4626 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004627 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004628 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4629 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4630 LOG(FATAL) << DebugName() << " is not defined for float values";
4631 UNREACHABLE();
4632 }
4633 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4634 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4635 LOG(FATAL) << DebugName() << " is not defined for double values";
4636 UNREACHABLE();
4637 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004638
4639 DECLARE_INSTRUCTION(Or);
4640
4641 private:
4642 DISALLOW_COPY_AND_ASSIGN(HOr);
4643};
4644
4645class HXor : public HBinaryOperation {
4646 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004647 HXor(Primitive::Type result_type,
4648 HInstruction* left,
4649 HInstruction* right,
4650 uint32_t dex_pc = kNoDexPc)
4651 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004652
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004653 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004654
Roland Levillaine53bd812016-02-24 14:54:18 +00004655 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004656
4657 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004658 return GetBlock()->GetGraph()->GetIntConstant(
4659 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004660 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004661 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004662 return GetBlock()->GetGraph()->GetLongConstant(
4663 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004664 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004665 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4666 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4667 LOG(FATAL) << DebugName() << " is not defined for float values";
4668 UNREACHABLE();
4669 }
4670 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4671 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4672 LOG(FATAL) << DebugName() << " is not defined for double values";
4673 UNREACHABLE();
4674 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004675
4676 DECLARE_INSTRUCTION(Xor);
4677
4678 private:
4679 DISALLOW_COPY_AND_ASSIGN(HXor);
4680};
4681
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004682class HRor : public HBinaryOperation {
4683 public:
4684 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4685 : HBinaryOperation(result_type, value, distance) {}
4686
4687 template <typename T, typename U, typename V>
4688 T Compute(T x, U y, V max_shift_value) const {
4689 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4690 "V is not the unsigned integer type corresponding to T");
4691 V ux = static_cast<V>(x);
4692 if ((y & max_shift_value) == 0) {
4693 return static_cast<T>(ux);
4694 } else {
4695 const V reg_bits = sizeof(T) * 8;
4696 return static_cast<T>(ux >> (y & max_shift_value)) |
4697 (x << (reg_bits - (y & max_shift_value)));
4698 }
4699 }
4700
4701 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4702 return GetBlock()->GetGraph()->GetIntConstant(
4703 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4704 }
4705 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4706 return GetBlock()->GetGraph()->GetLongConstant(
4707 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4708 }
4709 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4710 return GetBlock()->GetGraph()->GetLongConstant(
4711 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4712 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004713 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4714 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4715 LOG(FATAL) << DebugName() << " is not defined for float values";
4716 UNREACHABLE();
4717 }
4718 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4719 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4720 LOG(FATAL) << DebugName() << " is not defined for double values";
4721 UNREACHABLE();
4722 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004723
4724 DECLARE_INSTRUCTION(Ror);
4725
4726 private:
4727 DISALLOW_COPY_AND_ASSIGN(HRor);
4728};
4729
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004730// The value of a parameter in this method. Its location depends on
4731// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004732class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004733 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004734 HParameterValue(const DexFile& dex_file,
4735 uint16_t type_index,
4736 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004737 Primitive::Type parameter_type,
4738 bool is_this = false)
4739 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004740 dex_file_(dex_file),
4741 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004742 index_(index) {
4743 SetPackedFlag<kFlagIsThis>(is_this);
4744 SetPackedFlag<kFlagCanBeNull>(!is_this);
4745 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004746
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004747 const DexFile& GetDexFile() const { return dex_file_; }
4748 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004749 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004750 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004751
Vladimir Markoa1de9182016-02-25 11:37:38 +00004752 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4753 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004754
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004755 DECLARE_INSTRUCTION(ParameterValue);
4756
4757 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004758 // Whether or not the parameter value corresponds to 'this' argument.
4759 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4760 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4761 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4762 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4763 "Too many packed fields.");
4764
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004765 const DexFile& dex_file_;
4766 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004767 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004768 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004769 const uint8_t index_;
4770
4771 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4772};
4773
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004774class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004775 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004776 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4777 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004778
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004779 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004780 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004781 return true;
4782 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004783
Roland Levillain9867bc72015-08-05 10:21:34 +01004784 template <typename T> T Compute(T x) const { return ~x; }
4785
4786 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004787 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004788 }
4789 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004790 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004791 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004792 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4793 LOG(FATAL) << DebugName() << " is not defined for float values";
4794 UNREACHABLE();
4795 }
4796 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4797 LOG(FATAL) << DebugName() << " is not defined for double values";
4798 UNREACHABLE();
4799 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004800
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004801 DECLARE_INSTRUCTION(Not);
4802
4803 private:
4804 DISALLOW_COPY_AND_ASSIGN(HNot);
4805};
4806
David Brazdil66d126e2015-04-03 16:02:44 +01004807class HBooleanNot : public HUnaryOperation {
4808 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004809 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4810 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004811
4812 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004813 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004814 return true;
4815 }
4816
Roland Levillain9867bc72015-08-05 10:21:34 +01004817 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004818 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004819 return !x;
4820 }
4821
Roland Levillain9867bc72015-08-05 10:21:34 +01004822 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004823 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004824 }
4825 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4826 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004827 UNREACHABLE();
4828 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004829 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4830 LOG(FATAL) << DebugName() << " is not defined for float values";
4831 UNREACHABLE();
4832 }
4833 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4834 LOG(FATAL) << DebugName() << " is not defined for double values";
4835 UNREACHABLE();
4836 }
David Brazdil66d126e2015-04-03 16:02:44 +01004837
4838 DECLARE_INSTRUCTION(BooleanNot);
4839
4840 private:
4841 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4842};
4843
Roland Levillaindff1f282014-11-05 14:15:05 +00004844class HTypeConversion : public HExpression<1> {
4845 public:
4846 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004847 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004848 : HExpression(result_type,
4849 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4850 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004851 SetRawInputAt(0, input);
4852 DCHECK_NE(input->GetType(), result_type);
4853 }
4854
4855 HInstruction* GetInput() const { return InputAt(0); }
4856 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4857 Primitive::Type GetResultType() const { return GetType(); }
4858
4859 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004860 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004861
Mark Mendelle82549b2015-05-06 10:55:34 -04004862 // Try to statically evaluate the conversion and return a HConstant
4863 // containing the result. If the input cannot be converted, return nullptr.
4864 HConstant* TryStaticEvaluation() const;
4865
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004866 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4867 Primitive::Type result_type) {
4868 // Some architectures may not require the 'GC' side effects, but at this point
4869 // in the compilation process we do not know what architecture we will
4870 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004871 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4872 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004873 return SideEffects::CanTriggerGC();
4874 }
4875 return SideEffects::None();
4876 }
4877
Roland Levillaindff1f282014-11-05 14:15:05 +00004878 DECLARE_INSTRUCTION(TypeConversion);
4879
4880 private:
4881 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4882};
4883
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004884static constexpr uint32_t kNoRegNumber = -1;
4885
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004886class HPhi : public HInstruction {
4887 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004888 HPhi(ArenaAllocator* arena,
4889 uint32_t reg_number,
4890 size_t number_of_inputs,
4891 Primitive::Type type,
4892 uint32_t dex_pc = kNoDexPc)
4893 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004894 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004895 reg_number_(reg_number) {
4896 SetPackedField<TypeField>(ToPhiType(type));
4897 DCHECK_NE(GetType(), Primitive::kPrimVoid);
4898 // Phis are constructed live and marked dead if conflicting or unused.
4899 // Individual steps of SsaBuilder should assume that if a phi has been
4900 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4901 SetPackedFlag<kFlagIsLive>(true);
4902 SetPackedFlag<kFlagCanBeNull>(true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004903 }
4904
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004905 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4906 static Primitive::Type ToPhiType(Primitive::Type type) {
4907 switch (type) {
4908 case Primitive::kPrimBoolean:
4909 case Primitive::kPrimByte:
4910 case Primitive::kPrimShort:
4911 case Primitive::kPrimChar:
4912 return Primitive::kPrimInt;
4913 default:
4914 return type;
4915 }
4916 }
4917
David Brazdilffee3d32015-07-06 11:48:53 +01004918 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4919
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004920 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004921
4922 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004923 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004924
Vladimir Markoa1de9182016-02-25 11:37:38 +00004925 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
David Brazdil4833f5a2015-12-16 10:37:39 +00004926 void SetType(Primitive::Type new_type) {
4927 // Make sure that only valid type changes occur. The following are allowed:
4928 // (1) int -> float/ref (primitive type propagation),
4929 // (2) long -> double (primitive type propagation).
Vladimir Markoa1de9182016-02-25 11:37:38 +00004930 DCHECK(GetType() == new_type ||
4931 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4932 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4933 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4934 SetPackedField<TypeField>(new_type);
David Brazdil4833f5a2015-12-16 10:37:39 +00004935 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004936
Vladimir Markoa1de9182016-02-25 11:37:38 +00004937 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4938 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004939
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004940 uint32_t GetRegNumber() const { return reg_number_; }
4941
Vladimir Markoa1de9182016-02-25 11:37:38 +00004942 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
4943 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
4944 bool IsDead() const { return !IsLive(); }
4945 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004946
David Brazdil77a48ae2015-09-15 12:34:04 +00004947 bool IsVRegEquivalentOf(HInstruction* other) const {
4948 return other != nullptr
4949 && other->IsPhi()
4950 && other->AsPhi()->GetBlock() == GetBlock()
4951 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4952 }
4953
Calin Juravlea4f88312015-04-16 12:57:19 +01004954 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4955 // An equivalent phi is a phi having the same dex register and type.
4956 // It assumes that phis with the same dex register are adjacent.
4957 HPhi* GetNextEquivalentPhiWithSameType() {
4958 HInstruction* next = GetNext();
4959 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4960 if (next->GetType() == GetType()) {
4961 return next->AsPhi();
4962 }
4963 next = next->GetNext();
4964 }
4965 return nullptr;
4966 }
4967
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004968 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004969
David Brazdil1abb4192015-02-17 18:33:36 +00004970 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004971 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004972 return inputs_[index];
4973 }
David Brazdil1abb4192015-02-17 18:33:36 +00004974
4975 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004976 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004977 }
4978
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004979 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004980 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
4981 static constexpr size_t kFieldTypeSize =
4982 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4983 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
4984 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
4985 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
4986 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4987 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
4988
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004989 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004990 const uint32_t reg_number_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004991
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004992 DISALLOW_COPY_AND_ASSIGN(HPhi);
4993};
4994
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004995class HNullCheck : public HExpression<1> {
4996 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004997 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4998 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004999 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005000 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005001 SetRawInputAt(0, value);
5002 }
5003
Calin Juravle10e244f2015-01-26 18:54:32 +00005004 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005005 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005006 return true;
5007 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005008
Calin Juravle10e244f2015-01-26 18:54:32 +00005009 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005010
Calin Juravle10e244f2015-01-26 18:54:32 +00005011 bool CanThrow() const OVERRIDE { return true; }
5012
5013 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005014
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005015
5016 DECLARE_INSTRUCTION(NullCheck);
5017
5018 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005019 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5020};
5021
5022class FieldInfo : public ValueObject {
5023 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005024 FieldInfo(MemberOffset field_offset,
5025 Primitive::Type field_type,
5026 bool is_volatile,
5027 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005028 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005029 const DexFile& dex_file,
5030 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005031 : field_offset_(field_offset),
5032 field_type_(field_type),
5033 is_volatile_(is_volatile),
5034 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005035 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005036 dex_file_(dex_file),
5037 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005038
5039 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005040 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005041 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005042 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005043 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005044 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005045 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005046
5047 private:
5048 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005049 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005050 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005051 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005052 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005053 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005054 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005055};
5056
5057class HInstanceFieldGet : public HExpression<1> {
5058 public:
5059 HInstanceFieldGet(HInstruction* value,
5060 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005061 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005062 bool is_volatile,
5063 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005064 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005065 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005066 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005067 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005068 : HExpression(field_type,
5069 SideEffects::FieldReadOfType(field_type, is_volatile),
5070 dex_pc),
5071 field_info_(field_offset,
5072 field_type,
5073 is_volatile,
5074 field_idx,
5075 declaring_class_def_index,
5076 dex_file,
5077 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005078 SetRawInputAt(0, value);
5079 }
5080
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005081 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005082
5083 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5084 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
5085 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005086 }
5087
Calin Juravle641547a2015-04-21 22:08:51 +01005088 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5089 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005090 }
5091
5092 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005093 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5094 }
5095
Calin Juravle52c48962014-12-16 17:02:57 +00005096 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005097 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005098 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005099 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005100
5101 DECLARE_INSTRUCTION(InstanceFieldGet);
5102
5103 private:
5104 const FieldInfo field_info_;
5105
5106 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5107};
5108
5109class HInstanceFieldSet : public HTemplateInstruction<2> {
5110 public:
5111 HInstanceFieldSet(HInstruction* object,
5112 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005113 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005114 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005115 bool is_volatile,
5116 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005117 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005118 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005119 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005120 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005121 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5122 dex_pc),
5123 field_info_(field_offset,
5124 field_type,
5125 is_volatile,
5126 field_idx,
5127 declaring_class_def_index,
5128 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005129 dex_cache) {
5130 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005131 SetRawInputAt(0, object);
5132 SetRawInputAt(1, value);
5133 }
5134
Calin Juravle641547a2015-04-21 22:08:51 +01005135 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5136 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005137 }
5138
Calin Juravle52c48962014-12-16 17:02:57 +00005139 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005140 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005141 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005142 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005143 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005144 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5145 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005146
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005147 DECLARE_INSTRUCTION(InstanceFieldSet);
5148
5149 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005150 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5151 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5152 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5153 "Too many packed fields.");
5154
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005155 const FieldInfo field_info_;
5156
5157 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5158};
5159
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005160class HArrayGet : public HExpression<2> {
5161 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005162 HArrayGet(HInstruction* array,
5163 HInstruction* index,
5164 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005165 uint32_t dex_pc,
5166 SideEffects additional_side_effects = SideEffects::None())
5167 : HExpression(type,
5168 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00005169 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005170 SetRawInputAt(0, array);
5171 SetRawInputAt(1, index);
5172 }
5173
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005174 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005175 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005176 return true;
5177 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005178 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005179 // TODO: We can be smarter here.
5180 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5181 // which generates the implicit null check. There are cases when these can be removed
5182 // to produce better code. If we ever add optimizations to do so we should allow an
5183 // implicit check here (as long as the address falls in the first page).
5184 return false;
5185 }
5186
David Brazdil4833f5a2015-12-16 10:37:39 +00005187 bool IsEquivalentOf(HArrayGet* other) const {
5188 bool result = (GetDexPc() == other->GetDexPc());
5189 if (kIsDebugBuild && result) {
5190 DCHECK_EQ(GetBlock(), other->GetBlock());
5191 DCHECK_EQ(GetArray(), other->GetArray());
5192 DCHECK_EQ(GetIndex(), other->GetIndex());
5193 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005194 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005195 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005196 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5197 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005198 }
5199 }
5200 return result;
5201 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005202
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005203 HInstruction* GetArray() const { return InputAt(0); }
5204 HInstruction* GetIndex() const { return InputAt(1); }
5205
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005206 DECLARE_INSTRUCTION(ArrayGet);
5207
5208 private:
5209 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5210};
5211
5212class HArraySet : public HTemplateInstruction<3> {
5213 public:
5214 HArraySet(HInstruction* array,
5215 HInstruction* index,
5216 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005217 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005218 uint32_t dex_pc,
5219 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005220 : HTemplateInstruction(
5221 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005222 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
5223 additional_side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005224 dex_pc) {
5225 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5226 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5227 SetPackedFlag<kFlagValueCanBeNull>(true);
5228 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005229 SetRawInputAt(0, array);
5230 SetRawInputAt(1, index);
5231 SetRawInputAt(2, value);
5232 }
5233
Calin Juravle77520bc2015-01-12 18:45:46 +00005234 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005235 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005236 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005237 }
5238
Mingyao Yang81014cb2015-06-02 03:16:27 -07005239 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005240 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005241
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005242 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005243 // TODO: Same as for ArrayGet.
5244 return false;
5245 }
5246
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005247 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005248 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005249 }
5250
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005251 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005252 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005253 }
5254
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005255 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005256 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005257 }
5258
Vladimir Markoa1de9182016-02-25 11:37:38 +00005259 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5260 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5261 bool StaticTypeOfArrayIsObjectArray() const {
5262 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5263 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005264
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005265 HInstruction* GetArray() const { return InputAt(0); }
5266 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005267 HInstruction* GetValue() const { return InputAt(2); }
5268
5269 Primitive::Type GetComponentType() const {
5270 // The Dex format does not type floating point index operations. Since the
5271 // `expected_component_type_` is set during building and can therefore not
5272 // be correct, we also check what is the value type. If it is a floating
5273 // point type, we must use that type.
5274 Primitive::Type value_type = GetValue()->GetType();
5275 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5276 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005277 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005278 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005279
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005280 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005281 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005282 }
5283
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005284 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5285 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5286 }
5287
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005288 DECLARE_INSTRUCTION(ArraySet);
5289
5290 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005291 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5292 static constexpr size_t kFieldExpectedComponentTypeSize =
5293 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5294 static constexpr size_t kFlagNeedsTypeCheck =
5295 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5296 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005297 // Cached information for the reference_type_info_ so that codegen
5298 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005299 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5300 static constexpr size_t kNumberOfArraySetPackedBits =
5301 kFlagStaticTypeOfArrayIsObjectArray + 1;
5302 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5303 using ExpectedComponentTypeField =
5304 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005305
5306 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5307};
5308
5309class HArrayLength : public HExpression<1> {
5310 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005311 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005312 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005313 // Note that arrays do not change length, so the instruction does not
5314 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005315 SetRawInputAt(0, array);
5316 }
5317
Calin Juravle77520bc2015-01-12 18:45:46 +00005318 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005319 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005320 return true;
5321 }
Calin Juravle641547a2015-04-21 22:08:51 +01005322 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5323 return obj == InputAt(0);
5324 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005325
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005326 DECLARE_INSTRUCTION(ArrayLength);
5327
5328 private:
5329 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5330};
5331
5332class HBoundsCheck : public HExpression<2> {
5333 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005334 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5335 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005336 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005337 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005338 DCHECK(index->GetType() == Primitive::kPrimInt);
5339 SetRawInputAt(0, index);
5340 SetRawInputAt(1, length);
5341 }
5342
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005343 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005344 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005345 return true;
5346 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005347
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005348 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005349
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005350 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005351
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005352 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005353
5354 DECLARE_INSTRUCTION(BoundsCheck);
5355
5356 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005357 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5358};
5359
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005360class HSuspendCheck : public HTemplateInstruction<0> {
5361 public:
5362 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005363 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005364
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005365 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005366 return true;
5367 }
5368
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005369 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5370 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005371
5372 DECLARE_INSTRUCTION(SuspendCheck);
5373
5374 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005375 // Only used for code generation, in order to share the same slow path between back edges
5376 // of a same loop.
5377 SlowPathCode* slow_path_;
5378
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005379 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5380};
5381
David Srbecky0cf44932015-12-09 14:09:59 +00005382// Pseudo-instruction which provides the native debugger with mapping information.
5383// It ensures that we can generate line number and local variables at this point.
5384class HNativeDebugInfo : public HTemplateInstruction<0> {
5385 public:
5386 explicit HNativeDebugInfo(uint32_t dex_pc)
5387 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5388
5389 bool NeedsEnvironment() const OVERRIDE {
5390 return true;
5391 }
5392
5393 DECLARE_INSTRUCTION(NativeDebugInfo);
5394
5395 private:
5396 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5397};
5398
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005399/**
5400 * Instruction to load a Class object.
5401 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005402class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005403 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005404 HLoadClass(HCurrentMethod* current_method,
5405 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005406 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005407 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005408 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005409 bool needs_access_check,
5410 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005411 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005412 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005413 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005414 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005415 // Referrers class should not need access check. We never inline unverified
5416 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005417 DCHECK(!is_referrers_class || !needs_access_check);
5418
5419 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5420 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5421 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5422 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005423 SetRawInputAt(0, current_method);
5424 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005425
5426 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005427
5428 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005429 // Note that we don't need to test for generate_clinit_check_.
5430 // Whether or not we need to generate the clinit check is processed in
5431 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005432 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005433 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005434 }
5435
5436 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5437
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005438 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005439 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005440
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005441 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005442 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005443 }
5444
Calin Juravle0ba218d2015-05-19 18:46:01 +01005445 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005446 // The entrypoint the code generator is going to call does not do
5447 // clinit of the class.
5448 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005449 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005450 }
5451
5452 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005453 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005454 (!IsReferrersClass() && !IsInDexCache()) ||
5455 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005456 }
5457
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005458
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005459 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005460 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005461 }
5462
Calin Juravleacf735c2015-02-12 15:25:22 +00005463 ReferenceTypeInfo GetLoadedClassRTI() {
5464 return loaded_class_rti_;
5465 }
5466
5467 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5468 // Make sure we only set exact types (the loaded class should never be merged).
5469 DCHECK(rti.IsExact());
5470 loaded_class_rti_ = rti;
5471 }
5472
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005473 const DexFile& GetDexFile() { return dex_file_; }
5474
Vladimir Markoa1de9182016-02-25 11:37:38 +00005475 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005476
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005477 static SideEffects SideEffectsForArchRuntimeCalls() {
5478 return SideEffects::CanTriggerGC();
5479 }
5480
Vladimir Markoa1de9182016-02-25 11:37:38 +00005481 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5482 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5483 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5484 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005485
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005486 DECLARE_INSTRUCTION(LoadClass);
5487
5488 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005489 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5490 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5491 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005492 // Whether this instruction must generate the initialization check.
5493 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005494 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5495 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5496 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5497
5498 const uint16_t type_index_;
5499 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005500
Calin Juravleacf735c2015-02-12 15:25:22 +00005501 ReferenceTypeInfo loaded_class_rti_;
5502
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005503 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5504};
5505
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005506class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005507 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005508 HLoadString(HCurrentMethod* current_method,
5509 uint32_t string_index,
5510 uint32_t dex_pc,
5511 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005512 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005513 string_index_(string_index) {
5514 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005515 SetRawInputAt(0, current_method);
5516 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005517
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005518 bool CanBeMoved() const OVERRIDE { return true; }
5519
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005520 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5521 return other->AsLoadString()->string_index_ == string_index_;
5522 }
5523
5524 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5525
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005526 uint32_t GetStringIndex() const { return string_index_; }
5527
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005528 // Will call the runtime if the string is not already in the dex cache.
5529 bool NeedsEnvironment() const OVERRIDE { return !IsInDexCache(); }
5530
Vladimir Markodc151b22015-10-15 18:02:30 +01005531 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005532 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005533 bool CanThrow() const OVERRIDE { return !IsInDexCache(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005534
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005535 static SideEffects SideEffectsForArchRuntimeCalls() {
5536 return SideEffects::CanTriggerGC();
5537 }
5538
Vladimir Markoa1de9182016-02-25 11:37:38 +00005539 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5540
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005541 DECLARE_INSTRUCTION(LoadString);
5542
5543 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005544 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
5545 static constexpr size_t kNumberOfLoadStringPackedBits = kFlagIsInDexCache + 1;
5546 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5547
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005548 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005549
5550 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5551};
5552
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005553/**
5554 * Performs an initialization check on its Class object input.
5555 */
5556class HClinitCheck : public HExpression<1> {
5557 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005558 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005559 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005560 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005561 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5562 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005563 SetRawInputAt(0, constant);
5564 }
5565
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005566 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005567 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005568 return true;
5569 }
5570
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005571 bool NeedsEnvironment() const OVERRIDE {
5572 // May call runtime to initialize the class.
5573 return true;
5574 }
5575
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005576 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005577
5578 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5579
5580 DECLARE_INSTRUCTION(ClinitCheck);
5581
5582 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005583 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5584};
5585
5586class HStaticFieldGet : public HExpression<1> {
5587 public:
5588 HStaticFieldGet(HInstruction* cls,
5589 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005590 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005591 bool is_volatile,
5592 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005593 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005594 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005595 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005596 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005597 : HExpression(field_type,
5598 SideEffects::FieldReadOfType(field_type, is_volatile),
5599 dex_pc),
5600 field_info_(field_offset,
5601 field_type,
5602 is_volatile,
5603 field_idx,
5604 declaring_class_def_index,
5605 dex_file,
5606 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005607 SetRawInputAt(0, cls);
5608 }
5609
Calin Juravle52c48962014-12-16 17:02:57 +00005610
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005611 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005612
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005613 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005614 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5615 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005616 }
5617
5618 size_t ComputeHashCode() const OVERRIDE {
5619 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5620 }
5621
Calin Juravle52c48962014-12-16 17:02:57 +00005622 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005623 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5624 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005625 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005626
5627 DECLARE_INSTRUCTION(StaticFieldGet);
5628
5629 private:
5630 const FieldInfo field_info_;
5631
5632 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5633};
5634
5635class HStaticFieldSet : public HTemplateInstruction<2> {
5636 public:
5637 HStaticFieldSet(HInstruction* cls,
5638 HInstruction* value,
5639 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005640 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005641 bool is_volatile,
5642 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005643 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005644 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005645 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005646 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005647 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5648 dex_pc),
5649 field_info_(field_offset,
5650 field_type,
5651 is_volatile,
5652 field_idx,
5653 declaring_class_def_index,
5654 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005655 dex_cache) {
5656 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005657 SetRawInputAt(0, cls);
5658 SetRawInputAt(1, value);
5659 }
5660
Calin Juravle52c48962014-12-16 17:02:57 +00005661 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005662 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5663 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005664 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005665
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005666 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005667 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5668 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005669
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005670 DECLARE_INSTRUCTION(StaticFieldSet);
5671
5672 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005673 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5674 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5675 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5676 "Too many packed fields.");
5677
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005678 const FieldInfo field_info_;
5679
5680 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5681};
5682
Calin Juravlee460d1d2015-09-29 04:52:17 +01005683class HUnresolvedInstanceFieldGet : public HExpression<1> {
5684 public:
5685 HUnresolvedInstanceFieldGet(HInstruction* obj,
5686 Primitive::Type field_type,
5687 uint32_t field_index,
5688 uint32_t dex_pc)
5689 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5690 field_index_(field_index) {
5691 SetRawInputAt(0, obj);
5692 }
5693
5694 bool NeedsEnvironment() const OVERRIDE { return true; }
5695 bool CanThrow() const OVERRIDE { return true; }
5696
5697 Primitive::Type GetFieldType() const { return GetType(); }
5698 uint32_t GetFieldIndex() const { return field_index_; }
5699
5700 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5701
5702 private:
5703 const uint32_t field_index_;
5704
5705 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5706};
5707
5708class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5709 public:
5710 HUnresolvedInstanceFieldSet(HInstruction* obj,
5711 HInstruction* value,
5712 Primitive::Type field_type,
5713 uint32_t field_index,
5714 uint32_t dex_pc)
5715 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005716 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005717 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005718 DCHECK_EQ(field_type, value->GetType());
5719 SetRawInputAt(0, obj);
5720 SetRawInputAt(1, value);
5721 }
5722
5723 bool NeedsEnvironment() const OVERRIDE { return true; }
5724 bool CanThrow() const OVERRIDE { return true; }
5725
Vladimir Markoa1de9182016-02-25 11:37:38 +00005726 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005727 uint32_t GetFieldIndex() const { return field_index_; }
5728
5729 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5730
5731 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005732 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5733 static constexpr size_t kFieldFieldTypeSize =
5734 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5735 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5736 kFieldFieldType + kFieldFieldTypeSize;
5737 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5738 "Too many packed fields.");
5739 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5740
Calin Juravlee460d1d2015-09-29 04:52:17 +01005741 const uint32_t field_index_;
5742
5743 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5744};
5745
5746class HUnresolvedStaticFieldGet : public HExpression<0> {
5747 public:
5748 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5749 uint32_t field_index,
5750 uint32_t dex_pc)
5751 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5752 field_index_(field_index) {
5753 }
5754
5755 bool NeedsEnvironment() const OVERRIDE { return true; }
5756 bool CanThrow() const OVERRIDE { return true; }
5757
5758 Primitive::Type GetFieldType() const { return GetType(); }
5759 uint32_t GetFieldIndex() const { return field_index_; }
5760
5761 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5762
5763 private:
5764 const uint32_t field_index_;
5765
5766 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5767};
5768
5769class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5770 public:
5771 HUnresolvedStaticFieldSet(HInstruction* value,
5772 Primitive::Type field_type,
5773 uint32_t field_index,
5774 uint32_t dex_pc)
5775 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005776 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005777 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005778 DCHECK_EQ(field_type, value->GetType());
5779 SetRawInputAt(0, value);
5780 }
5781
5782 bool NeedsEnvironment() const OVERRIDE { return true; }
5783 bool CanThrow() const OVERRIDE { return true; }
5784
Vladimir Markoa1de9182016-02-25 11:37:38 +00005785 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005786 uint32_t GetFieldIndex() const { return field_index_; }
5787
5788 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5789
5790 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005791 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5792 static constexpr size_t kFieldFieldTypeSize =
5793 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5794 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5795 kFieldFieldType + kFieldFieldTypeSize;
5796 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5797 "Too many packed fields.");
5798 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5799
Calin Juravlee460d1d2015-09-29 04:52:17 +01005800 const uint32_t field_index_;
5801
5802 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5803};
5804
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005805// Implement the move-exception DEX instruction.
5806class HLoadException : public HExpression<0> {
5807 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005808 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5809 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005810
David Brazdilbbd733e2015-08-18 17:48:17 +01005811 bool CanBeNull() const OVERRIDE { return false; }
5812
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005813 DECLARE_INSTRUCTION(LoadException);
5814
5815 private:
5816 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5817};
5818
David Brazdilcb1c0552015-08-04 16:22:25 +01005819// Implicit part of move-exception which clears thread-local exception storage.
5820// Must not be removed because the runtime expects the TLS to get cleared.
5821class HClearException : public HTemplateInstruction<0> {
5822 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005823 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5824 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005825
5826 DECLARE_INSTRUCTION(ClearException);
5827
5828 private:
5829 DISALLOW_COPY_AND_ASSIGN(HClearException);
5830};
5831
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005832class HThrow : public HTemplateInstruction<1> {
5833 public:
5834 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005835 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005836 SetRawInputAt(0, exception);
5837 }
5838
5839 bool IsControlFlow() const OVERRIDE { return true; }
5840
5841 bool NeedsEnvironment() const OVERRIDE { return true; }
5842
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005843 bool CanThrow() const OVERRIDE { return true; }
5844
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005845
5846 DECLARE_INSTRUCTION(Throw);
5847
5848 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005849 DISALLOW_COPY_AND_ASSIGN(HThrow);
5850};
5851
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005852/**
5853 * Implementation strategies for the code generator of a HInstanceOf
5854 * or `HCheckCast`.
5855 */
5856enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005857 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005858 kExactCheck, // Can do a single class compare.
5859 kClassHierarchyCheck, // Can just walk the super class chain.
5860 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5861 kInterfaceCheck, // No optimization yet when checking against an interface.
5862 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005863 kArrayCheck, // No optimization yet when checking against a generic array.
5864 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005865};
5866
Roland Levillain86503782016-02-11 19:07:30 +00005867std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5868
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005869class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005870 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005871 HInstanceOf(HInstruction* object,
5872 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005873 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005874 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005875 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005876 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005877 dex_pc) {
5878 SetPackedField<TypeCheckKindField>(check_kind);
5879 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005880 SetRawInputAt(0, object);
5881 SetRawInputAt(1, constant);
5882 }
5883
5884 bool CanBeMoved() const OVERRIDE { return true; }
5885
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005886 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005887 return true;
5888 }
5889
5890 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005891 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005892 }
5893
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005894 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005895 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5896 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5897 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5898 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005899
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005900 static bool CanCallRuntime(TypeCheckKind check_kind) {
5901 // Mips currently does runtime calls for any other checks.
5902 return check_kind != TypeCheckKind::kExactCheck;
5903 }
5904
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005905 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005906 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005907 }
5908
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005909 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005910
5911 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005912 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
5913 static constexpr size_t kFieldTypeCheckKindSize =
5914 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
5915 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
5916 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
5917 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5918 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005919
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005920 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5921};
5922
Calin Juravleb1498f62015-02-16 13:13:29 +00005923class HBoundType : public HExpression<1> {
5924 public:
David Brazdilf5552582015-12-27 13:36:12 +00005925 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005926 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005927 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
5928 SetPackedFlag<kFlagUpperCanBeNull>(true);
5929 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00005930 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005931 SetRawInputAt(0, input);
5932 }
5933
David Brazdilf5552582015-12-27 13:36:12 +00005934 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005935 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005936 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00005937 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005938
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005939 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005940 DCHECK(GetUpperCanBeNull() || !can_be_null);
5941 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005942 }
5943
Vladimir Markoa1de9182016-02-25 11:37:38 +00005944 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005945
Calin Juravleb1498f62015-02-16 13:13:29 +00005946 DECLARE_INSTRUCTION(BoundType);
5947
5948 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005949 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5950 // is false then CanBeNull() cannot be true).
5951 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
5952 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
5953 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
5954 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5955
Calin Juravleb1498f62015-02-16 13:13:29 +00005956 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005957 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5958 // It is used to bound the type in cases like:
5959 // if (x instanceof ClassX) {
5960 // // uper_bound_ will be ClassX
5961 // }
David Brazdilf5552582015-12-27 13:36:12 +00005962 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005963
5964 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5965};
5966
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005967class HCheckCast : public HTemplateInstruction<2> {
5968 public:
5969 HCheckCast(HInstruction* object,
5970 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005971 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005972 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00005973 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
5974 SetPackedField<TypeCheckKindField>(check_kind);
5975 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005976 SetRawInputAt(0, object);
5977 SetRawInputAt(1, constant);
5978 }
5979
5980 bool CanBeMoved() const OVERRIDE { return true; }
5981
5982 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5983 return true;
5984 }
5985
5986 bool NeedsEnvironment() const OVERRIDE {
5987 // Instruction may throw a CheckCastError.
5988 return true;
5989 }
5990
5991 bool CanThrow() const OVERRIDE { return true; }
5992
Vladimir Markoa1de9182016-02-25 11:37:38 +00005993 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5994 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5995 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5996 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005997
5998 DECLARE_INSTRUCTION(CheckCast);
5999
6000 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006001 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6002 static constexpr size_t kFieldTypeCheckKindSize =
6003 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6004 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6005 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6006 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6007 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006008
6009 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006010};
6011
Calin Juravle27df7582015-04-17 19:12:31 +01006012class HMemoryBarrier : public HTemplateInstruction<0> {
6013 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006014 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006015 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006016 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6017 SetPackedField<BarrierKindField>(barrier_kind);
6018 }
Calin Juravle27df7582015-04-17 19:12:31 +01006019
Vladimir Markoa1de9182016-02-25 11:37:38 +00006020 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006021
6022 DECLARE_INSTRUCTION(MemoryBarrier);
6023
6024 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006025 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6026 static constexpr size_t kFieldBarrierKindSize =
6027 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6028 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6029 kFieldBarrierKind + kFieldBarrierKindSize;
6030 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6031 "Too many packed fields.");
6032 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006033
6034 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6035};
6036
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006037class HMonitorOperation : public HTemplateInstruction<1> {
6038 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006039 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006040 kEnter,
6041 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006042 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006043 };
6044
6045 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006046 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006047 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6048 dex_pc) {
6049 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006050 SetRawInputAt(0, object);
6051 }
6052
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006053 // Instruction may go into runtime, so we need an environment.
6054 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006055
6056 bool CanThrow() const OVERRIDE {
6057 // Verifier guarantees that monitor-exit cannot throw.
6058 // This is important because it allows the HGraphBuilder to remove
6059 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6060 return IsEnter();
6061 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006062
Vladimir Markoa1de9182016-02-25 11:37:38 +00006063 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6064 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006065
6066 DECLARE_INSTRUCTION(MonitorOperation);
6067
Calin Juravle52c48962014-12-16 17:02:57 +00006068 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006069 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6070 static constexpr size_t kFieldOperationKindSize =
6071 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6072 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6073 kFieldOperationKind + kFieldOperationKindSize;
6074 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6075 "Too many packed fields.");
6076 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006077
6078 private:
6079 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6080};
6081
David Brazdil74eb1b22015-12-14 11:44:01 +00006082class HSelect : public HExpression<3> {
6083 public:
6084 HSelect(HInstruction* condition,
6085 HInstruction* true_value,
6086 HInstruction* false_value,
6087 uint32_t dex_pc)
6088 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6089 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6090
6091 // First input must be `true_value` or `false_value` to allow codegens to
6092 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6093 // that architectures which implement HSelect as a conditional move also
6094 // will not need to invert the condition.
6095 SetRawInputAt(0, false_value);
6096 SetRawInputAt(1, true_value);
6097 SetRawInputAt(2, condition);
6098 }
6099
6100 HInstruction* GetFalseValue() const { return InputAt(0); }
6101 HInstruction* GetTrueValue() const { return InputAt(1); }
6102 HInstruction* GetCondition() const { return InputAt(2); }
6103
6104 bool CanBeMoved() const OVERRIDE { return true; }
6105 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6106
6107 bool CanBeNull() const OVERRIDE {
6108 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6109 }
6110
6111 DECLARE_INSTRUCTION(Select);
6112
6113 private:
6114 DISALLOW_COPY_AND_ASSIGN(HSelect);
6115};
6116
Vladimir Markof9f64412015-09-02 14:05:49 +01006117class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006118 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006119 MoveOperands(Location source,
6120 Location destination,
6121 Primitive::Type type,
6122 HInstruction* instruction)
6123 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006124
6125 Location GetSource() const { return source_; }
6126 Location GetDestination() const { return destination_; }
6127
6128 void SetSource(Location value) { source_ = value; }
6129 void SetDestination(Location value) { destination_ = value; }
6130
6131 // The parallel move resolver marks moves as "in-progress" by clearing the
6132 // destination (but not the source).
6133 Location MarkPending() {
6134 DCHECK(!IsPending());
6135 Location dest = destination_;
6136 destination_ = Location::NoLocation();
6137 return dest;
6138 }
6139
6140 void ClearPending(Location dest) {
6141 DCHECK(IsPending());
6142 destination_ = dest;
6143 }
6144
6145 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006146 DCHECK(source_.IsValid() || destination_.IsInvalid());
6147 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006148 }
6149
6150 // True if this blocks a move from the given location.
6151 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006152 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006153 }
6154
6155 // A move is redundant if it's been eliminated, if its source and
6156 // destination are the same, or if its destination is unneeded.
6157 bool IsRedundant() const {
6158 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6159 }
6160
6161 // We clear both operands to indicate move that's been eliminated.
6162 void Eliminate() {
6163 source_ = destination_ = Location::NoLocation();
6164 }
6165
6166 bool IsEliminated() const {
6167 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6168 return source_.IsInvalid();
6169 }
6170
Alexey Frunze4dda3372015-06-01 18:31:49 -07006171 Primitive::Type GetType() const { return type_; }
6172
Nicolas Geoffray90218252015-04-15 11:56:51 +01006173 bool Is64BitMove() const {
6174 return Primitive::Is64BitType(type_);
6175 }
6176
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006177 HInstruction* GetInstruction() const { return instruction_; }
6178
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006179 private:
6180 Location source_;
6181 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006182 // The type this move is for.
6183 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006184 // The instruction this move is assocatied with. Null when this move is
6185 // for moving an input in the expected locations of user (including a phi user).
6186 // This is only used in debug mode, to ensure we do not connect interval siblings
6187 // in the same parallel move.
6188 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006189};
6190
Roland Levillainc9285912015-12-18 10:38:42 +00006191std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6192
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006193static constexpr size_t kDefaultNumberOfMoves = 4;
6194
6195class HParallelMove : public HTemplateInstruction<0> {
6196 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006197 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006198 : HTemplateInstruction(SideEffects::None(), dex_pc),
6199 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6200 moves_.reserve(kDefaultNumberOfMoves);
6201 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006202
Nicolas Geoffray90218252015-04-15 11:56:51 +01006203 void AddMove(Location source,
6204 Location destination,
6205 Primitive::Type type,
6206 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006207 DCHECK(source.IsValid());
6208 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006209 if (kIsDebugBuild) {
6210 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006211 for (const MoveOperands& move : moves_) {
6212 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006213 // Special case the situation where the move is for the spill slot
6214 // of the instruction.
6215 if ((GetPrevious() == instruction)
6216 || ((GetPrevious() == nullptr)
6217 && instruction->IsPhi()
6218 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006219 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006220 << "Doing parallel moves for the same instruction.";
6221 } else {
6222 DCHECK(false) << "Doing parallel moves for the same instruction.";
6223 }
6224 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006225 }
6226 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006227 for (const MoveOperands& move : moves_) {
6228 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006229 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006230 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006231 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006232 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006233 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006234 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006235 }
6236
Vladimir Marko225b6462015-09-28 12:17:40 +01006237 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006238 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006239 }
6240
Vladimir Marko225b6462015-09-28 12:17:40 +01006241 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006242
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006243 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006244
6245 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006246 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006247
6248 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6249};
6250
Mark Mendell0616ae02015-04-17 12:49:27 -04006251} // namespace art
6252
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006253#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6254#include "nodes_shared.h"
6255#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006256#ifdef ART_ENABLE_CODEGEN_arm
6257#include "nodes_arm.h"
6258#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006259#ifdef ART_ENABLE_CODEGEN_arm64
6260#include "nodes_arm64.h"
6261#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006262#ifdef ART_ENABLE_CODEGEN_x86
6263#include "nodes_x86.h"
6264#endif
6265
6266namespace art {
6267
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006268class HGraphVisitor : public ValueObject {
6269 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006270 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6271 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006272
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006273 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006274 virtual void VisitBasicBlock(HBasicBlock* block);
6275
Roland Levillain633021e2014-10-01 14:12:25 +01006276 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006277 void VisitInsertionOrder();
6278
Roland Levillain633021e2014-10-01 14:12:25 +01006279 // Visit the graph following dominator tree reverse post-order.
6280 void VisitReversePostOrder();
6281
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006282 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006283
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006284 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006285#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006286 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6287
6288 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6289
6290#undef DECLARE_VISIT_INSTRUCTION
6291
6292 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006293 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006294
6295 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6296};
6297
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006298class HGraphDelegateVisitor : public HGraphVisitor {
6299 public:
6300 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6301 virtual ~HGraphDelegateVisitor() {}
6302
6303 // Visit functions that delegate to to super class.
6304#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006305 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006306
6307 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6308
6309#undef DECLARE_VISIT_INSTRUCTION
6310
6311 private:
6312 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6313};
6314
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006315class HInsertionOrderIterator : public ValueObject {
6316 public:
6317 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6318
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006319 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006320 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006321 void Advance() { ++index_; }
6322
6323 private:
6324 const HGraph& graph_;
6325 size_t index_;
6326
6327 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6328};
6329
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006330class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006331 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006332 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6333 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006334 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006335 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006336
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006337 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6338 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006339 void Advance() { ++index_; }
6340
6341 private:
6342 const HGraph& graph_;
6343 size_t index_;
6344
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006345 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006346};
6347
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006348class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006349 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006350 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006351 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006352 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006353 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006354 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006355
6356 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006357 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006358 void Advance() { --index_; }
6359
6360 private:
6361 const HGraph& graph_;
6362 size_t index_;
6363
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006364 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006365};
6366
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006367class HLinearPostOrderIterator : public ValueObject {
6368 public:
6369 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006370 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006371
6372 bool Done() const { return index_ == 0; }
6373
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006374 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006375
6376 void Advance() {
6377 --index_;
6378 DCHECK_GE(index_, 0U);
6379 }
6380
6381 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006382 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006383 size_t index_;
6384
6385 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6386};
6387
6388class HLinearOrderIterator : public ValueObject {
6389 public:
6390 explicit HLinearOrderIterator(const HGraph& graph)
6391 : order_(graph.GetLinearOrder()), index_(0) {}
6392
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006393 bool Done() const { return index_ == order_.size(); }
6394 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006395 void Advance() { ++index_; }
6396
6397 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006398 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006399 size_t index_;
6400
6401 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6402};
6403
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006404// Iterator over the blocks that art part of the loop. Includes blocks part
6405// of an inner loop. The order in which the blocks are iterated is on their
6406// block id.
6407class HBlocksInLoopIterator : public ValueObject {
6408 public:
6409 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6410 : blocks_in_loop_(info.GetBlocks()),
6411 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6412 index_(0) {
6413 if (!blocks_in_loop_.IsBitSet(index_)) {
6414 Advance();
6415 }
6416 }
6417
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006418 bool Done() const { return index_ == blocks_.size(); }
6419 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006420 void Advance() {
6421 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006422 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006423 if (blocks_in_loop_.IsBitSet(index_)) {
6424 break;
6425 }
6426 }
6427 }
6428
6429 private:
6430 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006431 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006432 size_t index_;
6433
6434 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6435};
6436
Mingyao Yang3584bce2015-05-19 16:01:59 -07006437// Iterator over the blocks that art part of the loop. Includes blocks part
6438// of an inner loop. The order in which the blocks are iterated is reverse
6439// post order.
6440class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6441 public:
6442 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6443 : blocks_in_loop_(info.GetBlocks()),
6444 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6445 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006446 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006447 Advance();
6448 }
6449 }
6450
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006451 bool Done() const { return index_ == blocks_.size(); }
6452 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006453 void Advance() {
6454 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006455 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6456 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006457 break;
6458 }
6459 }
6460 }
6461
6462 private:
6463 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006464 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006465 size_t index_;
6466
6467 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6468};
6469
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006470inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006471 if (constant->IsIntConstant()) {
6472 return constant->AsIntConstant()->GetValue();
6473 } else if (constant->IsLongConstant()) {
6474 return constant->AsLongConstant()->GetValue();
6475 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006476 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006477 return 0;
6478 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006479}
6480
Vladimir Marko58155012015-08-19 12:49:41 +00006481inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6482 // For the purposes of the compiler, the dex files must actually be the same object
6483 // if we want to safely treat them as the same. This is especially important for JIT
6484 // as custom class loaders can open the same underlying file (or memory) multiple
6485 // times and provide different class resolution but no two class loaders should ever
6486 // use the same DexFile object - doing so is an unsupported hack that can lead to
6487 // all sorts of weird failures.
6488 return &lhs == &rhs;
6489}
6490
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006491#define INSTRUCTION_TYPE_CHECK(type, super) \
6492 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6493 inline const H##type* HInstruction::As##type() const { \
6494 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6495 } \
6496 inline H##type* HInstruction::As##type() { \
6497 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6498 }
6499
6500 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6501#undef INSTRUCTION_TYPE_CHECK
6502
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00006503class SwitchTable : public ValueObject {
6504 public:
6505 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
6506 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
6507 int32_t table_offset = instruction.VRegB_31t();
6508 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
6509 if (sparse) {
6510 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
6511 } else {
6512 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
6513 }
6514 num_entries_ = table[1];
6515 values_ = reinterpret_cast<const int32_t*>(&table[2]);
6516 }
6517
6518 uint16_t GetNumEntries() const {
6519 return num_entries_;
6520 }
6521
6522 void CheckIndex(size_t index) const {
6523 if (sparse_) {
6524 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6525 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
6526 } else {
6527 // In a packed table, we have the starting key and num_entries_ values.
6528 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
6529 }
6530 }
6531
6532 int32_t GetEntryAt(size_t index) const {
6533 CheckIndex(index);
6534 return values_[index];
6535 }
6536
6537 uint32_t GetDexPcForIndex(size_t index) const {
6538 CheckIndex(index);
6539 return dex_pc_ +
6540 (reinterpret_cast<const int16_t*>(values_ + index) -
6541 reinterpret_cast<const int16_t*>(&instruction_));
6542 }
6543
6544 // Index of the first value in the table.
6545 size_t GetFirstValueIndex() const {
6546 if (sparse_) {
6547 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6548 return num_entries_;
6549 } else {
6550 // In a packed table, we have the starting key and num_entries_ values.
6551 return 1;
6552 }
6553 }
6554
6555 private:
6556 const Instruction& instruction_;
6557 const uint32_t dex_pc_;
6558
6559 // Whether this is a sparse-switch table (or a packed-switch one).
6560 const bool sparse_;
6561
6562 // This can't be const as it needs to be computed off of the given instruction, and complicated
6563 // expressions in the initializer list seemed very ugly.
6564 uint16_t num_entries_;
6565
6566 const int32_t* values_;
6567
6568 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
6569};
6570
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006571// Create space in `blocks` for adding `number_of_new_blocks` entries
6572// starting at location `at`. Blocks after `at` are moved accordingly.
6573inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6574 size_t number_of_new_blocks,
6575 size_t after) {
6576 DCHECK_LT(after, blocks->size());
6577 size_t old_size = blocks->size();
6578 size_t new_size = old_size + number_of_new_blocks;
6579 blocks->resize(new_size);
6580 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6581}
6582
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006583} // namespace art
6584
6585#endif // ART_COMPILER_OPTIMIZING_NODES_H_