blob: 673631958a0251f9d0585d90d96d75d590fb67e7 [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)
4151 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01004152
Roland Levillain9867bc72015-08-05 10:21:34 +01004153 template <typename T> T Compute(T x) const { return -x; }
4154
4155 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004156 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004157 }
4158 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004159 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004160 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004161 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4162 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4163 }
4164 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4165 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4166 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004167
Roland Levillain88cb1752014-10-20 16:36:47 +01004168 DECLARE_INSTRUCTION(Neg);
4169
4170 private:
4171 DISALLOW_COPY_AND_ASSIGN(HNeg);
4172};
4173
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004174class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004175 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004176 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004177 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004178 uint32_t dex_pc,
4179 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004180 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004181 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004182 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004183 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004184 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004185 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004186 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004187 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004188 }
4189
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004190 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004191 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004192
4193 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004194 bool NeedsEnvironment() const OVERRIDE { return true; }
4195
Mingyao Yang0c365e62015-03-31 15:09:29 -07004196 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4197 bool CanThrow() const OVERRIDE { return true; }
4198
Calin Juravle10e244f2015-01-26 18:54:32 +00004199 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004200
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004201 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4202
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004203 DECLARE_INSTRUCTION(NewArray);
4204
4205 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004206 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004207 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004208 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004209
4210 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4211};
4212
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004213class HAdd : public HBinaryOperation {
4214 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004215 HAdd(Primitive::Type result_type,
4216 HInstruction* left,
4217 HInstruction* right,
4218 uint32_t dex_pc = kNoDexPc)
4219 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004220
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004221 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004222
Roland Levillain9867bc72015-08-05 10:21:34 +01004223 template <typename T> T Compute(T x, T y) const { return x + y; }
4224
4225 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004226 return GetBlock()->GetGraph()->GetIntConstant(
4227 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004228 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004229 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004230 return GetBlock()->GetGraph()->GetLongConstant(
4231 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004232 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004233 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4234 return GetBlock()->GetGraph()->GetFloatConstant(
4235 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4236 }
4237 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4238 return GetBlock()->GetGraph()->GetDoubleConstant(
4239 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4240 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004241
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004242 DECLARE_INSTRUCTION(Add);
4243
4244 private:
4245 DISALLOW_COPY_AND_ASSIGN(HAdd);
4246};
4247
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004248class HSub : public HBinaryOperation {
4249 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004250 HSub(Primitive::Type result_type,
4251 HInstruction* left,
4252 HInstruction* right,
4253 uint32_t dex_pc = kNoDexPc)
4254 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004255
Roland Levillain9867bc72015-08-05 10:21:34 +01004256 template <typename T> T Compute(T x, T y) const { return x - y; }
4257
4258 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004259 return GetBlock()->GetGraph()->GetIntConstant(
4260 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004261 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004262 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004263 return GetBlock()->GetGraph()->GetLongConstant(
4264 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004265 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004266 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4267 return GetBlock()->GetGraph()->GetFloatConstant(
4268 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4269 }
4270 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4271 return GetBlock()->GetGraph()->GetDoubleConstant(
4272 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4273 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004274
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004275 DECLARE_INSTRUCTION(Sub);
4276
4277 private:
4278 DISALLOW_COPY_AND_ASSIGN(HSub);
4279};
4280
Calin Juravle34bacdf2014-10-07 20:23:36 +01004281class HMul : public HBinaryOperation {
4282 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004283 HMul(Primitive::Type result_type,
4284 HInstruction* left,
4285 HInstruction* right,
4286 uint32_t dex_pc = kNoDexPc)
4287 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004288
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004289 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004290
Roland Levillain9867bc72015-08-05 10:21:34 +01004291 template <typename T> T Compute(T x, T y) const { return x * y; }
4292
4293 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004294 return GetBlock()->GetGraph()->GetIntConstant(
4295 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004296 }
4297 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004298 return GetBlock()->GetGraph()->GetLongConstant(
4299 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004300 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004301 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4302 return GetBlock()->GetGraph()->GetFloatConstant(
4303 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4304 }
4305 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4306 return GetBlock()->GetGraph()->GetDoubleConstant(
4307 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4308 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004309
4310 DECLARE_INSTRUCTION(Mul);
4311
4312 private:
4313 DISALLOW_COPY_AND_ASSIGN(HMul);
4314};
4315
Calin Juravle7c4954d2014-10-28 16:57:40 +00004316class HDiv : public HBinaryOperation {
4317 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004318 HDiv(Primitive::Type result_type,
4319 HInstruction* left,
4320 HInstruction* right,
4321 uint32_t dex_pc)
4322 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004323
Roland Levillain9867bc72015-08-05 10:21:34 +01004324 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004325 T ComputeIntegral(T x, T y) const {
4326 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004327 // Our graph structure ensures we never have 0 for `y` during
4328 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004329 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004330 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004331 return (y == -1) ? -x : x / y;
4332 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004333
Roland Levillain31dd3d62016-02-16 12:21:02 +00004334 template <typename T>
4335 T ComputeFP(T x, T y) const {
4336 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4337 return x / y;
4338 }
4339
Roland Levillain9867bc72015-08-05 10:21:34 +01004340 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004341 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004342 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004343 }
4344 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004345 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004346 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4347 }
4348 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4349 return GetBlock()->GetGraph()->GetFloatConstant(
4350 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4351 }
4352 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4353 return GetBlock()->GetGraph()->GetDoubleConstant(
4354 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004355 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004356
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004357 static SideEffects SideEffectsForArchRuntimeCalls() {
4358 // The generated code can use a runtime call.
4359 return SideEffects::CanTriggerGC();
4360 }
4361
Calin Juravle7c4954d2014-10-28 16:57:40 +00004362 DECLARE_INSTRUCTION(Div);
4363
4364 private:
4365 DISALLOW_COPY_AND_ASSIGN(HDiv);
4366};
4367
Calin Juravlebacfec32014-11-14 15:54:36 +00004368class HRem : public HBinaryOperation {
4369 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004370 HRem(Primitive::Type result_type,
4371 HInstruction* left,
4372 HInstruction* right,
4373 uint32_t dex_pc)
4374 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004375
Roland Levillain9867bc72015-08-05 10:21:34 +01004376 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004377 T ComputeIntegral(T x, T y) const {
4378 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004379 // Our graph structure ensures we never have 0 for `y` during
4380 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004381 DCHECK_NE(y, 0);
4382 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4383 return (y == -1) ? 0 : x % y;
4384 }
4385
Roland Levillain31dd3d62016-02-16 12:21:02 +00004386 template <typename T>
4387 T ComputeFP(T x, T y) const {
4388 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4389 return std::fmod(x, y);
4390 }
4391
Roland Levillain9867bc72015-08-05 10:21:34 +01004392 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004393 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004394 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004395 }
4396 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004397 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004398 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004399 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004400 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4401 return GetBlock()->GetGraph()->GetFloatConstant(
4402 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4403 }
4404 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4405 return GetBlock()->GetGraph()->GetDoubleConstant(
4406 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4407 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004408
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004409 static SideEffects SideEffectsForArchRuntimeCalls() {
4410 return SideEffects::CanTriggerGC();
4411 }
4412
Calin Juravlebacfec32014-11-14 15:54:36 +00004413 DECLARE_INSTRUCTION(Rem);
4414
4415 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004416 DISALLOW_COPY_AND_ASSIGN(HRem);
4417};
4418
Calin Juravled0d48522014-11-04 16:40:20 +00004419class HDivZeroCheck : public HExpression<1> {
4420 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004421 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4422 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004423 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004424 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004425 SetRawInputAt(0, value);
4426 }
4427
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004428 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4429
Calin Juravled0d48522014-11-04 16:40:20 +00004430 bool CanBeMoved() const OVERRIDE { return true; }
4431
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004432 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004433 return true;
4434 }
4435
4436 bool NeedsEnvironment() const OVERRIDE { return true; }
4437 bool CanThrow() const OVERRIDE { return true; }
4438
Calin Juravled0d48522014-11-04 16:40:20 +00004439 DECLARE_INSTRUCTION(DivZeroCheck);
4440
4441 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004442 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4443};
4444
Calin Juravle9aec02f2014-11-18 23:06:35 +00004445class HShl : public HBinaryOperation {
4446 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004447 HShl(Primitive::Type result_type,
4448 HInstruction* left,
4449 HInstruction* right,
4450 uint32_t dex_pc = kNoDexPc)
4451 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004452
Roland Levillain9867bc72015-08-05 10:21:34 +01004453 template <typename T, typename U, typename V>
4454 T Compute(T x, U y, V max_shift_value) const {
4455 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4456 "V is not the unsigned integer type corresponding to T");
4457 return x << (y & max_shift_value);
4458 }
4459
4460 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4461 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004462 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004463 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004464 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4465 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004466 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004467 }
4468 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4469 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004470 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004471 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004472 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4473 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4474 LOG(FATAL) << DebugName() << " is not defined for float values";
4475 UNREACHABLE();
4476 }
4477 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4478 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4479 LOG(FATAL) << DebugName() << " is not defined for double values";
4480 UNREACHABLE();
4481 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004482
4483 DECLARE_INSTRUCTION(Shl);
4484
4485 private:
4486 DISALLOW_COPY_AND_ASSIGN(HShl);
4487};
4488
4489class HShr : public HBinaryOperation {
4490 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004491 HShr(Primitive::Type result_type,
4492 HInstruction* left,
4493 HInstruction* right,
4494 uint32_t dex_pc = kNoDexPc)
4495 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004496
Roland Levillain9867bc72015-08-05 10:21:34 +01004497 template <typename T, typename U, typename V>
4498 T Compute(T x, U y, V max_shift_value) const {
4499 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4500 "V is not the unsigned integer type corresponding to T");
4501 return x >> (y & max_shift_value);
4502 }
4503
4504 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4505 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004506 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004507 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004508 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4509 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004510 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004511 }
4512 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4513 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004514 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004515 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004516 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4517 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4518 LOG(FATAL) << DebugName() << " is not defined for float values";
4519 UNREACHABLE();
4520 }
4521 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4522 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4523 LOG(FATAL) << DebugName() << " is not defined for double values";
4524 UNREACHABLE();
4525 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004526
4527 DECLARE_INSTRUCTION(Shr);
4528
4529 private:
4530 DISALLOW_COPY_AND_ASSIGN(HShr);
4531};
4532
4533class HUShr : public HBinaryOperation {
4534 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004535 HUShr(Primitive::Type result_type,
4536 HInstruction* left,
4537 HInstruction* right,
4538 uint32_t dex_pc = kNoDexPc)
4539 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004540
Roland Levillain9867bc72015-08-05 10:21:34 +01004541 template <typename T, typename U, typename V>
4542 T Compute(T x, U y, V max_shift_value) const {
4543 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4544 "V is not the unsigned integer type corresponding to T");
4545 V ux = static_cast<V>(x);
4546 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004547 }
4548
Roland Levillain9867bc72015-08-05 10:21:34 +01004549 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4550 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004551 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004552 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004553 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4554 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004555 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004556 }
4557 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4558 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004559 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004560 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004561 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4562 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4563 LOG(FATAL) << DebugName() << " is not defined for float values";
4564 UNREACHABLE();
4565 }
4566 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4567 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4568 LOG(FATAL) << DebugName() << " is not defined for double values";
4569 UNREACHABLE();
4570 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004571
4572 DECLARE_INSTRUCTION(UShr);
4573
4574 private:
4575 DISALLOW_COPY_AND_ASSIGN(HUShr);
4576};
4577
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004578class HAnd : public HBinaryOperation {
4579 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004580 HAnd(Primitive::Type result_type,
4581 HInstruction* left,
4582 HInstruction* right,
4583 uint32_t dex_pc = kNoDexPc)
4584 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004585
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004586 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004587
Roland Levillaine53bd812016-02-24 14:54:18 +00004588 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004589
4590 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004591 return GetBlock()->GetGraph()->GetIntConstant(
4592 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004593 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004594 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004595 return GetBlock()->GetGraph()->GetLongConstant(
4596 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004597 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004598 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4599 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4600 LOG(FATAL) << DebugName() << " is not defined for float values";
4601 UNREACHABLE();
4602 }
4603 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4604 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4605 LOG(FATAL) << DebugName() << " is not defined for double values";
4606 UNREACHABLE();
4607 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004608
4609 DECLARE_INSTRUCTION(And);
4610
4611 private:
4612 DISALLOW_COPY_AND_ASSIGN(HAnd);
4613};
4614
4615class HOr : public HBinaryOperation {
4616 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004617 HOr(Primitive::Type result_type,
4618 HInstruction* left,
4619 HInstruction* right,
4620 uint32_t dex_pc = kNoDexPc)
4621 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004622
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004623 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004624
Roland Levillaine53bd812016-02-24 14:54:18 +00004625 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004626
4627 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004628 return GetBlock()->GetGraph()->GetIntConstant(
4629 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004630 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004631 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004632 return GetBlock()->GetGraph()->GetLongConstant(
4633 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004634 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004635 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4636 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4637 LOG(FATAL) << DebugName() << " is not defined for float values";
4638 UNREACHABLE();
4639 }
4640 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4641 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4642 LOG(FATAL) << DebugName() << " is not defined for double values";
4643 UNREACHABLE();
4644 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004645
4646 DECLARE_INSTRUCTION(Or);
4647
4648 private:
4649 DISALLOW_COPY_AND_ASSIGN(HOr);
4650};
4651
4652class HXor : public HBinaryOperation {
4653 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004654 HXor(Primitive::Type result_type,
4655 HInstruction* left,
4656 HInstruction* right,
4657 uint32_t dex_pc = kNoDexPc)
4658 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004659
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004660 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004661
Roland Levillaine53bd812016-02-24 14:54:18 +00004662 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004663
4664 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004665 return GetBlock()->GetGraph()->GetIntConstant(
4666 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004667 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004668 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004669 return GetBlock()->GetGraph()->GetLongConstant(
4670 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004671 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004672 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4673 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4674 LOG(FATAL) << DebugName() << " is not defined for float values";
4675 UNREACHABLE();
4676 }
4677 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4678 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4679 LOG(FATAL) << DebugName() << " is not defined for double values";
4680 UNREACHABLE();
4681 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004682
4683 DECLARE_INSTRUCTION(Xor);
4684
4685 private:
4686 DISALLOW_COPY_AND_ASSIGN(HXor);
4687};
4688
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004689class HRor : public HBinaryOperation {
4690 public:
4691 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004692 : HBinaryOperation(result_type, value, distance) {
4693 if (kIsDebugBuild) {
4694 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4695 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
4696 }
4697 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004698
4699 template <typename T, typename U, typename V>
4700 T Compute(T x, U y, V max_shift_value) const {
4701 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4702 "V is not the unsigned integer type corresponding to T");
4703 V ux = static_cast<V>(x);
4704 if ((y & max_shift_value) == 0) {
4705 return static_cast<T>(ux);
4706 } else {
4707 const V reg_bits = sizeof(T) * 8;
4708 return static_cast<T>(ux >> (y & max_shift_value)) |
4709 (x << (reg_bits - (y & max_shift_value)));
4710 }
4711 }
4712
4713 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4714 return GetBlock()->GetGraph()->GetIntConstant(
4715 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4716 }
4717 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4718 return GetBlock()->GetGraph()->GetLongConstant(
4719 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4720 }
4721 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4722 return GetBlock()->GetGraph()->GetLongConstant(
4723 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4724 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004725 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4726 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4727 LOG(FATAL) << DebugName() << " is not defined for float values";
4728 UNREACHABLE();
4729 }
4730 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4731 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4732 LOG(FATAL) << DebugName() << " is not defined for double values";
4733 UNREACHABLE();
4734 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004735
4736 DECLARE_INSTRUCTION(Ror);
4737
4738 private:
4739 DISALLOW_COPY_AND_ASSIGN(HRor);
4740};
4741
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004742// The value of a parameter in this method. Its location depends on
4743// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004744class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004745 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004746 HParameterValue(const DexFile& dex_file,
4747 uint16_t type_index,
4748 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004749 Primitive::Type parameter_type,
4750 bool is_this = false)
4751 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004752 dex_file_(dex_file),
4753 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004754 index_(index) {
4755 SetPackedFlag<kFlagIsThis>(is_this);
4756 SetPackedFlag<kFlagCanBeNull>(!is_this);
4757 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004758
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004759 const DexFile& GetDexFile() const { return dex_file_; }
4760 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004761 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004762 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004763
Vladimir Markoa1de9182016-02-25 11:37:38 +00004764 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4765 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004766
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004767 DECLARE_INSTRUCTION(ParameterValue);
4768
4769 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004770 // Whether or not the parameter value corresponds to 'this' argument.
4771 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4772 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4773 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4774 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4775 "Too many packed fields.");
4776
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004777 const DexFile& dex_file_;
4778 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004779 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004780 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004781 const uint8_t index_;
4782
4783 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4784};
4785
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004786class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004787 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004788 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4789 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004790
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004791 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004792 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004793 return true;
4794 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004795
Roland Levillain9867bc72015-08-05 10:21:34 +01004796 template <typename T> T Compute(T x) const { return ~x; }
4797
4798 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004799 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004800 }
4801 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004802 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004803 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004804 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4805 LOG(FATAL) << DebugName() << " is not defined for float values";
4806 UNREACHABLE();
4807 }
4808 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4809 LOG(FATAL) << DebugName() << " is not defined for double values";
4810 UNREACHABLE();
4811 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004812
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004813 DECLARE_INSTRUCTION(Not);
4814
4815 private:
4816 DISALLOW_COPY_AND_ASSIGN(HNot);
4817};
4818
David Brazdil66d126e2015-04-03 16:02:44 +01004819class HBooleanNot : public HUnaryOperation {
4820 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004821 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4822 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004823
4824 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004825 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004826 return true;
4827 }
4828
Roland Levillain9867bc72015-08-05 10:21:34 +01004829 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004830 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004831 return !x;
4832 }
4833
Roland Levillain9867bc72015-08-05 10:21:34 +01004834 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004835 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004836 }
4837 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4838 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004839 UNREACHABLE();
4840 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004841 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4842 LOG(FATAL) << DebugName() << " is not defined for float values";
4843 UNREACHABLE();
4844 }
4845 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4846 LOG(FATAL) << DebugName() << " is not defined for double values";
4847 UNREACHABLE();
4848 }
David Brazdil66d126e2015-04-03 16:02:44 +01004849
4850 DECLARE_INSTRUCTION(BooleanNot);
4851
4852 private:
4853 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4854};
4855
Roland Levillaindff1f282014-11-05 14:15:05 +00004856class HTypeConversion : public HExpression<1> {
4857 public:
4858 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004859 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004860 : HExpression(result_type,
4861 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4862 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004863 SetRawInputAt(0, input);
4864 DCHECK_NE(input->GetType(), result_type);
4865 }
4866
4867 HInstruction* GetInput() const { return InputAt(0); }
4868 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4869 Primitive::Type GetResultType() const { return GetType(); }
4870
4871 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004872 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004873
Mark Mendelle82549b2015-05-06 10:55:34 -04004874 // Try to statically evaluate the conversion and return a HConstant
4875 // containing the result. If the input cannot be converted, return nullptr.
4876 HConstant* TryStaticEvaluation() const;
4877
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004878 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4879 Primitive::Type result_type) {
4880 // Some architectures may not require the 'GC' side effects, but at this point
4881 // in the compilation process we do not know what architecture we will
4882 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004883 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4884 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004885 return SideEffects::CanTriggerGC();
4886 }
4887 return SideEffects::None();
4888 }
4889
Roland Levillaindff1f282014-11-05 14:15:05 +00004890 DECLARE_INSTRUCTION(TypeConversion);
4891
4892 private:
4893 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4894};
4895
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004896static constexpr uint32_t kNoRegNumber = -1;
4897
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004898class HPhi : public HInstruction {
4899 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004900 HPhi(ArenaAllocator* arena,
4901 uint32_t reg_number,
4902 size_t number_of_inputs,
4903 Primitive::Type type,
4904 uint32_t dex_pc = kNoDexPc)
4905 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004906 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004907 reg_number_(reg_number) {
4908 SetPackedField<TypeField>(ToPhiType(type));
4909 DCHECK_NE(GetType(), Primitive::kPrimVoid);
4910 // Phis are constructed live and marked dead if conflicting or unused.
4911 // Individual steps of SsaBuilder should assume that if a phi has been
4912 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4913 SetPackedFlag<kFlagIsLive>(true);
4914 SetPackedFlag<kFlagCanBeNull>(true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004915 }
4916
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004917 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4918 static Primitive::Type ToPhiType(Primitive::Type type) {
4919 switch (type) {
4920 case Primitive::kPrimBoolean:
4921 case Primitive::kPrimByte:
4922 case Primitive::kPrimShort:
4923 case Primitive::kPrimChar:
4924 return Primitive::kPrimInt;
4925 default:
4926 return type;
4927 }
4928 }
4929
David Brazdilffee3d32015-07-06 11:48:53 +01004930 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4931
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004932 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004933
4934 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004935 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004936
Vladimir Markoa1de9182016-02-25 11:37:38 +00004937 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
David Brazdil4833f5a2015-12-16 10:37:39 +00004938 void SetType(Primitive::Type new_type) {
4939 // Make sure that only valid type changes occur. The following are allowed:
4940 // (1) int -> float/ref (primitive type propagation),
4941 // (2) long -> double (primitive type propagation).
Vladimir Markoa1de9182016-02-25 11:37:38 +00004942 DCHECK(GetType() == new_type ||
4943 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4944 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4945 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4946 SetPackedField<TypeField>(new_type);
David Brazdil4833f5a2015-12-16 10:37:39 +00004947 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004948
Vladimir Markoa1de9182016-02-25 11:37:38 +00004949 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4950 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004951
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004952 uint32_t GetRegNumber() const { return reg_number_; }
4953
Vladimir Markoa1de9182016-02-25 11:37:38 +00004954 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
4955 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
4956 bool IsDead() const { return !IsLive(); }
4957 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004958
David Brazdil77a48ae2015-09-15 12:34:04 +00004959 bool IsVRegEquivalentOf(HInstruction* other) const {
4960 return other != nullptr
4961 && other->IsPhi()
4962 && other->AsPhi()->GetBlock() == GetBlock()
4963 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4964 }
4965
Calin Juravlea4f88312015-04-16 12:57:19 +01004966 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4967 // An equivalent phi is a phi having the same dex register and type.
4968 // It assumes that phis with the same dex register are adjacent.
4969 HPhi* GetNextEquivalentPhiWithSameType() {
4970 HInstruction* next = GetNext();
4971 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4972 if (next->GetType() == GetType()) {
4973 return next->AsPhi();
4974 }
4975 next = next->GetNext();
4976 }
4977 return nullptr;
4978 }
4979
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004980 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004981
David Brazdil1abb4192015-02-17 18:33:36 +00004982 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004983 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004984 return inputs_[index];
4985 }
David Brazdil1abb4192015-02-17 18:33:36 +00004986
4987 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004988 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004989 }
4990
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004991 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004992 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
4993 static constexpr size_t kFieldTypeSize =
4994 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4995 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
4996 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
4997 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
4998 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4999 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
5000
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005001 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005002 const uint32_t reg_number_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005003
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005004 DISALLOW_COPY_AND_ASSIGN(HPhi);
5005};
5006
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005007class HNullCheck : public HExpression<1> {
5008 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005009 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5010 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005011 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005012 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005013 SetRawInputAt(0, value);
5014 }
5015
Calin Juravle10e244f2015-01-26 18:54:32 +00005016 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005017 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005018 return true;
5019 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005020
Calin Juravle10e244f2015-01-26 18:54:32 +00005021 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005022
Calin Juravle10e244f2015-01-26 18:54:32 +00005023 bool CanThrow() const OVERRIDE { return true; }
5024
5025 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005026
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005027
5028 DECLARE_INSTRUCTION(NullCheck);
5029
5030 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005031 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5032};
5033
5034class FieldInfo : public ValueObject {
5035 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005036 FieldInfo(MemberOffset field_offset,
5037 Primitive::Type field_type,
5038 bool is_volatile,
5039 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005040 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005041 const DexFile& dex_file,
5042 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005043 : field_offset_(field_offset),
5044 field_type_(field_type),
5045 is_volatile_(is_volatile),
5046 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005047 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005048 dex_file_(dex_file),
5049 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005050
5051 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005052 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005053 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005054 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005055 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005056 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005057 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005058
5059 private:
5060 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005061 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005062 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005063 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005064 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005065 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005066 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005067};
5068
5069class HInstanceFieldGet : public HExpression<1> {
5070 public:
5071 HInstanceFieldGet(HInstruction* value,
5072 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005073 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005074 bool is_volatile,
5075 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005076 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005077 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005078 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005079 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005080 : HExpression(field_type,
5081 SideEffects::FieldReadOfType(field_type, is_volatile),
5082 dex_pc),
5083 field_info_(field_offset,
5084 field_type,
5085 is_volatile,
5086 field_idx,
5087 declaring_class_def_index,
5088 dex_file,
5089 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005090 SetRawInputAt(0, value);
5091 }
5092
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005093 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005094
5095 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5096 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
5097 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005098 }
5099
Calin Juravle641547a2015-04-21 22:08:51 +01005100 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5101 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005102 }
5103
5104 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005105 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5106 }
5107
Calin Juravle52c48962014-12-16 17:02:57 +00005108 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005109 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005110 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005111 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005112
5113 DECLARE_INSTRUCTION(InstanceFieldGet);
5114
5115 private:
5116 const FieldInfo field_info_;
5117
5118 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5119};
5120
5121class HInstanceFieldSet : public HTemplateInstruction<2> {
5122 public:
5123 HInstanceFieldSet(HInstruction* object,
5124 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005125 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005126 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005127 bool is_volatile,
5128 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005129 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005130 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005131 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005132 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005133 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5134 dex_pc),
5135 field_info_(field_offset,
5136 field_type,
5137 is_volatile,
5138 field_idx,
5139 declaring_class_def_index,
5140 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005141 dex_cache) {
5142 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005143 SetRawInputAt(0, object);
5144 SetRawInputAt(1, value);
5145 }
5146
Calin Juravle641547a2015-04-21 22:08:51 +01005147 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5148 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005149 }
5150
Calin Juravle52c48962014-12-16 17:02:57 +00005151 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005152 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005153 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005154 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005155 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005156 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5157 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005158
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005159 DECLARE_INSTRUCTION(InstanceFieldSet);
5160
5161 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005162 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5163 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5164 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5165 "Too many packed fields.");
5166
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005167 const FieldInfo field_info_;
5168
5169 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5170};
5171
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005172class HArrayGet : public HExpression<2> {
5173 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005174 HArrayGet(HInstruction* array,
5175 HInstruction* index,
5176 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005177 uint32_t dex_pc,
5178 SideEffects additional_side_effects = SideEffects::None())
5179 : HExpression(type,
5180 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00005181 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005182 SetRawInputAt(0, array);
5183 SetRawInputAt(1, index);
5184 }
5185
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005186 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005187 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005188 return true;
5189 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005190 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005191 // TODO: We can be smarter here.
5192 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5193 // which generates the implicit null check. There are cases when these can be removed
5194 // to produce better code. If we ever add optimizations to do so we should allow an
5195 // implicit check here (as long as the address falls in the first page).
5196 return false;
5197 }
5198
David Brazdil4833f5a2015-12-16 10:37:39 +00005199 bool IsEquivalentOf(HArrayGet* other) const {
5200 bool result = (GetDexPc() == other->GetDexPc());
5201 if (kIsDebugBuild && result) {
5202 DCHECK_EQ(GetBlock(), other->GetBlock());
5203 DCHECK_EQ(GetArray(), other->GetArray());
5204 DCHECK_EQ(GetIndex(), other->GetIndex());
5205 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005206 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005207 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005208 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5209 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005210 }
5211 }
5212 return result;
5213 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005214
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005215 HInstruction* GetArray() const { return InputAt(0); }
5216 HInstruction* GetIndex() const { return InputAt(1); }
5217
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005218 DECLARE_INSTRUCTION(ArrayGet);
5219
5220 private:
5221 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5222};
5223
5224class HArraySet : public HTemplateInstruction<3> {
5225 public:
5226 HArraySet(HInstruction* array,
5227 HInstruction* index,
5228 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005229 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005230 uint32_t dex_pc,
5231 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005232 : HTemplateInstruction(
5233 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005234 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
5235 additional_side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005236 dex_pc) {
5237 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5238 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5239 SetPackedFlag<kFlagValueCanBeNull>(true);
5240 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005241 SetRawInputAt(0, array);
5242 SetRawInputAt(1, index);
5243 SetRawInputAt(2, value);
5244 }
5245
Calin Juravle77520bc2015-01-12 18:45:46 +00005246 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005247 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005248 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005249 }
5250
Mingyao Yang81014cb2015-06-02 03:16:27 -07005251 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005252 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005253
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005254 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005255 // TODO: Same as for ArrayGet.
5256 return false;
5257 }
5258
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005259 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005260 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005261 }
5262
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005263 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005264 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005265 }
5266
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005267 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005268 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005269 }
5270
Vladimir Markoa1de9182016-02-25 11:37:38 +00005271 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5272 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5273 bool StaticTypeOfArrayIsObjectArray() const {
5274 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5275 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005276
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005277 HInstruction* GetArray() const { return InputAt(0); }
5278 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005279 HInstruction* GetValue() const { return InputAt(2); }
5280
5281 Primitive::Type GetComponentType() const {
5282 // The Dex format does not type floating point index operations. Since the
5283 // `expected_component_type_` is set during building and can therefore not
5284 // be correct, we also check what is the value type. If it is a floating
5285 // point type, we must use that type.
5286 Primitive::Type value_type = GetValue()->GetType();
5287 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5288 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005289 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005290 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005291
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005292 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005293 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005294 }
5295
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005296 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5297 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5298 }
5299
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005300 DECLARE_INSTRUCTION(ArraySet);
5301
5302 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005303 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5304 static constexpr size_t kFieldExpectedComponentTypeSize =
5305 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5306 static constexpr size_t kFlagNeedsTypeCheck =
5307 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5308 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005309 // Cached information for the reference_type_info_ so that codegen
5310 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005311 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5312 static constexpr size_t kNumberOfArraySetPackedBits =
5313 kFlagStaticTypeOfArrayIsObjectArray + 1;
5314 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5315 using ExpectedComponentTypeField =
5316 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005317
5318 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5319};
5320
5321class HArrayLength : public HExpression<1> {
5322 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005323 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005324 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005325 // Note that arrays do not change length, so the instruction does not
5326 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005327 SetRawInputAt(0, array);
5328 }
5329
Calin Juravle77520bc2015-01-12 18:45:46 +00005330 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005331 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005332 return true;
5333 }
Calin Juravle641547a2015-04-21 22:08:51 +01005334 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5335 return obj == InputAt(0);
5336 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005337
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005338 DECLARE_INSTRUCTION(ArrayLength);
5339
5340 private:
5341 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5342};
5343
5344class HBoundsCheck : public HExpression<2> {
5345 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005346 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5347 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005348 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005349 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005350 DCHECK(index->GetType() == Primitive::kPrimInt);
5351 SetRawInputAt(0, index);
5352 SetRawInputAt(1, length);
5353 }
5354
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005355 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005356 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005357 return true;
5358 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005359
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005360 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005361
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005362 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005363
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005364 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005365
5366 DECLARE_INSTRUCTION(BoundsCheck);
5367
5368 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005369 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5370};
5371
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005372class HSuspendCheck : public HTemplateInstruction<0> {
5373 public:
5374 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005375 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005376
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005377 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005378 return true;
5379 }
5380
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005381 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5382 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005383
5384 DECLARE_INSTRUCTION(SuspendCheck);
5385
5386 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005387 // Only used for code generation, in order to share the same slow path between back edges
5388 // of a same loop.
5389 SlowPathCode* slow_path_;
5390
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005391 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5392};
5393
David Srbecky0cf44932015-12-09 14:09:59 +00005394// Pseudo-instruction which provides the native debugger with mapping information.
5395// It ensures that we can generate line number and local variables at this point.
5396class HNativeDebugInfo : public HTemplateInstruction<0> {
5397 public:
5398 explicit HNativeDebugInfo(uint32_t dex_pc)
5399 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5400
5401 bool NeedsEnvironment() const OVERRIDE {
5402 return true;
5403 }
5404
5405 DECLARE_INSTRUCTION(NativeDebugInfo);
5406
5407 private:
5408 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5409};
5410
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005411/**
5412 * Instruction to load a Class object.
5413 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005414class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005415 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005416 HLoadClass(HCurrentMethod* current_method,
5417 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005418 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005419 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005420 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005421 bool needs_access_check,
5422 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005423 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005424 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005425 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005426 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005427 // Referrers class should not need access check. We never inline unverified
5428 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005429 DCHECK(!is_referrers_class || !needs_access_check);
5430
5431 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5432 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5433 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5434 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005435 SetRawInputAt(0, current_method);
5436 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005437
5438 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005439
5440 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005441 // Note that we don't need to test for generate_clinit_check_.
5442 // Whether or not we need to generate the clinit check is processed in
5443 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005444 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005445 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005446 }
5447
5448 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5449
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005450 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005451 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005452
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005453 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005454 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005455 }
5456
Calin Juravle0ba218d2015-05-19 18:46:01 +01005457 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005458 // The entrypoint the code generator is going to call does not do
5459 // clinit of the class.
5460 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005461 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005462 }
5463
5464 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005465 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005466 (!IsReferrersClass() && !IsInDexCache()) ||
5467 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005468 }
5469
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005470
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005471 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005472 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005473 }
5474
Calin Juravleacf735c2015-02-12 15:25:22 +00005475 ReferenceTypeInfo GetLoadedClassRTI() {
5476 return loaded_class_rti_;
5477 }
5478
5479 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5480 // Make sure we only set exact types (the loaded class should never be merged).
5481 DCHECK(rti.IsExact());
5482 loaded_class_rti_ = rti;
5483 }
5484
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005485 const DexFile& GetDexFile() { return dex_file_; }
5486
Vladimir Markoa1de9182016-02-25 11:37:38 +00005487 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005488
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005489 static SideEffects SideEffectsForArchRuntimeCalls() {
5490 return SideEffects::CanTriggerGC();
5491 }
5492
Vladimir Markoa1de9182016-02-25 11:37:38 +00005493 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5494 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5495 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5496 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005497
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005498 DECLARE_INSTRUCTION(LoadClass);
5499
5500 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005501 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5502 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5503 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005504 // Whether this instruction must generate the initialization check.
5505 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005506 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5507 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5508 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5509
5510 const uint16_t type_index_;
5511 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005512
Calin Juravleacf735c2015-02-12 15:25:22 +00005513 ReferenceTypeInfo loaded_class_rti_;
5514
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005515 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5516};
5517
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005518class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005519 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005520 HLoadString(HCurrentMethod* current_method,
5521 uint32_t string_index,
5522 uint32_t dex_pc,
5523 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005524 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005525 string_index_(string_index) {
5526 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005527 SetRawInputAt(0, current_method);
5528 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005529
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005530 bool CanBeMoved() const OVERRIDE { return true; }
5531
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005532 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5533 return other->AsLoadString()->string_index_ == string_index_;
5534 }
5535
5536 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5537
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005538 uint32_t GetStringIndex() const { return string_index_; }
5539
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005540 // Will call the runtime if the string is not already in the dex cache.
5541 bool NeedsEnvironment() const OVERRIDE { return !IsInDexCache(); }
5542
Vladimir Markodc151b22015-10-15 18:02:30 +01005543 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005544 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005545 bool CanThrow() const OVERRIDE { return !IsInDexCache(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005546
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005547 static SideEffects SideEffectsForArchRuntimeCalls() {
5548 return SideEffects::CanTriggerGC();
5549 }
5550
Vladimir Markoa1de9182016-02-25 11:37:38 +00005551 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5552
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005553 DECLARE_INSTRUCTION(LoadString);
5554
5555 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005556 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
5557 static constexpr size_t kNumberOfLoadStringPackedBits = kFlagIsInDexCache + 1;
5558 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5559
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005560 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005561
5562 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5563};
5564
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005565/**
5566 * Performs an initialization check on its Class object input.
5567 */
5568class HClinitCheck : public HExpression<1> {
5569 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005570 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005571 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005572 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005573 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5574 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005575 SetRawInputAt(0, constant);
5576 }
5577
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005578 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005579 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005580 return true;
5581 }
5582
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005583 bool NeedsEnvironment() const OVERRIDE {
5584 // May call runtime to initialize the class.
5585 return true;
5586 }
5587
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005588 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005589
5590 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5591
5592 DECLARE_INSTRUCTION(ClinitCheck);
5593
5594 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005595 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5596};
5597
5598class HStaticFieldGet : public HExpression<1> {
5599 public:
5600 HStaticFieldGet(HInstruction* cls,
5601 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005602 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005603 bool is_volatile,
5604 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005605 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005606 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005607 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005608 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005609 : HExpression(field_type,
5610 SideEffects::FieldReadOfType(field_type, is_volatile),
5611 dex_pc),
5612 field_info_(field_offset,
5613 field_type,
5614 is_volatile,
5615 field_idx,
5616 declaring_class_def_index,
5617 dex_file,
5618 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005619 SetRawInputAt(0, cls);
5620 }
5621
Calin Juravle52c48962014-12-16 17:02:57 +00005622
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005623 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005624
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005625 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005626 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5627 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005628 }
5629
5630 size_t ComputeHashCode() const OVERRIDE {
5631 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5632 }
5633
Calin Juravle52c48962014-12-16 17:02:57 +00005634 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005635 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5636 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005637 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005638
5639 DECLARE_INSTRUCTION(StaticFieldGet);
5640
5641 private:
5642 const FieldInfo field_info_;
5643
5644 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5645};
5646
5647class HStaticFieldSet : public HTemplateInstruction<2> {
5648 public:
5649 HStaticFieldSet(HInstruction* cls,
5650 HInstruction* value,
5651 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005652 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005653 bool is_volatile,
5654 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005655 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005656 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005657 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005658 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005659 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5660 dex_pc),
5661 field_info_(field_offset,
5662 field_type,
5663 is_volatile,
5664 field_idx,
5665 declaring_class_def_index,
5666 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005667 dex_cache) {
5668 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005669 SetRawInputAt(0, cls);
5670 SetRawInputAt(1, value);
5671 }
5672
Calin Juravle52c48962014-12-16 17:02:57 +00005673 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005674 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5675 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005676 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005677
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005678 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005679 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5680 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005681
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005682 DECLARE_INSTRUCTION(StaticFieldSet);
5683
5684 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005685 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5686 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5687 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5688 "Too many packed fields.");
5689
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005690 const FieldInfo field_info_;
5691
5692 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5693};
5694
Calin Juravlee460d1d2015-09-29 04:52:17 +01005695class HUnresolvedInstanceFieldGet : public HExpression<1> {
5696 public:
5697 HUnresolvedInstanceFieldGet(HInstruction* obj,
5698 Primitive::Type field_type,
5699 uint32_t field_index,
5700 uint32_t dex_pc)
5701 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5702 field_index_(field_index) {
5703 SetRawInputAt(0, obj);
5704 }
5705
5706 bool NeedsEnvironment() const OVERRIDE { return true; }
5707 bool CanThrow() const OVERRIDE { return true; }
5708
5709 Primitive::Type GetFieldType() const { return GetType(); }
5710 uint32_t GetFieldIndex() const { return field_index_; }
5711
5712 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5713
5714 private:
5715 const uint32_t field_index_;
5716
5717 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5718};
5719
5720class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5721 public:
5722 HUnresolvedInstanceFieldSet(HInstruction* obj,
5723 HInstruction* value,
5724 Primitive::Type field_type,
5725 uint32_t field_index,
5726 uint32_t dex_pc)
5727 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005728 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005729 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005730 DCHECK_EQ(field_type, value->GetType());
5731 SetRawInputAt(0, obj);
5732 SetRawInputAt(1, value);
5733 }
5734
5735 bool NeedsEnvironment() const OVERRIDE { return true; }
5736 bool CanThrow() const OVERRIDE { return true; }
5737
Vladimir Markoa1de9182016-02-25 11:37:38 +00005738 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005739 uint32_t GetFieldIndex() const { return field_index_; }
5740
5741 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5742
5743 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005744 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5745 static constexpr size_t kFieldFieldTypeSize =
5746 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5747 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5748 kFieldFieldType + kFieldFieldTypeSize;
5749 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5750 "Too many packed fields.");
5751 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5752
Calin Juravlee460d1d2015-09-29 04:52:17 +01005753 const uint32_t field_index_;
5754
5755 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5756};
5757
5758class HUnresolvedStaticFieldGet : public HExpression<0> {
5759 public:
5760 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5761 uint32_t field_index,
5762 uint32_t dex_pc)
5763 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5764 field_index_(field_index) {
5765 }
5766
5767 bool NeedsEnvironment() const OVERRIDE { return true; }
5768 bool CanThrow() const OVERRIDE { return true; }
5769
5770 Primitive::Type GetFieldType() const { return GetType(); }
5771 uint32_t GetFieldIndex() const { return field_index_; }
5772
5773 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5774
5775 private:
5776 const uint32_t field_index_;
5777
5778 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5779};
5780
5781class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5782 public:
5783 HUnresolvedStaticFieldSet(HInstruction* value,
5784 Primitive::Type field_type,
5785 uint32_t field_index,
5786 uint32_t dex_pc)
5787 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005788 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005789 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005790 DCHECK_EQ(field_type, value->GetType());
5791 SetRawInputAt(0, value);
5792 }
5793
5794 bool NeedsEnvironment() const OVERRIDE { return true; }
5795 bool CanThrow() const OVERRIDE { return true; }
5796
Vladimir Markoa1de9182016-02-25 11:37:38 +00005797 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005798 uint32_t GetFieldIndex() const { return field_index_; }
5799
5800 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5801
5802 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005803 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5804 static constexpr size_t kFieldFieldTypeSize =
5805 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5806 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5807 kFieldFieldType + kFieldFieldTypeSize;
5808 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5809 "Too many packed fields.");
5810 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5811
Calin Juravlee460d1d2015-09-29 04:52:17 +01005812 const uint32_t field_index_;
5813
5814 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5815};
5816
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005817// Implement the move-exception DEX instruction.
5818class HLoadException : public HExpression<0> {
5819 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005820 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5821 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005822
David Brazdilbbd733e2015-08-18 17:48:17 +01005823 bool CanBeNull() const OVERRIDE { return false; }
5824
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005825 DECLARE_INSTRUCTION(LoadException);
5826
5827 private:
5828 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5829};
5830
David Brazdilcb1c0552015-08-04 16:22:25 +01005831// Implicit part of move-exception which clears thread-local exception storage.
5832// Must not be removed because the runtime expects the TLS to get cleared.
5833class HClearException : public HTemplateInstruction<0> {
5834 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005835 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5836 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005837
5838 DECLARE_INSTRUCTION(ClearException);
5839
5840 private:
5841 DISALLOW_COPY_AND_ASSIGN(HClearException);
5842};
5843
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005844class HThrow : public HTemplateInstruction<1> {
5845 public:
5846 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005847 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005848 SetRawInputAt(0, exception);
5849 }
5850
5851 bool IsControlFlow() const OVERRIDE { return true; }
5852
5853 bool NeedsEnvironment() const OVERRIDE { return true; }
5854
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005855 bool CanThrow() const OVERRIDE { return true; }
5856
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005857
5858 DECLARE_INSTRUCTION(Throw);
5859
5860 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005861 DISALLOW_COPY_AND_ASSIGN(HThrow);
5862};
5863
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005864/**
5865 * Implementation strategies for the code generator of a HInstanceOf
5866 * or `HCheckCast`.
5867 */
5868enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005869 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005870 kExactCheck, // Can do a single class compare.
5871 kClassHierarchyCheck, // Can just walk the super class chain.
5872 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5873 kInterfaceCheck, // No optimization yet when checking against an interface.
5874 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005875 kArrayCheck, // No optimization yet when checking against a generic array.
5876 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005877};
5878
Roland Levillain86503782016-02-11 19:07:30 +00005879std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5880
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005881class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005882 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005883 HInstanceOf(HInstruction* object,
5884 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005885 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005886 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005887 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005888 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005889 dex_pc) {
5890 SetPackedField<TypeCheckKindField>(check_kind);
5891 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005892 SetRawInputAt(0, object);
5893 SetRawInputAt(1, constant);
5894 }
5895
5896 bool CanBeMoved() const OVERRIDE { return true; }
5897
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005898 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005899 return true;
5900 }
5901
5902 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005903 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005904 }
5905
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005906 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005907 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5908 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5909 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5910 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005911
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005912 static bool CanCallRuntime(TypeCheckKind check_kind) {
5913 // Mips currently does runtime calls for any other checks.
5914 return check_kind != TypeCheckKind::kExactCheck;
5915 }
5916
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005917 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005918 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005919 }
5920
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005921 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005922
5923 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005924 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
5925 static constexpr size_t kFieldTypeCheckKindSize =
5926 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
5927 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
5928 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
5929 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5930 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005931
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005932 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5933};
5934
Calin Juravleb1498f62015-02-16 13:13:29 +00005935class HBoundType : public HExpression<1> {
5936 public:
David Brazdilf5552582015-12-27 13:36:12 +00005937 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005938 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005939 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
5940 SetPackedFlag<kFlagUpperCanBeNull>(true);
5941 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00005942 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005943 SetRawInputAt(0, input);
5944 }
5945
David Brazdilf5552582015-12-27 13:36:12 +00005946 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005947 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005948 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00005949 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005950
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005951 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005952 DCHECK(GetUpperCanBeNull() || !can_be_null);
5953 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005954 }
5955
Vladimir Markoa1de9182016-02-25 11:37:38 +00005956 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005957
Calin Juravleb1498f62015-02-16 13:13:29 +00005958 DECLARE_INSTRUCTION(BoundType);
5959
5960 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005961 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5962 // is false then CanBeNull() cannot be true).
5963 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
5964 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
5965 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
5966 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5967
Calin Juravleb1498f62015-02-16 13:13:29 +00005968 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005969 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5970 // It is used to bound the type in cases like:
5971 // if (x instanceof ClassX) {
5972 // // uper_bound_ will be ClassX
5973 // }
David Brazdilf5552582015-12-27 13:36:12 +00005974 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005975
5976 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5977};
5978
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005979class HCheckCast : public HTemplateInstruction<2> {
5980 public:
5981 HCheckCast(HInstruction* object,
5982 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005983 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005984 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00005985 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
5986 SetPackedField<TypeCheckKindField>(check_kind);
5987 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005988 SetRawInputAt(0, object);
5989 SetRawInputAt(1, constant);
5990 }
5991
5992 bool CanBeMoved() const OVERRIDE { return true; }
5993
5994 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5995 return true;
5996 }
5997
5998 bool NeedsEnvironment() const OVERRIDE {
5999 // Instruction may throw a CheckCastError.
6000 return true;
6001 }
6002
6003 bool CanThrow() const OVERRIDE { return true; }
6004
Vladimir Markoa1de9182016-02-25 11:37:38 +00006005 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6006 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6007 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6008 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006009
6010 DECLARE_INSTRUCTION(CheckCast);
6011
6012 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006013 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6014 static constexpr size_t kFieldTypeCheckKindSize =
6015 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6016 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6017 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6018 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6019 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006020
6021 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006022};
6023
Calin Juravle27df7582015-04-17 19:12:31 +01006024class HMemoryBarrier : public HTemplateInstruction<0> {
6025 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006026 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006027 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006028 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6029 SetPackedField<BarrierKindField>(barrier_kind);
6030 }
Calin Juravle27df7582015-04-17 19:12:31 +01006031
Vladimir Markoa1de9182016-02-25 11:37:38 +00006032 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006033
6034 DECLARE_INSTRUCTION(MemoryBarrier);
6035
6036 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006037 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6038 static constexpr size_t kFieldBarrierKindSize =
6039 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6040 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6041 kFieldBarrierKind + kFieldBarrierKindSize;
6042 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6043 "Too many packed fields.");
6044 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006045
6046 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6047};
6048
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006049class HMonitorOperation : public HTemplateInstruction<1> {
6050 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006051 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006052 kEnter,
6053 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006054 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006055 };
6056
6057 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006058 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006059 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6060 dex_pc) {
6061 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006062 SetRawInputAt(0, object);
6063 }
6064
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006065 // Instruction may go into runtime, so we need an environment.
6066 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006067
6068 bool CanThrow() const OVERRIDE {
6069 // Verifier guarantees that monitor-exit cannot throw.
6070 // This is important because it allows the HGraphBuilder to remove
6071 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6072 return IsEnter();
6073 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006074
Vladimir Markoa1de9182016-02-25 11:37:38 +00006075 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6076 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006077
6078 DECLARE_INSTRUCTION(MonitorOperation);
6079
Calin Juravle52c48962014-12-16 17:02:57 +00006080 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006081 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6082 static constexpr size_t kFieldOperationKindSize =
6083 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6084 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6085 kFieldOperationKind + kFieldOperationKindSize;
6086 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6087 "Too many packed fields.");
6088 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006089
6090 private:
6091 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6092};
6093
David Brazdil74eb1b22015-12-14 11:44:01 +00006094class HSelect : public HExpression<3> {
6095 public:
6096 HSelect(HInstruction* condition,
6097 HInstruction* true_value,
6098 HInstruction* false_value,
6099 uint32_t dex_pc)
6100 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6101 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6102
6103 // First input must be `true_value` or `false_value` to allow codegens to
6104 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6105 // that architectures which implement HSelect as a conditional move also
6106 // will not need to invert the condition.
6107 SetRawInputAt(0, false_value);
6108 SetRawInputAt(1, true_value);
6109 SetRawInputAt(2, condition);
6110 }
6111
6112 HInstruction* GetFalseValue() const { return InputAt(0); }
6113 HInstruction* GetTrueValue() const { return InputAt(1); }
6114 HInstruction* GetCondition() const { return InputAt(2); }
6115
6116 bool CanBeMoved() const OVERRIDE { return true; }
6117 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6118
6119 bool CanBeNull() const OVERRIDE {
6120 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6121 }
6122
6123 DECLARE_INSTRUCTION(Select);
6124
6125 private:
6126 DISALLOW_COPY_AND_ASSIGN(HSelect);
6127};
6128
Vladimir Markof9f64412015-09-02 14:05:49 +01006129class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006130 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006131 MoveOperands(Location source,
6132 Location destination,
6133 Primitive::Type type,
6134 HInstruction* instruction)
6135 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006136
6137 Location GetSource() const { return source_; }
6138 Location GetDestination() const { return destination_; }
6139
6140 void SetSource(Location value) { source_ = value; }
6141 void SetDestination(Location value) { destination_ = value; }
6142
6143 // The parallel move resolver marks moves as "in-progress" by clearing the
6144 // destination (but not the source).
6145 Location MarkPending() {
6146 DCHECK(!IsPending());
6147 Location dest = destination_;
6148 destination_ = Location::NoLocation();
6149 return dest;
6150 }
6151
6152 void ClearPending(Location dest) {
6153 DCHECK(IsPending());
6154 destination_ = dest;
6155 }
6156
6157 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006158 DCHECK(source_.IsValid() || destination_.IsInvalid());
6159 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006160 }
6161
6162 // True if this blocks a move from the given location.
6163 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006164 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006165 }
6166
6167 // A move is redundant if it's been eliminated, if its source and
6168 // destination are the same, or if its destination is unneeded.
6169 bool IsRedundant() const {
6170 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6171 }
6172
6173 // We clear both operands to indicate move that's been eliminated.
6174 void Eliminate() {
6175 source_ = destination_ = Location::NoLocation();
6176 }
6177
6178 bool IsEliminated() const {
6179 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6180 return source_.IsInvalid();
6181 }
6182
Alexey Frunze4dda3372015-06-01 18:31:49 -07006183 Primitive::Type GetType() const { return type_; }
6184
Nicolas Geoffray90218252015-04-15 11:56:51 +01006185 bool Is64BitMove() const {
6186 return Primitive::Is64BitType(type_);
6187 }
6188
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006189 HInstruction* GetInstruction() const { return instruction_; }
6190
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006191 private:
6192 Location source_;
6193 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006194 // The type this move is for.
6195 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006196 // The instruction this move is assocatied with. Null when this move is
6197 // for moving an input in the expected locations of user (including a phi user).
6198 // This is only used in debug mode, to ensure we do not connect interval siblings
6199 // in the same parallel move.
6200 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006201};
6202
Roland Levillainc9285912015-12-18 10:38:42 +00006203std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6204
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006205static constexpr size_t kDefaultNumberOfMoves = 4;
6206
6207class HParallelMove : public HTemplateInstruction<0> {
6208 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006209 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006210 : HTemplateInstruction(SideEffects::None(), dex_pc),
6211 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6212 moves_.reserve(kDefaultNumberOfMoves);
6213 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006214
Nicolas Geoffray90218252015-04-15 11:56:51 +01006215 void AddMove(Location source,
6216 Location destination,
6217 Primitive::Type type,
6218 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006219 DCHECK(source.IsValid());
6220 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006221 if (kIsDebugBuild) {
6222 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006223 for (const MoveOperands& move : moves_) {
6224 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006225 // Special case the situation where the move is for the spill slot
6226 // of the instruction.
6227 if ((GetPrevious() == instruction)
6228 || ((GetPrevious() == nullptr)
6229 && instruction->IsPhi()
6230 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006231 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006232 << "Doing parallel moves for the same instruction.";
6233 } else {
6234 DCHECK(false) << "Doing parallel moves for the same instruction.";
6235 }
6236 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006237 }
6238 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006239 for (const MoveOperands& move : moves_) {
6240 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006241 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006242 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006243 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006244 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006245 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006246 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006247 }
6248
Vladimir Marko225b6462015-09-28 12:17:40 +01006249 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006250 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006251 }
6252
Vladimir Marko225b6462015-09-28 12:17:40 +01006253 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006254
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006255 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006256
6257 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006258 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006259
6260 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6261};
6262
Mark Mendell0616ae02015-04-17 12:49:27 -04006263} // namespace art
6264
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006265#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6266#include "nodes_shared.h"
6267#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006268#ifdef ART_ENABLE_CODEGEN_arm
6269#include "nodes_arm.h"
6270#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006271#ifdef ART_ENABLE_CODEGEN_arm64
6272#include "nodes_arm64.h"
6273#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006274#ifdef ART_ENABLE_CODEGEN_x86
6275#include "nodes_x86.h"
6276#endif
6277
6278namespace art {
6279
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006280class HGraphVisitor : public ValueObject {
6281 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006282 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6283 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006284
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006285 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006286 virtual void VisitBasicBlock(HBasicBlock* block);
6287
Roland Levillain633021e2014-10-01 14:12:25 +01006288 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006289 void VisitInsertionOrder();
6290
Roland Levillain633021e2014-10-01 14:12:25 +01006291 // Visit the graph following dominator tree reverse post-order.
6292 void VisitReversePostOrder();
6293
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006294 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006295
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006296 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006297#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006298 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6299
6300 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6301
6302#undef DECLARE_VISIT_INSTRUCTION
6303
6304 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006305 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006306
6307 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6308};
6309
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006310class HGraphDelegateVisitor : public HGraphVisitor {
6311 public:
6312 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6313 virtual ~HGraphDelegateVisitor() {}
6314
6315 // Visit functions that delegate to to super class.
6316#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006317 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006318
6319 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6320
6321#undef DECLARE_VISIT_INSTRUCTION
6322
6323 private:
6324 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6325};
6326
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006327class HInsertionOrderIterator : public ValueObject {
6328 public:
6329 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6330
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006331 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006332 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006333 void Advance() { ++index_; }
6334
6335 private:
6336 const HGraph& graph_;
6337 size_t index_;
6338
6339 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6340};
6341
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006342class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006343 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006344 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6345 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006346 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006347 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006348
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006349 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6350 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006351 void Advance() { ++index_; }
6352
6353 private:
6354 const HGraph& graph_;
6355 size_t index_;
6356
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006357 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006358};
6359
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006360class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006361 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006362 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006363 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006364 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006365 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006366 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006367
6368 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006369 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006370 void Advance() { --index_; }
6371
6372 private:
6373 const HGraph& graph_;
6374 size_t index_;
6375
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006376 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006377};
6378
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006379class HLinearPostOrderIterator : public ValueObject {
6380 public:
6381 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006382 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006383
6384 bool Done() const { return index_ == 0; }
6385
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006386 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006387
6388 void Advance() {
6389 --index_;
6390 DCHECK_GE(index_, 0U);
6391 }
6392
6393 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006394 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006395 size_t index_;
6396
6397 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6398};
6399
6400class HLinearOrderIterator : public ValueObject {
6401 public:
6402 explicit HLinearOrderIterator(const HGraph& graph)
6403 : order_(graph.GetLinearOrder()), index_(0) {}
6404
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006405 bool Done() const { return index_ == order_.size(); }
6406 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006407 void Advance() { ++index_; }
6408
6409 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006410 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006411 size_t index_;
6412
6413 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6414};
6415
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006416// Iterator over the blocks that art part of the loop. Includes blocks part
6417// of an inner loop. The order in which the blocks are iterated is on their
6418// block id.
6419class HBlocksInLoopIterator : public ValueObject {
6420 public:
6421 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6422 : blocks_in_loop_(info.GetBlocks()),
6423 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6424 index_(0) {
6425 if (!blocks_in_loop_.IsBitSet(index_)) {
6426 Advance();
6427 }
6428 }
6429
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006430 bool Done() const { return index_ == blocks_.size(); }
6431 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006432 void Advance() {
6433 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006434 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006435 if (blocks_in_loop_.IsBitSet(index_)) {
6436 break;
6437 }
6438 }
6439 }
6440
6441 private:
6442 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006443 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006444 size_t index_;
6445
6446 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6447};
6448
Mingyao Yang3584bce2015-05-19 16:01:59 -07006449// Iterator over the blocks that art part of the loop. Includes blocks part
6450// of an inner loop. The order in which the blocks are iterated is reverse
6451// post order.
6452class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6453 public:
6454 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6455 : blocks_in_loop_(info.GetBlocks()),
6456 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6457 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006458 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006459 Advance();
6460 }
6461 }
6462
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006463 bool Done() const { return index_ == blocks_.size(); }
6464 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006465 void Advance() {
6466 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006467 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6468 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006469 break;
6470 }
6471 }
6472 }
6473
6474 private:
6475 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006476 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006477 size_t index_;
6478
6479 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6480};
6481
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006482inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006483 if (constant->IsIntConstant()) {
6484 return constant->AsIntConstant()->GetValue();
6485 } else if (constant->IsLongConstant()) {
6486 return constant->AsLongConstant()->GetValue();
6487 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006488 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006489 return 0;
6490 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006491}
6492
Vladimir Marko58155012015-08-19 12:49:41 +00006493inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6494 // For the purposes of the compiler, the dex files must actually be the same object
6495 // if we want to safely treat them as the same. This is especially important for JIT
6496 // as custom class loaders can open the same underlying file (or memory) multiple
6497 // times and provide different class resolution but no two class loaders should ever
6498 // use the same DexFile object - doing so is an unsupported hack that can lead to
6499 // all sorts of weird failures.
6500 return &lhs == &rhs;
6501}
6502
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006503#define INSTRUCTION_TYPE_CHECK(type, super) \
6504 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6505 inline const H##type* HInstruction::As##type() const { \
6506 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6507 } \
6508 inline H##type* HInstruction::As##type() { \
6509 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6510 }
6511
6512 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6513#undef INSTRUCTION_TYPE_CHECK
6514
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00006515class SwitchTable : public ValueObject {
6516 public:
6517 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
6518 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
6519 int32_t table_offset = instruction.VRegB_31t();
6520 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
6521 if (sparse) {
6522 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
6523 } else {
6524 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
6525 }
6526 num_entries_ = table[1];
6527 values_ = reinterpret_cast<const int32_t*>(&table[2]);
6528 }
6529
6530 uint16_t GetNumEntries() const {
6531 return num_entries_;
6532 }
6533
6534 void CheckIndex(size_t index) const {
6535 if (sparse_) {
6536 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6537 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
6538 } else {
6539 // In a packed table, we have the starting key and num_entries_ values.
6540 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
6541 }
6542 }
6543
6544 int32_t GetEntryAt(size_t index) const {
6545 CheckIndex(index);
6546 return values_[index];
6547 }
6548
6549 uint32_t GetDexPcForIndex(size_t index) const {
6550 CheckIndex(index);
6551 return dex_pc_ +
6552 (reinterpret_cast<const int16_t*>(values_ + index) -
6553 reinterpret_cast<const int16_t*>(&instruction_));
6554 }
6555
6556 // Index of the first value in the table.
6557 size_t GetFirstValueIndex() const {
6558 if (sparse_) {
6559 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6560 return num_entries_;
6561 } else {
6562 // In a packed table, we have the starting key and num_entries_ values.
6563 return 1;
6564 }
6565 }
6566
6567 private:
6568 const Instruction& instruction_;
6569 const uint32_t dex_pc_;
6570
6571 // Whether this is a sparse-switch table (or a packed-switch one).
6572 const bool sparse_;
6573
6574 // This can't be const as it needs to be computed off of the given instruction, and complicated
6575 // expressions in the initializer list seemed very ugly.
6576 uint16_t num_entries_;
6577
6578 const int32_t* values_;
6579
6580 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
6581};
6582
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006583// Create space in `blocks` for adding `number_of_new_blocks` entries
6584// starting at location `at`. Blocks after `at` are moved accordingly.
6585inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6586 size_t number_of_new_blocks,
6587 size_t after) {
6588 DCHECK_LT(after, blocks->size());
6589 size_t old_size = blocks->size();
6590 size_t new_size = old_size + number_of_new_blocks;
6591 blocks->resize(new_size);
6592 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6593}
6594
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006595} // namespace art
6596
6597#endif // ART_COMPILER_OPTIMIZING_NODES_H_