blob: a09ad00cdd12706b78371fba6c2eb07291e5365d [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 Markof6a35de2016-03-21 12:01:50 +0000648 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100649 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 {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002546 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002547 }
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 {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002588 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002589 }
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:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003431 // Note that `comparison_type` is the type of comparison performed
3432 // between the comparison's inputs, not the type of the instantiated
3433 // HCompare instruction (which is always Primitive::kPrimInt).
3434 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003435 HInstruction* first,
3436 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003437 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003438 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003439 : HBinaryOperation(Primitive::kPrimInt,
3440 first,
3441 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003442 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003443 dex_pc) {
3444 SetPackedField<ComparisonBiasField>(bias);
Roland Levillaina5c4a402016-03-15 15:02:50 +00003445 if (kIsDebugBuild) {
3446 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3447 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
3448 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003449 }
3450
Roland Levillain9867bc72015-08-05 10:21:34 +01003451 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003452 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3453
3454 template <typename T>
3455 int32_t ComputeFP(T x, T y) const {
3456 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3457 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3458 // Handle the bias.
3459 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3460 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003461
Roland Levillain9867bc72015-08-05 10:21:34 +01003462 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003463 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3464 // reach this code path when processing a freshly built HIR
3465 // graph. However HCompare integer instructions can be synthesized
3466 // by the instruction simplifier to implement IntegerCompare and
3467 // IntegerSignum intrinsics, so we have to handle this case.
3468 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003469 }
3470 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003471 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3472 }
3473 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3474 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3475 }
3476 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3477 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003478 }
3479
Calin Juravleddb7df22014-11-25 20:56:51 +00003480 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003481 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003482 }
3483
Vladimir Markoa1de9182016-02-25 11:37:38 +00003484 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003485
Roland Levillain31dd3d62016-02-16 12:21:02 +00003486 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003487 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003488 bool IsGtBias() const {
3489 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003490 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003491 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003492
Roland Levillain1693a1f2016-03-15 14:57:31 +00003493 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3494 // Comparisons do not require a runtime call in any back end.
3495 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003496 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003497
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003498 DECLARE_INSTRUCTION(Compare);
3499
Roland Levillain31dd3d62016-02-16 12:21:02 +00003500 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003501 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3502 static constexpr size_t kFieldComparisonBiasSize =
3503 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3504 static constexpr size_t kNumberOfComparePackedBits =
3505 kFieldComparisonBias + kFieldComparisonBiasSize;
3506 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3507 using ComparisonBiasField =
3508 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3509
Roland Levillain31dd3d62016-02-16 12:21:02 +00003510 // Return an integer constant containing the result of a comparison evaluated at compile time.
3511 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3512 DCHECK(value == -1 || value == 0 || value == 1) << value;
3513 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3514 }
3515
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003516 private:
3517 DISALLOW_COPY_AND_ASSIGN(HCompare);
3518};
3519
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003520// A local in the graph. Corresponds to a Dex register.
3521class HLocal : public HTemplateInstruction<0> {
3522 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003523 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003524 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003525
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003526 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003527
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003528 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003529
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003530 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003531 // The Dex register number.
3532 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003533
3534 DISALLOW_COPY_AND_ASSIGN(HLocal);
3535};
3536
3537// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003538class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003539 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003540 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3541 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003542 SetRawInputAt(0, local);
3543 }
3544
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003545 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3546
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003547 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003548
3549 private:
3550 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3551};
3552
3553// Store a value in a given local. This instruction has two inputs: the value
3554// and the local.
3555class HStoreLocal : public HTemplateInstruction<2> {
3556 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003557 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3558 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003559 SetRawInputAt(0, local);
3560 SetRawInputAt(1, value);
3561 }
3562
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003563 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3564
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003565 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003566
3567 private:
3568 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3569};
3570
David Brazdil6de19382016-01-08 17:37:10 +00003571class HNewInstance : public HExpression<2> {
3572 public:
3573 HNewInstance(HInstruction* cls,
3574 HCurrentMethod* current_method,
3575 uint32_t dex_pc,
3576 uint16_t type_index,
3577 const DexFile& dex_file,
3578 bool can_throw,
3579 bool finalizable,
3580 QuickEntrypointEnum entrypoint)
3581 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3582 type_index_(type_index),
3583 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003584 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003585 SetPackedFlag<kFlagCanThrow>(can_throw);
3586 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003587 SetRawInputAt(0, cls);
3588 SetRawInputAt(1, current_method);
3589 }
3590
3591 uint16_t GetTypeIndex() const { return type_index_; }
3592 const DexFile& GetDexFile() const { return dex_file_; }
3593
3594 // Calls runtime so needs an environment.
3595 bool NeedsEnvironment() const OVERRIDE { return true; }
3596
3597 // It may throw when called on type that's not instantiable/accessible.
3598 // It can throw OOME.
3599 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003600 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>() || true; }
David Brazdil6de19382016-01-08 17:37:10 +00003601
Vladimir Markoa1de9182016-02-25 11:37:38 +00003602 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003603
3604 bool CanBeNull() const OVERRIDE { return false; }
3605
3606 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3607
3608 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3609 entrypoint_ = entrypoint;
3610 }
3611
3612 bool IsStringAlloc() const;
3613
3614 DECLARE_INSTRUCTION(NewInstance);
3615
3616 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003617 static constexpr size_t kFlagCanThrow = kNumberOfExpressionPackedBits;
3618 static constexpr size_t kFlagFinalizable = kFlagCanThrow + 1;
3619 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3620 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3621 "Too many packed fields.");
3622
David Brazdil6de19382016-01-08 17:37:10 +00003623 const uint16_t type_index_;
3624 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003625 QuickEntrypointEnum entrypoint_;
3626
3627 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3628};
3629
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003630enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003631#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3632 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003633#include "intrinsics_list.h"
3634 kNone,
3635 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3636#undef INTRINSICS_LIST
3637#undef OPTIMIZING_INTRINSICS
3638};
3639std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3640
Agi Csaki05f20562015-08-19 14:58:14 -07003641enum IntrinsicNeedsEnvironmentOrCache {
3642 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3643 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003644};
3645
Aart Bik5d75afe2015-12-14 11:57:01 -08003646enum IntrinsicSideEffects {
3647 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3648 kReadSideEffects, // Intrinsic may read heap memory.
3649 kWriteSideEffects, // Intrinsic may write heap memory.
3650 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3651};
3652
3653enum IntrinsicExceptions {
3654 kNoThrow, // Intrinsic does not throw any exceptions.
3655 kCanThrow // Intrinsic may throw exceptions.
3656};
3657
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003658class HInvoke : public HInstruction {
3659 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003660 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003661
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003662 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003663
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003664 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003665 SetRawInputAt(index, argument);
3666 }
3667
Roland Levillain3e3d7332015-04-28 11:00:54 +01003668 // Return the number of arguments. This number can be lower than
3669 // the number of inputs returned by InputCount(), as some invoke
3670 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3671 // inputs at the end of their list of inputs.
3672 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3673
Vladimir Markoa1de9182016-02-25 11:37:38 +00003674 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003675
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003676 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003677 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003678
Vladimir Markoa1de9182016-02-25 11:37:38 +00003679 InvokeType GetOriginalInvokeType() const {
3680 return GetPackedField<OriginalInvokeTypeField>();
3681 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003682
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003683 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003684 return intrinsic_;
3685 }
3686
Aart Bik5d75afe2015-12-14 11:57:01 -08003687 void SetIntrinsic(Intrinsics intrinsic,
3688 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3689 IntrinsicSideEffects side_effects,
3690 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003691
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003692 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003693 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003694 }
3695
Vladimir Markoa1de9182016-02-25 11:37:38 +00003696 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003697
3698 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3699
3700 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3701 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3702 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003703
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003704 uint32_t* GetIntrinsicOptimizations() {
3705 return &intrinsic_optimizations_;
3706 }
3707
3708 const uint32_t* GetIntrinsicOptimizations() const {
3709 return &intrinsic_optimizations_;
3710 }
3711
3712 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3713
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003714 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003715
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003716 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003717 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3718 static constexpr size_t kFieldOriginalInvokeTypeSize =
3719 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3720 static constexpr size_t kFieldReturnType =
3721 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3722 static constexpr size_t kFieldReturnTypeSize =
3723 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3724 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3725 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3726 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3727 using OriginalInvokeTypeField =
3728 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3729 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3730
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003731 HInvoke(ArenaAllocator* arena,
3732 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003733 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003734 Primitive::Type return_type,
3735 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003736 uint32_t dex_method_index,
3737 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003738 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003739 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003740 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003741 inputs_(number_of_arguments + number_of_other_inputs,
3742 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003743 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003744 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003745 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003746 SetPackedField<ReturnTypeField>(return_type);
3747 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3748 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003749 }
3750
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003751 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003752 return inputs_[index];
3753 }
3754
David Brazdil1abb4192015-02-17 18:33:36 +00003755 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003756 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003757 }
3758
Vladimir Markoa1de9182016-02-25 11:37:38 +00003759 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003760
Roland Levillain3e3d7332015-04-28 11:00:54 +01003761 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003762 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003763 const uint32_t dex_method_index_;
3764 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003765
3766 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3767 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003768
3769 private:
3770 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3771};
3772
Calin Juravle175dc732015-08-25 15:42:32 +01003773class HInvokeUnresolved : public HInvoke {
3774 public:
3775 HInvokeUnresolved(ArenaAllocator* arena,
3776 uint32_t number_of_arguments,
3777 Primitive::Type return_type,
3778 uint32_t dex_pc,
3779 uint32_t dex_method_index,
3780 InvokeType invoke_type)
3781 : HInvoke(arena,
3782 number_of_arguments,
3783 0u /* number_of_other_inputs */,
3784 return_type,
3785 dex_pc,
3786 dex_method_index,
3787 invoke_type) {
3788 }
3789
3790 DECLARE_INSTRUCTION(InvokeUnresolved);
3791
3792 private:
3793 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3794};
3795
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003796class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003797 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003798 // Requirements of this method call regarding the class
3799 // initialization (clinit) check of its declaring class.
3800 enum class ClinitCheckRequirement {
3801 kNone, // Class already initialized.
3802 kExplicit, // Static call having explicit clinit check as last input.
3803 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003804 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003805 };
3806
Vladimir Marko58155012015-08-19 12:49:41 +00003807 // Determines how to load the target ArtMethod*.
3808 enum class MethodLoadKind {
3809 // Use a String init ArtMethod* loaded from Thread entrypoints.
3810 kStringInit,
3811
3812 // Use the method's own ArtMethod* loaded by the register allocator.
3813 kRecursive,
3814
3815 // Use ArtMethod* at a known address, embed the direct address in the code.
3816 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3817 kDirectAddress,
3818
3819 // Use ArtMethod* at an address that will be known at link time, embed the direct
3820 // address in the code. If the image is relocatable, emit .patch_oat entry.
3821 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3822 // the image relocatable or not.
3823 kDirectAddressWithFixup,
3824
Vladimir Markoa1de9182016-02-25 11:37:38 +00003825 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003826 // Used when we need to use the dex cache, for example for invoke-static that
3827 // may cause class initialization (the entry may point to a resolution method),
3828 // and we know that we can access the dex cache arrays using a PC-relative load.
3829 kDexCachePcRelative,
3830
3831 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3832 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3833 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3834 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3835 kDexCacheViaMethod,
3836 };
3837
3838 // Determines the location of the code pointer.
3839 enum class CodePtrLocation {
3840 // Recursive call, use local PC-relative call instruction.
3841 kCallSelf,
3842
3843 // Use PC-relative call instruction patched at link time.
3844 // Used for calls within an oat file, boot->boot or app->app.
3845 kCallPCRelative,
3846
3847 // Call to a known target address, embed the direct address in code.
3848 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3849 kCallDirect,
3850
3851 // Call to a target address that will be known at link time, embed the direct
3852 // address in code. If the image is relocatable, emit .patch_oat entry.
3853 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3854 // the image relocatable or not.
3855 kCallDirectWithFixup,
3856
3857 // Use code pointer from the ArtMethod*.
3858 // Used when we don't know the target code. This is also the last-resort-kind used when
3859 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3860 kCallArtMethod,
3861 };
3862
3863 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003864 MethodLoadKind method_load_kind;
3865 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003866 // The method load data holds
3867 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3868 // Note that there are multiple string init methods, each having its own offset.
3869 // - the method address for kDirectAddress
3870 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003871 uint64_t method_load_data;
3872 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003873 };
3874
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003875 HInvokeStaticOrDirect(ArenaAllocator* arena,
3876 uint32_t number_of_arguments,
3877 Primitive::Type return_type,
3878 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003879 uint32_t method_index,
3880 MethodReference target_method,
3881 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003882 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003883 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003884 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003885 : HInvoke(arena,
3886 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003887 // There is potentially one extra argument for the HCurrentMethod node, and
3888 // potentially one other if the clinit check is explicit, and potentially
3889 // one other if the method is a string factory.
3890 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3891 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3892 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003893 return_type,
3894 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003895 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003896 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003897 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003898 dispatch_info_(dispatch_info) {
3899 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3900 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3901 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003902
Vladimir Markodc151b22015-10-15 18:02:30 +01003903 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003904 bool had_current_method_input = HasCurrentMethodInput();
3905 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3906
3907 // Using the current method is the default and once we find a better
3908 // method load kind, we should not go back to using the current method.
3909 DCHECK(had_current_method_input || !needs_current_method_input);
3910
3911 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003912 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3913 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003914 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003915 dispatch_info_ = dispatch_info;
3916 }
3917
Vladimir Markoc53c0792015-11-19 15:48:33 +00003918 void AddSpecialInput(HInstruction* input) {
3919 // We allow only one special input.
3920 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3921 DCHECK(InputCount() == GetSpecialInputIndex() ||
3922 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3923 InsertInputAt(GetSpecialInputIndex(), input);
3924 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003925
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003926 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003927 // We access the method via the dex cache so we can't do an implicit null check.
3928 // TODO: for intrinsics we can generate implicit null checks.
3929 return false;
3930 }
3931
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003932 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003933 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003934 }
3935
Vladimir Markoc53c0792015-11-19 15:48:33 +00003936 // Get the index of the special input, if any.
3937 //
David Brazdil6de19382016-01-08 17:37:10 +00003938 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3939 // method pointer; otherwise there may be one platform-specific special input,
3940 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003941 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003942 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003943
Vladimir Markoa1de9182016-02-25 11:37:38 +00003944 InvokeType GetOptimizedInvokeType() const {
3945 return GetPackedField<OptimizedInvokeTypeField>();
3946 }
3947
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003948 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003949 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003950 }
3951
Vladimir Marko58155012015-08-19 12:49:41 +00003952 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3953 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3954 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003955 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003956 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003957 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003958 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003959 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3960 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003961 bool HasCurrentMethodInput() const {
3962 // This function can be called only after the invoke has been fully initialized by the builder.
3963 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003964 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003965 return true;
3966 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003967 DCHECK(InputCount() == GetSpecialInputIndex() ||
3968 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003969 return false;
3970 }
3971 }
Vladimir Marko58155012015-08-19 12:49:41 +00003972 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3973 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003974 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003975
3976 int32_t GetStringInitOffset() const {
3977 DCHECK(IsStringInit());
3978 return dispatch_info_.method_load_data;
3979 }
3980
3981 uint64_t GetMethodAddress() const {
3982 DCHECK(HasMethodAddress());
3983 return dispatch_info_.method_load_data;
3984 }
3985
3986 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003987 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003988 return dispatch_info_.method_load_data;
3989 }
3990
3991 uint64_t GetDirectCodePtr() const {
3992 DCHECK(HasDirectCodePtr());
3993 return dispatch_info_.direct_code_ptr;
3994 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003995
Vladimir Markoa1de9182016-02-25 11:37:38 +00003996 ClinitCheckRequirement GetClinitCheckRequirement() const {
3997 return GetPackedField<ClinitCheckRequirementField>();
3998 }
Calin Juravle68ad6492015-08-18 17:08:12 +01003999
Roland Levillain4c0eb422015-04-24 16:43:49 +01004000 // Is this instruction a call to a static method?
4001 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004002 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004003 }
4004
Vladimir Markofbb184a2015-11-13 14:47:00 +00004005 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4006 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4007 // instruction; only relevant for static calls with explicit clinit check.
4008 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004009 DCHECK(IsStaticWithExplicitClinitCheck());
4010 size_t last_input_index = InputCount() - 1;
4011 HInstruction* last_input = InputAt(last_input_index);
4012 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004013 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004014 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004015 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004016 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004017 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004018 }
4019
David Brazdilbc9ab162016-01-20 14:50:19 +00004020 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00004021 DCHECK(IsStringInit());
4022 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00004023 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00004024 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004025 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00004026 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004027 }
4028
Roland Levillain4c0eb422015-04-24 16:43:49 +01004029 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004030 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004031 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004032 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004033 }
4034
4035 // Is this a call to a static method whose declaring class has an
4036 // implicit intialization check requirement?
4037 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004038 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004039 }
4040
Vladimir Markob554b5a2015-11-06 12:57:55 +00004041 // Does this method load kind need the current method as an input?
4042 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4043 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4044 }
4045
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004046 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004047
Roland Levillain4c0eb422015-04-24 16:43:49 +01004048 protected:
4049 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
4050 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
4051 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
4052 HInstruction* input = input_record.GetInstruction();
4053 // `input` is the last input of a static invoke marked as having
4054 // an explicit clinit check. It must either be:
4055 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4056 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4057 DCHECK(input != nullptr);
4058 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
4059 }
4060 return input_record;
4061 }
4062
Vladimir Markoc53c0792015-11-19 15:48:33 +00004063 void InsertInputAt(size_t index, HInstruction* input);
4064 void RemoveInputAt(size_t index);
4065
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004066 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004067 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4068 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4069 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4070 static constexpr size_t kFieldClinitCheckRequirement =
4071 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4072 static constexpr size_t kFieldClinitCheckRequirementSize =
4073 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4074 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4075 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4076 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4077 "Too many packed fields.");
4078 using OptimizedInvokeTypeField =
4079 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4080 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4081 kFieldClinitCheckRequirement,
4082 kFieldClinitCheckRequirementSize>;
4083
Vladimir Marko58155012015-08-19 12:49:41 +00004084 // The target method may refer to different dex file or method index than the original
4085 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4086 // in derived class to increase visibility.
4087 MethodReference target_method_;
4088 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004089
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004090 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004091};
Vladimir Markof64242a2015-12-01 14:58:23 +00004092std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004093std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004094
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004095class HInvokeVirtual : public HInvoke {
4096 public:
4097 HInvokeVirtual(ArenaAllocator* arena,
4098 uint32_t number_of_arguments,
4099 Primitive::Type return_type,
4100 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004101 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004102 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004103 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004104 vtable_index_(vtable_index) {}
4105
Calin Juravle641547a2015-04-21 22:08:51 +01004106 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004107 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004108 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004109 }
4110
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004111 uint32_t GetVTableIndex() const { return vtable_index_; }
4112
4113 DECLARE_INSTRUCTION(InvokeVirtual);
4114
4115 private:
4116 const uint32_t vtable_index_;
4117
4118 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4119};
4120
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004121class HInvokeInterface : public HInvoke {
4122 public:
4123 HInvokeInterface(ArenaAllocator* arena,
4124 uint32_t number_of_arguments,
4125 Primitive::Type return_type,
4126 uint32_t dex_pc,
4127 uint32_t dex_method_index,
4128 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004129 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004130 imt_index_(imt_index) {}
4131
Calin Juravle641547a2015-04-21 22:08:51 +01004132 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004133 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004134 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004135 }
4136
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004137 uint32_t GetImtIndex() const { return imt_index_; }
4138 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4139
4140 DECLARE_INSTRUCTION(InvokeInterface);
4141
4142 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004143 const uint32_t imt_index_;
4144
4145 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4146};
4147
Roland Levillain88cb1752014-10-20 16:36:47 +01004148class HNeg : public HUnaryOperation {
4149 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004150 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004151 : HUnaryOperation(result_type, input, dex_pc) {
4152 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4153 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004154
Roland Levillain9867bc72015-08-05 10:21:34 +01004155 template <typename T> T Compute(T x) const { return -x; }
4156
4157 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004158 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004159 }
4160 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004161 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004162 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004163 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4164 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4165 }
4166 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4167 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4168 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004169
Roland Levillain88cb1752014-10-20 16:36:47 +01004170 DECLARE_INSTRUCTION(Neg);
4171
4172 private:
4173 DISALLOW_COPY_AND_ASSIGN(HNeg);
4174};
4175
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004176class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004177 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004178 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004179 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004180 uint32_t dex_pc,
4181 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004182 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004183 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004184 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004185 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004186 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004187 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004188 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004189 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004190 }
4191
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004192 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004193 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004194
4195 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004196 bool NeedsEnvironment() const OVERRIDE { return true; }
4197
Mingyao Yang0c365e62015-03-31 15:09:29 -07004198 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4199 bool CanThrow() const OVERRIDE { return true; }
4200
Calin Juravle10e244f2015-01-26 18:54:32 +00004201 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004202
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004203 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4204
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004205 DECLARE_INSTRUCTION(NewArray);
4206
4207 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004208 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004209 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004210 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004211
4212 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4213};
4214
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004215class HAdd : public HBinaryOperation {
4216 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004217 HAdd(Primitive::Type result_type,
4218 HInstruction* left,
4219 HInstruction* right,
4220 uint32_t dex_pc = kNoDexPc)
4221 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004222
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004223 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004224
Roland Levillain9867bc72015-08-05 10:21:34 +01004225 template <typename T> T Compute(T x, T y) const { return x + y; }
4226
4227 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004228 return GetBlock()->GetGraph()->GetIntConstant(
4229 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004230 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004231 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004232 return GetBlock()->GetGraph()->GetLongConstant(
4233 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004234 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004235 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4236 return GetBlock()->GetGraph()->GetFloatConstant(
4237 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4238 }
4239 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4240 return GetBlock()->GetGraph()->GetDoubleConstant(
4241 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4242 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004243
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004244 DECLARE_INSTRUCTION(Add);
4245
4246 private:
4247 DISALLOW_COPY_AND_ASSIGN(HAdd);
4248};
4249
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004250class HSub : public HBinaryOperation {
4251 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004252 HSub(Primitive::Type result_type,
4253 HInstruction* left,
4254 HInstruction* right,
4255 uint32_t dex_pc = kNoDexPc)
4256 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004257
Roland Levillain9867bc72015-08-05 10:21:34 +01004258 template <typename T> T Compute(T x, T y) const { return x - y; }
4259
4260 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004261 return GetBlock()->GetGraph()->GetIntConstant(
4262 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004263 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004264 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004265 return GetBlock()->GetGraph()->GetLongConstant(
4266 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004267 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004268 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4269 return GetBlock()->GetGraph()->GetFloatConstant(
4270 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4271 }
4272 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4273 return GetBlock()->GetGraph()->GetDoubleConstant(
4274 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4275 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004276
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004277 DECLARE_INSTRUCTION(Sub);
4278
4279 private:
4280 DISALLOW_COPY_AND_ASSIGN(HSub);
4281};
4282
Calin Juravle34bacdf2014-10-07 20:23:36 +01004283class HMul : public HBinaryOperation {
4284 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004285 HMul(Primitive::Type result_type,
4286 HInstruction* left,
4287 HInstruction* right,
4288 uint32_t dex_pc = kNoDexPc)
4289 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004290
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004291 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004292
Roland Levillain9867bc72015-08-05 10:21:34 +01004293 template <typename T> T Compute(T x, T y) const { return x * y; }
4294
4295 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004296 return GetBlock()->GetGraph()->GetIntConstant(
4297 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004298 }
4299 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004300 return GetBlock()->GetGraph()->GetLongConstant(
4301 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004302 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004303 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4304 return GetBlock()->GetGraph()->GetFloatConstant(
4305 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4306 }
4307 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4308 return GetBlock()->GetGraph()->GetDoubleConstant(
4309 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4310 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004311
4312 DECLARE_INSTRUCTION(Mul);
4313
4314 private:
4315 DISALLOW_COPY_AND_ASSIGN(HMul);
4316};
4317
Calin Juravle7c4954d2014-10-28 16:57:40 +00004318class HDiv : public HBinaryOperation {
4319 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004320 HDiv(Primitive::Type result_type,
4321 HInstruction* left,
4322 HInstruction* right,
4323 uint32_t dex_pc)
4324 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004325
Roland Levillain9867bc72015-08-05 10:21:34 +01004326 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004327 T ComputeIntegral(T x, T y) const {
4328 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004329 // Our graph structure ensures we never have 0 for `y` during
4330 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004331 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004332 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004333 return (y == -1) ? -x : x / y;
4334 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004335
Roland Levillain31dd3d62016-02-16 12:21:02 +00004336 template <typename T>
4337 T ComputeFP(T x, T y) const {
4338 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4339 return x / y;
4340 }
4341
Roland Levillain9867bc72015-08-05 10:21:34 +01004342 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004343 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004344 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004345 }
4346 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004347 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004348 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4349 }
4350 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4351 return GetBlock()->GetGraph()->GetFloatConstant(
4352 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4353 }
4354 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4355 return GetBlock()->GetGraph()->GetDoubleConstant(
4356 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004357 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004358
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004359 static SideEffects SideEffectsForArchRuntimeCalls() {
4360 // The generated code can use a runtime call.
4361 return SideEffects::CanTriggerGC();
4362 }
4363
Calin Juravle7c4954d2014-10-28 16:57:40 +00004364 DECLARE_INSTRUCTION(Div);
4365
4366 private:
4367 DISALLOW_COPY_AND_ASSIGN(HDiv);
4368};
4369
Calin Juravlebacfec32014-11-14 15:54:36 +00004370class HRem : public HBinaryOperation {
4371 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004372 HRem(Primitive::Type result_type,
4373 HInstruction* left,
4374 HInstruction* right,
4375 uint32_t dex_pc)
4376 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004377
Roland Levillain9867bc72015-08-05 10:21:34 +01004378 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004379 T ComputeIntegral(T x, T y) const {
4380 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004381 // Our graph structure ensures we never have 0 for `y` during
4382 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004383 DCHECK_NE(y, 0);
4384 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4385 return (y == -1) ? 0 : x % y;
4386 }
4387
Roland Levillain31dd3d62016-02-16 12:21:02 +00004388 template <typename T>
4389 T ComputeFP(T x, T y) const {
4390 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4391 return std::fmod(x, y);
4392 }
4393
Roland Levillain9867bc72015-08-05 10:21:34 +01004394 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004395 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004396 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004397 }
4398 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004399 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004400 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004401 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004402 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4403 return GetBlock()->GetGraph()->GetFloatConstant(
4404 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4405 }
4406 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4407 return GetBlock()->GetGraph()->GetDoubleConstant(
4408 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4409 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004410
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004411 static SideEffects SideEffectsForArchRuntimeCalls() {
4412 return SideEffects::CanTriggerGC();
4413 }
4414
Calin Juravlebacfec32014-11-14 15:54:36 +00004415 DECLARE_INSTRUCTION(Rem);
4416
4417 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004418 DISALLOW_COPY_AND_ASSIGN(HRem);
4419};
4420
Calin Juravled0d48522014-11-04 16:40:20 +00004421class HDivZeroCheck : public HExpression<1> {
4422 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004423 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4424 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004425 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004426 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004427 SetRawInputAt(0, value);
4428 }
4429
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004430 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4431
Calin Juravled0d48522014-11-04 16:40:20 +00004432 bool CanBeMoved() const OVERRIDE { return true; }
4433
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004434 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004435 return true;
4436 }
4437
4438 bool NeedsEnvironment() const OVERRIDE { return true; }
4439 bool CanThrow() const OVERRIDE { return true; }
4440
Calin Juravled0d48522014-11-04 16:40:20 +00004441 DECLARE_INSTRUCTION(DivZeroCheck);
4442
4443 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004444 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4445};
4446
Calin Juravle9aec02f2014-11-18 23:06:35 +00004447class HShl : public HBinaryOperation {
4448 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004449 HShl(Primitive::Type result_type,
4450 HInstruction* left,
4451 HInstruction* right,
4452 uint32_t dex_pc = kNoDexPc)
4453 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004454
Roland Levillain9867bc72015-08-05 10:21:34 +01004455 template <typename T, typename U, typename V>
4456 T Compute(T x, U y, V max_shift_value) const {
4457 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4458 "V is not the unsigned integer type corresponding to T");
4459 return x << (y & max_shift_value);
4460 }
4461
4462 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4463 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004464 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004465 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004466 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4467 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004468 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004469 }
4470 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4471 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004472 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004473 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004474 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4475 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4476 LOG(FATAL) << DebugName() << " is not defined for float values";
4477 UNREACHABLE();
4478 }
4479 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4480 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4481 LOG(FATAL) << DebugName() << " is not defined for double values";
4482 UNREACHABLE();
4483 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004484
4485 DECLARE_INSTRUCTION(Shl);
4486
4487 private:
4488 DISALLOW_COPY_AND_ASSIGN(HShl);
4489};
4490
4491class HShr : public HBinaryOperation {
4492 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004493 HShr(Primitive::Type result_type,
4494 HInstruction* left,
4495 HInstruction* right,
4496 uint32_t dex_pc = kNoDexPc)
4497 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004498
Roland Levillain9867bc72015-08-05 10:21:34 +01004499 template <typename T, typename U, typename V>
4500 T Compute(T x, U y, V max_shift_value) const {
4501 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4502 "V is not the unsigned integer type corresponding to T");
4503 return x >> (y & max_shift_value);
4504 }
4505
4506 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4507 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004508 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004509 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004510 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4511 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004512 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004513 }
4514 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4515 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004516 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004517 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004518 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4519 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4520 LOG(FATAL) << DebugName() << " is not defined for float values";
4521 UNREACHABLE();
4522 }
4523 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4524 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4525 LOG(FATAL) << DebugName() << " is not defined for double values";
4526 UNREACHABLE();
4527 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004528
4529 DECLARE_INSTRUCTION(Shr);
4530
4531 private:
4532 DISALLOW_COPY_AND_ASSIGN(HShr);
4533};
4534
4535class HUShr : public HBinaryOperation {
4536 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004537 HUShr(Primitive::Type result_type,
4538 HInstruction* left,
4539 HInstruction* right,
4540 uint32_t dex_pc = kNoDexPc)
4541 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004542
Roland Levillain9867bc72015-08-05 10:21:34 +01004543 template <typename T, typename U, typename V>
4544 T Compute(T x, U y, V max_shift_value) const {
4545 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4546 "V is not the unsigned integer type corresponding to T");
4547 V ux = static_cast<V>(x);
4548 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004549 }
4550
Roland Levillain9867bc72015-08-05 10:21:34 +01004551 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4552 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004553 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004554 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004555 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4556 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004557 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004558 }
4559 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4560 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004561 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004562 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004563 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4564 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4565 LOG(FATAL) << DebugName() << " is not defined for float values";
4566 UNREACHABLE();
4567 }
4568 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4569 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4570 LOG(FATAL) << DebugName() << " is not defined for double values";
4571 UNREACHABLE();
4572 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004573
4574 DECLARE_INSTRUCTION(UShr);
4575
4576 private:
4577 DISALLOW_COPY_AND_ASSIGN(HUShr);
4578};
4579
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004580class HAnd : public HBinaryOperation {
4581 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004582 HAnd(Primitive::Type result_type,
4583 HInstruction* left,
4584 HInstruction* right,
4585 uint32_t dex_pc = kNoDexPc)
4586 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004587
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004588 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004589
Roland Levillaine53bd812016-02-24 14:54:18 +00004590 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004591
4592 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004593 return GetBlock()->GetGraph()->GetIntConstant(
4594 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004595 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004596 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004597 return GetBlock()->GetGraph()->GetLongConstant(
4598 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004599 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004600 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4601 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4602 LOG(FATAL) << DebugName() << " is not defined for float values";
4603 UNREACHABLE();
4604 }
4605 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4606 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4607 LOG(FATAL) << DebugName() << " is not defined for double values";
4608 UNREACHABLE();
4609 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004610
4611 DECLARE_INSTRUCTION(And);
4612
4613 private:
4614 DISALLOW_COPY_AND_ASSIGN(HAnd);
4615};
4616
4617class HOr : public HBinaryOperation {
4618 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004619 HOr(Primitive::Type result_type,
4620 HInstruction* left,
4621 HInstruction* right,
4622 uint32_t dex_pc = kNoDexPc)
4623 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004624
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004625 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004626
Roland Levillaine53bd812016-02-24 14:54:18 +00004627 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004628
4629 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004630 return GetBlock()->GetGraph()->GetIntConstant(
4631 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004632 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004633 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004634 return GetBlock()->GetGraph()->GetLongConstant(
4635 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004636 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004637 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4638 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4639 LOG(FATAL) << DebugName() << " is not defined for float values";
4640 UNREACHABLE();
4641 }
4642 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4643 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4644 LOG(FATAL) << DebugName() << " is not defined for double values";
4645 UNREACHABLE();
4646 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004647
4648 DECLARE_INSTRUCTION(Or);
4649
4650 private:
4651 DISALLOW_COPY_AND_ASSIGN(HOr);
4652};
4653
4654class HXor : public HBinaryOperation {
4655 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004656 HXor(Primitive::Type result_type,
4657 HInstruction* left,
4658 HInstruction* right,
4659 uint32_t dex_pc = kNoDexPc)
4660 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004661
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004662 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004663
Roland Levillaine53bd812016-02-24 14:54:18 +00004664 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004665
4666 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004667 return GetBlock()->GetGraph()->GetIntConstant(
4668 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004669 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004670 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004671 return GetBlock()->GetGraph()->GetLongConstant(
4672 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004673 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004674 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4675 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4676 LOG(FATAL) << DebugName() << " is not defined for float values";
4677 UNREACHABLE();
4678 }
4679 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4680 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4681 LOG(FATAL) << DebugName() << " is not defined for double values";
4682 UNREACHABLE();
4683 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004684
4685 DECLARE_INSTRUCTION(Xor);
4686
4687 private:
4688 DISALLOW_COPY_AND_ASSIGN(HXor);
4689};
4690
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004691class HRor : public HBinaryOperation {
4692 public:
4693 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004694 : HBinaryOperation(result_type, value, distance) {
4695 if (kIsDebugBuild) {
4696 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4697 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
4698 }
4699 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004700
4701 template <typename T, typename U, typename V>
4702 T Compute(T x, U y, V max_shift_value) const {
4703 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4704 "V is not the unsigned integer type corresponding to T");
4705 V ux = static_cast<V>(x);
4706 if ((y & max_shift_value) == 0) {
4707 return static_cast<T>(ux);
4708 } else {
4709 const V reg_bits = sizeof(T) * 8;
4710 return static_cast<T>(ux >> (y & max_shift_value)) |
4711 (x << (reg_bits - (y & max_shift_value)));
4712 }
4713 }
4714
4715 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4716 return GetBlock()->GetGraph()->GetIntConstant(
4717 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4718 }
4719 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4720 return GetBlock()->GetGraph()->GetLongConstant(
4721 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4722 }
4723 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4724 return GetBlock()->GetGraph()->GetLongConstant(
4725 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4726 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004727 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4728 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4729 LOG(FATAL) << DebugName() << " is not defined for float values";
4730 UNREACHABLE();
4731 }
4732 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4733 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4734 LOG(FATAL) << DebugName() << " is not defined for double values";
4735 UNREACHABLE();
4736 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004737
4738 DECLARE_INSTRUCTION(Ror);
4739
4740 private:
4741 DISALLOW_COPY_AND_ASSIGN(HRor);
4742};
4743
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004744// The value of a parameter in this method. Its location depends on
4745// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004746class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004747 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004748 HParameterValue(const DexFile& dex_file,
4749 uint16_t type_index,
4750 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004751 Primitive::Type parameter_type,
4752 bool is_this = false)
4753 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004754 dex_file_(dex_file),
4755 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004756 index_(index) {
4757 SetPackedFlag<kFlagIsThis>(is_this);
4758 SetPackedFlag<kFlagCanBeNull>(!is_this);
4759 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004760
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004761 const DexFile& GetDexFile() const { return dex_file_; }
4762 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004763 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004764 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004765
Vladimir Markoa1de9182016-02-25 11:37:38 +00004766 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4767 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004768
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004769 DECLARE_INSTRUCTION(ParameterValue);
4770
4771 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004772 // Whether or not the parameter value corresponds to 'this' argument.
4773 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4774 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4775 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4776 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4777 "Too many packed fields.");
4778
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004779 const DexFile& dex_file_;
4780 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004781 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004782 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004783 const uint8_t index_;
4784
4785 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4786};
4787
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004788class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004789 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004790 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4791 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004792
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004793 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004794 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004795 return true;
4796 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004797
Roland Levillain9867bc72015-08-05 10:21:34 +01004798 template <typename T> T Compute(T x) const { return ~x; }
4799
4800 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004801 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004802 }
4803 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004804 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004805 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004806 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4807 LOG(FATAL) << DebugName() << " is not defined for float values";
4808 UNREACHABLE();
4809 }
4810 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4811 LOG(FATAL) << DebugName() << " is not defined for double values";
4812 UNREACHABLE();
4813 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004814
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004815 DECLARE_INSTRUCTION(Not);
4816
4817 private:
4818 DISALLOW_COPY_AND_ASSIGN(HNot);
4819};
4820
David Brazdil66d126e2015-04-03 16:02:44 +01004821class HBooleanNot : public HUnaryOperation {
4822 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004823 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4824 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004825
4826 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004827 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004828 return true;
4829 }
4830
Roland Levillain9867bc72015-08-05 10:21:34 +01004831 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004832 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004833 return !x;
4834 }
4835
Roland Levillain9867bc72015-08-05 10:21:34 +01004836 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004837 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004838 }
4839 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4840 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004841 UNREACHABLE();
4842 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004843 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4844 LOG(FATAL) << DebugName() << " is not defined for float values";
4845 UNREACHABLE();
4846 }
4847 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4848 LOG(FATAL) << DebugName() << " is not defined for double values";
4849 UNREACHABLE();
4850 }
David Brazdil66d126e2015-04-03 16:02:44 +01004851
4852 DECLARE_INSTRUCTION(BooleanNot);
4853
4854 private:
4855 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4856};
4857
Roland Levillaindff1f282014-11-05 14:15:05 +00004858class HTypeConversion : public HExpression<1> {
4859 public:
4860 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004861 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004862 : HExpression(result_type,
4863 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4864 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004865 SetRawInputAt(0, input);
4866 DCHECK_NE(input->GetType(), result_type);
4867 }
4868
4869 HInstruction* GetInput() const { return InputAt(0); }
4870 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4871 Primitive::Type GetResultType() const { return GetType(); }
4872
4873 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004874 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004875
Mark Mendelle82549b2015-05-06 10:55:34 -04004876 // Try to statically evaluate the conversion and return a HConstant
4877 // containing the result. If the input cannot be converted, return nullptr.
4878 HConstant* TryStaticEvaluation() const;
4879
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004880 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4881 Primitive::Type result_type) {
4882 // Some architectures may not require the 'GC' side effects, but at this point
4883 // in the compilation process we do not know what architecture we will
4884 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004885 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4886 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004887 return SideEffects::CanTriggerGC();
4888 }
4889 return SideEffects::None();
4890 }
4891
Roland Levillaindff1f282014-11-05 14:15:05 +00004892 DECLARE_INSTRUCTION(TypeConversion);
4893
4894 private:
4895 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4896};
4897
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004898static constexpr uint32_t kNoRegNumber = -1;
4899
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004900class HPhi : public HInstruction {
4901 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004902 HPhi(ArenaAllocator* arena,
4903 uint32_t reg_number,
4904 size_t number_of_inputs,
4905 Primitive::Type type,
4906 uint32_t dex_pc = kNoDexPc)
4907 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004908 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004909 reg_number_(reg_number) {
4910 SetPackedField<TypeField>(ToPhiType(type));
4911 DCHECK_NE(GetType(), Primitive::kPrimVoid);
4912 // Phis are constructed live and marked dead if conflicting or unused.
4913 // Individual steps of SsaBuilder should assume that if a phi has been
4914 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4915 SetPackedFlag<kFlagIsLive>(true);
4916 SetPackedFlag<kFlagCanBeNull>(true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004917 }
4918
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004919 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4920 static Primitive::Type ToPhiType(Primitive::Type type) {
4921 switch (type) {
4922 case Primitive::kPrimBoolean:
4923 case Primitive::kPrimByte:
4924 case Primitive::kPrimShort:
4925 case Primitive::kPrimChar:
4926 return Primitive::kPrimInt;
4927 default:
4928 return type;
4929 }
4930 }
4931
David Brazdilffee3d32015-07-06 11:48:53 +01004932 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4933
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004934 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004935
4936 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004937 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004938
Vladimir Markoa1de9182016-02-25 11:37:38 +00004939 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
David Brazdil4833f5a2015-12-16 10:37:39 +00004940 void SetType(Primitive::Type new_type) {
4941 // Make sure that only valid type changes occur. The following are allowed:
4942 // (1) int -> float/ref (primitive type propagation),
4943 // (2) long -> double (primitive type propagation).
Vladimir Markoa1de9182016-02-25 11:37:38 +00004944 DCHECK(GetType() == new_type ||
4945 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4946 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4947 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4948 SetPackedField<TypeField>(new_type);
David Brazdil4833f5a2015-12-16 10:37:39 +00004949 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004950
Vladimir Markoa1de9182016-02-25 11:37:38 +00004951 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4952 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004953
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004954 uint32_t GetRegNumber() const { return reg_number_; }
4955
Vladimir Markoa1de9182016-02-25 11:37:38 +00004956 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
4957 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
4958 bool IsDead() const { return !IsLive(); }
4959 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004960
David Brazdil77a48ae2015-09-15 12:34:04 +00004961 bool IsVRegEquivalentOf(HInstruction* other) const {
4962 return other != nullptr
4963 && other->IsPhi()
4964 && other->AsPhi()->GetBlock() == GetBlock()
4965 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4966 }
4967
Calin Juravlea4f88312015-04-16 12:57:19 +01004968 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4969 // An equivalent phi is a phi having the same dex register and type.
4970 // It assumes that phis with the same dex register are adjacent.
4971 HPhi* GetNextEquivalentPhiWithSameType() {
4972 HInstruction* next = GetNext();
4973 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4974 if (next->GetType() == GetType()) {
4975 return next->AsPhi();
4976 }
4977 next = next->GetNext();
4978 }
4979 return nullptr;
4980 }
4981
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004982 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004983
David Brazdil1abb4192015-02-17 18:33:36 +00004984 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004985 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004986 return inputs_[index];
4987 }
David Brazdil1abb4192015-02-17 18:33:36 +00004988
4989 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004990 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004991 }
4992
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004993 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004994 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
4995 static constexpr size_t kFieldTypeSize =
4996 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4997 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
4998 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
4999 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
5000 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5001 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
5002
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005003 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005004 const uint32_t reg_number_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005005
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005006 DISALLOW_COPY_AND_ASSIGN(HPhi);
5007};
5008
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005009class HNullCheck : public HExpression<1> {
5010 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005011 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5012 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005013 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005014 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005015 SetRawInputAt(0, value);
5016 }
5017
Calin Juravle10e244f2015-01-26 18:54:32 +00005018 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005019 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005020 return true;
5021 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005022
Calin Juravle10e244f2015-01-26 18:54:32 +00005023 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005024
Calin Juravle10e244f2015-01-26 18:54:32 +00005025 bool CanThrow() const OVERRIDE { return true; }
5026
5027 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005028
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005029
5030 DECLARE_INSTRUCTION(NullCheck);
5031
5032 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005033 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5034};
5035
5036class FieldInfo : public ValueObject {
5037 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005038 FieldInfo(MemberOffset field_offset,
5039 Primitive::Type field_type,
5040 bool is_volatile,
5041 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005042 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005043 const DexFile& dex_file,
5044 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005045 : field_offset_(field_offset),
5046 field_type_(field_type),
5047 is_volatile_(is_volatile),
5048 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005049 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005050 dex_file_(dex_file),
5051 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005052
5053 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005054 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005055 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005056 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005057 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005058 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005059 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005060
5061 private:
5062 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005063 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005064 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005065 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005066 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005067 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005068 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005069};
5070
5071class HInstanceFieldGet : public HExpression<1> {
5072 public:
5073 HInstanceFieldGet(HInstruction* value,
5074 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005075 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005076 bool is_volatile,
5077 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005078 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005079 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005080 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005081 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005082 : HExpression(field_type,
5083 SideEffects::FieldReadOfType(field_type, is_volatile),
5084 dex_pc),
5085 field_info_(field_offset,
5086 field_type,
5087 is_volatile,
5088 field_idx,
5089 declaring_class_def_index,
5090 dex_file,
5091 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005092 SetRawInputAt(0, value);
5093 }
5094
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005095 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005096
5097 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5098 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
5099 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005100 }
5101
Calin Juravle641547a2015-04-21 22:08:51 +01005102 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5103 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005104 }
5105
5106 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005107 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5108 }
5109
Calin Juravle52c48962014-12-16 17:02:57 +00005110 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005111 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005112 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005113 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005114
5115 DECLARE_INSTRUCTION(InstanceFieldGet);
5116
5117 private:
5118 const FieldInfo field_info_;
5119
5120 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5121};
5122
5123class HInstanceFieldSet : public HTemplateInstruction<2> {
5124 public:
5125 HInstanceFieldSet(HInstruction* object,
5126 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005127 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005128 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005129 bool is_volatile,
5130 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005131 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005132 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005133 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005134 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005135 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5136 dex_pc),
5137 field_info_(field_offset,
5138 field_type,
5139 is_volatile,
5140 field_idx,
5141 declaring_class_def_index,
5142 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005143 dex_cache) {
5144 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005145 SetRawInputAt(0, object);
5146 SetRawInputAt(1, value);
5147 }
5148
Calin Juravle641547a2015-04-21 22:08:51 +01005149 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5150 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005151 }
5152
Calin Juravle52c48962014-12-16 17:02:57 +00005153 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005154 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005155 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005156 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005157 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005158 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5159 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005160
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005161 DECLARE_INSTRUCTION(InstanceFieldSet);
5162
5163 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005164 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5165 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5166 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5167 "Too many packed fields.");
5168
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005169 const FieldInfo field_info_;
5170
5171 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5172};
5173
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005174class HArrayGet : public HExpression<2> {
5175 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005176 HArrayGet(HInstruction* array,
5177 HInstruction* index,
5178 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005179 uint32_t dex_pc,
5180 SideEffects additional_side_effects = SideEffects::None())
5181 : HExpression(type,
5182 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00005183 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005184 SetRawInputAt(0, array);
5185 SetRawInputAt(1, index);
5186 }
5187
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005188 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005189 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005190 return true;
5191 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005192 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005193 // TODO: We can be smarter here.
5194 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5195 // which generates the implicit null check. There are cases when these can be removed
5196 // to produce better code. If we ever add optimizations to do so we should allow an
5197 // implicit check here (as long as the address falls in the first page).
5198 return false;
5199 }
5200
David Brazdil4833f5a2015-12-16 10:37:39 +00005201 bool IsEquivalentOf(HArrayGet* other) const {
5202 bool result = (GetDexPc() == other->GetDexPc());
5203 if (kIsDebugBuild && result) {
5204 DCHECK_EQ(GetBlock(), other->GetBlock());
5205 DCHECK_EQ(GetArray(), other->GetArray());
5206 DCHECK_EQ(GetIndex(), other->GetIndex());
5207 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005208 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005209 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005210 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5211 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005212 }
5213 }
5214 return result;
5215 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005216
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005217 HInstruction* GetArray() const { return InputAt(0); }
5218 HInstruction* GetIndex() const { return InputAt(1); }
5219
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005220 DECLARE_INSTRUCTION(ArrayGet);
5221
5222 private:
5223 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5224};
5225
5226class HArraySet : public HTemplateInstruction<3> {
5227 public:
5228 HArraySet(HInstruction* array,
5229 HInstruction* index,
5230 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005231 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005232 uint32_t dex_pc,
5233 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005234 : HTemplateInstruction(
5235 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005236 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
5237 additional_side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005238 dex_pc) {
5239 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5240 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5241 SetPackedFlag<kFlagValueCanBeNull>(true);
5242 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005243 SetRawInputAt(0, array);
5244 SetRawInputAt(1, index);
5245 SetRawInputAt(2, value);
5246 }
5247
Calin Juravle77520bc2015-01-12 18:45:46 +00005248 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005249 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005250 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005251 }
5252
Mingyao Yang81014cb2015-06-02 03:16:27 -07005253 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005254 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005255
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005256 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005257 // TODO: Same as for ArrayGet.
5258 return false;
5259 }
5260
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005261 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005262 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005263 }
5264
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005265 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005266 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005267 }
5268
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005269 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005270 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005271 }
5272
Vladimir Markoa1de9182016-02-25 11:37:38 +00005273 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5274 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5275 bool StaticTypeOfArrayIsObjectArray() const {
5276 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5277 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005278
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005279 HInstruction* GetArray() const { return InputAt(0); }
5280 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005281 HInstruction* GetValue() const { return InputAt(2); }
5282
5283 Primitive::Type GetComponentType() const {
5284 // The Dex format does not type floating point index operations. Since the
5285 // `expected_component_type_` is set during building and can therefore not
5286 // be correct, we also check what is the value type. If it is a floating
5287 // point type, we must use that type.
5288 Primitive::Type value_type = GetValue()->GetType();
5289 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5290 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005291 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005292 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005293
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005294 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005295 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005296 }
5297
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005298 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5299 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5300 }
5301
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005302 DECLARE_INSTRUCTION(ArraySet);
5303
5304 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005305 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5306 static constexpr size_t kFieldExpectedComponentTypeSize =
5307 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5308 static constexpr size_t kFlagNeedsTypeCheck =
5309 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5310 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005311 // Cached information for the reference_type_info_ so that codegen
5312 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005313 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5314 static constexpr size_t kNumberOfArraySetPackedBits =
5315 kFlagStaticTypeOfArrayIsObjectArray + 1;
5316 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5317 using ExpectedComponentTypeField =
5318 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005319
5320 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5321};
5322
5323class HArrayLength : public HExpression<1> {
5324 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005325 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005326 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005327 // Note that arrays do not change length, so the instruction does not
5328 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005329 SetRawInputAt(0, array);
5330 }
5331
Calin Juravle77520bc2015-01-12 18:45:46 +00005332 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005333 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005334 return true;
5335 }
Calin Juravle641547a2015-04-21 22:08:51 +01005336 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5337 return obj == InputAt(0);
5338 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005339
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005340 DECLARE_INSTRUCTION(ArrayLength);
5341
5342 private:
5343 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5344};
5345
5346class HBoundsCheck : public HExpression<2> {
5347 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005348 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5349 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005350 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005351 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005352 DCHECK(index->GetType() == Primitive::kPrimInt);
5353 SetRawInputAt(0, index);
5354 SetRawInputAt(1, length);
5355 }
5356
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005357 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005358 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005359 return true;
5360 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005361
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005362 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005363
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005364 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005365
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005366 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005367
5368 DECLARE_INSTRUCTION(BoundsCheck);
5369
5370 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005371 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5372};
5373
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005374class HSuspendCheck : public HTemplateInstruction<0> {
5375 public:
5376 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005377 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005378
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005379 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005380 return true;
5381 }
5382
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005383 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5384 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005385
5386 DECLARE_INSTRUCTION(SuspendCheck);
5387
5388 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005389 // Only used for code generation, in order to share the same slow path between back edges
5390 // of a same loop.
5391 SlowPathCode* slow_path_;
5392
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005393 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5394};
5395
David Srbecky0cf44932015-12-09 14:09:59 +00005396// Pseudo-instruction which provides the native debugger with mapping information.
5397// It ensures that we can generate line number and local variables at this point.
5398class HNativeDebugInfo : public HTemplateInstruction<0> {
5399 public:
5400 explicit HNativeDebugInfo(uint32_t dex_pc)
5401 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5402
5403 bool NeedsEnvironment() const OVERRIDE {
5404 return true;
5405 }
5406
5407 DECLARE_INSTRUCTION(NativeDebugInfo);
5408
5409 private:
5410 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5411};
5412
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005413/**
5414 * Instruction to load a Class object.
5415 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005416class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005417 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005418 HLoadClass(HCurrentMethod* current_method,
5419 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005420 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005421 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005422 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005423 bool needs_access_check,
5424 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005425 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005426 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005427 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005428 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005429 // Referrers class should not need access check. We never inline unverified
5430 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005431 DCHECK(!is_referrers_class || !needs_access_check);
5432
5433 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5434 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5435 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5436 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005437 SetRawInputAt(0, current_method);
5438 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005439
5440 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005441
5442 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005443 // Note that we don't need to test for generate_clinit_check_.
5444 // Whether or not we need to generate the clinit check is processed in
5445 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005446 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005447 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005448 }
5449
5450 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5451
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005452 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005453 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005454
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005455 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005456 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005457 }
5458
Calin Juravle0ba218d2015-05-19 18:46:01 +01005459 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005460 // The entrypoint the code generator is going to call does not do
5461 // clinit of the class.
5462 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005463 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005464 }
5465
5466 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005467 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005468 (!IsReferrersClass() && !IsInDexCache()) ||
5469 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005470 }
5471
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005472
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005473 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005474 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005475 }
5476
Calin Juravleacf735c2015-02-12 15:25:22 +00005477 ReferenceTypeInfo GetLoadedClassRTI() {
5478 return loaded_class_rti_;
5479 }
5480
5481 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5482 // Make sure we only set exact types (the loaded class should never be merged).
5483 DCHECK(rti.IsExact());
5484 loaded_class_rti_ = rti;
5485 }
5486
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005487 const DexFile& GetDexFile() { return dex_file_; }
5488
Vladimir Markoa1de9182016-02-25 11:37:38 +00005489 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005490
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005491 static SideEffects SideEffectsForArchRuntimeCalls() {
5492 return SideEffects::CanTriggerGC();
5493 }
5494
Vladimir Markoa1de9182016-02-25 11:37:38 +00005495 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5496 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5497 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5498 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005499
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005500 DECLARE_INSTRUCTION(LoadClass);
5501
5502 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005503 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5504 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5505 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005506 // Whether this instruction must generate the initialization check.
5507 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005508 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5509 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5510 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5511
5512 const uint16_t type_index_;
5513 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005514
Calin Juravleacf735c2015-02-12 15:25:22 +00005515 ReferenceTypeInfo loaded_class_rti_;
5516
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005517 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5518};
5519
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005520class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005521 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005522 HLoadString(HCurrentMethod* current_method,
5523 uint32_t string_index,
5524 uint32_t dex_pc,
5525 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005526 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005527 string_index_(string_index) {
5528 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005529 SetRawInputAt(0, current_method);
5530 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005531
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005532 bool CanBeMoved() const OVERRIDE { return true; }
5533
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005534 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5535 return other->AsLoadString()->string_index_ == string_index_;
5536 }
5537
5538 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5539
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005540 uint32_t GetStringIndex() const { return string_index_; }
5541
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005542 // Will call the runtime if the string is not already in the dex cache.
5543 bool NeedsEnvironment() const OVERRIDE { return !IsInDexCache(); }
5544
Vladimir Markodc151b22015-10-15 18:02:30 +01005545 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005546 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005547 bool CanThrow() const OVERRIDE { return !IsInDexCache(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005548
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005549 static SideEffects SideEffectsForArchRuntimeCalls() {
5550 return SideEffects::CanTriggerGC();
5551 }
5552
Vladimir Markoa1de9182016-02-25 11:37:38 +00005553 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5554
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005555 DECLARE_INSTRUCTION(LoadString);
5556
5557 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005558 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
5559 static constexpr size_t kNumberOfLoadStringPackedBits = kFlagIsInDexCache + 1;
5560 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5561
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005562 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005563
5564 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5565};
5566
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005567/**
5568 * Performs an initialization check on its Class object input.
5569 */
5570class HClinitCheck : public HExpression<1> {
5571 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005572 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005573 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005574 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005575 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5576 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005577 SetRawInputAt(0, constant);
5578 }
5579
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005580 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005581 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005582 return true;
5583 }
5584
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005585 bool NeedsEnvironment() const OVERRIDE {
5586 // May call runtime to initialize the class.
5587 return true;
5588 }
5589
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005590 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005591
5592 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5593
5594 DECLARE_INSTRUCTION(ClinitCheck);
5595
5596 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005597 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5598};
5599
5600class HStaticFieldGet : public HExpression<1> {
5601 public:
5602 HStaticFieldGet(HInstruction* cls,
5603 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005604 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005605 bool is_volatile,
5606 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005607 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005608 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005609 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005610 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005611 : HExpression(field_type,
5612 SideEffects::FieldReadOfType(field_type, is_volatile),
5613 dex_pc),
5614 field_info_(field_offset,
5615 field_type,
5616 is_volatile,
5617 field_idx,
5618 declaring_class_def_index,
5619 dex_file,
5620 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005621 SetRawInputAt(0, cls);
5622 }
5623
Calin Juravle52c48962014-12-16 17:02:57 +00005624
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005625 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005626
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005627 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005628 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5629 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005630 }
5631
5632 size_t ComputeHashCode() const OVERRIDE {
5633 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5634 }
5635
Calin Juravle52c48962014-12-16 17:02:57 +00005636 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005637 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5638 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005639 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005640
5641 DECLARE_INSTRUCTION(StaticFieldGet);
5642
5643 private:
5644 const FieldInfo field_info_;
5645
5646 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5647};
5648
5649class HStaticFieldSet : public HTemplateInstruction<2> {
5650 public:
5651 HStaticFieldSet(HInstruction* cls,
5652 HInstruction* value,
5653 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005654 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005655 bool is_volatile,
5656 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005657 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005658 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005659 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005660 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005661 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5662 dex_pc),
5663 field_info_(field_offset,
5664 field_type,
5665 is_volatile,
5666 field_idx,
5667 declaring_class_def_index,
5668 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005669 dex_cache) {
5670 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005671 SetRawInputAt(0, cls);
5672 SetRawInputAt(1, value);
5673 }
5674
Calin Juravle52c48962014-12-16 17:02:57 +00005675 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005676 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5677 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005678 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005679
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005680 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005681 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5682 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005683
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005684 DECLARE_INSTRUCTION(StaticFieldSet);
5685
5686 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005687 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5688 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5689 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5690 "Too many packed fields.");
5691
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005692 const FieldInfo field_info_;
5693
5694 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5695};
5696
Calin Juravlee460d1d2015-09-29 04:52:17 +01005697class HUnresolvedInstanceFieldGet : public HExpression<1> {
5698 public:
5699 HUnresolvedInstanceFieldGet(HInstruction* obj,
5700 Primitive::Type field_type,
5701 uint32_t field_index,
5702 uint32_t dex_pc)
5703 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5704 field_index_(field_index) {
5705 SetRawInputAt(0, obj);
5706 }
5707
5708 bool NeedsEnvironment() const OVERRIDE { return true; }
5709 bool CanThrow() const OVERRIDE { return true; }
5710
5711 Primitive::Type GetFieldType() const { return GetType(); }
5712 uint32_t GetFieldIndex() const { return field_index_; }
5713
5714 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5715
5716 private:
5717 const uint32_t field_index_;
5718
5719 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5720};
5721
5722class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5723 public:
5724 HUnresolvedInstanceFieldSet(HInstruction* obj,
5725 HInstruction* value,
5726 Primitive::Type field_type,
5727 uint32_t field_index,
5728 uint32_t dex_pc)
5729 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005730 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005731 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005732 DCHECK_EQ(field_type, value->GetType());
5733 SetRawInputAt(0, obj);
5734 SetRawInputAt(1, value);
5735 }
5736
5737 bool NeedsEnvironment() const OVERRIDE { return true; }
5738 bool CanThrow() const OVERRIDE { return true; }
5739
Vladimir Markoa1de9182016-02-25 11:37:38 +00005740 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005741 uint32_t GetFieldIndex() const { return field_index_; }
5742
5743 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5744
5745 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005746 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5747 static constexpr size_t kFieldFieldTypeSize =
5748 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5749 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5750 kFieldFieldType + kFieldFieldTypeSize;
5751 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5752 "Too many packed fields.");
5753 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5754
Calin Juravlee460d1d2015-09-29 04:52:17 +01005755 const uint32_t field_index_;
5756
5757 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5758};
5759
5760class HUnresolvedStaticFieldGet : public HExpression<0> {
5761 public:
5762 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5763 uint32_t field_index,
5764 uint32_t dex_pc)
5765 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5766 field_index_(field_index) {
5767 }
5768
5769 bool NeedsEnvironment() const OVERRIDE { return true; }
5770 bool CanThrow() const OVERRIDE { return true; }
5771
5772 Primitive::Type GetFieldType() const { return GetType(); }
5773 uint32_t GetFieldIndex() const { return field_index_; }
5774
5775 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5776
5777 private:
5778 const uint32_t field_index_;
5779
5780 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5781};
5782
5783class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5784 public:
5785 HUnresolvedStaticFieldSet(HInstruction* value,
5786 Primitive::Type field_type,
5787 uint32_t field_index,
5788 uint32_t dex_pc)
5789 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005790 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005791 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005792 DCHECK_EQ(field_type, value->GetType());
5793 SetRawInputAt(0, value);
5794 }
5795
5796 bool NeedsEnvironment() const OVERRIDE { return true; }
5797 bool CanThrow() const OVERRIDE { return true; }
5798
Vladimir Markoa1de9182016-02-25 11:37:38 +00005799 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005800 uint32_t GetFieldIndex() const { return field_index_; }
5801
5802 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5803
5804 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005805 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5806 static constexpr size_t kFieldFieldTypeSize =
5807 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5808 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5809 kFieldFieldType + kFieldFieldTypeSize;
5810 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5811 "Too many packed fields.");
5812 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5813
Calin Juravlee460d1d2015-09-29 04:52:17 +01005814 const uint32_t field_index_;
5815
5816 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5817};
5818
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005819// Implement the move-exception DEX instruction.
5820class HLoadException : public HExpression<0> {
5821 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005822 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5823 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005824
David Brazdilbbd733e2015-08-18 17:48:17 +01005825 bool CanBeNull() const OVERRIDE { return false; }
5826
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005827 DECLARE_INSTRUCTION(LoadException);
5828
5829 private:
5830 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5831};
5832
David Brazdilcb1c0552015-08-04 16:22:25 +01005833// Implicit part of move-exception which clears thread-local exception storage.
5834// Must not be removed because the runtime expects the TLS to get cleared.
5835class HClearException : public HTemplateInstruction<0> {
5836 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005837 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5838 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005839
5840 DECLARE_INSTRUCTION(ClearException);
5841
5842 private:
5843 DISALLOW_COPY_AND_ASSIGN(HClearException);
5844};
5845
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005846class HThrow : public HTemplateInstruction<1> {
5847 public:
5848 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005849 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005850 SetRawInputAt(0, exception);
5851 }
5852
5853 bool IsControlFlow() const OVERRIDE { return true; }
5854
5855 bool NeedsEnvironment() const OVERRIDE { return true; }
5856
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005857 bool CanThrow() const OVERRIDE { return true; }
5858
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005859
5860 DECLARE_INSTRUCTION(Throw);
5861
5862 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005863 DISALLOW_COPY_AND_ASSIGN(HThrow);
5864};
5865
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005866/**
5867 * Implementation strategies for the code generator of a HInstanceOf
5868 * or `HCheckCast`.
5869 */
5870enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005871 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005872 kExactCheck, // Can do a single class compare.
5873 kClassHierarchyCheck, // Can just walk the super class chain.
5874 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5875 kInterfaceCheck, // No optimization yet when checking against an interface.
5876 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005877 kArrayCheck, // No optimization yet when checking against a generic array.
5878 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005879};
5880
Roland Levillain86503782016-02-11 19:07:30 +00005881std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5882
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005883class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005884 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005885 HInstanceOf(HInstruction* object,
5886 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005887 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005888 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005889 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005890 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005891 dex_pc) {
5892 SetPackedField<TypeCheckKindField>(check_kind);
5893 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005894 SetRawInputAt(0, object);
5895 SetRawInputAt(1, constant);
5896 }
5897
5898 bool CanBeMoved() const OVERRIDE { return true; }
5899
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005900 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005901 return true;
5902 }
5903
5904 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005905 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005906 }
5907
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005908 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005909 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5910 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5911 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5912 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005913
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005914 static bool CanCallRuntime(TypeCheckKind check_kind) {
5915 // Mips currently does runtime calls for any other checks.
5916 return check_kind != TypeCheckKind::kExactCheck;
5917 }
5918
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005919 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005920 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005921 }
5922
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005923 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005924
5925 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005926 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
5927 static constexpr size_t kFieldTypeCheckKindSize =
5928 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
5929 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
5930 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
5931 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5932 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005933
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005934 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5935};
5936
Calin Juravleb1498f62015-02-16 13:13:29 +00005937class HBoundType : public HExpression<1> {
5938 public:
David Brazdilf5552582015-12-27 13:36:12 +00005939 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005940 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005941 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
5942 SetPackedFlag<kFlagUpperCanBeNull>(true);
5943 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00005944 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005945 SetRawInputAt(0, input);
5946 }
5947
David Brazdilf5552582015-12-27 13:36:12 +00005948 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005949 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005950 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00005951 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005952
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005953 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005954 DCHECK(GetUpperCanBeNull() || !can_be_null);
5955 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005956 }
5957
Vladimir Markoa1de9182016-02-25 11:37:38 +00005958 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005959
Calin Juravleb1498f62015-02-16 13:13:29 +00005960 DECLARE_INSTRUCTION(BoundType);
5961
5962 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005963 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5964 // is false then CanBeNull() cannot be true).
5965 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
5966 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
5967 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
5968 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5969
Calin Juravleb1498f62015-02-16 13:13:29 +00005970 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005971 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5972 // It is used to bound the type in cases like:
5973 // if (x instanceof ClassX) {
5974 // // uper_bound_ will be ClassX
5975 // }
David Brazdilf5552582015-12-27 13:36:12 +00005976 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005977
5978 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5979};
5980
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005981class HCheckCast : public HTemplateInstruction<2> {
5982 public:
5983 HCheckCast(HInstruction* object,
5984 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005985 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005986 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00005987 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
5988 SetPackedField<TypeCheckKindField>(check_kind);
5989 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005990 SetRawInputAt(0, object);
5991 SetRawInputAt(1, constant);
5992 }
5993
5994 bool CanBeMoved() const OVERRIDE { return true; }
5995
5996 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5997 return true;
5998 }
5999
6000 bool NeedsEnvironment() const OVERRIDE {
6001 // Instruction may throw a CheckCastError.
6002 return true;
6003 }
6004
6005 bool CanThrow() const OVERRIDE { return true; }
6006
Vladimir Markoa1de9182016-02-25 11:37:38 +00006007 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6008 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6009 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6010 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006011
6012 DECLARE_INSTRUCTION(CheckCast);
6013
6014 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006015 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6016 static constexpr size_t kFieldTypeCheckKindSize =
6017 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6018 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6019 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6020 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6021 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006022
6023 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006024};
6025
Calin Juravle27df7582015-04-17 19:12:31 +01006026class HMemoryBarrier : public HTemplateInstruction<0> {
6027 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006028 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006029 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006030 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6031 SetPackedField<BarrierKindField>(barrier_kind);
6032 }
Calin Juravle27df7582015-04-17 19:12:31 +01006033
Vladimir Markoa1de9182016-02-25 11:37:38 +00006034 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006035
6036 DECLARE_INSTRUCTION(MemoryBarrier);
6037
6038 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006039 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6040 static constexpr size_t kFieldBarrierKindSize =
6041 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6042 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6043 kFieldBarrierKind + kFieldBarrierKindSize;
6044 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6045 "Too many packed fields.");
6046 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006047
6048 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6049};
6050
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006051class HMonitorOperation : public HTemplateInstruction<1> {
6052 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006053 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006054 kEnter,
6055 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006056 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006057 };
6058
6059 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006060 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006061 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6062 dex_pc) {
6063 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006064 SetRawInputAt(0, object);
6065 }
6066
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006067 // Instruction may go into runtime, so we need an environment.
6068 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006069
6070 bool CanThrow() const OVERRIDE {
6071 // Verifier guarantees that monitor-exit cannot throw.
6072 // This is important because it allows the HGraphBuilder to remove
6073 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6074 return IsEnter();
6075 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006076
Vladimir Markoa1de9182016-02-25 11:37:38 +00006077 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6078 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006079
6080 DECLARE_INSTRUCTION(MonitorOperation);
6081
Calin Juravle52c48962014-12-16 17:02:57 +00006082 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006083 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6084 static constexpr size_t kFieldOperationKindSize =
6085 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6086 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6087 kFieldOperationKind + kFieldOperationKindSize;
6088 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6089 "Too many packed fields.");
6090 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006091
6092 private:
6093 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6094};
6095
David Brazdil74eb1b22015-12-14 11:44:01 +00006096class HSelect : public HExpression<3> {
6097 public:
6098 HSelect(HInstruction* condition,
6099 HInstruction* true_value,
6100 HInstruction* false_value,
6101 uint32_t dex_pc)
6102 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6103 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6104
6105 // First input must be `true_value` or `false_value` to allow codegens to
6106 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6107 // that architectures which implement HSelect as a conditional move also
6108 // will not need to invert the condition.
6109 SetRawInputAt(0, false_value);
6110 SetRawInputAt(1, true_value);
6111 SetRawInputAt(2, condition);
6112 }
6113
6114 HInstruction* GetFalseValue() const { return InputAt(0); }
6115 HInstruction* GetTrueValue() const { return InputAt(1); }
6116 HInstruction* GetCondition() const { return InputAt(2); }
6117
6118 bool CanBeMoved() const OVERRIDE { return true; }
6119 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6120
6121 bool CanBeNull() const OVERRIDE {
6122 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6123 }
6124
6125 DECLARE_INSTRUCTION(Select);
6126
6127 private:
6128 DISALLOW_COPY_AND_ASSIGN(HSelect);
6129};
6130
Vladimir Markof9f64412015-09-02 14:05:49 +01006131class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006132 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006133 MoveOperands(Location source,
6134 Location destination,
6135 Primitive::Type type,
6136 HInstruction* instruction)
6137 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006138
6139 Location GetSource() const { return source_; }
6140 Location GetDestination() const { return destination_; }
6141
6142 void SetSource(Location value) { source_ = value; }
6143 void SetDestination(Location value) { destination_ = value; }
6144
6145 // The parallel move resolver marks moves as "in-progress" by clearing the
6146 // destination (but not the source).
6147 Location MarkPending() {
6148 DCHECK(!IsPending());
6149 Location dest = destination_;
6150 destination_ = Location::NoLocation();
6151 return dest;
6152 }
6153
6154 void ClearPending(Location dest) {
6155 DCHECK(IsPending());
6156 destination_ = dest;
6157 }
6158
6159 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006160 DCHECK(source_.IsValid() || destination_.IsInvalid());
6161 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006162 }
6163
6164 // True if this blocks a move from the given location.
6165 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006166 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006167 }
6168
6169 // A move is redundant if it's been eliminated, if its source and
6170 // destination are the same, or if its destination is unneeded.
6171 bool IsRedundant() const {
6172 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6173 }
6174
6175 // We clear both operands to indicate move that's been eliminated.
6176 void Eliminate() {
6177 source_ = destination_ = Location::NoLocation();
6178 }
6179
6180 bool IsEliminated() const {
6181 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6182 return source_.IsInvalid();
6183 }
6184
Alexey Frunze4dda3372015-06-01 18:31:49 -07006185 Primitive::Type GetType() const { return type_; }
6186
Nicolas Geoffray90218252015-04-15 11:56:51 +01006187 bool Is64BitMove() const {
6188 return Primitive::Is64BitType(type_);
6189 }
6190
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006191 HInstruction* GetInstruction() const { return instruction_; }
6192
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006193 private:
6194 Location source_;
6195 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006196 // The type this move is for.
6197 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006198 // The instruction this move is assocatied with. Null when this move is
6199 // for moving an input in the expected locations of user (including a phi user).
6200 // This is only used in debug mode, to ensure we do not connect interval siblings
6201 // in the same parallel move.
6202 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006203};
6204
Roland Levillainc9285912015-12-18 10:38:42 +00006205std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6206
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006207static constexpr size_t kDefaultNumberOfMoves = 4;
6208
6209class HParallelMove : public HTemplateInstruction<0> {
6210 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006211 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006212 : HTemplateInstruction(SideEffects::None(), dex_pc),
6213 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6214 moves_.reserve(kDefaultNumberOfMoves);
6215 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006216
Nicolas Geoffray90218252015-04-15 11:56:51 +01006217 void AddMove(Location source,
6218 Location destination,
6219 Primitive::Type type,
6220 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006221 DCHECK(source.IsValid());
6222 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006223 if (kIsDebugBuild) {
6224 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006225 for (const MoveOperands& move : moves_) {
6226 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006227 // Special case the situation where the move is for the spill slot
6228 // of the instruction.
6229 if ((GetPrevious() == instruction)
6230 || ((GetPrevious() == nullptr)
6231 && instruction->IsPhi()
6232 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006233 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006234 << "Doing parallel moves for the same instruction.";
6235 } else {
6236 DCHECK(false) << "Doing parallel moves for the same instruction.";
6237 }
6238 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006239 }
6240 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006241 for (const MoveOperands& move : moves_) {
6242 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006243 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006244 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006245 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006246 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006247 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006248 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006249 }
6250
Vladimir Marko225b6462015-09-28 12:17:40 +01006251 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006252 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006253 }
6254
Vladimir Marko225b6462015-09-28 12:17:40 +01006255 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006256
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006257 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006258
6259 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006260 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006261
6262 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6263};
6264
Mark Mendell0616ae02015-04-17 12:49:27 -04006265} // namespace art
6266
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006267#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6268#include "nodes_shared.h"
6269#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006270#ifdef ART_ENABLE_CODEGEN_arm
6271#include "nodes_arm.h"
6272#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006273#ifdef ART_ENABLE_CODEGEN_arm64
6274#include "nodes_arm64.h"
6275#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006276#ifdef ART_ENABLE_CODEGEN_x86
6277#include "nodes_x86.h"
6278#endif
6279
6280namespace art {
6281
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006282class HGraphVisitor : public ValueObject {
6283 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006284 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6285 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006286
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006287 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006288 virtual void VisitBasicBlock(HBasicBlock* block);
6289
Roland Levillain633021e2014-10-01 14:12:25 +01006290 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006291 void VisitInsertionOrder();
6292
Roland Levillain633021e2014-10-01 14:12:25 +01006293 // Visit the graph following dominator tree reverse post-order.
6294 void VisitReversePostOrder();
6295
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006296 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006297
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006298 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006299#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006300 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6301
6302 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6303
6304#undef DECLARE_VISIT_INSTRUCTION
6305
6306 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006307 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006308
6309 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6310};
6311
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006312class HGraphDelegateVisitor : public HGraphVisitor {
6313 public:
6314 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6315 virtual ~HGraphDelegateVisitor() {}
6316
6317 // Visit functions that delegate to to super class.
6318#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006319 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006320
6321 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6322
6323#undef DECLARE_VISIT_INSTRUCTION
6324
6325 private:
6326 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6327};
6328
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006329class HInsertionOrderIterator : public ValueObject {
6330 public:
6331 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6332
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006333 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006334 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006335 void Advance() { ++index_; }
6336
6337 private:
6338 const HGraph& graph_;
6339 size_t index_;
6340
6341 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6342};
6343
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006344class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006345 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006346 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6347 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006348 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006349 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006350
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006351 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6352 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006353 void Advance() { ++index_; }
6354
6355 private:
6356 const HGraph& graph_;
6357 size_t index_;
6358
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006359 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006360};
6361
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006362class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006363 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006364 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006365 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006366 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006367 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006368 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006369
6370 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006371 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006372 void Advance() { --index_; }
6373
6374 private:
6375 const HGraph& graph_;
6376 size_t index_;
6377
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006378 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006379};
6380
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006381class HLinearPostOrderIterator : public ValueObject {
6382 public:
6383 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006384 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006385
6386 bool Done() const { return index_ == 0; }
6387
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006388 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006389
6390 void Advance() {
6391 --index_;
6392 DCHECK_GE(index_, 0U);
6393 }
6394
6395 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006396 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006397 size_t index_;
6398
6399 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6400};
6401
6402class HLinearOrderIterator : public ValueObject {
6403 public:
6404 explicit HLinearOrderIterator(const HGraph& graph)
6405 : order_(graph.GetLinearOrder()), index_(0) {}
6406
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006407 bool Done() const { return index_ == order_.size(); }
6408 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006409 void Advance() { ++index_; }
6410
6411 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006412 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006413 size_t index_;
6414
6415 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6416};
6417
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006418// Iterator over the blocks that art part of the loop. Includes blocks part
6419// of an inner loop. The order in which the blocks are iterated is on their
6420// block id.
6421class HBlocksInLoopIterator : public ValueObject {
6422 public:
6423 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6424 : blocks_in_loop_(info.GetBlocks()),
6425 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6426 index_(0) {
6427 if (!blocks_in_loop_.IsBitSet(index_)) {
6428 Advance();
6429 }
6430 }
6431
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006432 bool Done() const { return index_ == blocks_.size(); }
6433 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006434 void Advance() {
6435 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006436 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006437 if (blocks_in_loop_.IsBitSet(index_)) {
6438 break;
6439 }
6440 }
6441 }
6442
6443 private:
6444 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006445 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006446 size_t index_;
6447
6448 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6449};
6450
Mingyao Yang3584bce2015-05-19 16:01:59 -07006451// Iterator over the blocks that art part of the loop. Includes blocks part
6452// of an inner loop. The order in which the blocks are iterated is reverse
6453// post order.
6454class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6455 public:
6456 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6457 : blocks_in_loop_(info.GetBlocks()),
6458 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6459 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006460 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006461 Advance();
6462 }
6463 }
6464
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006465 bool Done() const { return index_ == blocks_.size(); }
6466 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006467 void Advance() {
6468 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006469 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6470 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006471 break;
6472 }
6473 }
6474 }
6475
6476 private:
6477 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006478 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006479 size_t index_;
6480
6481 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6482};
6483
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006484inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006485 if (constant->IsIntConstant()) {
6486 return constant->AsIntConstant()->GetValue();
6487 } else if (constant->IsLongConstant()) {
6488 return constant->AsLongConstant()->GetValue();
6489 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006490 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006491 return 0;
6492 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006493}
6494
Vladimir Marko58155012015-08-19 12:49:41 +00006495inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6496 // For the purposes of the compiler, the dex files must actually be the same object
6497 // if we want to safely treat them as the same. This is especially important for JIT
6498 // as custom class loaders can open the same underlying file (or memory) multiple
6499 // times and provide different class resolution but no two class loaders should ever
6500 // use the same DexFile object - doing so is an unsupported hack that can lead to
6501 // all sorts of weird failures.
6502 return &lhs == &rhs;
6503}
6504
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006505#define INSTRUCTION_TYPE_CHECK(type, super) \
6506 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6507 inline const H##type* HInstruction::As##type() const { \
6508 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6509 } \
6510 inline H##type* HInstruction::As##type() { \
6511 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6512 }
6513
6514 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6515#undef INSTRUCTION_TYPE_CHECK
6516
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00006517class SwitchTable : public ValueObject {
6518 public:
6519 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
6520 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
6521 int32_t table_offset = instruction.VRegB_31t();
6522 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
6523 if (sparse) {
6524 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
6525 } else {
6526 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
6527 }
6528 num_entries_ = table[1];
6529 values_ = reinterpret_cast<const int32_t*>(&table[2]);
6530 }
6531
6532 uint16_t GetNumEntries() const {
6533 return num_entries_;
6534 }
6535
6536 void CheckIndex(size_t index) const {
6537 if (sparse_) {
6538 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6539 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
6540 } else {
6541 // In a packed table, we have the starting key and num_entries_ values.
6542 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
6543 }
6544 }
6545
6546 int32_t GetEntryAt(size_t index) const {
6547 CheckIndex(index);
6548 return values_[index];
6549 }
6550
6551 uint32_t GetDexPcForIndex(size_t index) const {
6552 CheckIndex(index);
6553 return dex_pc_ +
6554 (reinterpret_cast<const int16_t*>(values_ + index) -
6555 reinterpret_cast<const int16_t*>(&instruction_));
6556 }
6557
6558 // Index of the first value in the table.
6559 size_t GetFirstValueIndex() const {
6560 if (sparse_) {
6561 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6562 return num_entries_;
6563 } else {
6564 // In a packed table, we have the starting key and num_entries_ values.
6565 return 1;
6566 }
6567 }
6568
6569 private:
6570 const Instruction& instruction_;
6571 const uint32_t dex_pc_;
6572
6573 // Whether this is a sparse-switch table (or a packed-switch one).
6574 const bool sparse_;
6575
6576 // This can't be const as it needs to be computed off of the given instruction, and complicated
6577 // expressions in the initializer list seemed very ugly.
6578 uint16_t num_entries_;
6579
6580 const int32_t* values_;
6581
6582 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
6583};
6584
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006585// Create space in `blocks` for adding `number_of_new_blocks` entries
6586// starting at location `at`. Blocks after `at` are moved accordingly.
6587inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6588 size_t number_of_new_blocks,
6589 size_t after) {
6590 DCHECK_LT(after, blocks->size());
6591 size_t old_size = blocks->size();
6592 size_t new_size = old_size + number_of_new_blocks;
6593 blocks->resize(new_size);
6594 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6595}
6596
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006597} // namespace art
6598
6599#endif // ART_COMPILER_OPTIMIZING_NODES_H_