blob: 1bb5f5df51169e5e7249be338b30be62b14af987 [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
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000512 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
513
David Brazdil2d7352b2015-04-20 14:52:42 +0100514 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000515 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100516 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000517
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000518 template <class InstructionType, typename ValueType>
519 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600520 ArenaSafeMap<ValueType, InstructionType*>* cache,
521 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000522 // Try to find an existing constant of the given value.
523 InstructionType* constant = nullptr;
524 auto cached_constant = cache->find(value);
525 if (cached_constant != cache->end()) {
526 constant = cached_constant->second;
527 }
528
529 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100530 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000531 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600532 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000533 cache->Overwrite(value, constant);
534 InsertConstant(constant);
535 }
536 return constant;
537 }
538
David Brazdil8d5b8b22015-03-24 10:51:52 +0000539 void InsertConstant(HConstant* instruction);
540
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000541 // Cache a float constant into the graph. This method should only be
542 // called by the SsaBuilder when creating "equivalent" instructions.
543 void CacheFloatConstant(HFloatConstant* constant);
544
545 // See CacheFloatConstant comment.
546 void CacheDoubleConstant(HDoubleConstant* constant);
547
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000548 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000549
550 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100551 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000552
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100553 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100554 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000555
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100556 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100557 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100558
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000559 HBasicBlock* entry_block_;
560 HBasicBlock* exit_block_;
561
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100562 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100563 uint16_t maximum_number_of_out_vregs_;
564
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100565 // The number of virtual registers in this method. Contains the parameters.
566 uint16_t number_of_vregs_;
567
568 // The number of virtual registers used by parameters of this method.
569 uint16_t number_of_in_vregs_;
570
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000571 // Number of vreg size slots that the temporaries use (used in baseline compiler).
572 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100573
Mark Mendell1152c922015-04-24 17:06:35 -0400574 // Has bounds checks. We can totally skip BCE if it's false.
575 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800576
David Brazdil77a48ae2015-09-15 12:34:04 +0000577 // Flag whether there are any try/catch blocks in the graph. We will skip
578 // try/catch-related passes if false.
579 bool has_try_catch_;
580
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000581 // Flag whether there are any irreducible loops in the graph.
582 bool has_irreducible_loops_;
583
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000584 // Indicates whether the graph should be compiled in a way that
585 // ensures full debuggability. If false, we can apply more
586 // aggressive optimizations that may limit the level of debugging.
587 const bool debuggable_;
588
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000589 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000590 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000591
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100592 // The dex file from which the method is from.
593 const DexFile& dex_file_;
594
595 // The method index in the dex file.
596 const uint32_t method_idx_;
597
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100598 // If inlined, this encodes how the callee is being invoked.
599 const InvokeType invoke_type_;
600
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100601 // Whether the graph has been transformed to SSA form. Only used
602 // in debug mode to ensure we are not using properties only valid
603 // for non-SSA form (like the number of temporaries).
604 bool in_ssa_form_;
605
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100606 const bool should_generate_constructor_barrier_;
607
Mathieu Chartiere401d142015-04-22 13:56:20 -0700608 const InstructionSet instruction_set_;
609
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000610 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000611 HNullConstant* cached_null_constant_;
612 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000613 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000614 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000615 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000616
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100617 HCurrentMethod* cached_current_method_;
618
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100619 // The ArtMethod this graph is for. Note that for AOT, it may be null,
620 // for example for methods whose declaring class could not be resolved
621 // (such as when the superclass could not be found).
622 ArtMethod* art_method_;
623
David Brazdil4833f5a2015-12-16 10:37:39 +0000624 // Keep the RTI of inexact Object to avoid having to pass stack handle
625 // collection pointer to passes which may create NullConstant.
626 ReferenceTypeInfo inexact_object_rti_;
627
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000628 // Whether we are compiling this graph for on stack replacement: this will
629 // make all loops seen as irreducible and emit special stack maps to mark
630 // compiled code entries which the interpreter can directly jump to.
631 const bool osr_;
632
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000633 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100634 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000635 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000636 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000637 DISALLOW_COPY_AND_ASSIGN(HGraph);
638};
639
Vladimir Markof9f64412015-09-02 14:05:49 +0100640class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000641 public:
642 HLoopInformation(HBasicBlock* header, HGraph* graph)
643 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100644 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000645 irreducible_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100646 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100647 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100648 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
649 back_edges_.reserve(kDefaultNumberOfBackEdges);
650 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100651
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000652 bool IsIrreducible() const { return irreducible_; }
653
654 void Dump(std::ostream& os);
655
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100656 HBasicBlock* GetHeader() const {
657 return header_;
658 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000659
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000660 void SetHeader(HBasicBlock* block) {
661 header_ = block;
662 }
663
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100664 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
665 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
666 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
667
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000668 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100669 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000670 }
671
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100672 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100673 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100674 }
675
David Brazdil46e2a392015-03-16 17:31:52 +0000676 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100677 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100678 }
679
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000680 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100681 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000682 }
683
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100684 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100685
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100686 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100687 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100688 }
689
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100690 // Returns the lifetime position of the back edge that has the
691 // greatest lifetime position.
692 size_t GetLifetimeEnd() const;
693
694 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100695 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100696 }
697
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000698 // Finds blocks that are part of this loop.
699 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100700
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100701 // Returns whether this loop information contains `block`.
702 // Note that this loop information *must* be populated before entering this function.
703 bool Contains(const HBasicBlock& block) const;
704
705 // Returns whether this loop information is an inner loop of `other`.
706 // Note that `other` *must* be populated before entering this function.
707 bool IsIn(const HLoopInformation& other) const;
708
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800709 // Returns true if instruction is not defined within this loop.
710 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700711
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100712 const ArenaBitVector& GetBlocks() const { return blocks_; }
713
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000714 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000715 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000716
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000717 void ClearAllBlocks() {
718 blocks_.ClearAllBits();
719 }
720
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000721 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100722 // Internal recursive implementation of `Populate`.
723 void PopulateRecursive(HBasicBlock* block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000724 void PopulateIrreducibleRecursive(HBasicBlock* block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100725
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000726 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100727 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000728 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100729 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100730 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000731
732 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
733};
734
David Brazdilec16f792015-08-19 15:04:01 +0100735// Stores try/catch information for basic blocks.
736// Note that HGraph is constructed so that catch blocks cannot simultaneously
737// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100738class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100739 public:
740 // Try block information constructor.
741 explicit TryCatchInformation(const HTryBoundary& try_entry)
742 : try_entry_(&try_entry),
743 catch_dex_file_(nullptr),
744 catch_type_index_(DexFile::kDexNoIndex16) {
745 DCHECK(try_entry_ != nullptr);
746 }
747
748 // Catch block information constructor.
749 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
750 : try_entry_(nullptr),
751 catch_dex_file_(&dex_file),
752 catch_type_index_(catch_type_index) {}
753
754 bool IsTryBlock() const { return try_entry_ != nullptr; }
755
756 const HTryBoundary& GetTryEntry() const {
757 DCHECK(IsTryBlock());
758 return *try_entry_;
759 }
760
761 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
762
763 bool IsCatchAllTypeIndex() const {
764 DCHECK(IsCatchBlock());
765 return catch_type_index_ == DexFile::kDexNoIndex16;
766 }
767
768 uint16_t GetCatchTypeIndex() const {
769 DCHECK(IsCatchBlock());
770 return catch_type_index_;
771 }
772
773 const DexFile& GetCatchDexFile() const {
774 DCHECK(IsCatchBlock());
775 return *catch_dex_file_;
776 }
777
778 private:
779 // One of possibly several TryBoundary instructions entering the block's try.
780 // Only set for try blocks.
781 const HTryBoundary* try_entry_;
782
783 // Exception type information. Only set for catch blocks.
784 const DexFile* catch_dex_file_;
785 const uint16_t catch_type_index_;
786};
787
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100788static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100789static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100790
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000791// A block in a method. Contains the list of instructions represented
792// as a double linked list. Each block knows its predecessors and
793// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100794
Vladimir Markof9f64412015-09-02 14:05:49 +0100795class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000796 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600797 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000798 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000799 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
800 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000801 loop_information_(nullptr),
802 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000803 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100804 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100805 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100806 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000807 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000808 try_catch_information_(nullptr) {
809 predecessors_.reserve(kDefaultNumberOfPredecessors);
810 successors_.reserve(kDefaultNumberOfSuccessors);
811 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
812 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000813
Vladimir Marko60584552015-09-03 13:35:12 +0000814 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100815 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000816 }
817
Vladimir Marko60584552015-09-03 13:35:12 +0000818 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100819 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000820 }
821
David Brazdild26a4112015-11-10 11:07:31 +0000822 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
823 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
824
Vladimir Marko60584552015-09-03 13:35:12 +0000825 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
826 return ContainsElement(successors_, block, start_from);
827 }
828
829 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100830 return dominated_blocks_;
831 }
832
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100833 bool IsEntryBlock() const {
834 return graph_->GetEntryBlock() == this;
835 }
836
837 bool IsExitBlock() const {
838 return graph_->GetExitBlock() == this;
839 }
840
David Brazdil46e2a392015-03-16 17:31:52 +0000841 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000842 bool IsSingleTryBoundary() const;
843
844 // Returns true if this block emits nothing but a jump.
845 bool IsSingleJump() const {
846 HLoopInformation* loop_info = GetLoopInformation();
847 return (IsSingleGoto() || IsSingleTryBoundary())
848 // Back edges generate a suspend check.
849 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
850 }
David Brazdil46e2a392015-03-16 17:31:52 +0000851
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000852 void AddBackEdge(HBasicBlock* back_edge) {
853 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000854 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000855 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100856 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000857 loop_information_->AddBackEdge(back_edge);
858 }
859
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000860 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000861 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000862
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100863 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000864 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600865 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000866
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000867 HBasicBlock* GetDominator() const { return dominator_; }
868 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000869 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
870
871 void RemoveDominatedBlock(HBasicBlock* block) {
872 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100873 }
Vladimir Marko60584552015-09-03 13:35:12 +0000874
875 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
876 ReplaceElement(dominated_blocks_, existing, new_block);
877 }
878
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100879 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000880
881 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100882 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000883 }
884
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100885 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
886 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100887 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100888 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100889 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
890 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000891
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000892 HInstruction* GetFirstInstructionDisregardMoves() const;
893
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000894 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000895 successors_.push_back(block);
896 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000897 }
898
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100899 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
900 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100901 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000902 new_block->predecessors_.push_back(this);
903 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000904 }
905
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000906 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
907 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000908 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000909 new_block->successors_.push_back(this);
910 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000911 }
912
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100913 // Insert `this` between `predecessor` and `successor. This method
914 // preserves the indicies, and will update the first edge found between
915 // `predecessor` and `successor`.
916 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
917 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100918 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000919 successor->predecessors_[predecessor_index] = this;
920 predecessor->successors_[successor_index] = this;
921 successors_.push_back(successor);
922 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100923 }
924
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100925 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000926 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100927 }
928
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000929 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000930 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000931 }
932
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100933 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000934 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100935 }
936
937 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000938 predecessors_.push_back(block);
939 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100940 }
941
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100942 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000943 DCHECK_EQ(predecessors_.size(), 2u);
944 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100945 }
946
David Brazdil769c9e52015-04-27 13:54:09 +0100947 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000948 DCHECK_EQ(successors_.size(), 2u);
949 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100950 }
951
David Brazdilfc6a86a2015-06-26 10:33:45 +0000952 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000953 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100954 }
955
David Brazdilfc6a86a2015-06-26 10:33:45 +0000956 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000957 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100958 }
959
David Brazdilfc6a86a2015-06-26 10:33:45 +0000960 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000961 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100962 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000963 }
964
965 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000966 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100967 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000968 }
969
970 // Returns whether the first occurrence of `predecessor` in the list of
971 // predecessors is at index `idx`.
972 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100973 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000974 return GetPredecessorIndexOf(predecessor) == idx;
975 }
976
David Brazdild7558da2015-09-22 13:04:14 +0100977 // Create a new block between this block and its predecessors. The new block
978 // is added to the graph, all predecessor edges are relinked to it and an edge
979 // is created to `this`. Returns the new empty block. Reverse post order or
980 // loop and try/catch information are not updated.
981 HBasicBlock* CreateImmediateDominator();
982
David Brazdilfc6a86a2015-06-26 10:33:45 +0000983 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100984 // created, latter block. Note that this method will add the block to the
985 // graph, create a Goto at the end of the former block and will create an edge
986 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100987 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000988 HBasicBlock* SplitBefore(HInstruction* cursor);
989
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000990 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000991 // created block. Note that this method just updates raw block information,
992 // like predecessors, successors, dominators, and instruction list. It does not
993 // update the graph, reverse post order, loop information, nor make sure the
994 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000995 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
996
997 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
998 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000999
David Brazdil9bc43612015-11-05 21:25:24 +00001000 // Split catch block into two blocks after the original move-exception bytecode
1001 // instruction, or at the beginning if not present. Returns the newly created,
1002 // latter block, or nullptr if such block could not be created (must be dead
1003 // in that case). Note that this method just updates raw block information,
1004 // like predecessors, successors, dominators, and instruction list. It does not
1005 // update the graph, reverse post order, loop information, nor make sure the
1006 // blocks are consistent (for example ending with a control flow instruction).
1007 HBasicBlock* SplitCatchBlockAfterMoveException();
1008
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001009 // Merge `other` at the end of `this`. Successors and dominated blocks of
1010 // `other` are changed to be successors and dominated blocks of `this`. Note
1011 // that this method does not update the graph, reverse post order, loop
1012 // information, nor make sure the blocks are consistent (for example ending
1013 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001014 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001015
1016 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1017 // of `this` are moved to `other`.
1018 // Note that this method does not update the graph, reverse post order, loop
1019 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001020 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001021 void ReplaceWith(HBasicBlock* other);
1022
David Brazdil2d7352b2015-04-20 14:52:42 +01001023 // Merge `other` at the end of `this`. This method updates loops, reverse post
1024 // order, links to predecessors, successors, dominators and deletes the block
1025 // from the graph. The two blocks must be successive, i.e. `this` the only
1026 // predecessor of `other` and vice versa.
1027 void MergeWith(HBasicBlock* other);
1028
1029 // Disconnects `this` from all its predecessors, successors and dominator,
1030 // removes it from all loops it is included in and eventually from the graph.
1031 // The block must not dominate any other block. Predecessors and successors
1032 // are safely updated.
1033 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001034
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001035 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001036 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001037 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001038 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001039 // Replace instruction `initial` with `replacement` within this block.
1040 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1041 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001042 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001043 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001044 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001045 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1046 // instruction list. With 'ensure_safety' set to true, it verifies that the
1047 // instruction is not in use and removes it from the use lists of its inputs.
1048 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1049 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001050 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001051
1052 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001053 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001054 }
1055
Roland Levillain6b879dd2014-09-22 17:13:44 +01001056 bool IsLoopPreHeaderFirstPredecessor() const {
1057 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001058 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001059 }
1060
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001061 bool IsFirstPredecessorBackEdge() const {
1062 DCHECK(IsLoopHeader());
1063 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1064 }
1065
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001066 HLoopInformation* GetLoopInformation() const {
1067 return loop_information_;
1068 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001069
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001070 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001071 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001072 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001073 void SetInLoop(HLoopInformation* info) {
1074 if (IsLoopHeader()) {
1075 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001076 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001077 loop_information_ = info;
1078 } else if (loop_information_->Contains(*info->GetHeader())) {
1079 // Block is currently part of an outer loop. Make it part of this inner loop.
1080 // Note that a non loop header having a loop information means this loop information
1081 // has already been populated
1082 loop_information_ = info;
1083 } else {
1084 // Block is part of an inner loop. Do not update the loop information.
1085 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1086 // at this point, because this method is being called while populating `info`.
1087 }
1088 }
1089
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001090 // Raw update of the loop information.
1091 void SetLoopInformation(HLoopInformation* info) {
1092 loop_information_ = info;
1093 }
1094
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001095 bool IsInLoop() const { return loop_information_ != nullptr; }
1096
David Brazdilec16f792015-08-19 15:04:01 +01001097 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1098
1099 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1100 try_catch_information_ = try_catch_information;
1101 }
1102
1103 bool IsTryBlock() const {
1104 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1105 }
1106
1107 bool IsCatchBlock() const {
1108 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1109 }
David Brazdilffee3d32015-07-06 11:48:53 +01001110
1111 // Returns the try entry that this block's successors should have. They will
1112 // be in the same try, unless the block ends in a try boundary. In that case,
1113 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001114 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001115
David Brazdild7558da2015-09-22 13:04:14 +01001116 bool HasThrowingInstructions() const;
1117
David Brazdila4b8c212015-05-07 09:59:30 +01001118 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001119 bool Dominates(HBasicBlock* block) const;
1120
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001121 size_t GetLifetimeStart() const { return lifetime_start_; }
1122 size_t GetLifetimeEnd() const { return lifetime_end_; }
1123
1124 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1125 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1126
David Brazdil8d5b8b22015-03-24 10:51:52 +00001127 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001128 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001129 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001130 bool HasSinglePhi() const;
1131
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001132 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001133 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001134 ArenaVector<HBasicBlock*> predecessors_;
1135 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001136 HInstructionList instructions_;
1137 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001138 HLoopInformation* loop_information_;
1139 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001140 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001141 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001142 // The dex program counter of the first instruction of this block.
1143 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001144 size_t lifetime_start_;
1145 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001146 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001147
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001148 friend class HGraph;
1149 friend class HInstruction;
1150
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001151 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1152};
1153
David Brazdilb2bd1c52015-03-25 11:17:37 +00001154// Iterates over the LoopInformation of all loops which contain 'block'
1155// from the innermost to the outermost.
1156class HLoopInformationOutwardIterator : public ValueObject {
1157 public:
1158 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1159 : current_(block.GetLoopInformation()) {}
1160
1161 bool Done() const { return current_ == nullptr; }
1162
1163 void Advance() {
1164 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001165 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001166 }
1167
1168 HLoopInformation* Current() const {
1169 DCHECK(!Done());
1170 return current_;
1171 }
1172
1173 private:
1174 HLoopInformation* current_;
1175
1176 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1177};
1178
Alexandre Ramesef20f712015-06-09 10:29:30 +01001179#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001180 M(Above, Condition) \
1181 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001182 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001183 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001184 M(ArrayGet, Instruction) \
1185 M(ArrayLength, Instruction) \
1186 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001187 M(Below, Condition) \
1188 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001189 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001190 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001191 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001192 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001193 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001194 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001195 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001196 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001197 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001198 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001199 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001200 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001201 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001202 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001203 M(Exit, Instruction) \
1204 M(FloatConstant, Constant) \
1205 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001206 M(GreaterThan, Condition) \
1207 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001208 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001209 M(InstanceFieldGet, Instruction) \
1210 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001211 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001212 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001213 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001214 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001215 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001216 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001217 M(LessThan, Condition) \
1218 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001219 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001220 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001221 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001222 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001223 M(Local, Instruction) \
1224 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001225 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001226 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001227 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001228 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001229 M(Neg, UnaryOperation) \
1230 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001231 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001232 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001233 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001234 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001235 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001236 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001237 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001238 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001239 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001240 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001241 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001242 M(Return, Instruction) \
1243 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001244 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001245 M(Shl, BinaryOperation) \
1246 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001247 M(StaticFieldGet, Instruction) \
1248 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001249 M(UnresolvedInstanceFieldGet, Instruction) \
1250 M(UnresolvedInstanceFieldSet, Instruction) \
1251 M(UnresolvedStaticFieldGet, Instruction) \
1252 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001253 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001254 M(StoreLocal, Instruction) \
1255 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001256 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001257 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001258 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001259 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001260 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001261 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001262
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001263/*
1264 * Instructions, shared across several (not all) architectures.
1265 */
1266#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1267#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1268#else
1269#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001270 M(BitwiseNegatedRight, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001271 M(MultiplyAccumulate, Instruction)
1272#endif
1273
Vladimir Markob4536b72015-11-24 13:45:23 +00001274#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001275#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001276#else
1277#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1278 M(ArmDexCacheArraysBase, Instruction)
1279#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001280
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001281#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001282#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001283#else
1284#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001285 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001286 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001287#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001288
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001289#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1290
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001291#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1292
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001293#ifndef ART_ENABLE_CODEGEN_x86
1294#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1295#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001296#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1297 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001298 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001299 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001300 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001301#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001302
1303#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1304
1305#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1306 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001307 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001308 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1309 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001310 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001311 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001312 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1313 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1314
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001315#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1316 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001317 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001318 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001319 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001320 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001321
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001322#define FOR_EACH_INSTRUCTION(M) \
1323 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1324 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1325
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001326#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001327FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1328#undef FORWARD_DECLARATION
1329
Roland Levillainccc07a92014-09-16 14:48:16 +01001330#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001331 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001332 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001333 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001334 return other->Is##type(); \
1335 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001336 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001337
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001338#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1339 bool Is##type() const { return As##type() != nullptr; } \
1340 const H##type* As##type() const { return this; } \
1341 H##type* As##type() { return this; }
1342
David Brazdiled596192015-01-23 10:39:45 +00001343template <typename T> class HUseList;
1344
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001345template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001346class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001347 public:
David Brazdiled596192015-01-23 10:39:45 +00001348 HUseListNode* GetPrevious() const { return prev_; }
1349 HUseListNode* GetNext() const { return next_; }
1350 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001351 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001352 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001353
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001354 private:
David Brazdiled596192015-01-23 10:39:45 +00001355 HUseListNode(T user, size_t index)
1356 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1357
1358 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001359 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001360 HUseListNode<T>* prev_;
1361 HUseListNode<T>* next_;
1362
1363 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001364
1365 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1366};
1367
David Brazdiled596192015-01-23 10:39:45 +00001368template <typename T>
1369class HUseList : public ValueObject {
1370 public:
1371 HUseList() : first_(nullptr) {}
1372
1373 void Clear() {
1374 first_ = nullptr;
1375 }
1376
1377 // Adds a new entry at the beginning of the use list and returns
1378 // the newly created node.
1379 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001380 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001381 if (IsEmpty()) {
1382 first_ = new_node;
1383 } else {
1384 first_->prev_ = new_node;
1385 new_node->next_ = first_;
1386 first_ = new_node;
1387 }
1388 return new_node;
1389 }
1390
1391 HUseListNode<T>* GetFirst() const {
1392 return first_;
1393 }
1394
1395 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001396 DCHECK(node != nullptr);
1397 DCHECK(Contains(node));
1398
David Brazdiled596192015-01-23 10:39:45 +00001399 if (node->prev_ != nullptr) {
1400 node->prev_->next_ = node->next_;
1401 }
1402 if (node->next_ != nullptr) {
1403 node->next_->prev_ = node->prev_;
1404 }
1405 if (node == first_) {
1406 first_ = node->next_;
1407 }
1408 }
1409
David Brazdil1abb4192015-02-17 18:33:36 +00001410 bool Contains(const HUseListNode<T>* node) const {
1411 if (node == nullptr) {
1412 return false;
1413 }
1414 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1415 if (current == node) {
1416 return true;
1417 }
1418 }
1419 return false;
1420 }
1421
David Brazdiled596192015-01-23 10:39:45 +00001422 bool IsEmpty() const {
1423 return first_ == nullptr;
1424 }
1425
1426 bool HasOnlyOneUse() const {
1427 return first_ != nullptr && first_->next_ == nullptr;
1428 }
1429
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001430 size_t SizeSlow() const {
1431 size_t count = 0;
1432 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1433 ++count;
1434 }
1435 return count;
1436 }
1437
David Brazdiled596192015-01-23 10:39:45 +00001438 private:
1439 HUseListNode<T>* first_;
1440};
1441
1442template<typename T>
1443class HUseIterator : public ValueObject {
1444 public:
1445 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1446
1447 bool Done() const { return current_ == nullptr; }
1448
1449 void Advance() {
1450 DCHECK(!Done());
1451 current_ = current_->GetNext();
1452 }
1453
1454 HUseListNode<T>* Current() const {
1455 DCHECK(!Done());
1456 return current_;
1457 }
1458
1459 private:
1460 HUseListNode<T>* current_;
1461
1462 friend class HValue;
1463};
1464
David Brazdil1abb4192015-02-17 18:33:36 +00001465// This class is used by HEnvironment and HInstruction classes to record the
1466// instructions they use and pointers to the corresponding HUseListNodes kept
1467// by the used instructions.
1468template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001469class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001470 public:
1471 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1472 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1473
1474 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1475 : instruction_(old_record.instruction_), use_node_(use_node) {
1476 DCHECK(instruction_ != nullptr);
1477 DCHECK(use_node_ != nullptr);
1478 DCHECK(old_record.use_node_ == nullptr);
1479 }
1480
1481 HInstruction* GetInstruction() const { return instruction_; }
1482 HUseListNode<T>* GetUseNode() const { return use_node_; }
1483
1484 private:
1485 // Instruction used by the user.
1486 HInstruction* instruction_;
1487
1488 // Corresponding entry in the use list kept by 'instruction_'.
1489 HUseListNode<T>* use_node_;
1490};
1491
Aart Bik854a02b2015-07-14 16:07:00 -07001492/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001493 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001494 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001495 * For write/read dependences on fields/arrays, the dependence analysis uses
1496 * type disambiguation (e.g. a float field write cannot modify the value of an
1497 * integer field read) and the access type (e.g. a reference array write cannot
1498 * modify the value of a reference field read [although it may modify the
1499 * reference fetch prior to reading the field, which is represented by its own
1500 * write/read dependence]). The analysis makes conservative points-to
1501 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1502 * the same, and any reference read depends on any reference read without
1503 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001504 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001505 * The internal representation uses 38-bit and is described in the table below.
1506 * The first line indicates the side effect, and for field/array accesses the
1507 * second line indicates the type of the access (in the order of the
1508 * Primitive::Type enum).
1509 * The two numbered lines below indicate the bit position in the bitfield (read
1510 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001511 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001512 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1513 * +-------------+---------+---------+--------------+---------+---------+
1514 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1515 * | 3 |333333322|222222221| 1 |111111110|000000000|
1516 * | 7 |654321098|765432109| 8 |765432109|876543210|
1517 *
1518 * Note that, to ease the implementation, 'changes' bits are least significant
1519 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001520 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001521class SideEffects : public ValueObject {
1522 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001523 SideEffects() : flags_(0) {}
1524
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001525 static SideEffects None() {
1526 return SideEffects(0);
1527 }
1528
1529 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001530 return SideEffects(kAllChangeBits | kAllDependOnBits);
1531 }
1532
1533 static SideEffects AllChanges() {
1534 return SideEffects(kAllChangeBits);
1535 }
1536
1537 static SideEffects AllDependencies() {
1538 return SideEffects(kAllDependOnBits);
1539 }
1540
1541 static SideEffects AllExceptGCDependency() {
1542 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1543 }
1544
1545 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001546 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001547 }
1548
Aart Bik34c3ba92015-07-20 14:08:59 -07001549 static SideEffects AllWrites() {
1550 return SideEffects(kAllWrites);
1551 }
1552
1553 static SideEffects AllReads() {
1554 return SideEffects(kAllReads);
1555 }
1556
1557 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1558 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001559 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001560 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001561 }
1562
Aart Bik854a02b2015-07-14 16:07:00 -07001563 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1564 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001565 }
1566
Aart Bik34c3ba92015-07-20 14:08:59 -07001567 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1568 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001569 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001570 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001571 }
1572
1573 static SideEffects ArrayReadOfType(Primitive::Type type) {
1574 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1575 }
1576
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001577 static SideEffects CanTriggerGC() {
1578 return SideEffects(1ULL << kCanTriggerGCBit);
1579 }
1580
1581 static SideEffects DependsOnGC() {
1582 return SideEffects(1ULL << kDependsOnGCBit);
1583 }
1584
Aart Bik854a02b2015-07-14 16:07:00 -07001585 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001586 SideEffects Union(SideEffects other) const {
1587 return SideEffects(flags_ | other.flags_);
1588 }
1589
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001590 SideEffects Exclusion(SideEffects other) const {
1591 return SideEffects(flags_ & ~other.flags_);
1592 }
1593
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001594 void Add(SideEffects other) {
1595 flags_ |= other.flags_;
1596 }
1597
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001598 bool Includes(SideEffects other) const {
1599 return (other.flags_ & flags_) == other.flags_;
1600 }
1601
1602 bool HasSideEffects() const {
1603 return (flags_ & kAllChangeBits);
1604 }
1605
1606 bool HasDependencies() const {
1607 return (flags_ & kAllDependOnBits);
1608 }
1609
1610 // Returns true if there are no side effects or dependencies.
1611 bool DoesNothing() const {
1612 return flags_ == 0;
1613 }
1614
Aart Bik854a02b2015-07-14 16:07:00 -07001615 // Returns true if something is written.
1616 bool DoesAnyWrite() const {
1617 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001618 }
1619
Aart Bik854a02b2015-07-14 16:07:00 -07001620 // Returns true if something is read.
1621 bool DoesAnyRead() const {
1622 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001623 }
1624
Aart Bik854a02b2015-07-14 16:07:00 -07001625 // Returns true if potentially everything is written and read
1626 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001627 bool DoesAllReadWrite() const {
1628 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1629 }
1630
Aart Bik854a02b2015-07-14 16:07:00 -07001631 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001632 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001633 }
1634
Roland Levillain0d5a2812015-11-13 10:07:31 +00001635 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001636 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001637 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1638 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001639 }
1640
1641 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001642 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001643 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001644 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001645 for (int s = kLastBit; s >= 0; s--) {
1646 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1647 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1648 // This is a bit for the GC side effect.
1649 if (current_bit_is_set) {
1650 flags += "GC";
1651 }
Aart Bik854a02b2015-07-14 16:07:00 -07001652 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001653 } else {
1654 // This is a bit for the array/field analysis.
1655 // The underscore character stands for the 'can trigger GC' bit.
1656 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1657 if (current_bit_is_set) {
1658 flags += kDebug[s];
1659 }
1660 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1661 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1662 flags += "|";
1663 }
1664 }
Aart Bik854a02b2015-07-14 16:07:00 -07001665 }
1666 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001667 }
1668
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001669 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001670
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001671 private:
1672 static constexpr int kFieldArrayAnalysisBits = 9;
1673
1674 static constexpr int kFieldWriteOffset = 0;
1675 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1676 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1677 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1678
1679 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1680
1681 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1682 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1683 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1684 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1685
1686 static constexpr int kLastBit = kDependsOnGCBit;
1687 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1688
1689 // Aliases.
1690
1691 static_assert(kChangeBits == kDependOnBits,
1692 "the 'change' bits should match the 'depend on' bits.");
1693
1694 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1695 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1696 static constexpr uint64_t kAllWrites =
1697 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1698 static constexpr uint64_t kAllReads =
1699 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001700
Aart Bik854a02b2015-07-14 16:07:00 -07001701 // Work around the fact that HIR aliases I/F and J/D.
1702 // TODO: remove this interceptor once HIR types are clean
1703 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1704 switch (type) {
1705 case Primitive::kPrimInt:
1706 case Primitive::kPrimFloat:
1707 return TypeFlag(Primitive::kPrimInt, offset) |
1708 TypeFlag(Primitive::kPrimFloat, offset);
1709 case Primitive::kPrimLong:
1710 case Primitive::kPrimDouble:
1711 return TypeFlag(Primitive::kPrimLong, offset) |
1712 TypeFlag(Primitive::kPrimDouble, offset);
1713 default:
1714 return TypeFlag(type, offset);
1715 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001716 }
1717
Aart Bik854a02b2015-07-14 16:07:00 -07001718 // Translates type to bit flag.
1719 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1720 CHECK_NE(type, Primitive::kPrimVoid);
1721 const uint64_t one = 1;
1722 const int shift = type; // 0-based consecutive enum
1723 DCHECK_LE(kFieldWriteOffset, shift);
1724 DCHECK_LT(shift, kArrayWriteOffset);
1725 return one << (type + offset);
1726 }
1727
1728 // Private constructor on direct flags value.
1729 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1730
1731 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001732};
1733
David Brazdiled596192015-01-23 10:39:45 +00001734// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001735class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001736 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001737 HEnvironment(ArenaAllocator* arena,
1738 size_t number_of_vregs,
1739 const DexFile& dex_file,
1740 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001741 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001742 InvokeType invoke_type,
1743 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001744 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1745 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001746 parent_(nullptr),
1747 dex_file_(dex_file),
1748 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001749 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001750 invoke_type_(invoke_type),
1751 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001752 }
1753
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001754 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001755 : HEnvironment(arena,
1756 to_copy.Size(),
1757 to_copy.GetDexFile(),
1758 to_copy.GetMethodIdx(),
1759 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001760 to_copy.GetInvokeType(),
1761 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001762
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001763 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001764 if (parent_ != nullptr) {
1765 parent_->SetAndCopyParentChain(allocator, parent);
1766 } else {
1767 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1768 parent_->CopyFrom(parent);
1769 if (parent->GetParent() != nullptr) {
1770 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1771 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001772 }
David Brazdiled596192015-01-23 10:39:45 +00001773 }
1774
Vladimir Marko71bf8092015-09-15 15:33:14 +01001775 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001776 void CopyFrom(HEnvironment* environment);
1777
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001778 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1779 // input to the loop phi instead. This is for inserting instructions that
1780 // require an environment (like HDeoptimization) in the loop pre-header.
1781 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001782
1783 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001784 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001785 }
1786
1787 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001788 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001789 }
1790
David Brazdil1abb4192015-02-17 18:33:36 +00001791 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001792
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001793 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001794
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001795 HEnvironment* GetParent() const { return parent_; }
1796
1797 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001798 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001799 }
1800
1801 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001802 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001803 }
1804
1805 uint32_t GetDexPc() const {
1806 return dex_pc_;
1807 }
1808
1809 uint32_t GetMethodIdx() const {
1810 return method_idx_;
1811 }
1812
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001813 InvokeType GetInvokeType() const {
1814 return invoke_type_;
1815 }
1816
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001817 const DexFile& GetDexFile() const {
1818 return dex_file_;
1819 }
1820
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001821 HInstruction* GetHolder() const {
1822 return holder_;
1823 }
1824
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001825
1826 bool IsFromInlinedInvoke() const {
1827 return GetParent() != nullptr;
1828 }
1829
David Brazdiled596192015-01-23 10:39:45 +00001830 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001831 // Record instructions' use entries of this environment for constant-time removal.
1832 // It should only be called by HInstruction when a new environment use is added.
1833 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1834 DCHECK(env_use->GetUser() == this);
1835 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001836 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001837 }
David Brazdiled596192015-01-23 10:39:45 +00001838
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001839 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1840 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001841 HEnvironment* parent_;
1842 const DexFile& dex_file_;
1843 const uint32_t method_idx_;
1844 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001845 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001846
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001847 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001848 HInstruction* const holder_;
1849
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001850 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001851
1852 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1853};
1854
Vladimir Markof9f64412015-09-02 14:05:49 +01001855class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001856 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001857 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001858 : previous_(nullptr),
1859 next_(nullptr),
1860 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001861 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001862 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001863 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001864 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001865 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001866 locations_(nullptr),
1867 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001868 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001869 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001870 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1871 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1872 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001873
Dave Allison20dfc792014-06-16 20:44:29 -07001874 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001875
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001876#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001877 enum InstructionKind {
1878 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1879 };
1880#undef DECLARE_KIND
1881
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001882 HInstruction* GetNext() const { return next_; }
1883 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001884
Calin Juravle77520bc2015-01-12 18:45:46 +00001885 HInstruction* GetNextDisregardingMoves() const;
1886 HInstruction* GetPreviousDisregardingMoves() const;
1887
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001888 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001889 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001890 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001891 bool IsInBlock() const { return block_ != nullptr; }
1892 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001893 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1894 bool IsIrreducibleLoopHeaderPhi() const {
1895 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1896 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001897
Roland Levillain6b879dd2014-09-22 17:13:44 +01001898 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001899 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001900
1901 virtual void Accept(HGraphVisitor* visitor) = 0;
1902 virtual const char* DebugName() const = 0;
1903
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001904 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001905 void SetRawInputAt(size_t index, HInstruction* input) {
1906 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1907 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001908
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001909 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001910
1911 uint32_t GetDexPc() const { return dex_pc_; }
1912
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001913 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001914
Roland Levillaine161a2a2014-10-03 12:45:18 +01001915 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001916 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001917
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001918 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001919 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001920
Calin Juravle10e244f2015-01-26 18:54:32 +00001921 // Does not apply for all instructions, but having this at top level greatly
1922 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001923 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001924 virtual bool CanBeNull() const {
1925 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1926 return true;
1927 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001928
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001929 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001930 return false;
1931 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001932
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001933 virtual bool IsActualObject() const {
1934 return GetType() == Primitive::kPrimNot;
1935 }
1936
Calin Juravle2e768302015-07-28 14:41:11 +00001937 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001938
Calin Juravle61d544b2015-02-23 16:46:57 +00001939 ReferenceTypeInfo GetReferenceTypeInfo() const {
1940 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001941 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
1942 GetPackedFlag<kFlagReferenceTypeIsExact>());;
Calin Juravle61d544b2015-02-23 16:46:57 +00001943 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001944
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001945 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001946 DCHECK(user != nullptr);
1947 HUseListNode<HInstruction*>* use =
1948 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1949 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001950 }
1951
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001952 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001953 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001954 HUseListNode<HEnvironment*>* env_use =
1955 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1956 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001957 }
1958
David Brazdil1abb4192015-02-17 18:33:36 +00001959 void RemoveAsUserOfInput(size_t input) {
1960 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1961 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1962 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001963
David Brazdil1abb4192015-02-17 18:33:36 +00001964 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1965 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001966
David Brazdiled596192015-01-23 10:39:45 +00001967 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1968 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001969 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001970 bool HasOnlyOneNonEnvironmentUse() const {
1971 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1972 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001973
Roland Levillain6c82d402014-10-13 16:10:27 +01001974 // Does this instruction strictly dominate `other_instruction`?
1975 // Returns false if this instruction and `other_instruction` are the same.
1976 // Aborts if this instruction and `other_instruction` are both phis.
1977 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001978
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001979 int GetId() const { return id_; }
1980 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001981
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001982 int GetSsaIndex() const { return ssa_index_; }
1983 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1984 bool HasSsaIndex() const { return ssa_index_ != -1; }
1985
1986 bool HasEnvironment() const { return environment_ != nullptr; }
1987 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001988 // Set the `environment_` field. Raw because this method does not
1989 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001990 void SetRawEnvironment(HEnvironment* environment) {
1991 DCHECK(environment_ == nullptr);
1992 DCHECK_EQ(environment->GetHolder(), this);
1993 environment_ = environment;
1994 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001995
1996 // Set the environment of this instruction, copying it from `environment`. While
1997 // copying, the uses lists are being updated.
1998 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001999 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002000 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002001 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002002 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002003 if (environment->GetParent() != nullptr) {
2004 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2005 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002006 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002007
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002008 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2009 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002010 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002011 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002012 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002013 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002014 if (environment->GetParent() != nullptr) {
2015 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2016 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002017 }
2018
Nicolas Geoffray39468442014-09-02 15:17:15 +01002019 // Returns the number of entries in the environment. Typically, that is the
2020 // number of dex registers in a method. It could be more in case of inlining.
2021 size_t EnvironmentSize() const;
2022
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002023 LocationSummary* GetLocations() const { return locations_; }
2024 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002025
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002026 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002027 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002028
Alexandre Rames188d4312015-04-09 18:30:21 +01002029 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2030 // uses of this instruction by `other` are *not* updated.
2031 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2032 ReplaceWith(other);
2033 other->ReplaceInput(this, use_index);
2034 }
2035
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002036 // Move `this` instruction before `cursor`.
2037 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002038
Vladimir Markofb337ea2015-11-25 15:25:10 +00002039 // Move `this` before its first user and out of any loops. If there is no
2040 // out-of-loop user that dominates all other users, move the instruction
2041 // to the end of the out-of-loop common dominator of the user's blocks.
2042 //
2043 // This can be used only on non-throwing instructions with no side effects that
2044 // have at least one use but no environment uses.
2045 void MoveBeforeFirstUserAndOutOfLoops();
2046
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002047#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002048 bool Is##type() const; \
2049 const H##type* As##type() const; \
2050 H##type* As##type();
2051
2052 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2053#undef INSTRUCTION_TYPE_CHECK
2054
2055#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002056 bool Is##type() const { return (As##type() != nullptr); } \
2057 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002058 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002059 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002060#undef INSTRUCTION_TYPE_CHECK
2061
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002062 // Returns whether the instruction can be moved within the graph.
2063 virtual bool CanBeMoved() const { return false; }
2064
2065 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002066 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002067 return false;
2068 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002069
2070 // Returns whether any data encoded in the two instructions is equal.
2071 // This method does not look at the inputs. Both instructions must be
2072 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002073 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002074 return false;
2075 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002076
2077 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002078 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002079 // 2) Their inputs are identical.
2080 bool Equals(HInstruction* other) const;
2081
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002082 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2083 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2084 // the virtual function because the __attribute__((__pure__)) doesn't really
2085 // apply the strong requirement for virtual functions, preventing optimizations.
2086 InstructionKind GetKind() const PURE;
2087 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002088
2089 virtual size_t ComputeHashCode() const {
2090 size_t result = GetKind();
2091 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2092 result = (result * 31) + InputAt(i)->GetId();
2093 }
2094 return result;
2095 }
2096
2097 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002098 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002099 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002100
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002101 size_t GetLifetimePosition() const { return lifetime_position_; }
2102 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2103 LiveInterval* GetLiveInterval() const { return live_interval_; }
2104 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2105 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2106
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002107 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2108
2109 // Returns whether the code generation of the instruction will require to have access
2110 // to the current method. Such instructions are:
2111 // (1): Instructions that require an environment, as calling the runtime requires
2112 // to walk the stack and have the current method stored at a specific stack address.
2113 // (2): Object literals like classes and strings, that are loaded from the dex cache
2114 // fields of the current method.
2115 bool NeedsCurrentMethod() const {
2116 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2117 }
2118
Vladimir Markodc151b22015-10-15 18:02:30 +01002119 // Returns whether the code generation of the instruction will require to have access
2120 // to the dex cache of the current method's declaring class via the current method.
2121 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002122
Mark Mendellc4701932015-04-10 13:18:51 -04002123 // Does this instruction have any use in an environment before
2124 // control flow hits 'other'?
2125 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2126
2127 // Remove all references to environment uses of this instruction.
2128 // The caller must ensure that this is safe to do.
2129 void RemoveEnvironmentUsers();
2130
Vladimir Markoa1de9182016-02-25 11:37:38 +00002131 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2132 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002133
David Brazdil1abb4192015-02-17 18:33:36 +00002134 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002135 // If set, the machine code for this instruction is assumed to be generated by
2136 // its users. Used by liveness analysis to compute use positions accordingly.
2137 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2138 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2139 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2140 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2141
David Brazdil1abb4192015-02-17 18:33:36 +00002142 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2143 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2144
Vladimir Markoa1de9182016-02-25 11:37:38 +00002145 uint32_t GetPackedFields() const {
2146 return packed_fields_;
2147 }
2148
2149 template <size_t flag>
2150 bool GetPackedFlag() const {
2151 return (packed_fields_ & (1u << flag)) != 0u;
2152 }
2153
2154 template <size_t flag>
2155 void SetPackedFlag(bool value = true) {
2156 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2157 }
2158
2159 template <typename BitFieldType>
2160 typename BitFieldType::value_type GetPackedField() const {
2161 return BitFieldType::Decode(packed_fields_);
2162 }
2163
2164 template <typename BitFieldType>
2165 void SetPackedField(typename BitFieldType::value_type value) {
2166 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2167 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2168 }
2169
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002170 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002171 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2172
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002173 HInstruction* previous_;
2174 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002175 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002176 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002177
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002178 // An instruction gets an id when it is added to the graph.
2179 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002180 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002181 int id_;
2182
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002183 // When doing liveness analysis, instructions that have uses get an SSA index.
2184 int ssa_index_;
2185
Vladimir Markoa1de9182016-02-25 11:37:38 +00002186 // Packed fields.
2187 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002188
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002189 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002190 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002191
2192 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002193 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002194
Nicolas Geoffray39468442014-09-02 15:17:15 +01002195 // The environment associated with this instruction. Not null if the instruction
2196 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002197 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002198
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002199 // Set by the code generator.
2200 LocationSummary* locations_;
2201
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002202 // Set by the liveness analysis.
2203 LiveInterval* live_interval_;
2204
2205 // Set by the liveness analysis, this is the position in a linear
2206 // order of blocks where this instruction's live interval start.
2207 size_t lifetime_position_;
2208
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002209 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002210
Vladimir Markoa1de9182016-02-25 11:37:38 +00002211 // The reference handle part of the reference type info.
2212 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002213 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002214 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002215
David Brazdil1abb4192015-02-17 18:33:36 +00002216 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002217 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002218 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002219 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002220 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002221
2222 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2223};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002224std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002225
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002226class HInputIterator : public ValueObject {
2227 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002228 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002229
2230 bool Done() const { return index_ == instruction_->InputCount(); }
2231 HInstruction* Current() const { return instruction_->InputAt(index_); }
2232 void Advance() { index_++; }
2233
2234 private:
2235 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002236 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002237
2238 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2239};
2240
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002241class HInstructionIterator : public ValueObject {
2242 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002243 explicit HInstructionIterator(const HInstructionList& instructions)
2244 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002245 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002246 }
2247
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002248 bool Done() const { return instruction_ == nullptr; }
2249 HInstruction* Current() const { return instruction_; }
2250 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002251 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002252 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002253 }
2254
2255 private:
2256 HInstruction* instruction_;
2257 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002258
2259 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002260};
2261
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002262class HBackwardInstructionIterator : public ValueObject {
2263 public:
2264 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2265 : instruction_(instructions.last_instruction_) {
2266 next_ = Done() ? nullptr : instruction_->GetPrevious();
2267 }
2268
2269 bool Done() const { return instruction_ == nullptr; }
2270 HInstruction* Current() const { return instruction_; }
2271 void Advance() {
2272 instruction_ = next_;
2273 next_ = Done() ? nullptr : instruction_->GetPrevious();
2274 }
2275
2276 private:
2277 HInstruction* instruction_;
2278 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002279
2280 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002281};
2282
Vladimir Markof9f64412015-09-02 14:05:49 +01002283template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002284class HTemplateInstruction: public HInstruction {
2285 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002286 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002287 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002288 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002289
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002290 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002291
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002292 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002293 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2294 DCHECK_LT(i, N);
2295 return inputs_[i];
2296 }
David Brazdil1abb4192015-02-17 18:33:36 +00002297
2298 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002299 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002300 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002301 }
2302
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002303 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002304 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002305
2306 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002307};
2308
Vladimir Markof9f64412015-09-02 14:05:49 +01002309// HTemplateInstruction specialization for N=0.
2310template<>
2311class HTemplateInstruction<0>: public HInstruction {
2312 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002313 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002314 : HInstruction(side_effects, dex_pc) {}
2315
Vladimir Markof9f64412015-09-02 14:05:49 +01002316 virtual ~HTemplateInstruction() {}
2317
2318 size_t InputCount() const OVERRIDE { return 0; }
2319
2320 protected:
2321 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2322 LOG(FATAL) << "Unreachable";
2323 UNREACHABLE();
2324 }
2325
2326 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2327 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2328 LOG(FATAL) << "Unreachable";
2329 UNREACHABLE();
2330 }
2331
2332 private:
2333 friend class SsaBuilder;
2334};
2335
Dave Allison20dfc792014-06-16 20:44:29 -07002336template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002337class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002338 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002339 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002340 : HTemplateInstruction<N>(side_effects, dex_pc) {
2341 this->template SetPackedField<TypeField>(type);
2342 }
Dave Allison20dfc792014-06-16 20:44:29 -07002343 virtual ~HExpression() {}
2344
Vladimir Markoa1de9182016-02-25 11:37:38 +00002345 Primitive::Type GetType() const OVERRIDE {
2346 return TypeField::Decode(this->GetPackedFields());
2347 }
Dave Allison20dfc792014-06-16 20:44:29 -07002348
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002349 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002350 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2351 static constexpr size_t kFieldTypeSize =
2352 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2353 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2354 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2355 "Too many packed fields.");
2356 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002357};
2358
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002359// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2360// instruction that branches to the exit block.
2361class HReturnVoid : public HTemplateInstruction<0> {
2362 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002363 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2364 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002365
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002366 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002367
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002368 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002369
2370 private:
2371 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2372};
2373
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002374// Represents dex's RETURN opcodes. A HReturn is a control flow
2375// instruction that branches to the exit block.
2376class HReturn : public HTemplateInstruction<1> {
2377 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002378 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2379 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002380 SetRawInputAt(0, value);
2381 }
2382
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002383 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002384
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002385 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002386
2387 private:
2388 DISALLOW_COPY_AND_ASSIGN(HReturn);
2389};
2390
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002391// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002392// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002393// exit block.
2394class HExit : public HTemplateInstruction<0> {
2395 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002396 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002397
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002398 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002399
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002400 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002401
2402 private:
2403 DISALLOW_COPY_AND_ASSIGN(HExit);
2404};
2405
2406// Jumps from one block to another.
2407class HGoto : public HTemplateInstruction<0> {
2408 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002409 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002410
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002411 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002412
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002413 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002414 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002415 }
2416
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002417 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002418
2419 private:
2420 DISALLOW_COPY_AND_ASSIGN(HGoto);
2421};
2422
Roland Levillain9867bc72015-08-05 10:21:34 +01002423class HConstant : public HExpression<0> {
2424 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002425 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2426 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002427
2428 bool CanBeMoved() const OVERRIDE { return true; }
2429
2430 virtual bool IsMinusOne() const { return false; }
2431 virtual bool IsZero() const { return false; }
2432 virtual bool IsOne() const { return false; }
2433
David Brazdil77a48ae2015-09-15 12:34:04 +00002434 virtual uint64_t GetValueAsUint64() const = 0;
2435
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002436 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002437
2438 private:
2439 DISALLOW_COPY_AND_ASSIGN(HConstant);
2440};
2441
2442class HNullConstant : public HConstant {
2443 public:
2444 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2445 return true;
2446 }
2447
David Brazdil77a48ae2015-09-15 12:34:04 +00002448 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2449
Roland Levillain9867bc72015-08-05 10:21:34 +01002450 size_t ComputeHashCode() const OVERRIDE { return 0; }
2451
2452 DECLARE_INSTRUCTION(NullConstant);
2453
2454 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002455 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002456
2457 friend class HGraph;
2458 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2459};
2460
2461// Constants of the type int. Those can be from Dex instructions, or
2462// synthesized (for example with the if-eqz instruction).
2463class HIntConstant : public HConstant {
2464 public:
2465 int32_t GetValue() const { return value_; }
2466
David Brazdil9f389d42015-10-01 14:32:56 +01002467 uint64_t GetValueAsUint64() const OVERRIDE {
2468 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2469 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002470
Roland Levillain9867bc72015-08-05 10:21:34 +01002471 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002472 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002473 return other->AsIntConstant()->value_ == value_;
2474 }
2475
2476 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2477
2478 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2479 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2480 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2481
2482 DECLARE_INSTRUCTION(IntConstant);
2483
2484 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002485 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2486 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2487 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2488 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002489
2490 const int32_t value_;
2491
2492 friend class HGraph;
2493 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2494 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2495 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2496};
2497
2498class HLongConstant : public HConstant {
2499 public:
2500 int64_t GetValue() const { return value_; }
2501
David Brazdil77a48ae2015-09-15 12:34:04 +00002502 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2503
Roland Levillain9867bc72015-08-05 10:21:34 +01002504 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002505 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002506 return other->AsLongConstant()->value_ == value_;
2507 }
2508
2509 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2510
2511 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2512 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2513 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2514
2515 DECLARE_INSTRUCTION(LongConstant);
2516
2517 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002518 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2519 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002520
2521 const int64_t value_;
2522
2523 friend class HGraph;
2524 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2525};
Dave Allison20dfc792014-06-16 20:44:29 -07002526
Roland Levillain31dd3d62016-02-16 12:21:02 +00002527class HFloatConstant : public HConstant {
2528 public:
2529 float GetValue() const { return value_; }
2530
2531 uint64_t GetValueAsUint64() const OVERRIDE {
2532 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2533 }
2534
2535 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2536 DCHECK(other->IsFloatConstant()) << other->DebugName();
2537 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2538 }
2539
2540 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2541
2542 bool IsMinusOne() const OVERRIDE {
2543 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2544 }
2545 bool IsZero() const OVERRIDE {
2546 return value_ == 0.0f;
2547 }
2548 bool IsOne() const OVERRIDE {
2549 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2550 }
2551 bool IsNaN() const {
2552 return std::isnan(value_);
2553 }
2554
2555 DECLARE_INSTRUCTION(FloatConstant);
2556
2557 private:
2558 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2559 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2560 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2561 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2562
2563 const float value_;
2564
2565 // Only the SsaBuilder and HGraph can create floating-point constants.
2566 friend class SsaBuilder;
2567 friend class HGraph;
2568 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2569};
2570
2571class HDoubleConstant : public HConstant {
2572 public:
2573 double GetValue() const { return value_; }
2574
2575 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2576
2577 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2578 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2579 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2580 }
2581
2582 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2583
2584 bool IsMinusOne() const OVERRIDE {
2585 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2586 }
2587 bool IsZero() const OVERRIDE {
2588 return value_ == 0.0;
2589 }
2590 bool IsOne() const OVERRIDE {
2591 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2592 }
2593 bool IsNaN() const {
2594 return std::isnan(value_);
2595 }
2596
2597 DECLARE_INSTRUCTION(DoubleConstant);
2598
2599 private:
2600 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2601 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2602 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2603 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2604
2605 const double value_;
2606
2607 // Only the SsaBuilder and HGraph can create floating-point constants.
2608 friend class SsaBuilder;
2609 friend class HGraph;
2610 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2611};
2612
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002613// Conditional branch. A block ending with an HIf instruction must have
2614// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002615class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002616 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002617 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2618 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002619 SetRawInputAt(0, input);
2620 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002621
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002622 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002623
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002624 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002625 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002626 }
2627
2628 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002629 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002630 }
2631
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002632 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002633
2634 private:
2635 DISALLOW_COPY_AND_ASSIGN(HIf);
2636};
2637
David Brazdilfc6a86a2015-06-26 10:33:45 +00002638
2639// Abstract instruction which marks the beginning and/or end of a try block and
2640// links it to the respective exception handlers. Behaves the same as a Goto in
2641// non-exceptional control flow.
2642// Normal-flow successor is stored at index zero, exception handlers under
2643// higher indices in no particular order.
2644class HTryBoundary : public HTemplateInstruction<0> {
2645 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002646 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002647 kEntry,
2648 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002649 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002650 };
2651
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002652 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002653 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2654 SetPackedField<BoundaryKindField>(kind);
2655 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002656
2657 bool IsControlFlow() const OVERRIDE { return true; }
2658
2659 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002660 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002661
David Brazdild26a4112015-11-10 11:07:31 +00002662 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2663 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2664 }
2665
David Brazdilfc6a86a2015-06-26 10:33:45 +00002666 // Returns whether `handler` is among its exception handlers (non-zero index
2667 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002668 bool HasExceptionHandler(const HBasicBlock& handler) const {
2669 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002670 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002671 }
2672
2673 // If not present already, adds `handler` to its block's list of exception
2674 // handlers.
2675 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002676 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002677 GetBlock()->AddSuccessor(handler);
2678 }
2679 }
2680
Vladimir Markoa1de9182016-02-25 11:37:38 +00002681 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2682 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002683
David Brazdilffee3d32015-07-06 11:48:53 +01002684 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2685
David Brazdilfc6a86a2015-06-26 10:33:45 +00002686 DECLARE_INSTRUCTION(TryBoundary);
2687
2688 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002689 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2690 static constexpr size_t kFieldBoundaryKindSize =
2691 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2692 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2693 kFieldBoundaryKind + kFieldBoundaryKindSize;
2694 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2695 "Too many packed fields.");
2696 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002697
2698 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2699};
2700
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002701// Deoptimize to interpreter, upon checking a condition.
2702class HDeoptimize : public HTemplateInstruction<1> {
2703 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002704 // We set CanTriggerGC to prevent any intermediate address to be live
2705 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002706 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002707 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002708 SetRawInputAt(0, cond);
2709 }
2710
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002711 bool CanBeMoved() const OVERRIDE { return true; }
2712 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2713 return true;
2714 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002715 bool NeedsEnvironment() const OVERRIDE { return true; }
2716 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002717
2718 DECLARE_INSTRUCTION(Deoptimize);
2719
2720 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002721 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2722};
2723
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002724// Represents the ArtMethod that was passed as a first argument to
2725// the method. It is used by instructions that depend on it, like
2726// instructions that work with the dex cache.
2727class HCurrentMethod : public HExpression<0> {
2728 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002729 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2730 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002731
2732 DECLARE_INSTRUCTION(CurrentMethod);
2733
2734 private:
2735 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2736};
2737
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002738// Fetches an ArtMethod from the virtual table or the interface method table
2739// of a class.
2740class HClassTableGet : public HExpression<1> {
2741 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002742 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002743 kVTable,
2744 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002745 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002746 };
2747 HClassTableGet(HInstruction* cls,
2748 Primitive::Type type,
2749 TableKind kind,
2750 size_t index,
2751 uint32_t dex_pc)
2752 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002753 index_(index) {
2754 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002755 SetRawInputAt(0, cls);
2756 }
2757
2758 bool CanBeMoved() const OVERRIDE { return true; }
2759 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2760 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002761 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002762 }
2763
Vladimir Markoa1de9182016-02-25 11:37:38 +00002764 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002765 size_t GetIndex() const { return index_; }
2766
2767 DECLARE_INSTRUCTION(ClassTableGet);
2768
2769 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002770 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2771 static constexpr size_t kFieldTableKindSize =
2772 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2773 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2774 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2775 "Too many packed fields.");
2776 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2777
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002778 // The index of the ArtMethod in the table.
2779 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002780
2781 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2782};
2783
Mark Mendellfe57faa2015-09-18 09:26:15 -04002784// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2785// have one successor for each entry in the switch table, and the final successor
2786// will be the block containing the next Dex opcode.
2787class HPackedSwitch : public HTemplateInstruction<1> {
2788 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002789 HPackedSwitch(int32_t start_value,
2790 uint32_t num_entries,
2791 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002792 uint32_t dex_pc = kNoDexPc)
2793 : HTemplateInstruction(SideEffects::None(), dex_pc),
2794 start_value_(start_value),
2795 num_entries_(num_entries) {
2796 SetRawInputAt(0, input);
2797 }
2798
2799 bool IsControlFlow() const OVERRIDE { return true; }
2800
2801 int32_t GetStartValue() const { return start_value_; }
2802
Vladimir Marko211c2112015-09-24 16:52:33 +01002803 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002804
2805 HBasicBlock* GetDefaultBlock() const {
2806 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002807 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002808 }
2809 DECLARE_INSTRUCTION(PackedSwitch);
2810
2811 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002812 const int32_t start_value_;
2813 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002814
2815 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2816};
2817
Roland Levillain88cb1752014-10-20 16:36:47 +01002818class HUnaryOperation : public HExpression<1> {
2819 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002820 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2821 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002822 SetRawInputAt(0, input);
2823 }
2824
2825 HInstruction* GetInput() const { return InputAt(0); }
2826 Primitive::Type GetResultType() const { return GetType(); }
2827
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002828 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002829 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002830 return true;
2831 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002832
Roland Levillain31dd3d62016-02-16 12:21:02 +00002833 // Try to statically evaluate `this` and return a HConstant
2834 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002835 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002836 HConstant* TryStaticEvaluation() const;
2837
2838 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002839 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2840 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002841 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2842 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002843
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002844 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002845
2846 private:
2847 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2848};
2849
Dave Allison20dfc792014-06-16 20:44:29 -07002850class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002851 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002852 HBinaryOperation(Primitive::Type result_type,
2853 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002854 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002855 SideEffects side_effects = SideEffects::None(),
2856 uint32_t dex_pc = kNoDexPc)
2857 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002858 SetRawInputAt(0, left);
2859 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002860 }
2861
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002862 HInstruction* GetLeft() const { return InputAt(0); }
2863 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002864 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002865
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002866 virtual bool IsCommutative() const { return false; }
2867
2868 // Put constant on the right.
2869 // Returns whether order is changed.
2870 bool OrderInputsWithConstantOnTheRight() {
2871 HInstruction* left = InputAt(0);
2872 HInstruction* right = InputAt(1);
2873 if (left->IsConstant() && !right->IsConstant()) {
2874 ReplaceInput(right, 0);
2875 ReplaceInput(left, 1);
2876 return true;
2877 }
2878 return false;
2879 }
2880
2881 // Order inputs by instruction id, but favor constant on the right side.
2882 // This helps GVN for commutative ops.
2883 void OrderInputs() {
2884 DCHECK(IsCommutative());
2885 HInstruction* left = InputAt(0);
2886 HInstruction* right = InputAt(1);
2887 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2888 return;
2889 }
2890 if (OrderInputsWithConstantOnTheRight()) {
2891 return;
2892 }
2893 // Order according to instruction id.
2894 if (left->GetId() > right->GetId()) {
2895 ReplaceInput(right, 0);
2896 ReplaceInput(left, 1);
2897 }
2898 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002899
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002900 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002901 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002902 return true;
2903 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002904
Roland Levillain31dd3d62016-02-16 12:21:02 +00002905 // Try to statically evaluate `this` and return a HConstant
2906 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002907 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002908 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002909
2910 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002911 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2912 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002913 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2914 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002915 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002916 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2917 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002918 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2919 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002920 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2921 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01002922 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002923 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2924 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002925
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002926 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002927 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002928 HConstant* GetConstantRight() const;
2929
2930 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002931 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002932 HInstruction* GetLeastConstantLeft() const;
2933
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002934 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002935
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002936 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002937 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2938};
2939
Mark Mendellc4701932015-04-10 13:18:51 -04002940// The comparison bias applies for floating point operations and indicates how NaN
2941// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002942enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002943 kNoBias, // bias is not applicable (i.e. for long operation)
2944 kGtBias, // return 1 for NaN comparisons
2945 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00002946 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04002947};
2948
Roland Levillain31dd3d62016-02-16 12:21:02 +00002949std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
2950
Dave Allison20dfc792014-06-16 20:44:29 -07002951class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002952 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002953 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002954 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
2955 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
2956 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002957
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002958 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002959 // `instruction`, and disregard moves in between.
2960 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002961
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002962 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002963
2964 virtual IfCondition GetCondition() const = 0;
2965
Mark Mendellc4701932015-04-10 13:18:51 -04002966 virtual IfCondition GetOppositeCondition() const = 0;
2967
Vladimir Markoa1de9182016-02-25 11:37:38 +00002968 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06002969 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
2970
Vladimir Markoa1de9182016-02-25 11:37:38 +00002971 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
2972 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04002973
2974 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002975 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04002976 }
2977
Roland Levillain4fa13f62015-07-06 18:11:54 +01002978 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002979 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002980 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06002981 if (if_cond == kCondNE) {
2982 return true;
2983 } else if (if_cond == kCondEQ) {
2984 return false;
2985 }
2986 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002987 }
2988
2989 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002990 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002991 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06002992 if (if_cond == kCondEQ) {
2993 return true;
2994 } else if (if_cond == kCondNE) {
2995 return false;
2996 }
2997 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002998 }
2999
Roland Levillain31dd3d62016-02-16 12:21:02 +00003000 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003001 // Needed if we merge a HCompare into a HCondition.
3002 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3003 static constexpr size_t kFieldComparisonBiasSize =
3004 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3005 static constexpr size_t kNumberOfConditionPackedBits =
3006 kFieldComparisonBias + kFieldComparisonBiasSize;
3007 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3008 using ComparisonBiasField =
3009 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3010
Roland Levillain31dd3d62016-02-16 12:21:02 +00003011 template <typename T>
3012 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3013
3014 template <typename T>
3015 int32_t CompareFP(T x, T y) const {
3016 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3017 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3018 // Handle the bias.
3019 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3020 }
3021
3022 // Return an integer constant containing the result of a condition evaluated at compile time.
3023 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3024 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3025 }
3026
Dave Allison20dfc792014-06-16 20:44:29 -07003027 private:
3028 DISALLOW_COPY_AND_ASSIGN(HCondition);
3029};
3030
3031// Instruction to check if two inputs are equal to each other.
3032class HEqual : public HCondition {
3033 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003034 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3035 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003036
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003037 bool IsCommutative() const OVERRIDE { return true; }
3038
Vladimir Marko9e23df52015-11-10 17:14:35 +00003039 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3040 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003041 return MakeConstantCondition(true, GetDexPc());
3042 }
3043 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3044 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3045 }
3046 // In the following Evaluate methods, a HCompare instruction has
3047 // been merged into this HEqual instruction; evaluate it as
3048 // `Compare(x, y) == 0`.
3049 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3050 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3051 GetDexPc());
3052 }
3053 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3054 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3055 }
3056 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3057 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003058 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003059
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003060 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003061
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003062 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003063 return kCondEQ;
3064 }
3065
Mark Mendellc4701932015-04-10 13:18:51 -04003066 IfCondition GetOppositeCondition() const OVERRIDE {
3067 return kCondNE;
3068 }
3069
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003070 private:
Aart Bike9f37602015-10-09 11:15:55 -07003071 template <typename T> bool Compute(T x, T y) const { return x == y; }
3072
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003073 DISALLOW_COPY_AND_ASSIGN(HEqual);
3074};
3075
Dave Allison20dfc792014-06-16 20:44:29 -07003076class HNotEqual : public HCondition {
3077 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003078 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3079 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003080
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003081 bool IsCommutative() const OVERRIDE { return true; }
3082
Vladimir Marko9e23df52015-11-10 17:14:35 +00003083 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3084 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003085 return MakeConstantCondition(false, GetDexPc());
3086 }
3087 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3088 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3089 }
3090 // In the following Evaluate methods, a HCompare instruction has
3091 // been merged into this HNotEqual instruction; evaluate it as
3092 // `Compare(x, y) != 0`.
3093 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3094 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3095 }
3096 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3097 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3098 }
3099 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3100 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003101 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003102
Dave Allison20dfc792014-06-16 20:44:29 -07003103 DECLARE_INSTRUCTION(NotEqual);
3104
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003105 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003106 return kCondNE;
3107 }
3108
Mark Mendellc4701932015-04-10 13:18:51 -04003109 IfCondition GetOppositeCondition() const OVERRIDE {
3110 return kCondEQ;
3111 }
3112
Dave Allison20dfc792014-06-16 20:44:29 -07003113 private:
Aart Bike9f37602015-10-09 11:15:55 -07003114 template <typename T> bool Compute(T x, T y) const { return x != y; }
3115
Dave Allison20dfc792014-06-16 20:44:29 -07003116 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3117};
3118
3119class HLessThan : public HCondition {
3120 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003121 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3122 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003123
Roland Levillain9867bc72015-08-05 10:21:34 +01003124 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003125 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003126 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003127 // In the following Evaluate methods, a HCompare instruction has
3128 // been merged into this HLessThan instruction; evaluate it as
3129 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003130 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003131 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3132 }
3133 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3134 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3135 }
3136 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3137 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003138 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003139
Dave Allison20dfc792014-06-16 20:44:29 -07003140 DECLARE_INSTRUCTION(LessThan);
3141
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003142 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003143 return kCondLT;
3144 }
3145
Mark Mendellc4701932015-04-10 13:18:51 -04003146 IfCondition GetOppositeCondition() const OVERRIDE {
3147 return kCondGE;
3148 }
3149
Dave Allison20dfc792014-06-16 20:44:29 -07003150 private:
Aart Bike9f37602015-10-09 11:15:55 -07003151 template <typename T> bool Compute(T x, T y) const { return x < y; }
3152
Dave Allison20dfc792014-06-16 20:44:29 -07003153 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3154};
3155
3156class HLessThanOrEqual : public HCondition {
3157 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003158 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3159 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003160
Roland Levillain9867bc72015-08-05 10:21:34 +01003161 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003162 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003163 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003164 // In the following Evaluate methods, a HCompare instruction has
3165 // been merged into this HLessThanOrEqual instruction; evaluate it as
3166 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003167 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003168 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3169 }
3170 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3171 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3172 }
3173 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3174 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003175 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003176
Dave Allison20dfc792014-06-16 20:44:29 -07003177 DECLARE_INSTRUCTION(LessThanOrEqual);
3178
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003179 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003180 return kCondLE;
3181 }
3182
Mark Mendellc4701932015-04-10 13:18:51 -04003183 IfCondition GetOppositeCondition() const OVERRIDE {
3184 return kCondGT;
3185 }
3186
Dave Allison20dfc792014-06-16 20:44:29 -07003187 private:
Aart Bike9f37602015-10-09 11:15:55 -07003188 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3189
Dave Allison20dfc792014-06-16 20:44:29 -07003190 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3191};
3192
3193class HGreaterThan : public HCondition {
3194 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003195 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3196 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003197
Roland Levillain9867bc72015-08-05 10:21:34 +01003198 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003199 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003200 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003201 // In the following Evaluate methods, a HCompare instruction has
3202 // been merged into this HGreaterThan instruction; evaluate it as
3203 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003204 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003205 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3206 }
3207 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3208 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3209 }
3210 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3211 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003212 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003213
Dave Allison20dfc792014-06-16 20:44:29 -07003214 DECLARE_INSTRUCTION(GreaterThan);
3215
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003216 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003217 return kCondGT;
3218 }
3219
Mark Mendellc4701932015-04-10 13:18:51 -04003220 IfCondition GetOppositeCondition() const OVERRIDE {
3221 return kCondLE;
3222 }
3223
Dave Allison20dfc792014-06-16 20:44:29 -07003224 private:
Aart Bike9f37602015-10-09 11:15:55 -07003225 template <typename T> bool Compute(T x, T y) const { return x > y; }
3226
Dave Allison20dfc792014-06-16 20:44:29 -07003227 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3228};
3229
3230class HGreaterThanOrEqual : public HCondition {
3231 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003232 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3233 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003234
Roland Levillain9867bc72015-08-05 10:21:34 +01003235 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003236 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003237 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003238 // In the following Evaluate methods, a HCompare instruction has
3239 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3240 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003241 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003242 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3243 }
3244 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3245 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3246 }
3247 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3248 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003249 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003250
Dave Allison20dfc792014-06-16 20:44:29 -07003251 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3252
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003253 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003254 return kCondGE;
3255 }
3256
Mark Mendellc4701932015-04-10 13:18:51 -04003257 IfCondition GetOppositeCondition() const OVERRIDE {
3258 return kCondLT;
3259 }
3260
Dave Allison20dfc792014-06-16 20:44:29 -07003261 private:
Aart Bike9f37602015-10-09 11:15:55 -07003262 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3263
Dave Allison20dfc792014-06-16 20:44:29 -07003264 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3265};
3266
Aart Bike9f37602015-10-09 11:15:55 -07003267class HBelow : public HCondition {
3268 public:
3269 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3270 : HCondition(first, second, dex_pc) {}
3271
3272 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003273 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003274 }
3275 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003276 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3277 }
3278 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3279 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3280 LOG(FATAL) << DebugName() << " is not defined for float values";
3281 UNREACHABLE();
3282 }
3283 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3284 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3285 LOG(FATAL) << DebugName() << " is not defined for double values";
3286 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003287 }
3288
3289 DECLARE_INSTRUCTION(Below);
3290
3291 IfCondition GetCondition() const OVERRIDE {
3292 return kCondB;
3293 }
3294
3295 IfCondition GetOppositeCondition() const OVERRIDE {
3296 return kCondAE;
3297 }
3298
3299 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003300 template <typename T> bool Compute(T x, T y) const {
3301 return MakeUnsigned(x) < MakeUnsigned(y);
3302 }
Aart Bike9f37602015-10-09 11:15:55 -07003303
3304 DISALLOW_COPY_AND_ASSIGN(HBelow);
3305};
3306
3307class HBelowOrEqual : public HCondition {
3308 public:
3309 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3310 : HCondition(first, second, dex_pc) {}
3311
3312 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003313 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003314 }
3315 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003316 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3317 }
3318 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3319 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3320 LOG(FATAL) << DebugName() << " is not defined for float values";
3321 UNREACHABLE();
3322 }
3323 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3324 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3325 LOG(FATAL) << DebugName() << " is not defined for double values";
3326 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003327 }
3328
3329 DECLARE_INSTRUCTION(BelowOrEqual);
3330
3331 IfCondition GetCondition() const OVERRIDE {
3332 return kCondBE;
3333 }
3334
3335 IfCondition GetOppositeCondition() const OVERRIDE {
3336 return kCondA;
3337 }
3338
3339 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003340 template <typename T> bool Compute(T x, T y) const {
3341 return MakeUnsigned(x) <= MakeUnsigned(y);
3342 }
Aart Bike9f37602015-10-09 11:15:55 -07003343
3344 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3345};
3346
3347class HAbove : public HCondition {
3348 public:
3349 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3350 : HCondition(first, second, dex_pc) {}
3351
3352 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003353 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003354 }
3355 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003356 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3357 }
3358 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3359 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3360 LOG(FATAL) << DebugName() << " is not defined for float values";
3361 UNREACHABLE();
3362 }
3363 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3364 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3365 LOG(FATAL) << DebugName() << " is not defined for double values";
3366 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003367 }
3368
3369 DECLARE_INSTRUCTION(Above);
3370
3371 IfCondition GetCondition() const OVERRIDE {
3372 return kCondA;
3373 }
3374
3375 IfCondition GetOppositeCondition() const OVERRIDE {
3376 return kCondBE;
3377 }
3378
3379 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003380 template <typename T> bool Compute(T x, T y) const {
3381 return MakeUnsigned(x) > MakeUnsigned(y);
3382 }
Aart Bike9f37602015-10-09 11:15:55 -07003383
3384 DISALLOW_COPY_AND_ASSIGN(HAbove);
3385};
3386
3387class HAboveOrEqual : public HCondition {
3388 public:
3389 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3390 : HCondition(first, second, dex_pc) {}
3391
3392 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003393 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003394 }
3395 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003396 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3397 }
3398 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3399 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3400 LOG(FATAL) << DebugName() << " is not defined for float values";
3401 UNREACHABLE();
3402 }
3403 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3404 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3405 LOG(FATAL) << DebugName() << " is not defined for double values";
3406 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003407 }
3408
3409 DECLARE_INSTRUCTION(AboveOrEqual);
3410
3411 IfCondition GetCondition() const OVERRIDE {
3412 return kCondAE;
3413 }
3414
3415 IfCondition GetOppositeCondition() const OVERRIDE {
3416 return kCondB;
3417 }
3418
3419 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003420 template <typename T> bool Compute(T x, T y) const {
3421 return MakeUnsigned(x) >= MakeUnsigned(y);
3422 }
Aart Bike9f37602015-10-09 11:15:55 -07003423
3424 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3425};
Dave Allison20dfc792014-06-16 20:44:29 -07003426
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003427// Instruction to check how two inputs compare to each other.
3428// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3429class HCompare : public HBinaryOperation {
3430 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003431 HCompare(Primitive::Type type,
3432 HInstruction* first,
3433 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003434 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003435 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003436 : HBinaryOperation(Primitive::kPrimInt,
3437 first,
3438 second,
3439 SideEffectsForArchRuntimeCalls(type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003440 dex_pc) {
3441 SetPackedField<ComparisonBiasField>(bias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003442 DCHECK_EQ(type, first->GetType());
3443 DCHECK_EQ(type, second->GetType());
3444 }
3445
Roland Levillain9867bc72015-08-05 10:21:34 +01003446 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003447 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3448
3449 template <typename T>
3450 int32_t ComputeFP(T x, T y) const {
3451 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3452 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3453 // Handle the bias.
3454 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3455 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003456
Roland Levillain9867bc72015-08-05 10:21:34 +01003457 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003458 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3459 // reach this code path when processing a freshly built HIR
3460 // graph. However HCompare integer instructions can be synthesized
3461 // by the instruction simplifier to implement IntegerCompare and
3462 // IntegerSignum intrinsics, so we have to handle this case.
3463 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003464 }
3465 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003466 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3467 }
3468 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3469 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3470 }
3471 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3472 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003473 }
3474
Calin Juravleddb7df22014-11-25 20:56:51 +00003475 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003476 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003477 }
3478
Vladimir Markoa1de9182016-02-25 11:37:38 +00003479 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003480
Roland Levillain31dd3d62016-02-16 12:21:02 +00003481 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003482 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003483 bool IsGtBias() const {
3484 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003485 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003486 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003487
3488 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3489 // MIPS64 uses a runtime call for FP comparisons.
3490 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3491 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003492
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003493 DECLARE_INSTRUCTION(Compare);
3494
Roland Levillain31dd3d62016-02-16 12:21:02 +00003495 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003496 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3497 static constexpr size_t kFieldComparisonBiasSize =
3498 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3499 static constexpr size_t kNumberOfComparePackedBits =
3500 kFieldComparisonBias + kFieldComparisonBiasSize;
3501 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3502 using ComparisonBiasField =
3503 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3504
Roland Levillain31dd3d62016-02-16 12:21:02 +00003505 // Return an integer constant containing the result of a comparison evaluated at compile time.
3506 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3507 DCHECK(value == -1 || value == 0 || value == 1) << value;
3508 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3509 }
3510
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003511 private:
3512 DISALLOW_COPY_AND_ASSIGN(HCompare);
3513};
3514
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003515// A local in the graph. Corresponds to a Dex register.
3516class HLocal : public HTemplateInstruction<0> {
3517 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003518 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003519 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003520
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003521 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003522
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003523 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003524
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003525 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003526 // The Dex register number.
3527 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003528
3529 DISALLOW_COPY_AND_ASSIGN(HLocal);
3530};
3531
3532// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003533class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003534 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003535 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3536 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003537 SetRawInputAt(0, local);
3538 }
3539
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003540 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3541
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003542 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003543
3544 private:
3545 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3546};
3547
3548// Store a value in a given local. This instruction has two inputs: the value
3549// and the local.
3550class HStoreLocal : public HTemplateInstruction<2> {
3551 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003552 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3553 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003554 SetRawInputAt(0, local);
3555 SetRawInputAt(1, value);
3556 }
3557
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003558 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3559
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003560 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003561
3562 private:
3563 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3564};
3565
David Brazdil6de19382016-01-08 17:37:10 +00003566class HNewInstance : public HExpression<2> {
3567 public:
3568 HNewInstance(HInstruction* cls,
3569 HCurrentMethod* current_method,
3570 uint32_t dex_pc,
3571 uint16_t type_index,
3572 const DexFile& dex_file,
3573 bool can_throw,
3574 bool finalizable,
3575 QuickEntrypointEnum entrypoint)
3576 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3577 type_index_(type_index),
3578 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003579 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003580 SetPackedFlag<kFlagCanThrow>(can_throw);
3581 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003582 SetRawInputAt(0, cls);
3583 SetRawInputAt(1, current_method);
3584 }
3585
3586 uint16_t GetTypeIndex() const { return type_index_; }
3587 const DexFile& GetDexFile() const { return dex_file_; }
3588
3589 // Calls runtime so needs an environment.
3590 bool NeedsEnvironment() const OVERRIDE { return true; }
3591
3592 // It may throw when called on type that's not instantiable/accessible.
3593 // It can throw OOME.
3594 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003595 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>() || true; }
David Brazdil6de19382016-01-08 17:37:10 +00003596
Vladimir Markoa1de9182016-02-25 11:37:38 +00003597 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003598
3599 bool CanBeNull() const OVERRIDE { return false; }
3600
3601 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3602
3603 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3604 entrypoint_ = entrypoint;
3605 }
3606
3607 bool IsStringAlloc() const;
3608
3609 DECLARE_INSTRUCTION(NewInstance);
3610
3611 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003612 static constexpr size_t kFlagCanThrow = kNumberOfExpressionPackedBits;
3613 static constexpr size_t kFlagFinalizable = kFlagCanThrow + 1;
3614 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3615 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3616 "Too many packed fields.");
3617
David Brazdil6de19382016-01-08 17:37:10 +00003618 const uint16_t type_index_;
3619 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003620 QuickEntrypointEnum entrypoint_;
3621
3622 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3623};
3624
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003625enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003626#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3627 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003628#include "intrinsics_list.h"
3629 kNone,
3630 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3631#undef INTRINSICS_LIST
3632#undef OPTIMIZING_INTRINSICS
3633};
3634std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3635
Agi Csaki05f20562015-08-19 14:58:14 -07003636enum IntrinsicNeedsEnvironmentOrCache {
3637 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3638 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003639};
3640
Aart Bik5d75afe2015-12-14 11:57:01 -08003641enum IntrinsicSideEffects {
3642 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3643 kReadSideEffects, // Intrinsic may read heap memory.
3644 kWriteSideEffects, // Intrinsic may write heap memory.
3645 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3646};
3647
3648enum IntrinsicExceptions {
3649 kNoThrow, // Intrinsic does not throw any exceptions.
3650 kCanThrow // Intrinsic may throw exceptions.
3651};
3652
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003653class HInvoke : public HInstruction {
3654 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003655 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003656
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003657 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003658
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003659 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003660 SetRawInputAt(index, argument);
3661 }
3662
Roland Levillain3e3d7332015-04-28 11:00:54 +01003663 // Return the number of arguments. This number can be lower than
3664 // the number of inputs returned by InputCount(), as some invoke
3665 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3666 // inputs at the end of their list of inputs.
3667 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3668
Vladimir Markoa1de9182016-02-25 11:37:38 +00003669 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003670
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003671 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003672 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003673
Vladimir Markoa1de9182016-02-25 11:37:38 +00003674 InvokeType GetOriginalInvokeType() const {
3675 return GetPackedField<OriginalInvokeTypeField>();
3676 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003677
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003678 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003679 return intrinsic_;
3680 }
3681
Aart Bik5d75afe2015-12-14 11:57:01 -08003682 void SetIntrinsic(Intrinsics intrinsic,
3683 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3684 IntrinsicSideEffects side_effects,
3685 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003686
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003687 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003688 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003689 }
3690
Vladimir Markoa1de9182016-02-25 11:37:38 +00003691 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003692
3693 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3694
3695 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3696 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3697 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003698
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003699 uint32_t* GetIntrinsicOptimizations() {
3700 return &intrinsic_optimizations_;
3701 }
3702
3703 const uint32_t* GetIntrinsicOptimizations() const {
3704 return &intrinsic_optimizations_;
3705 }
3706
3707 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3708
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003709 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003710
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003711 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003712 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3713 static constexpr size_t kFieldOriginalInvokeTypeSize =
3714 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3715 static constexpr size_t kFieldReturnType =
3716 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3717 static constexpr size_t kFieldReturnTypeSize =
3718 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3719 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3720 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3721 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3722 using OriginalInvokeTypeField =
3723 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3724 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3725
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003726 HInvoke(ArenaAllocator* arena,
3727 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003728 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003729 Primitive::Type return_type,
3730 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003731 uint32_t dex_method_index,
3732 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003733 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003734 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003735 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003736 inputs_(number_of_arguments + number_of_other_inputs,
3737 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003738 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003739 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003740 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003741 SetPackedField<ReturnTypeField>(return_type);
3742 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3743 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003744 }
3745
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003746 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003747 return inputs_[index];
3748 }
3749
David Brazdil1abb4192015-02-17 18:33:36 +00003750 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003751 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003752 }
3753
Vladimir Markoa1de9182016-02-25 11:37:38 +00003754 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003755
Roland Levillain3e3d7332015-04-28 11:00:54 +01003756 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003757 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003758 const uint32_t dex_method_index_;
3759 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003760
3761 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3762 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003763
3764 private:
3765 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3766};
3767
Calin Juravle175dc732015-08-25 15:42:32 +01003768class HInvokeUnresolved : public HInvoke {
3769 public:
3770 HInvokeUnresolved(ArenaAllocator* arena,
3771 uint32_t number_of_arguments,
3772 Primitive::Type return_type,
3773 uint32_t dex_pc,
3774 uint32_t dex_method_index,
3775 InvokeType invoke_type)
3776 : HInvoke(arena,
3777 number_of_arguments,
3778 0u /* number_of_other_inputs */,
3779 return_type,
3780 dex_pc,
3781 dex_method_index,
3782 invoke_type) {
3783 }
3784
3785 DECLARE_INSTRUCTION(InvokeUnresolved);
3786
3787 private:
3788 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3789};
3790
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003791class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003792 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003793 // Requirements of this method call regarding the class
3794 // initialization (clinit) check of its declaring class.
3795 enum class ClinitCheckRequirement {
3796 kNone, // Class already initialized.
3797 kExplicit, // Static call having explicit clinit check as last input.
3798 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003799 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003800 };
3801
Vladimir Marko58155012015-08-19 12:49:41 +00003802 // Determines how to load the target ArtMethod*.
3803 enum class MethodLoadKind {
3804 // Use a String init ArtMethod* loaded from Thread entrypoints.
3805 kStringInit,
3806
3807 // Use the method's own ArtMethod* loaded by the register allocator.
3808 kRecursive,
3809
3810 // Use ArtMethod* at a known address, embed the direct address in the code.
3811 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3812 kDirectAddress,
3813
3814 // Use ArtMethod* at an address that will be known at link time, embed the direct
3815 // address in the code. If the image is relocatable, emit .patch_oat entry.
3816 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3817 // the image relocatable or not.
3818 kDirectAddressWithFixup,
3819
Vladimir Markoa1de9182016-02-25 11:37:38 +00003820 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003821 // Used when we need to use the dex cache, for example for invoke-static that
3822 // may cause class initialization (the entry may point to a resolution method),
3823 // and we know that we can access the dex cache arrays using a PC-relative load.
3824 kDexCachePcRelative,
3825
3826 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3827 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3828 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3829 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3830 kDexCacheViaMethod,
3831 };
3832
3833 // Determines the location of the code pointer.
3834 enum class CodePtrLocation {
3835 // Recursive call, use local PC-relative call instruction.
3836 kCallSelf,
3837
3838 // Use PC-relative call instruction patched at link time.
3839 // Used for calls within an oat file, boot->boot or app->app.
3840 kCallPCRelative,
3841
3842 // Call to a known target address, embed the direct address in code.
3843 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3844 kCallDirect,
3845
3846 // Call to a target address that will be known at link time, embed the direct
3847 // address in code. If the image is relocatable, emit .patch_oat entry.
3848 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3849 // the image relocatable or not.
3850 kCallDirectWithFixup,
3851
3852 // Use code pointer from the ArtMethod*.
3853 // Used when we don't know the target code. This is also the last-resort-kind used when
3854 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3855 kCallArtMethod,
3856 };
3857
3858 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003859 MethodLoadKind method_load_kind;
3860 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003861 // The method load data holds
3862 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3863 // Note that there are multiple string init methods, each having its own offset.
3864 // - the method address for kDirectAddress
3865 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003866 uint64_t method_load_data;
3867 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003868 };
3869
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003870 HInvokeStaticOrDirect(ArenaAllocator* arena,
3871 uint32_t number_of_arguments,
3872 Primitive::Type return_type,
3873 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003874 uint32_t method_index,
3875 MethodReference target_method,
3876 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003877 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003878 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003879 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003880 : HInvoke(arena,
3881 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003882 // There is potentially one extra argument for the HCurrentMethod node, and
3883 // potentially one other if the clinit check is explicit, and potentially
3884 // one other if the method is a string factory.
3885 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3886 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3887 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003888 return_type,
3889 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003890 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003891 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003892 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003893 dispatch_info_(dispatch_info) {
3894 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3895 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3896 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003897
Vladimir Markodc151b22015-10-15 18:02:30 +01003898 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003899 bool had_current_method_input = HasCurrentMethodInput();
3900 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3901
3902 // Using the current method is the default and once we find a better
3903 // method load kind, we should not go back to using the current method.
3904 DCHECK(had_current_method_input || !needs_current_method_input);
3905
3906 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003907 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3908 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003909 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003910 dispatch_info_ = dispatch_info;
3911 }
3912
Vladimir Markoc53c0792015-11-19 15:48:33 +00003913 void AddSpecialInput(HInstruction* input) {
3914 // We allow only one special input.
3915 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3916 DCHECK(InputCount() == GetSpecialInputIndex() ||
3917 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3918 InsertInputAt(GetSpecialInputIndex(), input);
3919 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003920
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003921 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003922 // We access the method via the dex cache so we can't do an implicit null check.
3923 // TODO: for intrinsics we can generate implicit null checks.
3924 return false;
3925 }
3926
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003927 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003928 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003929 }
3930
Vladimir Markoc53c0792015-11-19 15:48:33 +00003931 // Get the index of the special input, if any.
3932 //
David Brazdil6de19382016-01-08 17:37:10 +00003933 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3934 // method pointer; otherwise there may be one platform-specific special input,
3935 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003936 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003937 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003938
Vladimir Markoa1de9182016-02-25 11:37:38 +00003939 InvokeType GetOptimizedInvokeType() const {
3940 return GetPackedField<OptimizedInvokeTypeField>();
3941 }
3942
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003943 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003944 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003945 }
3946
Vladimir Marko58155012015-08-19 12:49:41 +00003947 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3948 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3949 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003950 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003951 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003952 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003953 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003954 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3955 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003956 bool HasCurrentMethodInput() const {
3957 // This function can be called only after the invoke has been fully initialized by the builder.
3958 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003959 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003960 return true;
3961 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003962 DCHECK(InputCount() == GetSpecialInputIndex() ||
3963 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003964 return false;
3965 }
3966 }
Vladimir Marko58155012015-08-19 12:49:41 +00003967 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3968 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003969 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003970
3971 int32_t GetStringInitOffset() const {
3972 DCHECK(IsStringInit());
3973 return dispatch_info_.method_load_data;
3974 }
3975
3976 uint64_t GetMethodAddress() const {
3977 DCHECK(HasMethodAddress());
3978 return dispatch_info_.method_load_data;
3979 }
3980
3981 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003982 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003983 return dispatch_info_.method_load_data;
3984 }
3985
3986 uint64_t GetDirectCodePtr() const {
3987 DCHECK(HasDirectCodePtr());
3988 return dispatch_info_.direct_code_ptr;
3989 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003990
Vladimir Markoa1de9182016-02-25 11:37:38 +00003991 ClinitCheckRequirement GetClinitCheckRequirement() const {
3992 return GetPackedField<ClinitCheckRequirementField>();
3993 }
Calin Juravle68ad6492015-08-18 17:08:12 +01003994
Roland Levillain4c0eb422015-04-24 16:43:49 +01003995 // Is this instruction a call to a static method?
3996 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003997 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003998 }
3999
Vladimir Markofbb184a2015-11-13 14:47:00 +00004000 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4001 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4002 // instruction; only relevant for static calls with explicit clinit check.
4003 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004004 DCHECK(IsStaticWithExplicitClinitCheck());
4005 size_t last_input_index = InputCount() - 1;
4006 HInstruction* last_input = InputAt(last_input_index);
4007 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004008 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004009 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004010 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004011 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004012 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004013 }
4014
David Brazdilbc9ab162016-01-20 14:50:19 +00004015 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00004016 DCHECK(IsStringInit());
4017 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00004018 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00004019 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004020 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00004021 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004022 }
4023
Roland Levillain4c0eb422015-04-24 16:43:49 +01004024 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004025 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004026 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004027 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004028 }
4029
4030 // Is this a call to a static method whose declaring class has an
4031 // implicit intialization check requirement?
4032 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004033 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004034 }
4035
Vladimir Markob554b5a2015-11-06 12:57:55 +00004036 // Does this method load kind need the current method as an input?
4037 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4038 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4039 }
4040
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004041 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004042
Roland Levillain4c0eb422015-04-24 16:43:49 +01004043 protected:
4044 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
4045 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
4046 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
4047 HInstruction* input = input_record.GetInstruction();
4048 // `input` is the last input of a static invoke marked as having
4049 // an explicit clinit check. It must either be:
4050 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4051 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4052 DCHECK(input != nullptr);
4053 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
4054 }
4055 return input_record;
4056 }
4057
Vladimir Markoc53c0792015-11-19 15:48:33 +00004058 void InsertInputAt(size_t index, HInstruction* input);
4059 void RemoveInputAt(size_t index);
4060
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004061 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004062 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4063 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4064 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4065 static constexpr size_t kFieldClinitCheckRequirement =
4066 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4067 static constexpr size_t kFieldClinitCheckRequirementSize =
4068 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4069 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4070 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4071 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4072 "Too many packed fields.");
4073 using OptimizedInvokeTypeField =
4074 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4075 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4076 kFieldClinitCheckRequirement,
4077 kFieldClinitCheckRequirementSize>;
4078
Vladimir Marko58155012015-08-19 12:49:41 +00004079 // The target method may refer to different dex file or method index than the original
4080 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4081 // in derived class to increase visibility.
4082 MethodReference target_method_;
4083 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004084
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004085 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004086};
Vladimir Markof64242a2015-12-01 14:58:23 +00004087std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004088std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004089
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004090class HInvokeVirtual : public HInvoke {
4091 public:
4092 HInvokeVirtual(ArenaAllocator* arena,
4093 uint32_t number_of_arguments,
4094 Primitive::Type return_type,
4095 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004096 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004097 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004098 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004099 vtable_index_(vtable_index) {}
4100
Calin Juravle641547a2015-04-21 22:08:51 +01004101 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004102 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004103 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004104 }
4105
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004106 uint32_t GetVTableIndex() const { return vtable_index_; }
4107
4108 DECLARE_INSTRUCTION(InvokeVirtual);
4109
4110 private:
4111 const uint32_t vtable_index_;
4112
4113 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4114};
4115
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004116class HInvokeInterface : public HInvoke {
4117 public:
4118 HInvokeInterface(ArenaAllocator* arena,
4119 uint32_t number_of_arguments,
4120 Primitive::Type return_type,
4121 uint32_t dex_pc,
4122 uint32_t dex_method_index,
4123 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004124 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004125 imt_index_(imt_index) {}
4126
Calin Juravle641547a2015-04-21 22:08:51 +01004127 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004128 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004129 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004130 }
4131
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004132 uint32_t GetImtIndex() const { return imt_index_; }
4133 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4134
4135 DECLARE_INSTRUCTION(InvokeInterface);
4136
4137 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004138 const uint32_t imt_index_;
4139
4140 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4141};
4142
Roland Levillain88cb1752014-10-20 16:36:47 +01004143class HNeg : public HUnaryOperation {
4144 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004145 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4146 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01004147
Roland Levillain9867bc72015-08-05 10:21:34 +01004148 template <typename T> T Compute(T x) const { return -x; }
4149
4150 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004151 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004152 }
4153 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004154 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004155 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004156 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4157 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4158 }
4159 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4160 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4161 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004162
Roland Levillain88cb1752014-10-20 16:36:47 +01004163 DECLARE_INSTRUCTION(Neg);
4164
4165 private:
4166 DISALLOW_COPY_AND_ASSIGN(HNeg);
4167};
4168
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004169class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004170 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004171 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004172 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004173 uint32_t dex_pc,
4174 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004175 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004176 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004177 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004178 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004179 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004180 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004181 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004182 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004183 }
4184
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004185 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004186 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004187
4188 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004189 bool NeedsEnvironment() const OVERRIDE { return true; }
4190
Mingyao Yang0c365e62015-03-31 15:09:29 -07004191 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4192 bool CanThrow() const OVERRIDE { return true; }
4193
Calin Juravle10e244f2015-01-26 18:54:32 +00004194 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004195
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004196 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4197
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004198 DECLARE_INSTRUCTION(NewArray);
4199
4200 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004201 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004202 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004203 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004204
4205 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4206};
4207
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004208class HAdd : public HBinaryOperation {
4209 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004210 HAdd(Primitive::Type result_type,
4211 HInstruction* left,
4212 HInstruction* right,
4213 uint32_t dex_pc = kNoDexPc)
4214 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004215
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004216 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004217
Roland Levillain9867bc72015-08-05 10:21:34 +01004218 template <typename T> T Compute(T x, T y) const { return x + y; }
4219
4220 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004221 return GetBlock()->GetGraph()->GetIntConstant(
4222 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004223 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004224 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004225 return GetBlock()->GetGraph()->GetLongConstant(
4226 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004227 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004228 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4229 return GetBlock()->GetGraph()->GetFloatConstant(
4230 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4231 }
4232 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4233 return GetBlock()->GetGraph()->GetDoubleConstant(
4234 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4235 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004236
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004237 DECLARE_INSTRUCTION(Add);
4238
4239 private:
4240 DISALLOW_COPY_AND_ASSIGN(HAdd);
4241};
4242
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004243class HSub : public HBinaryOperation {
4244 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004245 HSub(Primitive::Type result_type,
4246 HInstruction* left,
4247 HInstruction* right,
4248 uint32_t dex_pc = kNoDexPc)
4249 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004250
Roland Levillain9867bc72015-08-05 10:21:34 +01004251 template <typename T> T Compute(T x, T y) const { return x - y; }
4252
4253 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004254 return GetBlock()->GetGraph()->GetIntConstant(
4255 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004256 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004257 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004258 return GetBlock()->GetGraph()->GetLongConstant(
4259 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004260 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004261 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4262 return GetBlock()->GetGraph()->GetFloatConstant(
4263 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4264 }
4265 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4266 return GetBlock()->GetGraph()->GetDoubleConstant(
4267 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4268 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004269
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004270 DECLARE_INSTRUCTION(Sub);
4271
4272 private:
4273 DISALLOW_COPY_AND_ASSIGN(HSub);
4274};
4275
Calin Juravle34bacdf2014-10-07 20:23:36 +01004276class HMul : public HBinaryOperation {
4277 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004278 HMul(Primitive::Type result_type,
4279 HInstruction* left,
4280 HInstruction* right,
4281 uint32_t dex_pc = kNoDexPc)
4282 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004283
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004284 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004285
Roland Levillain9867bc72015-08-05 10:21:34 +01004286 template <typename T> T Compute(T x, T y) const { return x * y; }
4287
4288 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004289 return GetBlock()->GetGraph()->GetIntConstant(
4290 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004291 }
4292 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004293 return GetBlock()->GetGraph()->GetLongConstant(
4294 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004295 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004296 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4297 return GetBlock()->GetGraph()->GetFloatConstant(
4298 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4299 }
4300 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4301 return GetBlock()->GetGraph()->GetDoubleConstant(
4302 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4303 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004304
4305 DECLARE_INSTRUCTION(Mul);
4306
4307 private:
4308 DISALLOW_COPY_AND_ASSIGN(HMul);
4309};
4310
Calin Juravle7c4954d2014-10-28 16:57:40 +00004311class HDiv : public HBinaryOperation {
4312 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004313 HDiv(Primitive::Type result_type,
4314 HInstruction* left,
4315 HInstruction* right,
4316 uint32_t dex_pc)
4317 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004318
Roland Levillain9867bc72015-08-05 10:21:34 +01004319 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004320 T ComputeIntegral(T x, T y) const {
4321 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004322 // Our graph structure ensures we never have 0 for `y` during
4323 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004324 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004325 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004326 return (y == -1) ? -x : x / y;
4327 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004328
Roland Levillain31dd3d62016-02-16 12:21:02 +00004329 template <typename T>
4330 T ComputeFP(T x, T y) const {
4331 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4332 return x / y;
4333 }
4334
Roland Levillain9867bc72015-08-05 10:21:34 +01004335 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004336 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004337 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004338 }
4339 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004340 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004341 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4342 }
4343 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4344 return GetBlock()->GetGraph()->GetFloatConstant(
4345 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4346 }
4347 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4348 return GetBlock()->GetGraph()->GetDoubleConstant(
4349 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004350 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004351
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004352 static SideEffects SideEffectsForArchRuntimeCalls() {
4353 // The generated code can use a runtime call.
4354 return SideEffects::CanTriggerGC();
4355 }
4356
Calin Juravle7c4954d2014-10-28 16:57:40 +00004357 DECLARE_INSTRUCTION(Div);
4358
4359 private:
4360 DISALLOW_COPY_AND_ASSIGN(HDiv);
4361};
4362
Calin Juravlebacfec32014-11-14 15:54:36 +00004363class HRem : public HBinaryOperation {
4364 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004365 HRem(Primitive::Type result_type,
4366 HInstruction* left,
4367 HInstruction* right,
4368 uint32_t dex_pc)
4369 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004370
Roland Levillain9867bc72015-08-05 10:21:34 +01004371 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004372 T ComputeIntegral(T x, T y) const {
4373 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004374 // Our graph structure ensures we never have 0 for `y` during
4375 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004376 DCHECK_NE(y, 0);
4377 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4378 return (y == -1) ? 0 : x % y;
4379 }
4380
Roland Levillain31dd3d62016-02-16 12:21:02 +00004381 template <typename T>
4382 T ComputeFP(T x, T y) const {
4383 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4384 return std::fmod(x, y);
4385 }
4386
Roland Levillain9867bc72015-08-05 10:21:34 +01004387 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004388 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004389 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004390 }
4391 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004392 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004393 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004394 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004395 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4396 return GetBlock()->GetGraph()->GetFloatConstant(
4397 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4398 }
4399 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4400 return GetBlock()->GetGraph()->GetDoubleConstant(
4401 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4402 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004403
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004404 static SideEffects SideEffectsForArchRuntimeCalls() {
4405 return SideEffects::CanTriggerGC();
4406 }
4407
Calin Juravlebacfec32014-11-14 15:54:36 +00004408 DECLARE_INSTRUCTION(Rem);
4409
4410 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004411 DISALLOW_COPY_AND_ASSIGN(HRem);
4412};
4413
Calin Juravled0d48522014-11-04 16:40:20 +00004414class HDivZeroCheck : public HExpression<1> {
4415 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004416 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4417 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004418 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004419 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004420 SetRawInputAt(0, value);
4421 }
4422
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004423 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4424
Calin Juravled0d48522014-11-04 16:40:20 +00004425 bool CanBeMoved() const OVERRIDE { return true; }
4426
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004427 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004428 return true;
4429 }
4430
4431 bool NeedsEnvironment() const OVERRIDE { return true; }
4432 bool CanThrow() const OVERRIDE { return true; }
4433
Calin Juravled0d48522014-11-04 16:40:20 +00004434 DECLARE_INSTRUCTION(DivZeroCheck);
4435
4436 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004437 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4438};
4439
Calin Juravle9aec02f2014-11-18 23:06:35 +00004440class HShl : public HBinaryOperation {
4441 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004442 HShl(Primitive::Type result_type,
4443 HInstruction* left,
4444 HInstruction* right,
4445 uint32_t dex_pc = kNoDexPc)
4446 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004447
Roland Levillain9867bc72015-08-05 10:21:34 +01004448 template <typename T, typename U, typename V>
4449 T Compute(T x, U y, V max_shift_value) const {
4450 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4451 "V is not the unsigned integer type corresponding to T");
4452 return x << (y & max_shift_value);
4453 }
4454
4455 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4456 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004457 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004458 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004459 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4460 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004461 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004462 }
4463 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4464 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004465 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004466 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004467 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4468 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4469 LOG(FATAL) << DebugName() << " is not defined for float values";
4470 UNREACHABLE();
4471 }
4472 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4473 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4474 LOG(FATAL) << DebugName() << " is not defined for double values";
4475 UNREACHABLE();
4476 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004477
4478 DECLARE_INSTRUCTION(Shl);
4479
4480 private:
4481 DISALLOW_COPY_AND_ASSIGN(HShl);
4482};
4483
4484class HShr : public HBinaryOperation {
4485 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004486 HShr(Primitive::Type result_type,
4487 HInstruction* left,
4488 HInstruction* right,
4489 uint32_t dex_pc = kNoDexPc)
4490 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004491
Roland Levillain9867bc72015-08-05 10:21:34 +01004492 template <typename T, typename U, typename V>
4493 T Compute(T x, U y, V max_shift_value) const {
4494 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4495 "V is not the unsigned integer type corresponding to T");
4496 return x >> (y & max_shift_value);
4497 }
4498
4499 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4500 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004501 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004502 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004503 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4504 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004505 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004506 }
4507 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4508 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004509 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004510 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004511 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4512 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4513 LOG(FATAL) << DebugName() << " is not defined for float values";
4514 UNREACHABLE();
4515 }
4516 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4517 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4518 LOG(FATAL) << DebugName() << " is not defined for double values";
4519 UNREACHABLE();
4520 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004521
4522 DECLARE_INSTRUCTION(Shr);
4523
4524 private:
4525 DISALLOW_COPY_AND_ASSIGN(HShr);
4526};
4527
4528class HUShr : public HBinaryOperation {
4529 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004530 HUShr(Primitive::Type result_type,
4531 HInstruction* left,
4532 HInstruction* right,
4533 uint32_t dex_pc = kNoDexPc)
4534 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004535
Roland Levillain9867bc72015-08-05 10:21:34 +01004536 template <typename T, typename U, typename V>
4537 T Compute(T x, U y, V max_shift_value) const {
4538 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4539 "V is not the unsigned integer type corresponding to T");
4540 V ux = static_cast<V>(x);
4541 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004542 }
4543
Roland Levillain9867bc72015-08-05 10:21:34 +01004544 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4545 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004546 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004547 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004548 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4549 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004550 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004551 }
4552 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4553 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004554 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004555 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004556 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4557 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4558 LOG(FATAL) << DebugName() << " is not defined for float values";
4559 UNREACHABLE();
4560 }
4561 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4562 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4563 LOG(FATAL) << DebugName() << " is not defined for double values";
4564 UNREACHABLE();
4565 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004566
4567 DECLARE_INSTRUCTION(UShr);
4568
4569 private:
4570 DISALLOW_COPY_AND_ASSIGN(HUShr);
4571};
4572
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004573class HAnd : public HBinaryOperation {
4574 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004575 HAnd(Primitive::Type result_type,
4576 HInstruction* left,
4577 HInstruction* right,
4578 uint32_t dex_pc = kNoDexPc)
4579 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004580
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004581 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004582
Roland Levillaine53bd812016-02-24 14:54:18 +00004583 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004584
4585 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004586 return GetBlock()->GetGraph()->GetIntConstant(
4587 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004588 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004589 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004590 return GetBlock()->GetGraph()->GetLongConstant(
4591 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004592 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004593 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4594 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4595 LOG(FATAL) << DebugName() << " is not defined for float values";
4596 UNREACHABLE();
4597 }
4598 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4599 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4600 LOG(FATAL) << DebugName() << " is not defined for double values";
4601 UNREACHABLE();
4602 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004603
4604 DECLARE_INSTRUCTION(And);
4605
4606 private:
4607 DISALLOW_COPY_AND_ASSIGN(HAnd);
4608};
4609
4610class HOr : public HBinaryOperation {
4611 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004612 HOr(Primitive::Type result_type,
4613 HInstruction* left,
4614 HInstruction* right,
4615 uint32_t dex_pc = kNoDexPc)
4616 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004617
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004618 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004619
Roland Levillaine53bd812016-02-24 14:54:18 +00004620 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004621
4622 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004623 return GetBlock()->GetGraph()->GetIntConstant(
4624 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004625 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004626 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004627 return GetBlock()->GetGraph()->GetLongConstant(
4628 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004629 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004630 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4631 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4632 LOG(FATAL) << DebugName() << " is not defined for float values";
4633 UNREACHABLE();
4634 }
4635 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4636 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4637 LOG(FATAL) << DebugName() << " is not defined for double values";
4638 UNREACHABLE();
4639 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004640
4641 DECLARE_INSTRUCTION(Or);
4642
4643 private:
4644 DISALLOW_COPY_AND_ASSIGN(HOr);
4645};
4646
4647class HXor : public HBinaryOperation {
4648 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004649 HXor(Primitive::Type result_type,
4650 HInstruction* left,
4651 HInstruction* right,
4652 uint32_t dex_pc = kNoDexPc)
4653 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004654
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004655 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004656
Roland Levillaine53bd812016-02-24 14:54:18 +00004657 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004658
4659 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004660 return GetBlock()->GetGraph()->GetIntConstant(
4661 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004662 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004663 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004664 return GetBlock()->GetGraph()->GetLongConstant(
4665 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004666 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004667 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4668 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4669 LOG(FATAL) << DebugName() << " is not defined for float values";
4670 UNREACHABLE();
4671 }
4672 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4673 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4674 LOG(FATAL) << DebugName() << " is not defined for double values";
4675 UNREACHABLE();
4676 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004677
4678 DECLARE_INSTRUCTION(Xor);
4679
4680 private:
4681 DISALLOW_COPY_AND_ASSIGN(HXor);
4682};
4683
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004684class HRor : public HBinaryOperation {
4685 public:
4686 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4687 : HBinaryOperation(result_type, value, distance) {}
4688
4689 template <typename T, typename U, typename V>
4690 T Compute(T x, U y, V max_shift_value) const {
4691 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4692 "V is not the unsigned integer type corresponding to T");
4693 V ux = static_cast<V>(x);
4694 if ((y & max_shift_value) == 0) {
4695 return static_cast<T>(ux);
4696 } else {
4697 const V reg_bits = sizeof(T) * 8;
4698 return static_cast<T>(ux >> (y & max_shift_value)) |
4699 (x << (reg_bits - (y & max_shift_value)));
4700 }
4701 }
4702
4703 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4704 return GetBlock()->GetGraph()->GetIntConstant(
4705 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4706 }
4707 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4708 return GetBlock()->GetGraph()->GetLongConstant(
4709 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4710 }
4711 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4712 return GetBlock()->GetGraph()->GetLongConstant(
4713 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4714 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004715 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4716 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4717 LOG(FATAL) << DebugName() << " is not defined for float values";
4718 UNREACHABLE();
4719 }
4720 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4721 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4722 LOG(FATAL) << DebugName() << " is not defined for double values";
4723 UNREACHABLE();
4724 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004725
4726 DECLARE_INSTRUCTION(Ror);
4727
4728 private:
4729 DISALLOW_COPY_AND_ASSIGN(HRor);
4730};
4731
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004732// The value of a parameter in this method. Its location depends on
4733// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004734class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004735 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004736 HParameterValue(const DexFile& dex_file,
4737 uint16_t type_index,
4738 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004739 Primitive::Type parameter_type,
4740 bool is_this = false)
4741 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004742 dex_file_(dex_file),
4743 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004744 index_(index) {
4745 SetPackedFlag<kFlagIsThis>(is_this);
4746 SetPackedFlag<kFlagCanBeNull>(!is_this);
4747 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004748
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004749 const DexFile& GetDexFile() const { return dex_file_; }
4750 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004751 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004752 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004753
Vladimir Markoa1de9182016-02-25 11:37:38 +00004754 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4755 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004756
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004757 DECLARE_INSTRUCTION(ParameterValue);
4758
4759 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004760 // Whether or not the parameter value corresponds to 'this' argument.
4761 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4762 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4763 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4764 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4765 "Too many packed fields.");
4766
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004767 const DexFile& dex_file_;
4768 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004769 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004770 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004771 const uint8_t index_;
4772
4773 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4774};
4775
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004776class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004777 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004778 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4779 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004780
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004781 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004782 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004783 return true;
4784 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004785
Roland Levillain9867bc72015-08-05 10:21:34 +01004786 template <typename T> T Compute(T x) const { return ~x; }
4787
4788 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004789 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004790 }
4791 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004792 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004793 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004794 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4795 LOG(FATAL) << DebugName() << " is not defined for float values";
4796 UNREACHABLE();
4797 }
4798 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4799 LOG(FATAL) << DebugName() << " is not defined for double values";
4800 UNREACHABLE();
4801 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004802
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004803 DECLARE_INSTRUCTION(Not);
4804
4805 private:
4806 DISALLOW_COPY_AND_ASSIGN(HNot);
4807};
4808
David Brazdil66d126e2015-04-03 16:02:44 +01004809class HBooleanNot : public HUnaryOperation {
4810 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004811 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4812 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004813
4814 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004815 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004816 return true;
4817 }
4818
Roland Levillain9867bc72015-08-05 10:21:34 +01004819 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004820 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004821 return !x;
4822 }
4823
Roland Levillain9867bc72015-08-05 10:21:34 +01004824 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004825 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004826 }
4827 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4828 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004829 UNREACHABLE();
4830 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004831 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4832 LOG(FATAL) << DebugName() << " is not defined for float values";
4833 UNREACHABLE();
4834 }
4835 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4836 LOG(FATAL) << DebugName() << " is not defined for double values";
4837 UNREACHABLE();
4838 }
David Brazdil66d126e2015-04-03 16:02:44 +01004839
4840 DECLARE_INSTRUCTION(BooleanNot);
4841
4842 private:
4843 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4844};
4845
Roland Levillaindff1f282014-11-05 14:15:05 +00004846class HTypeConversion : public HExpression<1> {
4847 public:
4848 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004849 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004850 : HExpression(result_type,
4851 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4852 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004853 SetRawInputAt(0, input);
4854 DCHECK_NE(input->GetType(), result_type);
4855 }
4856
4857 HInstruction* GetInput() const { return InputAt(0); }
4858 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4859 Primitive::Type GetResultType() const { return GetType(); }
4860
4861 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004862 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004863
Mark Mendelle82549b2015-05-06 10:55:34 -04004864 // Try to statically evaluate the conversion and return a HConstant
4865 // containing the result. If the input cannot be converted, return nullptr.
4866 HConstant* TryStaticEvaluation() const;
4867
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004868 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4869 Primitive::Type result_type) {
4870 // Some architectures may not require the 'GC' side effects, but at this point
4871 // in the compilation process we do not know what architecture we will
4872 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004873 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4874 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004875 return SideEffects::CanTriggerGC();
4876 }
4877 return SideEffects::None();
4878 }
4879
Roland Levillaindff1f282014-11-05 14:15:05 +00004880 DECLARE_INSTRUCTION(TypeConversion);
4881
4882 private:
4883 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4884};
4885
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004886static constexpr uint32_t kNoRegNumber = -1;
4887
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004888class HPhi : public HInstruction {
4889 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004890 HPhi(ArenaAllocator* arena,
4891 uint32_t reg_number,
4892 size_t number_of_inputs,
4893 Primitive::Type type,
4894 uint32_t dex_pc = kNoDexPc)
4895 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004896 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004897 reg_number_(reg_number) {
4898 SetPackedField<TypeField>(ToPhiType(type));
4899 DCHECK_NE(GetType(), Primitive::kPrimVoid);
4900 // Phis are constructed live and marked dead if conflicting or unused.
4901 // Individual steps of SsaBuilder should assume that if a phi has been
4902 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4903 SetPackedFlag<kFlagIsLive>(true);
4904 SetPackedFlag<kFlagCanBeNull>(true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004905 }
4906
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004907 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4908 static Primitive::Type ToPhiType(Primitive::Type type) {
4909 switch (type) {
4910 case Primitive::kPrimBoolean:
4911 case Primitive::kPrimByte:
4912 case Primitive::kPrimShort:
4913 case Primitive::kPrimChar:
4914 return Primitive::kPrimInt;
4915 default:
4916 return type;
4917 }
4918 }
4919
David Brazdilffee3d32015-07-06 11:48:53 +01004920 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4921
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004922 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004923
4924 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004925 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004926
Vladimir Markoa1de9182016-02-25 11:37:38 +00004927 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
David Brazdil4833f5a2015-12-16 10:37:39 +00004928 void SetType(Primitive::Type new_type) {
4929 // Make sure that only valid type changes occur. The following are allowed:
4930 // (1) int -> float/ref (primitive type propagation),
4931 // (2) long -> double (primitive type propagation).
Vladimir Markoa1de9182016-02-25 11:37:38 +00004932 DCHECK(GetType() == new_type ||
4933 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4934 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4935 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4936 SetPackedField<TypeField>(new_type);
David Brazdil4833f5a2015-12-16 10:37:39 +00004937 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004938
Vladimir Markoa1de9182016-02-25 11:37:38 +00004939 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4940 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004941
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004942 uint32_t GetRegNumber() const { return reg_number_; }
4943
Vladimir Markoa1de9182016-02-25 11:37:38 +00004944 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
4945 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
4946 bool IsDead() const { return !IsLive(); }
4947 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004948
David Brazdil77a48ae2015-09-15 12:34:04 +00004949 bool IsVRegEquivalentOf(HInstruction* other) const {
4950 return other != nullptr
4951 && other->IsPhi()
4952 && other->AsPhi()->GetBlock() == GetBlock()
4953 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4954 }
4955
Calin Juravlea4f88312015-04-16 12:57:19 +01004956 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4957 // An equivalent phi is a phi having the same dex register and type.
4958 // It assumes that phis with the same dex register are adjacent.
4959 HPhi* GetNextEquivalentPhiWithSameType() {
4960 HInstruction* next = GetNext();
4961 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4962 if (next->GetType() == GetType()) {
4963 return next->AsPhi();
4964 }
4965 next = next->GetNext();
4966 }
4967 return nullptr;
4968 }
4969
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004970 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004971
David Brazdil1abb4192015-02-17 18:33:36 +00004972 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004973 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004974 return inputs_[index];
4975 }
David Brazdil1abb4192015-02-17 18:33:36 +00004976
4977 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004978 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004979 }
4980
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004981 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004982 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
4983 static constexpr size_t kFieldTypeSize =
4984 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4985 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
4986 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
4987 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
4988 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4989 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
4990
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004991 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004992 const uint32_t reg_number_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004993
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004994 DISALLOW_COPY_AND_ASSIGN(HPhi);
4995};
4996
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004997class HNullCheck : public HExpression<1> {
4998 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004999 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5000 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005001 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005002 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005003 SetRawInputAt(0, value);
5004 }
5005
Calin Juravle10e244f2015-01-26 18:54:32 +00005006 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005007 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005008 return true;
5009 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005010
Calin Juravle10e244f2015-01-26 18:54:32 +00005011 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005012
Calin Juravle10e244f2015-01-26 18:54:32 +00005013 bool CanThrow() const OVERRIDE { return true; }
5014
5015 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005016
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005017
5018 DECLARE_INSTRUCTION(NullCheck);
5019
5020 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005021 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5022};
5023
5024class FieldInfo : public ValueObject {
5025 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005026 FieldInfo(MemberOffset field_offset,
5027 Primitive::Type field_type,
5028 bool is_volatile,
5029 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005030 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005031 const DexFile& dex_file,
5032 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005033 : field_offset_(field_offset),
5034 field_type_(field_type),
5035 is_volatile_(is_volatile),
5036 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005037 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005038 dex_file_(dex_file),
5039 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005040
5041 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005042 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005043 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005044 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005045 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005046 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005047 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005048
5049 private:
5050 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005051 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005052 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005053 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005054 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005055 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005056 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005057};
5058
5059class HInstanceFieldGet : public HExpression<1> {
5060 public:
5061 HInstanceFieldGet(HInstruction* value,
5062 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005063 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005064 bool is_volatile,
5065 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005066 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005067 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005068 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005069 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005070 : HExpression(field_type,
5071 SideEffects::FieldReadOfType(field_type, is_volatile),
5072 dex_pc),
5073 field_info_(field_offset,
5074 field_type,
5075 is_volatile,
5076 field_idx,
5077 declaring_class_def_index,
5078 dex_file,
5079 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005080 SetRawInputAt(0, value);
5081 }
5082
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005083 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005084
5085 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5086 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
5087 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005088 }
5089
Calin Juravle641547a2015-04-21 22:08:51 +01005090 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5091 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005092 }
5093
5094 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005095 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5096 }
5097
Calin Juravle52c48962014-12-16 17:02:57 +00005098 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005099 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005100 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005101 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005102
5103 DECLARE_INSTRUCTION(InstanceFieldGet);
5104
5105 private:
5106 const FieldInfo field_info_;
5107
5108 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5109};
5110
5111class HInstanceFieldSet : public HTemplateInstruction<2> {
5112 public:
5113 HInstanceFieldSet(HInstruction* object,
5114 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005115 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005116 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005117 bool is_volatile,
5118 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005119 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005120 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005121 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005122 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005123 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5124 dex_pc),
5125 field_info_(field_offset,
5126 field_type,
5127 is_volatile,
5128 field_idx,
5129 declaring_class_def_index,
5130 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005131 dex_cache) {
5132 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005133 SetRawInputAt(0, object);
5134 SetRawInputAt(1, value);
5135 }
5136
Calin Juravle641547a2015-04-21 22:08:51 +01005137 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5138 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005139 }
5140
Calin Juravle52c48962014-12-16 17:02:57 +00005141 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005142 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005143 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005144 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005145 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005146 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5147 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005148
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005149 DECLARE_INSTRUCTION(InstanceFieldSet);
5150
5151 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005152 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5153 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5154 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5155 "Too many packed fields.");
5156
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005157 const FieldInfo field_info_;
5158
5159 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5160};
5161
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005162class HArrayGet : public HExpression<2> {
5163 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005164 HArrayGet(HInstruction* array,
5165 HInstruction* index,
5166 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005167 uint32_t dex_pc,
5168 SideEffects additional_side_effects = SideEffects::None())
5169 : HExpression(type,
5170 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00005171 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005172 SetRawInputAt(0, array);
5173 SetRawInputAt(1, index);
5174 }
5175
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005176 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005177 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005178 return true;
5179 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005180 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005181 // TODO: We can be smarter here.
5182 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5183 // which generates the implicit null check. There are cases when these can be removed
5184 // to produce better code. If we ever add optimizations to do so we should allow an
5185 // implicit check here (as long as the address falls in the first page).
5186 return false;
5187 }
5188
David Brazdil4833f5a2015-12-16 10:37:39 +00005189 bool IsEquivalentOf(HArrayGet* other) const {
5190 bool result = (GetDexPc() == other->GetDexPc());
5191 if (kIsDebugBuild && result) {
5192 DCHECK_EQ(GetBlock(), other->GetBlock());
5193 DCHECK_EQ(GetArray(), other->GetArray());
5194 DCHECK_EQ(GetIndex(), other->GetIndex());
5195 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005196 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005197 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005198 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5199 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005200 }
5201 }
5202 return result;
5203 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005204
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005205 HInstruction* GetArray() const { return InputAt(0); }
5206 HInstruction* GetIndex() const { return InputAt(1); }
5207
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005208 DECLARE_INSTRUCTION(ArrayGet);
5209
5210 private:
5211 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5212};
5213
5214class HArraySet : public HTemplateInstruction<3> {
5215 public:
5216 HArraySet(HInstruction* array,
5217 HInstruction* index,
5218 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005219 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005220 uint32_t dex_pc,
5221 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005222 : HTemplateInstruction(
5223 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005224 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
5225 additional_side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005226 dex_pc) {
5227 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5228 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5229 SetPackedFlag<kFlagValueCanBeNull>(true);
5230 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005231 SetRawInputAt(0, array);
5232 SetRawInputAt(1, index);
5233 SetRawInputAt(2, value);
5234 }
5235
Calin Juravle77520bc2015-01-12 18:45:46 +00005236 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005237 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005238 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005239 }
5240
Mingyao Yang81014cb2015-06-02 03:16:27 -07005241 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005242 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005243
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005244 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005245 // TODO: Same as for ArrayGet.
5246 return false;
5247 }
5248
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005249 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005250 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005251 }
5252
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005253 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005254 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005255 }
5256
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005257 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005258 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005259 }
5260
Vladimir Markoa1de9182016-02-25 11:37:38 +00005261 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5262 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5263 bool StaticTypeOfArrayIsObjectArray() const {
5264 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5265 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005266
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005267 HInstruction* GetArray() const { return InputAt(0); }
5268 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005269 HInstruction* GetValue() const { return InputAt(2); }
5270
5271 Primitive::Type GetComponentType() const {
5272 // The Dex format does not type floating point index operations. Since the
5273 // `expected_component_type_` is set during building and can therefore not
5274 // be correct, we also check what is the value type. If it is a floating
5275 // point type, we must use that type.
5276 Primitive::Type value_type = GetValue()->GetType();
5277 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5278 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005279 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005280 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005281
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005282 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005283 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005284 }
5285
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005286 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5287 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5288 }
5289
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005290 DECLARE_INSTRUCTION(ArraySet);
5291
5292 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005293 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5294 static constexpr size_t kFieldExpectedComponentTypeSize =
5295 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5296 static constexpr size_t kFlagNeedsTypeCheck =
5297 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5298 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005299 // Cached information for the reference_type_info_ so that codegen
5300 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005301 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5302 static constexpr size_t kNumberOfArraySetPackedBits =
5303 kFlagStaticTypeOfArrayIsObjectArray + 1;
5304 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5305 using ExpectedComponentTypeField =
5306 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005307
5308 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5309};
5310
5311class HArrayLength : public HExpression<1> {
5312 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005313 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005314 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005315 // Note that arrays do not change length, so the instruction does not
5316 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005317 SetRawInputAt(0, array);
5318 }
5319
Calin Juravle77520bc2015-01-12 18:45:46 +00005320 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005321 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005322 return true;
5323 }
Calin Juravle641547a2015-04-21 22:08:51 +01005324 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5325 return obj == InputAt(0);
5326 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005327
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005328 DECLARE_INSTRUCTION(ArrayLength);
5329
5330 private:
5331 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5332};
5333
5334class HBoundsCheck : public HExpression<2> {
5335 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005336 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5337 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005338 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005339 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005340 DCHECK(index->GetType() == Primitive::kPrimInt);
5341 SetRawInputAt(0, index);
5342 SetRawInputAt(1, length);
5343 }
5344
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005345 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005346 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005347 return true;
5348 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005349
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005350 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005351
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005352 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005353
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005354 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005355
5356 DECLARE_INSTRUCTION(BoundsCheck);
5357
5358 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005359 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5360};
5361
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005362class HSuspendCheck : public HTemplateInstruction<0> {
5363 public:
5364 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005365 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005366
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005367 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005368 return true;
5369 }
5370
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005371 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5372 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005373
5374 DECLARE_INSTRUCTION(SuspendCheck);
5375
5376 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005377 // Only used for code generation, in order to share the same slow path between back edges
5378 // of a same loop.
5379 SlowPathCode* slow_path_;
5380
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005381 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5382};
5383
David Srbecky0cf44932015-12-09 14:09:59 +00005384// Pseudo-instruction which provides the native debugger with mapping information.
5385// It ensures that we can generate line number and local variables at this point.
5386class HNativeDebugInfo : public HTemplateInstruction<0> {
5387 public:
5388 explicit HNativeDebugInfo(uint32_t dex_pc)
5389 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5390
5391 bool NeedsEnvironment() const OVERRIDE {
5392 return true;
5393 }
5394
5395 DECLARE_INSTRUCTION(NativeDebugInfo);
5396
5397 private:
5398 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5399};
5400
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005401/**
5402 * Instruction to load a Class object.
5403 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005404class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005405 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005406 HLoadClass(HCurrentMethod* current_method,
5407 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005408 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005409 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005410 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005411 bool needs_access_check,
5412 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005413 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005414 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005415 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005416 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005417 // Referrers class should not need access check. We never inline unverified
5418 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005419 DCHECK(!is_referrers_class || !needs_access_check);
5420
5421 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5422 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5423 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5424 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005425 SetRawInputAt(0, current_method);
5426 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005427
5428 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005429
5430 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005431 // Note that we don't need to test for generate_clinit_check_.
5432 // Whether or not we need to generate the clinit check is processed in
5433 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005434 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005435 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005436 }
5437
5438 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5439
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005440 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005441 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005442
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005443 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005444 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005445 }
5446
Calin Juravle0ba218d2015-05-19 18:46:01 +01005447 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005448 // The entrypoint the code generator is going to call does not do
5449 // clinit of the class.
5450 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005451 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005452 }
5453
5454 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005455 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005456 (!IsReferrersClass() && !IsInDexCache()) ||
5457 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005458 }
5459
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005460
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005461 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005462 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005463 }
5464
Calin Juravleacf735c2015-02-12 15:25:22 +00005465 ReferenceTypeInfo GetLoadedClassRTI() {
5466 return loaded_class_rti_;
5467 }
5468
5469 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5470 // Make sure we only set exact types (the loaded class should never be merged).
5471 DCHECK(rti.IsExact());
5472 loaded_class_rti_ = rti;
5473 }
5474
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005475 const DexFile& GetDexFile() { return dex_file_; }
5476
Vladimir Markoa1de9182016-02-25 11:37:38 +00005477 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005478
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005479 static SideEffects SideEffectsForArchRuntimeCalls() {
5480 return SideEffects::CanTriggerGC();
5481 }
5482
Vladimir Markoa1de9182016-02-25 11:37:38 +00005483 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5484 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5485 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5486 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005487
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005488 DECLARE_INSTRUCTION(LoadClass);
5489
5490 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005491 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5492 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5493 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005494 // Whether this instruction must generate the initialization check.
5495 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005496 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5497 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5498 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5499
5500 const uint16_t type_index_;
5501 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005502
Calin Juravleacf735c2015-02-12 15:25:22 +00005503 ReferenceTypeInfo loaded_class_rti_;
5504
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005505 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5506};
5507
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005508class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005509 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005510 HLoadString(HCurrentMethod* current_method,
5511 uint32_t string_index,
5512 uint32_t dex_pc,
5513 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005514 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005515 string_index_(string_index) {
5516 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005517 SetRawInputAt(0, current_method);
5518 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005519
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005520 bool CanBeMoved() const OVERRIDE { return true; }
5521
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005522 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5523 return other->AsLoadString()->string_index_ == string_index_;
5524 }
5525
5526 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5527
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005528 uint32_t GetStringIndex() const { return string_index_; }
5529
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005530 // Will call the runtime if the string is not already in the dex cache.
5531 bool NeedsEnvironment() const OVERRIDE { return !IsInDexCache(); }
5532
Vladimir Markodc151b22015-10-15 18:02:30 +01005533 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005534 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005535 bool CanThrow() const OVERRIDE { return !IsInDexCache(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005536
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005537 static SideEffects SideEffectsForArchRuntimeCalls() {
5538 return SideEffects::CanTriggerGC();
5539 }
5540
Vladimir Markoa1de9182016-02-25 11:37:38 +00005541 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5542
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005543 DECLARE_INSTRUCTION(LoadString);
5544
5545 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005546 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
5547 static constexpr size_t kNumberOfLoadStringPackedBits = kFlagIsInDexCache + 1;
5548 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5549
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005550 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005551
5552 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5553};
5554
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005555/**
5556 * Performs an initialization check on its Class object input.
5557 */
5558class HClinitCheck : public HExpression<1> {
5559 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005560 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005561 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005562 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005563 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5564 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005565 SetRawInputAt(0, constant);
5566 }
5567
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005568 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005569 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005570 return true;
5571 }
5572
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005573 bool NeedsEnvironment() const OVERRIDE {
5574 // May call runtime to initialize the class.
5575 return true;
5576 }
5577
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005578 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005579
5580 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5581
5582 DECLARE_INSTRUCTION(ClinitCheck);
5583
5584 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005585 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5586};
5587
5588class HStaticFieldGet : public HExpression<1> {
5589 public:
5590 HStaticFieldGet(HInstruction* cls,
5591 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005592 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005593 bool is_volatile,
5594 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005595 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005596 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005597 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005598 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005599 : HExpression(field_type,
5600 SideEffects::FieldReadOfType(field_type, is_volatile),
5601 dex_pc),
5602 field_info_(field_offset,
5603 field_type,
5604 is_volatile,
5605 field_idx,
5606 declaring_class_def_index,
5607 dex_file,
5608 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005609 SetRawInputAt(0, cls);
5610 }
5611
Calin Juravle52c48962014-12-16 17:02:57 +00005612
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005613 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005614
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005615 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005616 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5617 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005618 }
5619
5620 size_t ComputeHashCode() const OVERRIDE {
5621 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5622 }
5623
Calin Juravle52c48962014-12-16 17:02:57 +00005624 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005625 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5626 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005627 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005628
5629 DECLARE_INSTRUCTION(StaticFieldGet);
5630
5631 private:
5632 const FieldInfo field_info_;
5633
5634 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5635};
5636
5637class HStaticFieldSet : public HTemplateInstruction<2> {
5638 public:
5639 HStaticFieldSet(HInstruction* cls,
5640 HInstruction* value,
5641 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005642 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005643 bool is_volatile,
5644 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005645 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005646 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005647 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005648 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005649 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5650 dex_pc),
5651 field_info_(field_offset,
5652 field_type,
5653 is_volatile,
5654 field_idx,
5655 declaring_class_def_index,
5656 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005657 dex_cache) {
5658 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005659 SetRawInputAt(0, cls);
5660 SetRawInputAt(1, value);
5661 }
5662
Calin Juravle52c48962014-12-16 17:02:57 +00005663 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005664 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5665 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005666 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005667
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005668 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005669 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5670 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005671
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005672 DECLARE_INSTRUCTION(StaticFieldSet);
5673
5674 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005675 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5676 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5677 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5678 "Too many packed fields.");
5679
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005680 const FieldInfo field_info_;
5681
5682 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5683};
5684
Calin Juravlee460d1d2015-09-29 04:52:17 +01005685class HUnresolvedInstanceFieldGet : public HExpression<1> {
5686 public:
5687 HUnresolvedInstanceFieldGet(HInstruction* obj,
5688 Primitive::Type field_type,
5689 uint32_t field_index,
5690 uint32_t dex_pc)
5691 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5692 field_index_(field_index) {
5693 SetRawInputAt(0, obj);
5694 }
5695
5696 bool NeedsEnvironment() const OVERRIDE { return true; }
5697 bool CanThrow() const OVERRIDE { return true; }
5698
5699 Primitive::Type GetFieldType() const { return GetType(); }
5700 uint32_t GetFieldIndex() const { return field_index_; }
5701
5702 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5703
5704 private:
5705 const uint32_t field_index_;
5706
5707 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5708};
5709
5710class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5711 public:
5712 HUnresolvedInstanceFieldSet(HInstruction* obj,
5713 HInstruction* value,
5714 Primitive::Type field_type,
5715 uint32_t field_index,
5716 uint32_t dex_pc)
5717 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005718 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005719 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005720 DCHECK_EQ(field_type, value->GetType());
5721 SetRawInputAt(0, obj);
5722 SetRawInputAt(1, value);
5723 }
5724
5725 bool NeedsEnvironment() const OVERRIDE { return true; }
5726 bool CanThrow() const OVERRIDE { return true; }
5727
Vladimir Markoa1de9182016-02-25 11:37:38 +00005728 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005729 uint32_t GetFieldIndex() const { return field_index_; }
5730
5731 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5732
5733 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005734 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5735 static constexpr size_t kFieldFieldTypeSize =
5736 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5737 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5738 kFieldFieldType + kFieldFieldTypeSize;
5739 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5740 "Too many packed fields.");
5741 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5742
Calin Juravlee460d1d2015-09-29 04:52:17 +01005743 const uint32_t field_index_;
5744
5745 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5746};
5747
5748class HUnresolvedStaticFieldGet : public HExpression<0> {
5749 public:
5750 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5751 uint32_t field_index,
5752 uint32_t dex_pc)
5753 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5754 field_index_(field_index) {
5755 }
5756
5757 bool NeedsEnvironment() const OVERRIDE { return true; }
5758 bool CanThrow() const OVERRIDE { return true; }
5759
5760 Primitive::Type GetFieldType() const { return GetType(); }
5761 uint32_t GetFieldIndex() const { return field_index_; }
5762
5763 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5764
5765 private:
5766 const uint32_t field_index_;
5767
5768 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5769};
5770
5771class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5772 public:
5773 HUnresolvedStaticFieldSet(HInstruction* value,
5774 Primitive::Type field_type,
5775 uint32_t field_index,
5776 uint32_t dex_pc)
5777 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005778 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005779 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005780 DCHECK_EQ(field_type, value->GetType());
5781 SetRawInputAt(0, value);
5782 }
5783
5784 bool NeedsEnvironment() const OVERRIDE { return true; }
5785 bool CanThrow() const OVERRIDE { return true; }
5786
Vladimir Markoa1de9182016-02-25 11:37:38 +00005787 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005788 uint32_t GetFieldIndex() const { return field_index_; }
5789
5790 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5791
5792 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005793 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5794 static constexpr size_t kFieldFieldTypeSize =
5795 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5796 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5797 kFieldFieldType + kFieldFieldTypeSize;
5798 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5799 "Too many packed fields.");
5800 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5801
Calin Juravlee460d1d2015-09-29 04:52:17 +01005802 const uint32_t field_index_;
5803
5804 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5805};
5806
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005807// Implement the move-exception DEX instruction.
5808class HLoadException : public HExpression<0> {
5809 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005810 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5811 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005812
David Brazdilbbd733e2015-08-18 17:48:17 +01005813 bool CanBeNull() const OVERRIDE { return false; }
5814
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005815 DECLARE_INSTRUCTION(LoadException);
5816
5817 private:
5818 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5819};
5820
David Brazdilcb1c0552015-08-04 16:22:25 +01005821// Implicit part of move-exception which clears thread-local exception storage.
5822// Must not be removed because the runtime expects the TLS to get cleared.
5823class HClearException : public HTemplateInstruction<0> {
5824 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005825 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5826 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005827
5828 DECLARE_INSTRUCTION(ClearException);
5829
5830 private:
5831 DISALLOW_COPY_AND_ASSIGN(HClearException);
5832};
5833
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005834class HThrow : public HTemplateInstruction<1> {
5835 public:
5836 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005837 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005838 SetRawInputAt(0, exception);
5839 }
5840
5841 bool IsControlFlow() const OVERRIDE { return true; }
5842
5843 bool NeedsEnvironment() const OVERRIDE { return true; }
5844
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005845 bool CanThrow() const OVERRIDE { return true; }
5846
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005847
5848 DECLARE_INSTRUCTION(Throw);
5849
5850 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005851 DISALLOW_COPY_AND_ASSIGN(HThrow);
5852};
5853
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005854/**
5855 * Implementation strategies for the code generator of a HInstanceOf
5856 * or `HCheckCast`.
5857 */
5858enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005859 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005860 kExactCheck, // Can do a single class compare.
5861 kClassHierarchyCheck, // Can just walk the super class chain.
5862 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5863 kInterfaceCheck, // No optimization yet when checking against an interface.
5864 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005865 kArrayCheck, // No optimization yet when checking against a generic array.
5866 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005867};
5868
Roland Levillain86503782016-02-11 19:07:30 +00005869std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5870
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005871class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005872 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005873 HInstanceOf(HInstruction* object,
5874 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005875 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005876 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005877 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005878 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005879 dex_pc) {
5880 SetPackedField<TypeCheckKindField>(check_kind);
5881 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005882 SetRawInputAt(0, object);
5883 SetRawInputAt(1, constant);
5884 }
5885
5886 bool CanBeMoved() const OVERRIDE { return true; }
5887
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005888 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005889 return true;
5890 }
5891
5892 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005893 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005894 }
5895
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005896 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005897 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5898 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5899 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5900 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005901
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005902 static bool CanCallRuntime(TypeCheckKind check_kind) {
5903 // Mips currently does runtime calls for any other checks.
5904 return check_kind != TypeCheckKind::kExactCheck;
5905 }
5906
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005907 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005908 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005909 }
5910
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005911 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005912
5913 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005914 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
5915 static constexpr size_t kFieldTypeCheckKindSize =
5916 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
5917 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
5918 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
5919 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5920 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005921
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005922 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5923};
5924
Calin Juravleb1498f62015-02-16 13:13:29 +00005925class HBoundType : public HExpression<1> {
5926 public:
David Brazdilf5552582015-12-27 13:36:12 +00005927 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005928 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005929 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
5930 SetPackedFlag<kFlagUpperCanBeNull>(true);
5931 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00005932 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005933 SetRawInputAt(0, input);
5934 }
5935
David Brazdilf5552582015-12-27 13:36:12 +00005936 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005937 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005938 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00005939 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005940
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005941 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005942 DCHECK(GetUpperCanBeNull() || !can_be_null);
5943 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005944 }
5945
Vladimir Markoa1de9182016-02-25 11:37:38 +00005946 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005947
Calin Juravleb1498f62015-02-16 13:13:29 +00005948 DECLARE_INSTRUCTION(BoundType);
5949
5950 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005951 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5952 // is false then CanBeNull() cannot be true).
5953 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
5954 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
5955 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
5956 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5957
Calin Juravleb1498f62015-02-16 13:13:29 +00005958 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005959 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5960 // It is used to bound the type in cases like:
5961 // if (x instanceof ClassX) {
5962 // // uper_bound_ will be ClassX
5963 // }
David Brazdilf5552582015-12-27 13:36:12 +00005964 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005965
5966 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5967};
5968
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005969class HCheckCast : public HTemplateInstruction<2> {
5970 public:
5971 HCheckCast(HInstruction* object,
5972 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005973 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005974 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00005975 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
5976 SetPackedField<TypeCheckKindField>(check_kind);
5977 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005978 SetRawInputAt(0, object);
5979 SetRawInputAt(1, constant);
5980 }
5981
5982 bool CanBeMoved() const OVERRIDE { return true; }
5983
5984 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5985 return true;
5986 }
5987
5988 bool NeedsEnvironment() const OVERRIDE {
5989 // Instruction may throw a CheckCastError.
5990 return true;
5991 }
5992
5993 bool CanThrow() const OVERRIDE { return true; }
5994
Vladimir Markoa1de9182016-02-25 11:37:38 +00005995 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5996 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5997 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5998 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005999
6000 DECLARE_INSTRUCTION(CheckCast);
6001
6002 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006003 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6004 static constexpr size_t kFieldTypeCheckKindSize =
6005 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6006 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6007 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6008 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6009 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006010
6011 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006012};
6013
Calin Juravle27df7582015-04-17 19:12:31 +01006014class HMemoryBarrier : public HTemplateInstruction<0> {
6015 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006016 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006017 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006018 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6019 SetPackedField<BarrierKindField>(barrier_kind);
6020 }
Calin Juravle27df7582015-04-17 19:12:31 +01006021
Vladimir Markoa1de9182016-02-25 11:37:38 +00006022 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006023
6024 DECLARE_INSTRUCTION(MemoryBarrier);
6025
6026 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006027 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6028 static constexpr size_t kFieldBarrierKindSize =
6029 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6030 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6031 kFieldBarrierKind + kFieldBarrierKindSize;
6032 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6033 "Too many packed fields.");
6034 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006035
6036 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6037};
6038
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006039class HMonitorOperation : public HTemplateInstruction<1> {
6040 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006041 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006042 kEnter,
6043 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006044 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006045 };
6046
6047 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006048 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006049 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6050 dex_pc) {
6051 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006052 SetRawInputAt(0, object);
6053 }
6054
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006055 // Instruction may go into runtime, so we need an environment.
6056 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006057
6058 bool CanThrow() const OVERRIDE {
6059 // Verifier guarantees that monitor-exit cannot throw.
6060 // This is important because it allows the HGraphBuilder to remove
6061 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6062 return IsEnter();
6063 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006064
Vladimir Markoa1de9182016-02-25 11:37:38 +00006065 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6066 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006067
6068 DECLARE_INSTRUCTION(MonitorOperation);
6069
Calin Juravle52c48962014-12-16 17:02:57 +00006070 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006071 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6072 static constexpr size_t kFieldOperationKindSize =
6073 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6074 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6075 kFieldOperationKind + kFieldOperationKindSize;
6076 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6077 "Too many packed fields.");
6078 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006079
6080 private:
6081 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6082};
6083
David Brazdil74eb1b22015-12-14 11:44:01 +00006084class HSelect : public HExpression<3> {
6085 public:
6086 HSelect(HInstruction* condition,
6087 HInstruction* true_value,
6088 HInstruction* false_value,
6089 uint32_t dex_pc)
6090 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6091 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6092
6093 // First input must be `true_value` or `false_value` to allow codegens to
6094 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6095 // that architectures which implement HSelect as a conditional move also
6096 // will not need to invert the condition.
6097 SetRawInputAt(0, false_value);
6098 SetRawInputAt(1, true_value);
6099 SetRawInputAt(2, condition);
6100 }
6101
6102 HInstruction* GetFalseValue() const { return InputAt(0); }
6103 HInstruction* GetTrueValue() const { return InputAt(1); }
6104 HInstruction* GetCondition() const { return InputAt(2); }
6105
6106 bool CanBeMoved() const OVERRIDE { return true; }
6107 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6108
6109 bool CanBeNull() const OVERRIDE {
6110 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6111 }
6112
6113 DECLARE_INSTRUCTION(Select);
6114
6115 private:
6116 DISALLOW_COPY_AND_ASSIGN(HSelect);
6117};
6118
Vladimir Markof9f64412015-09-02 14:05:49 +01006119class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006120 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006121 MoveOperands(Location source,
6122 Location destination,
6123 Primitive::Type type,
6124 HInstruction* instruction)
6125 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006126
6127 Location GetSource() const { return source_; }
6128 Location GetDestination() const { return destination_; }
6129
6130 void SetSource(Location value) { source_ = value; }
6131 void SetDestination(Location value) { destination_ = value; }
6132
6133 // The parallel move resolver marks moves as "in-progress" by clearing the
6134 // destination (but not the source).
6135 Location MarkPending() {
6136 DCHECK(!IsPending());
6137 Location dest = destination_;
6138 destination_ = Location::NoLocation();
6139 return dest;
6140 }
6141
6142 void ClearPending(Location dest) {
6143 DCHECK(IsPending());
6144 destination_ = dest;
6145 }
6146
6147 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006148 DCHECK(source_.IsValid() || destination_.IsInvalid());
6149 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006150 }
6151
6152 // True if this blocks a move from the given location.
6153 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006154 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006155 }
6156
6157 // A move is redundant if it's been eliminated, if its source and
6158 // destination are the same, or if its destination is unneeded.
6159 bool IsRedundant() const {
6160 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6161 }
6162
6163 // We clear both operands to indicate move that's been eliminated.
6164 void Eliminate() {
6165 source_ = destination_ = Location::NoLocation();
6166 }
6167
6168 bool IsEliminated() const {
6169 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6170 return source_.IsInvalid();
6171 }
6172
Alexey Frunze4dda3372015-06-01 18:31:49 -07006173 Primitive::Type GetType() const { return type_; }
6174
Nicolas Geoffray90218252015-04-15 11:56:51 +01006175 bool Is64BitMove() const {
6176 return Primitive::Is64BitType(type_);
6177 }
6178
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006179 HInstruction* GetInstruction() const { return instruction_; }
6180
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006181 private:
6182 Location source_;
6183 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006184 // The type this move is for.
6185 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006186 // The instruction this move is assocatied with. Null when this move is
6187 // for moving an input in the expected locations of user (including a phi user).
6188 // This is only used in debug mode, to ensure we do not connect interval siblings
6189 // in the same parallel move.
6190 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006191};
6192
Roland Levillainc9285912015-12-18 10:38:42 +00006193std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6194
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006195static constexpr size_t kDefaultNumberOfMoves = 4;
6196
6197class HParallelMove : public HTemplateInstruction<0> {
6198 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006199 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006200 : HTemplateInstruction(SideEffects::None(), dex_pc),
6201 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6202 moves_.reserve(kDefaultNumberOfMoves);
6203 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006204
Nicolas Geoffray90218252015-04-15 11:56:51 +01006205 void AddMove(Location source,
6206 Location destination,
6207 Primitive::Type type,
6208 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006209 DCHECK(source.IsValid());
6210 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006211 if (kIsDebugBuild) {
6212 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006213 for (const MoveOperands& move : moves_) {
6214 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006215 // Special case the situation where the move is for the spill slot
6216 // of the instruction.
6217 if ((GetPrevious() == instruction)
6218 || ((GetPrevious() == nullptr)
6219 && instruction->IsPhi()
6220 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006221 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006222 << "Doing parallel moves for the same instruction.";
6223 } else {
6224 DCHECK(false) << "Doing parallel moves for the same instruction.";
6225 }
6226 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006227 }
6228 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006229 for (const MoveOperands& move : moves_) {
6230 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006231 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006232 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006233 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006234 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006235 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006236 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006237 }
6238
Vladimir Marko225b6462015-09-28 12:17:40 +01006239 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006240 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006241 }
6242
Vladimir Marko225b6462015-09-28 12:17:40 +01006243 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006244
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006245 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006246
6247 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006248 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006249
6250 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6251};
6252
Mark Mendell0616ae02015-04-17 12:49:27 -04006253} // namespace art
6254
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006255#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6256#include "nodes_shared.h"
6257#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006258#ifdef ART_ENABLE_CODEGEN_arm
6259#include "nodes_arm.h"
6260#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006261#ifdef ART_ENABLE_CODEGEN_arm64
6262#include "nodes_arm64.h"
6263#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006264#ifdef ART_ENABLE_CODEGEN_x86
6265#include "nodes_x86.h"
6266#endif
6267
6268namespace art {
6269
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006270class HGraphVisitor : public ValueObject {
6271 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006272 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6273 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006274
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006275 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006276 virtual void VisitBasicBlock(HBasicBlock* block);
6277
Roland Levillain633021e2014-10-01 14:12:25 +01006278 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006279 void VisitInsertionOrder();
6280
Roland Levillain633021e2014-10-01 14:12:25 +01006281 // Visit the graph following dominator tree reverse post-order.
6282 void VisitReversePostOrder();
6283
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006284 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006285
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006286 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006287#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006288 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6289
6290 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6291
6292#undef DECLARE_VISIT_INSTRUCTION
6293
6294 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006295 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006296
6297 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6298};
6299
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006300class HGraphDelegateVisitor : public HGraphVisitor {
6301 public:
6302 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6303 virtual ~HGraphDelegateVisitor() {}
6304
6305 // Visit functions that delegate to to super class.
6306#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006307 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006308
6309 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6310
6311#undef DECLARE_VISIT_INSTRUCTION
6312
6313 private:
6314 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6315};
6316
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006317class HInsertionOrderIterator : public ValueObject {
6318 public:
6319 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6320
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006321 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006322 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006323 void Advance() { ++index_; }
6324
6325 private:
6326 const HGraph& graph_;
6327 size_t index_;
6328
6329 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6330};
6331
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006332class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006333 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006334 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6335 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006336 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006337 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006338
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006339 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6340 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006341 void Advance() { ++index_; }
6342
6343 private:
6344 const HGraph& graph_;
6345 size_t index_;
6346
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006347 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006348};
6349
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006350class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006351 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006352 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006353 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006354 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006355 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006356 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006357
6358 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006359 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006360 void Advance() { --index_; }
6361
6362 private:
6363 const HGraph& graph_;
6364 size_t index_;
6365
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006366 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006367};
6368
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006369class HLinearPostOrderIterator : public ValueObject {
6370 public:
6371 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006372 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006373
6374 bool Done() const { return index_ == 0; }
6375
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006376 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006377
6378 void Advance() {
6379 --index_;
6380 DCHECK_GE(index_, 0U);
6381 }
6382
6383 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006384 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006385 size_t index_;
6386
6387 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6388};
6389
6390class HLinearOrderIterator : public ValueObject {
6391 public:
6392 explicit HLinearOrderIterator(const HGraph& graph)
6393 : order_(graph.GetLinearOrder()), index_(0) {}
6394
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006395 bool Done() const { return index_ == order_.size(); }
6396 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006397 void Advance() { ++index_; }
6398
6399 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006400 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006401 size_t index_;
6402
6403 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6404};
6405
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006406// Iterator over the blocks that art part of the loop. Includes blocks part
6407// of an inner loop. The order in which the blocks are iterated is on their
6408// block id.
6409class HBlocksInLoopIterator : public ValueObject {
6410 public:
6411 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6412 : blocks_in_loop_(info.GetBlocks()),
6413 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6414 index_(0) {
6415 if (!blocks_in_loop_.IsBitSet(index_)) {
6416 Advance();
6417 }
6418 }
6419
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006420 bool Done() const { return index_ == blocks_.size(); }
6421 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006422 void Advance() {
6423 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006424 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006425 if (blocks_in_loop_.IsBitSet(index_)) {
6426 break;
6427 }
6428 }
6429 }
6430
6431 private:
6432 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006433 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006434 size_t index_;
6435
6436 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6437};
6438
Mingyao Yang3584bce2015-05-19 16:01:59 -07006439// Iterator over the blocks that art part of the loop. Includes blocks part
6440// of an inner loop. The order in which the blocks are iterated is reverse
6441// post order.
6442class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6443 public:
6444 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6445 : blocks_in_loop_(info.GetBlocks()),
6446 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6447 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006448 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006449 Advance();
6450 }
6451 }
6452
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006453 bool Done() const { return index_ == blocks_.size(); }
6454 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006455 void Advance() {
6456 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006457 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6458 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006459 break;
6460 }
6461 }
6462 }
6463
6464 private:
6465 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006466 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006467 size_t index_;
6468
6469 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6470};
6471
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006472inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006473 if (constant->IsIntConstant()) {
6474 return constant->AsIntConstant()->GetValue();
6475 } else if (constant->IsLongConstant()) {
6476 return constant->AsLongConstant()->GetValue();
6477 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006478 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006479 return 0;
6480 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006481}
6482
Vladimir Marko58155012015-08-19 12:49:41 +00006483inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6484 // For the purposes of the compiler, the dex files must actually be the same object
6485 // if we want to safely treat them as the same. This is especially important for JIT
6486 // as custom class loaders can open the same underlying file (or memory) multiple
6487 // times and provide different class resolution but no two class loaders should ever
6488 // use the same DexFile object - doing so is an unsupported hack that can lead to
6489 // all sorts of weird failures.
6490 return &lhs == &rhs;
6491}
6492
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006493#define INSTRUCTION_TYPE_CHECK(type, super) \
6494 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6495 inline const H##type* HInstruction::As##type() const { \
6496 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6497 } \
6498 inline H##type* HInstruction::As##type() { \
6499 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6500 }
6501
6502 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6503#undef INSTRUCTION_TYPE_CHECK
6504
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00006505class SwitchTable : public ValueObject {
6506 public:
6507 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
6508 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
6509 int32_t table_offset = instruction.VRegB_31t();
6510 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
6511 if (sparse) {
6512 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
6513 } else {
6514 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
6515 }
6516 num_entries_ = table[1];
6517 values_ = reinterpret_cast<const int32_t*>(&table[2]);
6518 }
6519
6520 uint16_t GetNumEntries() const {
6521 return num_entries_;
6522 }
6523
6524 void CheckIndex(size_t index) const {
6525 if (sparse_) {
6526 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6527 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
6528 } else {
6529 // In a packed table, we have the starting key and num_entries_ values.
6530 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
6531 }
6532 }
6533
6534 int32_t GetEntryAt(size_t index) const {
6535 CheckIndex(index);
6536 return values_[index];
6537 }
6538
6539 uint32_t GetDexPcForIndex(size_t index) const {
6540 CheckIndex(index);
6541 return dex_pc_ +
6542 (reinterpret_cast<const int16_t*>(values_ + index) -
6543 reinterpret_cast<const int16_t*>(&instruction_));
6544 }
6545
6546 // Index of the first value in the table.
6547 size_t GetFirstValueIndex() const {
6548 if (sparse_) {
6549 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6550 return num_entries_;
6551 } else {
6552 // In a packed table, we have the starting key and num_entries_ values.
6553 return 1;
6554 }
6555 }
6556
6557 private:
6558 const Instruction& instruction_;
6559 const uint32_t dex_pc_;
6560
6561 // Whether this is a sparse-switch table (or a packed-switch one).
6562 const bool sparse_;
6563
6564 // This can't be const as it needs to be computed off of the given instruction, and complicated
6565 // expressions in the initializer list seemed very ugly.
6566 uint16_t num_entries_;
6567
6568 const int32_t* values_;
6569
6570 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
6571};
6572
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006573// Create space in `blocks` for adding `number_of_new_blocks` entries
6574// starting at location `at`. Blocks after `at` are moved accordingly.
6575inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6576 size_t number_of_new_blocks,
6577 size_t after) {
6578 DCHECK_LT(after, blocks->size());
6579 size_t old_size = blocks->size();
6580 size_t new_size = old_size + number_of_new_blocks;
6581 blocks->resize(new_size);
6582 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6583}
6584
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006585} // namespace art
6586
6587#endif // ART_COMPILER_OPTIMIZING_NODES_H_