blob: be304f610d43a16f86d50e6fd036b96d44c933ef [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010028#include "dex/compiler_enums.h"
Andreas Gampe7c9c31c2016-03-08 16:00:41 -080029#include "dex_instruction-inl.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000030#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000031#include "handle.h"
32#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000033#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010034#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000035#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000036#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010037#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070038#include "primitive.h"
David Brazdild26a4112015-11-10 11:07:31 +000039#include "utils/array_ref.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040
41namespace art {
42
David Brazdil1abb4192015-02-17 18:33:36 +000043class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010045class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000046class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010047class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000048class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000049class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000052class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000053class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000055class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010056class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010057class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010058class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010059class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000060class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010061class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000062class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000063
Mathieu Chartier736b5602015-09-02 14:54:11 -070064namespace mirror {
65class DexCache;
66} // namespace mirror
67
Nicolas Geoffray818f2102014-02-18 16:43:35 +000068static const int kDefaultNumberOfBlocks = 8;
69static const int kDefaultNumberOfSuccessors = 2;
70static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010071static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010072static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000073static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000074
Calin Juravle9aec02f2014-11-18 23:06:35 +000075static constexpr uint32_t kMaxIntShiftValue = 0x1f;
76static constexpr uint64_t kMaxLongShiftValue = 0x3f;
77
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010078static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070079static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010080
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010081static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
82
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060083static constexpr uint32_t kNoDexPc = -1;
84
Dave Allison20dfc792014-06-16 20:44:29 -070085enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070086 // All types.
87 kCondEQ, // ==
88 kCondNE, // !=
89 // Signed integers and floating-point numbers.
90 kCondLT, // <
91 kCondLE, // <=
92 kCondGT, // >
93 kCondGE, // >=
94 // Unsigned integers.
95 kCondB, // <
96 kCondBE, // <=
97 kCondA, // >
98 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -070099};
100
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000101enum GraphAnalysisResult {
David Brazdilbadd8262016-02-02 16:28:56 +0000102 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000103 kAnalysisFailThrowCatchLoop,
104 kAnalysisFailAmbiguousArrayOp,
105 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000106};
107
Vladimir Markof9f64412015-09-02 14:05:49 +0100108class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100109 public:
110 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
111
112 void AddInstruction(HInstruction* instruction);
113 void RemoveInstruction(HInstruction* instruction);
114
David Brazdilc3d743f2015-04-22 13:40:50 +0100115 // Insert `instruction` before/after an existing instruction `cursor`.
116 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
117 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
118
Roland Levillain6b469232014-09-25 10:10:38 +0100119 // Return true if this list contains `instruction`.
120 bool Contains(HInstruction* instruction) const;
121
Roland Levillainccc07a92014-09-16 14:48:16 +0100122 // Return true if `instruction1` is found before `instruction2` in
123 // this instruction list and false otherwise. Abort if none
124 // of these instructions is found.
125 bool FoundBefore(const HInstruction* instruction1,
126 const HInstruction* instruction2) const;
127
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000128 bool IsEmpty() const { return first_instruction_ == nullptr; }
129 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
130
131 // Update the block of all instructions to be `block`.
132 void SetBlockOfInstructions(HBasicBlock* block) const;
133
134 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000135 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000136 void Add(const HInstructionList& instruction_list);
137
David Brazdil2d7352b2015-04-20 14:52:42 +0100138 // Return the number of instructions in the list. This is an expensive operation.
139 size_t CountSize() const;
140
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100141 private:
142 HInstruction* first_instruction_;
143 HInstruction* last_instruction_;
144
145 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000146 friend class HGraph;
147 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100148 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100149 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100150
151 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
152};
153
David Brazdil4833f5a2015-12-16 10:37:39 +0000154class ReferenceTypeInfo : ValueObject {
155 public:
156 typedef Handle<mirror::Class> TypeHandle;
157
Vladimir Markoa1de9182016-02-25 11:37:38 +0000158 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
159
160 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000161 return ReferenceTypeInfo(type_handle, is_exact);
162 }
163
164 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
165
Vladimir Markof39745e2016-01-26 12:16:55 +0000166 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000167 return handle.GetReference() != nullptr;
168 }
169
170 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
171 return IsValidHandle(type_handle_);
172 }
173
174 bool IsExact() const { return is_exact_; }
175
176 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
177 DCHECK(IsValid());
178 return GetTypeHandle()->IsObjectClass();
179 }
180
181 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
182 DCHECK(IsValid());
183 return GetTypeHandle()->IsStringClass();
184 }
185
186 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
187 DCHECK(IsValid());
188 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
189 }
190
191 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
192 DCHECK(IsValid());
193 return GetTypeHandle()->IsInterface();
194 }
195
196 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
197 DCHECK(IsValid());
198 return GetTypeHandle()->IsArrayClass();
199 }
200
201 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
202 DCHECK(IsValid());
203 return GetTypeHandle()->IsPrimitiveArray();
204 }
205
206 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
207 DCHECK(IsValid());
208 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
209 }
210
211 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
212 DCHECK(IsValid());
213 if (!IsExact()) return false;
214 if (!IsArrayClass()) return false;
215 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
216 }
217
218 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
219 DCHECK(IsValid());
220 if (!IsExact()) return false;
221 if (!IsArrayClass()) return false;
222 if (!rti.IsArrayClass()) return false;
223 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
224 rti.GetTypeHandle()->GetComponentType());
225 }
226
227 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
228
229 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
230 DCHECK(IsValid());
231 DCHECK(rti.IsValid());
232 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
233 }
234
235 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
236 DCHECK(IsValid());
237 DCHECK(rti.IsValid());
238 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
239 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
240 }
241
242 // Returns true if the type information provide the same amount of details.
243 // Note that it does not mean that the instructions have the same actual type
244 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000245 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000246 if (!IsValid() && !rti.IsValid()) {
247 // Invalid types are equal.
248 return true;
249 }
250 if (!IsValid() || !rti.IsValid()) {
251 // One is valid, the other not.
252 return false;
253 }
254 return IsExact() == rti.IsExact()
255 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
256 }
257
258 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000259 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
260 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
261 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000262
263 // The class of the object.
264 TypeHandle type_handle_;
265 // Whether or not the type is exact or a superclass of the actual type.
266 // Whether or not we have any information about this type.
267 bool is_exact_;
268};
269
270std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
271
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000272// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100273class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000274 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100275 HGraph(ArenaAllocator* arena,
276 const DexFile& dex_file,
277 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100278 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700279 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100280 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100281 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000282 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100283 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000284 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100285 blocks_(arena->Adapter(kArenaAllocBlockList)),
286 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
287 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700288 entry_block_(nullptr),
289 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100290 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100291 number_of_vregs_(0),
292 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000293 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400294 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000295 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000296 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000297 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000298 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100299 dex_file_(dex_file),
300 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100301 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100302 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100303 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700304 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000305 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100306 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
307 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
308 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
309 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000310 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000311 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
312 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100313 blocks_.reserve(kDefaultNumberOfBlocks);
314 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000315
David Brazdilbadd8262016-02-02 16:28:56 +0000316 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
317 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
318
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000319 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100320 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
321
David Brazdil69ba7b72015-06-23 18:27:30 +0100322 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000323 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100324
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000325 HBasicBlock* GetEntryBlock() const { return entry_block_; }
326 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100327 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000328
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000329 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
330 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000331
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000332 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100333
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100334 void ComputeDominanceInformation();
335 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000336 void ClearLoopInformation();
337 void FindBackEdges(ArenaBitVector* visited);
338 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100339 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100340 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000341
David Brazdil4833f5a2015-12-16 10:37:39 +0000342 // Analyze all natural loops in this graph. Returns a code specifying that it
343 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000344 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000345 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100346
David Brazdilffee3d32015-07-06 11:48:53 +0100347 // Iterate over blocks to compute try block membership. Needs reverse post
348 // order and loop information.
349 void ComputeTryBlockInformation();
350
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000351 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000352 // Returns the instruction to replace the invoke expression or null if the
353 // invoke is for a void method. Note that the caller is responsible for replacing
354 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000355 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000356
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000357 // Update the loop and try membership of `block`, which was spawned from `reference`.
358 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
359 // should be the new back edge.
360 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
361 HBasicBlock* reference,
362 bool replace_if_back_edge);
363
Mingyao Yang3584bce2015-05-19 16:01:59 -0700364 // Need to add a couple of blocks to test if the loop body is entered and
365 // put deoptimization instructions, etc.
366 void TransformLoopHeaderForBCE(HBasicBlock* header);
367
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000368 // Removes `block` from the graph. Assumes `block` has been disconnected from
369 // other blocks and has no instructions or phis.
370 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000371
David Brazdilfc6a86a2015-06-26 10:33:45 +0000372 // Splits the edge between `block` and `successor` while preserving the
373 // indices in the predecessor/successor lists. If there are multiple edges
374 // between the blocks, the lowest indices are used.
375 // Returns the new block which is empty and has the same dex pc as `successor`.
376 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
377
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100378 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
379 void SimplifyLoop(HBasicBlock* header);
380
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000381 int32_t GetNextInstructionId() {
382 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000383 return current_instruction_id_++;
384 }
385
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000386 int32_t GetCurrentInstructionId() const {
387 return current_instruction_id_;
388 }
389
390 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000391 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000392 current_instruction_id_ = id;
393 }
394
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100395 uint16_t GetMaximumNumberOfOutVRegs() const {
396 return maximum_number_of_out_vregs_;
397 }
398
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000399 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
400 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100401 }
402
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100403 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
404 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
405 }
406
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000407 void UpdateTemporariesVRegSlots(size_t slots) {
408 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100409 }
410
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000411 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100412 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000413 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100414 }
415
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100416 void SetNumberOfVRegs(uint16_t number_of_vregs) {
417 number_of_vregs_ = number_of_vregs;
418 }
419
420 uint16_t GetNumberOfVRegs() const {
421 return number_of_vregs_;
422 }
423
424 void SetNumberOfInVRegs(uint16_t value) {
425 number_of_in_vregs_ = value;
426 }
427
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100428 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100429 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100430 return number_of_vregs_ - number_of_in_vregs_;
431 }
432
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100433 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100434 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100435 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100436
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100437 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100438 return linear_order_;
439 }
440
Mark Mendell1152c922015-04-24 17:06:35 -0400441 bool HasBoundsChecks() const {
442 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800443 }
444
Mark Mendell1152c922015-04-24 17:06:35 -0400445 void SetHasBoundsChecks(bool value) {
446 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800447 }
448
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100449 bool ShouldGenerateConstructorBarrier() const {
450 return should_generate_constructor_barrier_;
451 }
452
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000453 bool IsDebuggable() const { return debuggable_; }
454
David Brazdil8d5b8b22015-03-24 10:51:52 +0000455 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000456 // already, it is created and inserted into the graph. This method is only for
457 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600458 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000459
460 // TODO: This is problematic for the consistency of reference type propagation
461 // because it can be created anytime after the pass and thus it will be left
462 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600463 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000464
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600465 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
466 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000467 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600468 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
469 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000470 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600471 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
472 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000473 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600474 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
475 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000476 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000477
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100478 HCurrentMethod* GetCurrentMethod();
479
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100480 const DexFile& GetDexFile() const {
481 return dex_file_;
482 }
483
484 uint32_t GetMethodIdx() const {
485 return method_idx_;
486 }
487
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100488 InvokeType GetInvokeType() const {
489 return invoke_type_;
490 }
491
Mark Mendellc4701932015-04-10 13:18:51 -0400492 InstructionSet GetInstructionSet() const {
493 return instruction_set_;
494 }
495
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000496 bool IsCompilingOsr() const { return osr_; }
497
David Brazdil77a48ae2015-09-15 12:34:04 +0000498 bool HasTryCatch() const { return has_try_catch_; }
499 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100500
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000501 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
502 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
503
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100504 ArtMethod* GetArtMethod() const { return art_method_; }
505 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
506
Mark Mendellf6529172015-11-17 11:16:56 -0500507 // Returns an instruction with the opposite boolean value from 'cond'.
508 // The instruction has been inserted into the graph, either as a constant, or
509 // before cursor.
510 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
511
David Brazdil2d7352b2015-04-20 14:52:42 +0100512 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000513 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100514 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000515
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000516 template <class InstructionType, typename ValueType>
517 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600518 ArenaSafeMap<ValueType, InstructionType*>* cache,
519 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000520 // Try to find an existing constant of the given value.
521 InstructionType* constant = nullptr;
522 auto cached_constant = cache->find(value);
523 if (cached_constant != cache->end()) {
524 constant = cached_constant->second;
525 }
526
527 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100528 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000529 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600530 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000531 cache->Overwrite(value, constant);
532 InsertConstant(constant);
533 }
534 return constant;
535 }
536
David Brazdil8d5b8b22015-03-24 10:51:52 +0000537 void InsertConstant(HConstant* instruction);
538
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000539 // Cache a float constant into the graph. This method should only be
540 // called by the SsaBuilder when creating "equivalent" instructions.
541 void CacheFloatConstant(HFloatConstant* constant);
542
543 // See CacheFloatConstant comment.
544 void CacheDoubleConstant(HDoubleConstant* constant);
545
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000546 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000547
548 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100549 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000550
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100551 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100552 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000553
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100554 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100555 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100556
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000557 HBasicBlock* entry_block_;
558 HBasicBlock* exit_block_;
559
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100560 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100561 uint16_t maximum_number_of_out_vregs_;
562
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100563 // The number of virtual registers in this method. Contains the parameters.
564 uint16_t number_of_vregs_;
565
566 // The number of virtual registers used by parameters of this method.
567 uint16_t number_of_in_vregs_;
568
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000569 // Number of vreg size slots that the temporaries use (used in baseline compiler).
570 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100571
Mark Mendell1152c922015-04-24 17:06:35 -0400572 // Has bounds checks. We can totally skip BCE if it's false.
573 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800574
David Brazdil77a48ae2015-09-15 12:34:04 +0000575 // Flag whether there are any try/catch blocks in the graph. We will skip
576 // try/catch-related passes if false.
577 bool has_try_catch_;
578
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000579 // Flag whether there are any irreducible loops in the graph.
580 bool has_irreducible_loops_;
581
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000582 // Indicates whether the graph should be compiled in a way that
583 // ensures full debuggability. If false, we can apply more
584 // aggressive optimizations that may limit the level of debugging.
585 const bool debuggable_;
586
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000587 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000588 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000589
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100590 // The dex file from which the method is from.
591 const DexFile& dex_file_;
592
593 // The method index in the dex file.
594 const uint32_t method_idx_;
595
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100596 // If inlined, this encodes how the callee is being invoked.
597 const InvokeType invoke_type_;
598
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100599 // Whether the graph has been transformed to SSA form. Only used
600 // in debug mode to ensure we are not using properties only valid
601 // for non-SSA form (like the number of temporaries).
602 bool in_ssa_form_;
603
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100604 const bool should_generate_constructor_barrier_;
605
Mathieu Chartiere401d142015-04-22 13:56:20 -0700606 const InstructionSet instruction_set_;
607
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000608 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000609 HNullConstant* cached_null_constant_;
610 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000611 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000612 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000613 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000614
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100615 HCurrentMethod* cached_current_method_;
616
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100617 // The ArtMethod this graph is for. Note that for AOT, it may be null,
618 // for example for methods whose declaring class could not be resolved
619 // (such as when the superclass could not be found).
620 ArtMethod* art_method_;
621
David Brazdil4833f5a2015-12-16 10:37:39 +0000622 // Keep the RTI of inexact Object to avoid having to pass stack handle
623 // collection pointer to passes which may create NullConstant.
624 ReferenceTypeInfo inexact_object_rti_;
625
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000626 // Whether we are compiling this graph for on stack replacement: this will
627 // make all loops seen as irreducible and emit special stack maps to mark
628 // compiled code entries which the interpreter can directly jump to.
629 const bool osr_;
630
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000631 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100632 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000633 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000634 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000635 DISALLOW_COPY_AND_ASSIGN(HGraph);
636};
637
Vladimir Markof9f64412015-09-02 14:05:49 +0100638class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000639 public:
640 HLoopInformation(HBasicBlock* header, HGraph* graph)
641 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100642 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000643 irreducible_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100644 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100645 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100646 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
647 back_edges_.reserve(kDefaultNumberOfBackEdges);
648 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100649
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000650 bool IsIrreducible() const { return irreducible_; }
651
652 void Dump(std::ostream& os);
653
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100654 HBasicBlock* GetHeader() const {
655 return header_;
656 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000657
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000658 void SetHeader(HBasicBlock* block) {
659 header_ = block;
660 }
661
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100662 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
663 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
664 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
665
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000666 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100667 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000668 }
669
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100670 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100671 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100672 }
673
David Brazdil46e2a392015-03-16 17:31:52 +0000674 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100675 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100676 }
677
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000678 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100679 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000680 }
681
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100682 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100683
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100684 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100685 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100686 }
687
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100688 // Returns the lifetime position of the back edge that has the
689 // greatest lifetime position.
690 size_t GetLifetimeEnd() const;
691
692 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100693 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100694 }
695
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000696 // Finds blocks that are part of this loop.
697 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100698
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100699 // Returns whether this loop information contains `block`.
700 // Note that this loop information *must* be populated before entering this function.
701 bool Contains(const HBasicBlock& block) const;
702
703 // Returns whether this loop information is an inner loop of `other`.
704 // Note that `other` *must* be populated before entering this function.
705 bool IsIn(const HLoopInformation& other) const;
706
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800707 // Returns true if instruction is not defined within this loop.
708 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700709
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100710 const ArenaBitVector& GetBlocks() const { return blocks_; }
711
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000712 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000713 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000714
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000715 void ClearAllBlocks() {
716 blocks_.ClearAllBits();
717 }
718
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000719 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100720 // Internal recursive implementation of `Populate`.
721 void PopulateRecursive(HBasicBlock* block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000722 void PopulateIrreducibleRecursive(HBasicBlock* block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100723
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000724 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100725 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000726 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100727 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100728 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000729
730 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
731};
732
David Brazdilec16f792015-08-19 15:04:01 +0100733// Stores try/catch information for basic blocks.
734// Note that HGraph is constructed so that catch blocks cannot simultaneously
735// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100736class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100737 public:
738 // Try block information constructor.
739 explicit TryCatchInformation(const HTryBoundary& try_entry)
740 : try_entry_(&try_entry),
741 catch_dex_file_(nullptr),
742 catch_type_index_(DexFile::kDexNoIndex16) {
743 DCHECK(try_entry_ != nullptr);
744 }
745
746 // Catch block information constructor.
747 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
748 : try_entry_(nullptr),
749 catch_dex_file_(&dex_file),
750 catch_type_index_(catch_type_index) {}
751
752 bool IsTryBlock() const { return try_entry_ != nullptr; }
753
754 const HTryBoundary& GetTryEntry() const {
755 DCHECK(IsTryBlock());
756 return *try_entry_;
757 }
758
759 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
760
761 bool IsCatchAllTypeIndex() const {
762 DCHECK(IsCatchBlock());
763 return catch_type_index_ == DexFile::kDexNoIndex16;
764 }
765
766 uint16_t GetCatchTypeIndex() const {
767 DCHECK(IsCatchBlock());
768 return catch_type_index_;
769 }
770
771 const DexFile& GetCatchDexFile() const {
772 DCHECK(IsCatchBlock());
773 return *catch_dex_file_;
774 }
775
776 private:
777 // One of possibly several TryBoundary instructions entering the block's try.
778 // Only set for try blocks.
779 const HTryBoundary* try_entry_;
780
781 // Exception type information. Only set for catch blocks.
782 const DexFile* catch_dex_file_;
783 const uint16_t catch_type_index_;
784};
785
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100786static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100787static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100788
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000789// A block in a method. Contains the list of instructions represented
790// as a double linked list. Each block knows its predecessors and
791// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100792
Vladimir Markof9f64412015-09-02 14:05:49 +0100793class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000794 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600795 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000796 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000797 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
798 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000799 loop_information_(nullptr),
800 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000801 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100802 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100803 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100804 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000805 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000806 try_catch_information_(nullptr) {
807 predecessors_.reserve(kDefaultNumberOfPredecessors);
808 successors_.reserve(kDefaultNumberOfSuccessors);
809 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
810 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000811
Vladimir Marko60584552015-09-03 13:35:12 +0000812 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100813 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000814 }
815
Vladimir Marko60584552015-09-03 13:35:12 +0000816 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100817 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000818 }
819
David Brazdild26a4112015-11-10 11:07:31 +0000820 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
821 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
822
Vladimir Marko60584552015-09-03 13:35:12 +0000823 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
824 return ContainsElement(successors_, block, start_from);
825 }
826
827 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100828 return dominated_blocks_;
829 }
830
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100831 bool IsEntryBlock() const {
832 return graph_->GetEntryBlock() == this;
833 }
834
835 bool IsExitBlock() const {
836 return graph_->GetExitBlock() == this;
837 }
838
David Brazdil46e2a392015-03-16 17:31:52 +0000839 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000840 bool IsSingleTryBoundary() const;
841
842 // Returns true if this block emits nothing but a jump.
843 bool IsSingleJump() const {
844 HLoopInformation* loop_info = GetLoopInformation();
845 return (IsSingleGoto() || IsSingleTryBoundary())
846 // Back edges generate a suspend check.
847 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
848 }
David Brazdil46e2a392015-03-16 17:31:52 +0000849
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000850 void AddBackEdge(HBasicBlock* back_edge) {
851 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000852 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000853 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100854 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000855 loop_information_->AddBackEdge(back_edge);
856 }
857
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000858 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000859 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000860
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100861 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000862 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600863 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000864
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000865 HBasicBlock* GetDominator() const { return dominator_; }
866 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000867 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
868
869 void RemoveDominatedBlock(HBasicBlock* block) {
870 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100871 }
Vladimir Marko60584552015-09-03 13:35:12 +0000872
873 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
874 ReplaceElement(dominated_blocks_, existing, new_block);
875 }
876
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100877 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000878
879 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100880 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000881 }
882
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100883 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
884 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100885 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100886 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100887 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
888 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000889
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000890 HInstruction* GetFirstInstructionDisregardMoves() const;
891
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000892 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000893 successors_.push_back(block);
894 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000895 }
896
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100897 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
898 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100899 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000900 new_block->predecessors_.push_back(this);
901 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000902 }
903
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000904 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
905 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000906 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000907 new_block->successors_.push_back(this);
908 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000909 }
910
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100911 // Insert `this` between `predecessor` and `successor. This method
912 // preserves the indicies, and will update the first edge found between
913 // `predecessor` and `successor`.
914 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
915 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100916 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000917 successor->predecessors_[predecessor_index] = this;
918 predecessor->successors_[successor_index] = this;
919 successors_.push_back(successor);
920 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100921 }
922
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100923 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000924 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100925 }
926
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000927 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000928 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000929 }
930
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100931 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000932 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100933 }
934
935 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000936 predecessors_.push_back(block);
937 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100938 }
939
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100940 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000941 DCHECK_EQ(predecessors_.size(), 2u);
942 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100943 }
944
David Brazdil769c9e52015-04-27 13:54:09 +0100945 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000946 DCHECK_EQ(successors_.size(), 2u);
947 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100948 }
949
David Brazdilfc6a86a2015-06-26 10:33:45 +0000950 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000951 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100952 }
953
David Brazdilfc6a86a2015-06-26 10:33:45 +0000954 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000955 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100956 }
957
David Brazdilfc6a86a2015-06-26 10:33:45 +0000958 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000959 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100960 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000961 }
962
963 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000964 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100965 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000966 }
967
968 // Returns whether the first occurrence of `predecessor` in the list of
969 // predecessors is at index `idx`.
970 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100971 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000972 return GetPredecessorIndexOf(predecessor) == idx;
973 }
974
David Brazdild7558da2015-09-22 13:04:14 +0100975 // Create a new block between this block and its predecessors. The new block
976 // is added to the graph, all predecessor edges are relinked to it and an edge
977 // is created to `this`. Returns the new empty block. Reverse post order or
978 // loop and try/catch information are not updated.
979 HBasicBlock* CreateImmediateDominator();
980
David Brazdilfc6a86a2015-06-26 10:33:45 +0000981 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100982 // created, latter block. Note that this method will add the block to the
983 // graph, create a Goto at the end of the former block and will create an edge
984 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100985 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000986 HBasicBlock* SplitBefore(HInstruction* cursor);
987
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000988 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000989 // created block. Note that this method just updates raw block information,
990 // like predecessors, successors, dominators, and instruction list. It does not
991 // update the graph, reverse post order, loop information, nor make sure the
992 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000993 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
994
995 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
996 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000997
David Brazdil9bc43612015-11-05 21:25:24 +0000998 // Split catch block into two blocks after the original move-exception bytecode
999 // instruction, or at the beginning if not present. Returns the newly created,
1000 // latter block, or nullptr if such block could not be created (must be dead
1001 // in that case). Note that this method just updates raw block information,
1002 // like predecessors, successors, dominators, and instruction list. It does not
1003 // update the graph, reverse post order, loop information, nor make sure the
1004 // blocks are consistent (for example ending with a control flow instruction).
1005 HBasicBlock* SplitCatchBlockAfterMoveException();
1006
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001007 // Merge `other` at the end of `this`. Successors and dominated blocks of
1008 // `other` are changed to be successors and dominated blocks of `this`. Note
1009 // that this method does not update the graph, reverse post order, loop
1010 // information, nor make sure the blocks are consistent (for example ending
1011 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001012 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001013
1014 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1015 // of `this` are moved to `other`.
1016 // Note that this method does not update the graph, reverse post order, loop
1017 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001018 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001019 void ReplaceWith(HBasicBlock* other);
1020
David Brazdil2d7352b2015-04-20 14:52:42 +01001021 // Merge `other` at the end of `this`. This method updates loops, reverse post
1022 // order, links to predecessors, successors, dominators and deletes the block
1023 // from the graph. The two blocks must be successive, i.e. `this` the only
1024 // predecessor of `other` and vice versa.
1025 void MergeWith(HBasicBlock* other);
1026
1027 // Disconnects `this` from all its predecessors, successors and dominator,
1028 // removes it from all loops it is included in and eventually from the graph.
1029 // The block must not dominate any other block. Predecessors and successors
1030 // are safely updated.
1031 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001032
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001033 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001034 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001035 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001036 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001037 // Replace instruction `initial` with `replacement` within this block.
1038 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1039 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001040 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001041 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001042 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001043 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1044 // instruction list. With 'ensure_safety' set to true, it verifies that the
1045 // instruction is not in use and removes it from the use lists of its inputs.
1046 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1047 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001048 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001049
1050 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001051 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001052 }
1053
Roland Levillain6b879dd2014-09-22 17:13:44 +01001054 bool IsLoopPreHeaderFirstPredecessor() const {
1055 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001056 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001057 }
1058
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001059 bool IsFirstPredecessorBackEdge() const {
1060 DCHECK(IsLoopHeader());
1061 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1062 }
1063
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001064 HLoopInformation* GetLoopInformation() const {
1065 return loop_information_;
1066 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001067
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001068 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001069 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001070 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001071 void SetInLoop(HLoopInformation* info) {
1072 if (IsLoopHeader()) {
1073 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001074 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001075 loop_information_ = info;
1076 } else if (loop_information_->Contains(*info->GetHeader())) {
1077 // Block is currently part of an outer loop. Make it part of this inner loop.
1078 // Note that a non loop header having a loop information means this loop information
1079 // has already been populated
1080 loop_information_ = info;
1081 } else {
1082 // Block is part of an inner loop. Do not update the loop information.
1083 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1084 // at this point, because this method is being called while populating `info`.
1085 }
1086 }
1087
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001088 // Raw update of the loop information.
1089 void SetLoopInformation(HLoopInformation* info) {
1090 loop_information_ = info;
1091 }
1092
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001093 bool IsInLoop() const { return loop_information_ != nullptr; }
1094
David Brazdilec16f792015-08-19 15:04:01 +01001095 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1096
1097 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1098 try_catch_information_ = try_catch_information;
1099 }
1100
1101 bool IsTryBlock() const {
1102 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1103 }
1104
1105 bool IsCatchBlock() const {
1106 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1107 }
David Brazdilffee3d32015-07-06 11:48:53 +01001108
1109 // Returns the try entry that this block's successors should have. They will
1110 // be in the same try, unless the block ends in a try boundary. In that case,
1111 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001112 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001113
David Brazdild7558da2015-09-22 13:04:14 +01001114 bool HasThrowingInstructions() const;
1115
David Brazdila4b8c212015-05-07 09:59:30 +01001116 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001117 bool Dominates(HBasicBlock* block) const;
1118
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001119 size_t GetLifetimeStart() const { return lifetime_start_; }
1120 size_t GetLifetimeEnd() const { return lifetime_end_; }
1121
1122 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1123 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1124
David Brazdil8d5b8b22015-03-24 10:51:52 +00001125 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001126 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001127 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001128 bool HasSinglePhi() const;
1129
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001130 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001131 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001132 ArenaVector<HBasicBlock*> predecessors_;
1133 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001134 HInstructionList instructions_;
1135 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001136 HLoopInformation* loop_information_;
1137 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001138 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001139 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001140 // The dex program counter of the first instruction of this block.
1141 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001142 size_t lifetime_start_;
1143 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001144 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001145
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001146 friend class HGraph;
1147 friend class HInstruction;
1148
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001149 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1150};
1151
David Brazdilb2bd1c52015-03-25 11:17:37 +00001152// Iterates over the LoopInformation of all loops which contain 'block'
1153// from the innermost to the outermost.
1154class HLoopInformationOutwardIterator : public ValueObject {
1155 public:
1156 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1157 : current_(block.GetLoopInformation()) {}
1158
1159 bool Done() const { return current_ == nullptr; }
1160
1161 void Advance() {
1162 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001163 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001164 }
1165
1166 HLoopInformation* Current() const {
1167 DCHECK(!Done());
1168 return current_;
1169 }
1170
1171 private:
1172 HLoopInformation* current_;
1173
1174 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1175};
1176
Alexandre Ramesef20f712015-06-09 10:29:30 +01001177#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001178 M(Above, Condition) \
1179 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001180 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001181 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001182 M(ArrayGet, Instruction) \
1183 M(ArrayLength, Instruction) \
1184 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001185 M(Below, Condition) \
1186 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001187 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001188 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001189 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001190 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001191 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001192 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001193 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001194 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001195 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001196 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001197 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001198 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001199 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001200 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001201 M(Exit, Instruction) \
1202 M(FloatConstant, Constant) \
1203 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001204 M(GreaterThan, Condition) \
1205 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001206 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001207 M(InstanceFieldGet, Instruction) \
1208 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001209 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001210 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001211 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001212 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001213 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001214 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001215 M(LessThan, Condition) \
1216 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001217 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001218 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001219 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001220 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001221 M(Local, Instruction) \
1222 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001223 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001224 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001225 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001226 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001227 M(Neg, UnaryOperation) \
1228 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001229 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001230 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001231 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001232 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001233 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001234 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001235 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001236 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001237 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001238 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001239 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001240 M(Return, Instruction) \
1241 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001242 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001243 M(Shl, BinaryOperation) \
1244 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001245 M(StaticFieldGet, Instruction) \
1246 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001247 M(UnresolvedInstanceFieldGet, Instruction) \
1248 M(UnresolvedInstanceFieldSet, Instruction) \
1249 M(UnresolvedStaticFieldGet, Instruction) \
1250 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001251 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001252 M(StoreLocal, Instruction) \
1253 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001254 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001255 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001256 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001257 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001258 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001259 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001260
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001261/*
1262 * Instructions, shared across several (not all) architectures.
1263 */
1264#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1265#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1266#else
1267#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001268 M(BitwiseNegatedRight, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001269 M(MultiplyAccumulate, Instruction)
1270#endif
1271
Vladimir Markob4536b72015-11-24 13:45:23 +00001272#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001273#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001274#else
1275#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1276 M(ArmDexCacheArraysBase, Instruction)
1277#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001278
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001279#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001280#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001281#else
1282#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001283 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001284 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001285#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001286
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001287#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1288
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001289#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1290
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001291#ifndef ART_ENABLE_CODEGEN_x86
1292#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1293#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001294#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1295 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001296 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001297 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001298 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001299#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001300
1301#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1302
1303#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1304 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001305 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001306 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1307 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001308 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001309 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001310 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1311 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1312
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001313#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1314 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001315 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001316 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001317 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001318 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001319
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001320#define FOR_EACH_INSTRUCTION(M) \
1321 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1322 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1323
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001324#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001325FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1326#undef FORWARD_DECLARATION
1327
Roland Levillainccc07a92014-09-16 14:48:16 +01001328#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001329 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001330 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001331 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001332 return other->Is##type(); \
1333 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001334 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001335
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001336#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1337 bool Is##type() const { return As##type() != nullptr; } \
1338 const H##type* As##type() const { return this; } \
1339 H##type* As##type() { return this; }
1340
David Brazdiled596192015-01-23 10:39:45 +00001341template <typename T> class HUseList;
1342
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001343template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001344class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001345 public:
David Brazdiled596192015-01-23 10:39:45 +00001346 HUseListNode* GetPrevious() const { return prev_; }
1347 HUseListNode* GetNext() const { return next_; }
1348 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001349 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001350 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001351
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001352 private:
David Brazdiled596192015-01-23 10:39:45 +00001353 HUseListNode(T user, size_t index)
1354 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1355
1356 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001357 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001358 HUseListNode<T>* prev_;
1359 HUseListNode<T>* next_;
1360
1361 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001362
1363 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1364};
1365
David Brazdiled596192015-01-23 10:39:45 +00001366template <typename T>
1367class HUseList : public ValueObject {
1368 public:
1369 HUseList() : first_(nullptr) {}
1370
1371 void Clear() {
1372 first_ = nullptr;
1373 }
1374
1375 // Adds a new entry at the beginning of the use list and returns
1376 // the newly created node.
1377 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001378 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001379 if (IsEmpty()) {
1380 first_ = new_node;
1381 } else {
1382 first_->prev_ = new_node;
1383 new_node->next_ = first_;
1384 first_ = new_node;
1385 }
1386 return new_node;
1387 }
1388
1389 HUseListNode<T>* GetFirst() const {
1390 return first_;
1391 }
1392
1393 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001394 DCHECK(node != nullptr);
1395 DCHECK(Contains(node));
1396
David Brazdiled596192015-01-23 10:39:45 +00001397 if (node->prev_ != nullptr) {
1398 node->prev_->next_ = node->next_;
1399 }
1400 if (node->next_ != nullptr) {
1401 node->next_->prev_ = node->prev_;
1402 }
1403 if (node == first_) {
1404 first_ = node->next_;
1405 }
1406 }
1407
David Brazdil1abb4192015-02-17 18:33:36 +00001408 bool Contains(const HUseListNode<T>* node) const {
1409 if (node == nullptr) {
1410 return false;
1411 }
1412 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1413 if (current == node) {
1414 return true;
1415 }
1416 }
1417 return false;
1418 }
1419
David Brazdiled596192015-01-23 10:39:45 +00001420 bool IsEmpty() const {
1421 return first_ == nullptr;
1422 }
1423
1424 bool HasOnlyOneUse() const {
1425 return first_ != nullptr && first_->next_ == nullptr;
1426 }
1427
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001428 size_t SizeSlow() const {
1429 size_t count = 0;
1430 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1431 ++count;
1432 }
1433 return count;
1434 }
1435
David Brazdiled596192015-01-23 10:39:45 +00001436 private:
1437 HUseListNode<T>* first_;
1438};
1439
1440template<typename T>
1441class HUseIterator : public ValueObject {
1442 public:
1443 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1444
1445 bool Done() const { return current_ == nullptr; }
1446
1447 void Advance() {
1448 DCHECK(!Done());
1449 current_ = current_->GetNext();
1450 }
1451
1452 HUseListNode<T>* Current() const {
1453 DCHECK(!Done());
1454 return current_;
1455 }
1456
1457 private:
1458 HUseListNode<T>* current_;
1459
1460 friend class HValue;
1461};
1462
David Brazdil1abb4192015-02-17 18:33:36 +00001463// This class is used by HEnvironment and HInstruction classes to record the
1464// instructions they use and pointers to the corresponding HUseListNodes kept
1465// by the used instructions.
1466template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001467class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001468 public:
1469 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1470 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1471
1472 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1473 : instruction_(old_record.instruction_), use_node_(use_node) {
1474 DCHECK(instruction_ != nullptr);
1475 DCHECK(use_node_ != nullptr);
1476 DCHECK(old_record.use_node_ == nullptr);
1477 }
1478
1479 HInstruction* GetInstruction() const { return instruction_; }
1480 HUseListNode<T>* GetUseNode() const { return use_node_; }
1481
1482 private:
1483 // Instruction used by the user.
1484 HInstruction* instruction_;
1485
1486 // Corresponding entry in the use list kept by 'instruction_'.
1487 HUseListNode<T>* use_node_;
1488};
1489
Aart Bik854a02b2015-07-14 16:07:00 -07001490/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001491 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001492 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001493 * For write/read dependences on fields/arrays, the dependence analysis uses
1494 * type disambiguation (e.g. a float field write cannot modify the value of an
1495 * integer field read) and the access type (e.g. a reference array write cannot
1496 * modify the value of a reference field read [although it may modify the
1497 * reference fetch prior to reading the field, which is represented by its own
1498 * write/read dependence]). The analysis makes conservative points-to
1499 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1500 * the same, and any reference read depends on any reference read without
1501 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001502 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001503 * The internal representation uses 38-bit and is described in the table below.
1504 * The first line indicates the side effect, and for field/array accesses the
1505 * second line indicates the type of the access (in the order of the
1506 * Primitive::Type enum).
1507 * The two numbered lines below indicate the bit position in the bitfield (read
1508 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001509 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001510 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1511 * +-------------+---------+---------+--------------+---------+---------+
1512 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1513 * | 3 |333333322|222222221| 1 |111111110|000000000|
1514 * | 7 |654321098|765432109| 8 |765432109|876543210|
1515 *
1516 * Note that, to ease the implementation, 'changes' bits are least significant
1517 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001518 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001519class SideEffects : public ValueObject {
1520 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001521 SideEffects() : flags_(0) {}
1522
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001523 static SideEffects None() {
1524 return SideEffects(0);
1525 }
1526
1527 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001528 return SideEffects(kAllChangeBits | kAllDependOnBits);
1529 }
1530
1531 static SideEffects AllChanges() {
1532 return SideEffects(kAllChangeBits);
1533 }
1534
1535 static SideEffects AllDependencies() {
1536 return SideEffects(kAllDependOnBits);
1537 }
1538
1539 static SideEffects AllExceptGCDependency() {
1540 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1541 }
1542
1543 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001544 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001545 }
1546
Aart Bik34c3ba92015-07-20 14:08:59 -07001547 static SideEffects AllWrites() {
1548 return SideEffects(kAllWrites);
1549 }
1550
1551 static SideEffects AllReads() {
1552 return SideEffects(kAllReads);
1553 }
1554
1555 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1556 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001557 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001558 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001559 }
1560
Aart Bik854a02b2015-07-14 16:07:00 -07001561 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1562 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001563 }
1564
Aart Bik34c3ba92015-07-20 14:08:59 -07001565 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1566 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001567 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001568 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001569 }
1570
1571 static SideEffects ArrayReadOfType(Primitive::Type type) {
1572 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1573 }
1574
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001575 static SideEffects CanTriggerGC() {
1576 return SideEffects(1ULL << kCanTriggerGCBit);
1577 }
1578
1579 static SideEffects DependsOnGC() {
1580 return SideEffects(1ULL << kDependsOnGCBit);
1581 }
1582
Aart Bik854a02b2015-07-14 16:07:00 -07001583 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001584 SideEffects Union(SideEffects other) const {
1585 return SideEffects(flags_ | other.flags_);
1586 }
1587
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001588 SideEffects Exclusion(SideEffects other) const {
1589 return SideEffects(flags_ & ~other.flags_);
1590 }
1591
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001592 void Add(SideEffects other) {
1593 flags_ |= other.flags_;
1594 }
1595
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001596 bool Includes(SideEffects other) const {
1597 return (other.flags_ & flags_) == other.flags_;
1598 }
1599
1600 bool HasSideEffects() const {
1601 return (flags_ & kAllChangeBits);
1602 }
1603
1604 bool HasDependencies() const {
1605 return (flags_ & kAllDependOnBits);
1606 }
1607
1608 // Returns true if there are no side effects or dependencies.
1609 bool DoesNothing() const {
1610 return flags_ == 0;
1611 }
1612
Aart Bik854a02b2015-07-14 16:07:00 -07001613 // Returns true if something is written.
1614 bool DoesAnyWrite() const {
1615 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001616 }
1617
Aart Bik854a02b2015-07-14 16:07:00 -07001618 // Returns true if something is read.
1619 bool DoesAnyRead() const {
1620 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001621 }
1622
Aart Bik854a02b2015-07-14 16:07:00 -07001623 // Returns true if potentially everything is written and read
1624 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001625 bool DoesAllReadWrite() const {
1626 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1627 }
1628
Aart Bik854a02b2015-07-14 16:07:00 -07001629 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001630 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001631 }
1632
Roland Levillain0d5a2812015-11-13 10:07:31 +00001633 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001634 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001635 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1636 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001637 }
1638
1639 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001640 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001641 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001642 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001643 for (int s = kLastBit; s >= 0; s--) {
1644 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1645 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1646 // This is a bit for the GC side effect.
1647 if (current_bit_is_set) {
1648 flags += "GC";
1649 }
Aart Bik854a02b2015-07-14 16:07:00 -07001650 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001651 } else {
1652 // This is a bit for the array/field analysis.
1653 // The underscore character stands for the 'can trigger GC' bit.
1654 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1655 if (current_bit_is_set) {
1656 flags += kDebug[s];
1657 }
1658 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1659 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1660 flags += "|";
1661 }
1662 }
Aart Bik854a02b2015-07-14 16:07:00 -07001663 }
1664 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001665 }
1666
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001667 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001668
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001669 private:
1670 static constexpr int kFieldArrayAnalysisBits = 9;
1671
1672 static constexpr int kFieldWriteOffset = 0;
1673 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1674 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1675 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1676
1677 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1678
1679 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1680 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1681 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1682 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1683
1684 static constexpr int kLastBit = kDependsOnGCBit;
1685 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1686
1687 // Aliases.
1688
1689 static_assert(kChangeBits == kDependOnBits,
1690 "the 'change' bits should match the 'depend on' bits.");
1691
1692 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1693 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1694 static constexpr uint64_t kAllWrites =
1695 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1696 static constexpr uint64_t kAllReads =
1697 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001698
Aart Bik854a02b2015-07-14 16:07:00 -07001699 // Work around the fact that HIR aliases I/F and J/D.
1700 // TODO: remove this interceptor once HIR types are clean
1701 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1702 switch (type) {
1703 case Primitive::kPrimInt:
1704 case Primitive::kPrimFloat:
1705 return TypeFlag(Primitive::kPrimInt, offset) |
1706 TypeFlag(Primitive::kPrimFloat, offset);
1707 case Primitive::kPrimLong:
1708 case Primitive::kPrimDouble:
1709 return TypeFlag(Primitive::kPrimLong, offset) |
1710 TypeFlag(Primitive::kPrimDouble, offset);
1711 default:
1712 return TypeFlag(type, offset);
1713 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001714 }
1715
Aart Bik854a02b2015-07-14 16:07:00 -07001716 // Translates type to bit flag.
1717 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1718 CHECK_NE(type, Primitive::kPrimVoid);
1719 const uint64_t one = 1;
1720 const int shift = type; // 0-based consecutive enum
1721 DCHECK_LE(kFieldWriteOffset, shift);
1722 DCHECK_LT(shift, kArrayWriteOffset);
1723 return one << (type + offset);
1724 }
1725
1726 // Private constructor on direct flags value.
1727 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1728
1729 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001730};
1731
David Brazdiled596192015-01-23 10:39:45 +00001732// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001733class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001734 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001735 HEnvironment(ArenaAllocator* arena,
1736 size_t number_of_vregs,
1737 const DexFile& dex_file,
1738 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001739 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001740 InvokeType invoke_type,
1741 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001742 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1743 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001744 parent_(nullptr),
1745 dex_file_(dex_file),
1746 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001747 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001748 invoke_type_(invoke_type),
1749 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001750 }
1751
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001752 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001753 : HEnvironment(arena,
1754 to_copy.Size(),
1755 to_copy.GetDexFile(),
1756 to_copy.GetMethodIdx(),
1757 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001758 to_copy.GetInvokeType(),
1759 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001760
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001761 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001762 if (parent_ != nullptr) {
1763 parent_->SetAndCopyParentChain(allocator, parent);
1764 } else {
1765 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1766 parent_->CopyFrom(parent);
1767 if (parent->GetParent() != nullptr) {
1768 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1769 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001770 }
David Brazdiled596192015-01-23 10:39:45 +00001771 }
1772
Vladimir Marko71bf8092015-09-15 15:33:14 +01001773 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001774 void CopyFrom(HEnvironment* environment);
1775
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001776 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1777 // input to the loop phi instead. This is for inserting instructions that
1778 // require an environment (like HDeoptimization) in the loop pre-header.
1779 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001780
1781 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001782 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001783 }
1784
1785 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001786 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001787 }
1788
David Brazdil1abb4192015-02-17 18:33:36 +00001789 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001790
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001791 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001792
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001793 HEnvironment* GetParent() const { return parent_; }
1794
1795 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001796 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001797 }
1798
1799 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001800 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001801 }
1802
1803 uint32_t GetDexPc() const {
1804 return dex_pc_;
1805 }
1806
1807 uint32_t GetMethodIdx() const {
1808 return method_idx_;
1809 }
1810
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001811 InvokeType GetInvokeType() const {
1812 return invoke_type_;
1813 }
1814
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001815 const DexFile& GetDexFile() const {
1816 return dex_file_;
1817 }
1818
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001819 HInstruction* GetHolder() const {
1820 return holder_;
1821 }
1822
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001823
1824 bool IsFromInlinedInvoke() const {
1825 return GetParent() != nullptr;
1826 }
1827
David Brazdiled596192015-01-23 10:39:45 +00001828 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001829 // Record instructions' use entries of this environment for constant-time removal.
1830 // It should only be called by HInstruction when a new environment use is added.
1831 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1832 DCHECK(env_use->GetUser() == this);
1833 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001834 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001835 }
David Brazdiled596192015-01-23 10:39:45 +00001836
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001837 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1838 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001839 HEnvironment* parent_;
1840 const DexFile& dex_file_;
1841 const uint32_t method_idx_;
1842 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001843 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001844
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001845 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001846 HInstruction* const holder_;
1847
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001848 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001849
1850 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1851};
1852
Vladimir Markof9f64412015-09-02 14:05:49 +01001853class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001854 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001855 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001856 : previous_(nullptr),
1857 next_(nullptr),
1858 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001859 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001860 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001861 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001862 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001863 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001864 locations_(nullptr),
1865 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001866 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001867 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001868 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1869 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1870 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001871
Dave Allison20dfc792014-06-16 20:44:29 -07001872 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001873
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001874#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001875 enum InstructionKind {
1876 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1877 };
1878#undef DECLARE_KIND
1879
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001880 HInstruction* GetNext() const { return next_; }
1881 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001882
Calin Juravle77520bc2015-01-12 18:45:46 +00001883 HInstruction* GetNextDisregardingMoves() const;
1884 HInstruction* GetPreviousDisregardingMoves() const;
1885
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001886 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001887 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001888 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001889 bool IsInBlock() const { return block_ != nullptr; }
1890 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001891 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1892 bool IsIrreducibleLoopHeaderPhi() const {
1893 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1894 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001895
Roland Levillain6b879dd2014-09-22 17:13:44 +01001896 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001897 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001898
1899 virtual void Accept(HGraphVisitor* visitor) = 0;
1900 virtual const char* DebugName() const = 0;
1901
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001902 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001903 void SetRawInputAt(size_t index, HInstruction* input) {
1904 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1905 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001906
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001907 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001908
1909 uint32_t GetDexPc() const { return dex_pc_; }
1910
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001911 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001912
Roland Levillaine161a2a2014-10-03 12:45:18 +01001913 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001914 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001915
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001916 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001917 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001918
Calin Juravle10e244f2015-01-26 18:54:32 +00001919 // Does not apply for all instructions, but having this at top level greatly
1920 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001921 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001922 virtual bool CanBeNull() const {
1923 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1924 return true;
1925 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001926
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001927 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001928 return false;
1929 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001930
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001931 virtual bool IsActualObject() const {
1932 return GetType() == Primitive::kPrimNot;
1933 }
1934
Calin Juravle2e768302015-07-28 14:41:11 +00001935 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001936
Calin Juravle61d544b2015-02-23 16:46:57 +00001937 ReferenceTypeInfo GetReferenceTypeInfo() const {
1938 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001939 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
1940 GetPackedFlag<kFlagReferenceTypeIsExact>());;
Calin Juravle61d544b2015-02-23 16:46:57 +00001941 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001942
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001943 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001944 DCHECK(user != nullptr);
1945 HUseListNode<HInstruction*>* use =
1946 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1947 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001948 }
1949
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001950 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001951 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001952 HUseListNode<HEnvironment*>* env_use =
1953 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1954 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001955 }
1956
David Brazdil1abb4192015-02-17 18:33:36 +00001957 void RemoveAsUserOfInput(size_t input) {
1958 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1959 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1960 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001961
David Brazdil1abb4192015-02-17 18:33:36 +00001962 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1963 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001964
David Brazdiled596192015-01-23 10:39:45 +00001965 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1966 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001967 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001968 bool HasOnlyOneNonEnvironmentUse() const {
1969 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1970 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001971
Roland Levillain6c82d402014-10-13 16:10:27 +01001972 // Does this instruction strictly dominate `other_instruction`?
1973 // Returns false if this instruction and `other_instruction` are the same.
1974 // Aborts if this instruction and `other_instruction` are both phis.
1975 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001976
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001977 int GetId() const { return id_; }
1978 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001979
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001980 int GetSsaIndex() const { return ssa_index_; }
1981 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1982 bool HasSsaIndex() const { return ssa_index_ != -1; }
1983
1984 bool HasEnvironment() const { return environment_ != nullptr; }
1985 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001986 // Set the `environment_` field. Raw because this method does not
1987 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001988 void SetRawEnvironment(HEnvironment* environment) {
1989 DCHECK(environment_ == nullptr);
1990 DCHECK_EQ(environment->GetHolder(), this);
1991 environment_ = environment;
1992 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001993
1994 // Set the environment of this instruction, copying it from `environment`. While
1995 // copying, the uses lists are being updated.
1996 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001997 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001998 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001999 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002000 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002001 if (environment->GetParent() != nullptr) {
2002 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2003 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002004 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002005
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002006 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2007 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002008 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002009 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002010 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002011 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002012 if (environment->GetParent() != nullptr) {
2013 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2014 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002015 }
2016
Nicolas Geoffray39468442014-09-02 15:17:15 +01002017 // Returns the number of entries in the environment. Typically, that is the
2018 // number of dex registers in a method. It could be more in case of inlining.
2019 size_t EnvironmentSize() const;
2020
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002021 LocationSummary* GetLocations() const { return locations_; }
2022 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002023
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002024 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002025 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002026
Alexandre Rames188d4312015-04-09 18:30:21 +01002027 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2028 // uses of this instruction by `other` are *not* updated.
2029 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2030 ReplaceWith(other);
2031 other->ReplaceInput(this, use_index);
2032 }
2033
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002034 // Move `this` instruction before `cursor`.
2035 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002036
Vladimir Markofb337ea2015-11-25 15:25:10 +00002037 // Move `this` before its first user and out of any loops. If there is no
2038 // out-of-loop user that dominates all other users, move the instruction
2039 // to the end of the out-of-loop common dominator of the user's blocks.
2040 //
2041 // This can be used only on non-throwing instructions with no side effects that
2042 // have at least one use but no environment uses.
2043 void MoveBeforeFirstUserAndOutOfLoops();
2044
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002045#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002046 bool Is##type() const; \
2047 const H##type* As##type() const; \
2048 H##type* As##type();
2049
2050 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2051#undef INSTRUCTION_TYPE_CHECK
2052
2053#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002054 bool Is##type() const { return (As##type() != nullptr); } \
2055 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002056 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002057 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002058#undef INSTRUCTION_TYPE_CHECK
2059
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002060 // Returns whether the instruction can be moved within the graph.
2061 virtual bool CanBeMoved() const { return false; }
2062
2063 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002064 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002065 return false;
2066 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002067
2068 // Returns whether any data encoded in the two instructions is equal.
2069 // This method does not look at the inputs. Both instructions must be
2070 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002071 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002072 return false;
2073 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002074
2075 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002076 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002077 // 2) Their inputs are identical.
2078 bool Equals(HInstruction* other) const;
2079
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002080 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2081 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2082 // the virtual function because the __attribute__((__pure__)) doesn't really
2083 // apply the strong requirement for virtual functions, preventing optimizations.
2084 InstructionKind GetKind() const PURE;
2085 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002086
2087 virtual size_t ComputeHashCode() const {
2088 size_t result = GetKind();
2089 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2090 result = (result * 31) + InputAt(i)->GetId();
2091 }
2092 return result;
2093 }
2094
2095 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002096 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002097 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002098
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002099 size_t GetLifetimePosition() const { return lifetime_position_; }
2100 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2101 LiveInterval* GetLiveInterval() const { return live_interval_; }
2102 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2103 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2104
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002105 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2106
2107 // Returns whether the code generation of the instruction will require to have access
2108 // to the current method. Such instructions are:
2109 // (1): Instructions that require an environment, as calling the runtime requires
2110 // to walk the stack and have the current method stored at a specific stack address.
2111 // (2): Object literals like classes and strings, that are loaded from the dex cache
2112 // fields of the current method.
2113 bool NeedsCurrentMethod() const {
2114 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2115 }
2116
Vladimir Markodc151b22015-10-15 18:02:30 +01002117 // Returns whether the code generation of the instruction will require to have access
2118 // to the dex cache of the current method's declaring class via the current method.
2119 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002120
Mark Mendellc4701932015-04-10 13:18:51 -04002121 // Does this instruction have any use in an environment before
2122 // control flow hits 'other'?
2123 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2124
2125 // Remove all references to environment uses of this instruction.
2126 // The caller must ensure that this is safe to do.
2127 void RemoveEnvironmentUsers();
2128
Vladimir Markoa1de9182016-02-25 11:37:38 +00002129 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2130 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002131
David Brazdil1abb4192015-02-17 18:33:36 +00002132 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002133 // If set, the machine code for this instruction is assumed to be generated by
2134 // its users. Used by liveness analysis to compute use positions accordingly.
2135 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2136 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2137 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2138 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2139
David Brazdil1abb4192015-02-17 18:33:36 +00002140 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2141 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2142
Vladimir Markoa1de9182016-02-25 11:37:38 +00002143 uint32_t GetPackedFields() const {
2144 return packed_fields_;
2145 }
2146
2147 template <size_t flag>
2148 bool GetPackedFlag() const {
2149 return (packed_fields_ & (1u << flag)) != 0u;
2150 }
2151
2152 template <size_t flag>
2153 void SetPackedFlag(bool value = true) {
2154 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2155 }
2156
2157 template <typename BitFieldType>
2158 typename BitFieldType::value_type GetPackedField() const {
2159 return BitFieldType::Decode(packed_fields_);
2160 }
2161
2162 template <typename BitFieldType>
2163 void SetPackedField(typename BitFieldType::value_type value) {
2164 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2165 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2166 }
2167
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002168 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002169 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2170
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002171 HInstruction* previous_;
2172 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002173 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002174 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002175
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002176 // An instruction gets an id when it is added to the graph.
2177 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002178 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002179 int id_;
2180
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002181 // When doing liveness analysis, instructions that have uses get an SSA index.
2182 int ssa_index_;
2183
Vladimir Markoa1de9182016-02-25 11:37:38 +00002184 // Packed fields.
2185 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002186
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002187 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002188 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002189
2190 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002191 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002192
Nicolas Geoffray39468442014-09-02 15:17:15 +01002193 // The environment associated with this instruction. Not null if the instruction
2194 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002195 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002196
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002197 // Set by the code generator.
2198 LocationSummary* locations_;
2199
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002200 // Set by the liveness analysis.
2201 LiveInterval* live_interval_;
2202
2203 // Set by the liveness analysis, this is the position in a linear
2204 // order of blocks where this instruction's live interval start.
2205 size_t lifetime_position_;
2206
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002207 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002208
Vladimir Markoa1de9182016-02-25 11:37:38 +00002209 // The reference handle part of the reference type info.
2210 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002211 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002212 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002213
David Brazdil1abb4192015-02-17 18:33:36 +00002214 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002215 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002216 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002217 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002218 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002219
2220 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2221};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002222std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002223
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002224class HInputIterator : public ValueObject {
2225 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002226 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002227
2228 bool Done() const { return index_ == instruction_->InputCount(); }
2229 HInstruction* Current() const { return instruction_->InputAt(index_); }
2230 void Advance() { index_++; }
2231
2232 private:
2233 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002234 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002235
2236 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2237};
2238
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002239class HInstructionIterator : public ValueObject {
2240 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002241 explicit HInstructionIterator(const HInstructionList& instructions)
2242 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002243 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002244 }
2245
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002246 bool Done() const { return instruction_ == nullptr; }
2247 HInstruction* Current() const { return instruction_; }
2248 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002249 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002250 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002251 }
2252
2253 private:
2254 HInstruction* instruction_;
2255 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002256
2257 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002258};
2259
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002260class HBackwardInstructionIterator : public ValueObject {
2261 public:
2262 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2263 : instruction_(instructions.last_instruction_) {
2264 next_ = Done() ? nullptr : instruction_->GetPrevious();
2265 }
2266
2267 bool Done() const { return instruction_ == nullptr; }
2268 HInstruction* Current() const { return instruction_; }
2269 void Advance() {
2270 instruction_ = next_;
2271 next_ = Done() ? nullptr : instruction_->GetPrevious();
2272 }
2273
2274 private:
2275 HInstruction* instruction_;
2276 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002277
2278 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002279};
2280
Vladimir Markof9f64412015-09-02 14:05:49 +01002281template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002282class HTemplateInstruction: public HInstruction {
2283 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002284 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002285 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002286 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002287
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002288 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002289
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002290 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002291 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2292 DCHECK_LT(i, N);
2293 return inputs_[i];
2294 }
David Brazdil1abb4192015-02-17 18:33:36 +00002295
2296 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002297 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002298 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002299 }
2300
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002301 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002302 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002303
2304 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002305};
2306
Vladimir Markof9f64412015-09-02 14:05:49 +01002307// HTemplateInstruction specialization for N=0.
2308template<>
2309class HTemplateInstruction<0>: public HInstruction {
2310 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002311 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002312 : HInstruction(side_effects, dex_pc) {}
2313
Vladimir Markof9f64412015-09-02 14:05:49 +01002314 virtual ~HTemplateInstruction() {}
2315
2316 size_t InputCount() const OVERRIDE { return 0; }
2317
2318 protected:
2319 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2320 LOG(FATAL) << "Unreachable";
2321 UNREACHABLE();
2322 }
2323
2324 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2325 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2326 LOG(FATAL) << "Unreachable";
2327 UNREACHABLE();
2328 }
2329
2330 private:
2331 friend class SsaBuilder;
2332};
2333
Dave Allison20dfc792014-06-16 20:44:29 -07002334template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002335class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002336 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002337 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002338 : HTemplateInstruction<N>(side_effects, dex_pc) {
2339 this->template SetPackedField<TypeField>(type);
2340 }
Dave Allison20dfc792014-06-16 20:44:29 -07002341 virtual ~HExpression() {}
2342
Vladimir Markoa1de9182016-02-25 11:37:38 +00002343 Primitive::Type GetType() const OVERRIDE {
2344 return TypeField::Decode(this->GetPackedFields());
2345 }
Dave Allison20dfc792014-06-16 20:44:29 -07002346
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002347 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002348 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2349 static constexpr size_t kFieldTypeSize =
2350 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2351 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2352 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2353 "Too many packed fields.");
2354 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002355};
2356
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002357// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2358// instruction that branches to the exit block.
2359class HReturnVoid : public HTemplateInstruction<0> {
2360 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002361 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2362 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002363
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002364 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002365
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002366 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002367
2368 private:
2369 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2370};
2371
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002372// Represents dex's RETURN opcodes. A HReturn is a control flow
2373// instruction that branches to the exit block.
2374class HReturn : public HTemplateInstruction<1> {
2375 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002376 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2377 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002378 SetRawInputAt(0, value);
2379 }
2380
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002381 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002382
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002383 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002384
2385 private:
2386 DISALLOW_COPY_AND_ASSIGN(HReturn);
2387};
2388
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002389// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002390// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002391// exit block.
2392class HExit : public HTemplateInstruction<0> {
2393 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002394 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002395
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002396 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002397
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002398 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002399
2400 private:
2401 DISALLOW_COPY_AND_ASSIGN(HExit);
2402};
2403
2404// Jumps from one block to another.
2405class HGoto : public HTemplateInstruction<0> {
2406 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002407 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002408
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002409 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002410
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002411 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002412 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002413 }
2414
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002415 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002416
2417 private:
2418 DISALLOW_COPY_AND_ASSIGN(HGoto);
2419};
2420
Roland Levillain9867bc72015-08-05 10:21:34 +01002421class HConstant : public HExpression<0> {
2422 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002423 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2424 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002425
2426 bool CanBeMoved() const OVERRIDE { return true; }
2427
2428 virtual bool IsMinusOne() const { return false; }
2429 virtual bool IsZero() const { return false; }
2430 virtual bool IsOne() const { return false; }
2431
David Brazdil77a48ae2015-09-15 12:34:04 +00002432 virtual uint64_t GetValueAsUint64() const = 0;
2433
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002434 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002435
2436 private:
2437 DISALLOW_COPY_AND_ASSIGN(HConstant);
2438};
2439
2440class HNullConstant : public HConstant {
2441 public:
2442 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2443 return true;
2444 }
2445
David Brazdil77a48ae2015-09-15 12:34:04 +00002446 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2447
Roland Levillain9867bc72015-08-05 10:21:34 +01002448 size_t ComputeHashCode() const OVERRIDE { return 0; }
2449
2450 DECLARE_INSTRUCTION(NullConstant);
2451
2452 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002453 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002454
2455 friend class HGraph;
2456 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2457};
2458
2459// Constants of the type int. Those can be from Dex instructions, or
2460// synthesized (for example with the if-eqz instruction).
2461class HIntConstant : public HConstant {
2462 public:
2463 int32_t GetValue() const { return value_; }
2464
David Brazdil9f389d42015-10-01 14:32:56 +01002465 uint64_t GetValueAsUint64() const OVERRIDE {
2466 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2467 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002468
Roland Levillain9867bc72015-08-05 10:21:34 +01002469 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002470 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002471 return other->AsIntConstant()->value_ == value_;
2472 }
2473
2474 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2475
2476 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2477 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2478 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2479
2480 DECLARE_INSTRUCTION(IntConstant);
2481
2482 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002483 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2484 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2485 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2486 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002487
2488 const int32_t value_;
2489
2490 friend class HGraph;
2491 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2492 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2493 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2494};
2495
2496class HLongConstant : public HConstant {
2497 public:
2498 int64_t GetValue() const { return value_; }
2499
David Brazdil77a48ae2015-09-15 12:34:04 +00002500 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2501
Roland Levillain9867bc72015-08-05 10:21:34 +01002502 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002503 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002504 return other->AsLongConstant()->value_ == value_;
2505 }
2506
2507 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2508
2509 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2510 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2511 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2512
2513 DECLARE_INSTRUCTION(LongConstant);
2514
2515 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002516 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2517 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002518
2519 const int64_t value_;
2520
2521 friend class HGraph;
2522 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2523};
Dave Allison20dfc792014-06-16 20:44:29 -07002524
Roland Levillain31dd3d62016-02-16 12:21:02 +00002525class HFloatConstant : public HConstant {
2526 public:
2527 float GetValue() const { return value_; }
2528
2529 uint64_t GetValueAsUint64() const OVERRIDE {
2530 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2531 }
2532
2533 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2534 DCHECK(other->IsFloatConstant()) << other->DebugName();
2535 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2536 }
2537
2538 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2539
2540 bool IsMinusOne() const OVERRIDE {
2541 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2542 }
2543 bool IsZero() const OVERRIDE {
2544 return value_ == 0.0f;
2545 }
2546 bool IsOne() const OVERRIDE {
2547 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2548 }
2549 bool IsNaN() const {
2550 return std::isnan(value_);
2551 }
2552
2553 DECLARE_INSTRUCTION(FloatConstant);
2554
2555 private:
2556 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2557 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2558 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2559 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2560
2561 const float value_;
2562
2563 // Only the SsaBuilder and HGraph can create floating-point constants.
2564 friend class SsaBuilder;
2565 friend class HGraph;
2566 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2567};
2568
2569class HDoubleConstant : public HConstant {
2570 public:
2571 double GetValue() const { return value_; }
2572
2573 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2574
2575 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2576 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2577 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2578 }
2579
2580 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2581
2582 bool IsMinusOne() const OVERRIDE {
2583 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2584 }
2585 bool IsZero() const OVERRIDE {
2586 return value_ == 0.0;
2587 }
2588 bool IsOne() const OVERRIDE {
2589 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2590 }
2591 bool IsNaN() const {
2592 return std::isnan(value_);
2593 }
2594
2595 DECLARE_INSTRUCTION(DoubleConstant);
2596
2597 private:
2598 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2599 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2600 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2601 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2602
2603 const double value_;
2604
2605 // Only the SsaBuilder and HGraph can create floating-point constants.
2606 friend class SsaBuilder;
2607 friend class HGraph;
2608 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2609};
2610
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002611// Conditional branch. A block ending with an HIf instruction must have
2612// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002613class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002614 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002615 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2616 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002617 SetRawInputAt(0, input);
2618 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002619
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002620 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002621
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002622 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002623 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002624 }
2625
2626 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002627 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002628 }
2629
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002630 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002631
2632 private:
2633 DISALLOW_COPY_AND_ASSIGN(HIf);
2634};
2635
David Brazdilfc6a86a2015-06-26 10:33:45 +00002636
2637// Abstract instruction which marks the beginning and/or end of a try block and
2638// links it to the respective exception handlers. Behaves the same as a Goto in
2639// non-exceptional control flow.
2640// Normal-flow successor is stored at index zero, exception handlers under
2641// higher indices in no particular order.
2642class HTryBoundary : public HTemplateInstruction<0> {
2643 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002644 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002645 kEntry,
2646 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002647 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002648 };
2649
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002650 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002651 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2652 SetPackedField<BoundaryKindField>(kind);
2653 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002654
2655 bool IsControlFlow() const OVERRIDE { return true; }
2656
2657 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002658 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002659
David Brazdild26a4112015-11-10 11:07:31 +00002660 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2661 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2662 }
2663
David Brazdilfc6a86a2015-06-26 10:33:45 +00002664 // Returns whether `handler` is among its exception handlers (non-zero index
2665 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002666 bool HasExceptionHandler(const HBasicBlock& handler) const {
2667 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002668 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002669 }
2670
2671 // If not present already, adds `handler` to its block's list of exception
2672 // handlers.
2673 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002674 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002675 GetBlock()->AddSuccessor(handler);
2676 }
2677 }
2678
Vladimir Markoa1de9182016-02-25 11:37:38 +00002679 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2680 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002681
David Brazdilffee3d32015-07-06 11:48:53 +01002682 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2683
David Brazdilfc6a86a2015-06-26 10:33:45 +00002684 DECLARE_INSTRUCTION(TryBoundary);
2685
2686 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002687 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2688 static constexpr size_t kFieldBoundaryKindSize =
2689 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2690 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2691 kFieldBoundaryKind + kFieldBoundaryKindSize;
2692 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2693 "Too many packed fields.");
2694 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002695
2696 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2697};
2698
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002699// Deoptimize to interpreter, upon checking a condition.
2700class HDeoptimize : public HTemplateInstruction<1> {
2701 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002702 // We set CanTriggerGC to prevent any intermediate address to be live
2703 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002704 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002705 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002706 SetRawInputAt(0, cond);
2707 }
2708
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002709 bool CanBeMoved() const OVERRIDE { return true; }
2710 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2711 return true;
2712 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002713 bool NeedsEnvironment() const OVERRIDE { return true; }
2714 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002715
2716 DECLARE_INSTRUCTION(Deoptimize);
2717
2718 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002719 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2720};
2721
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002722// Represents the ArtMethod that was passed as a first argument to
2723// the method. It is used by instructions that depend on it, like
2724// instructions that work with the dex cache.
2725class HCurrentMethod : public HExpression<0> {
2726 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002727 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2728 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002729
2730 DECLARE_INSTRUCTION(CurrentMethod);
2731
2732 private:
2733 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2734};
2735
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002736// Fetches an ArtMethod from the virtual table or the interface method table
2737// of a class.
2738class HClassTableGet : public HExpression<1> {
2739 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002740 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002741 kVTable,
2742 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002743 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002744 };
2745 HClassTableGet(HInstruction* cls,
2746 Primitive::Type type,
2747 TableKind kind,
2748 size_t index,
2749 uint32_t dex_pc)
2750 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002751 index_(index) {
2752 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002753 SetRawInputAt(0, cls);
2754 }
2755
2756 bool CanBeMoved() const OVERRIDE { return true; }
2757 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2758 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002759 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002760 }
2761
Vladimir Markoa1de9182016-02-25 11:37:38 +00002762 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002763 size_t GetIndex() const { return index_; }
2764
2765 DECLARE_INSTRUCTION(ClassTableGet);
2766
2767 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002768 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2769 static constexpr size_t kFieldTableKindSize =
2770 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2771 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2772 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2773 "Too many packed fields.");
2774 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2775
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002776 // The index of the ArtMethod in the table.
2777 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002778
2779 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2780};
2781
Mark Mendellfe57faa2015-09-18 09:26:15 -04002782// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2783// have one successor for each entry in the switch table, and the final successor
2784// will be the block containing the next Dex opcode.
2785class HPackedSwitch : public HTemplateInstruction<1> {
2786 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002787 HPackedSwitch(int32_t start_value,
2788 uint32_t num_entries,
2789 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002790 uint32_t dex_pc = kNoDexPc)
2791 : HTemplateInstruction(SideEffects::None(), dex_pc),
2792 start_value_(start_value),
2793 num_entries_(num_entries) {
2794 SetRawInputAt(0, input);
2795 }
2796
2797 bool IsControlFlow() const OVERRIDE { return true; }
2798
2799 int32_t GetStartValue() const { return start_value_; }
2800
Vladimir Marko211c2112015-09-24 16:52:33 +01002801 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002802
2803 HBasicBlock* GetDefaultBlock() const {
2804 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002805 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002806 }
2807 DECLARE_INSTRUCTION(PackedSwitch);
2808
2809 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002810 const int32_t start_value_;
2811 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002812
2813 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2814};
2815
Roland Levillain88cb1752014-10-20 16:36:47 +01002816class HUnaryOperation : public HExpression<1> {
2817 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002818 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2819 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002820 SetRawInputAt(0, input);
2821 }
2822
2823 HInstruction* GetInput() const { return InputAt(0); }
2824 Primitive::Type GetResultType() const { return GetType(); }
2825
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002826 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002827 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002828 return true;
2829 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002830
Roland Levillain31dd3d62016-02-16 12:21:02 +00002831 // Try to statically evaluate `this` and return a HConstant
2832 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002833 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002834 HConstant* TryStaticEvaluation() const;
2835
2836 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002837 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2838 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002839 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2840 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002841
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002842 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002843
2844 private:
2845 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2846};
2847
Dave Allison20dfc792014-06-16 20:44:29 -07002848class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002849 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002850 HBinaryOperation(Primitive::Type result_type,
2851 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002852 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002853 SideEffects side_effects = SideEffects::None(),
2854 uint32_t dex_pc = kNoDexPc)
2855 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002856 SetRawInputAt(0, left);
2857 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002858 }
2859
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002860 HInstruction* GetLeft() const { return InputAt(0); }
2861 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002862 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002863
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002864 virtual bool IsCommutative() const { return false; }
2865
2866 // Put constant on the right.
2867 // Returns whether order is changed.
2868 bool OrderInputsWithConstantOnTheRight() {
2869 HInstruction* left = InputAt(0);
2870 HInstruction* right = InputAt(1);
2871 if (left->IsConstant() && !right->IsConstant()) {
2872 ReplaceInput(right, 0);
2873 ReplaceInput(left, 1);
2874 return true;
2875 }
2876 return false;
2877 }
2878
2879 // Order inputs by instruction id, but favor constant on the right side.
2880 // This helps GVN for commutative ops.
2881 void OrderInputs() {
2882 DCHECK(IsCommutative());
2883 HInstruction* left = InputAt(0);
2884 HInstruction* right = InputAt(1);
2885 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2886 return;
2887 }
2888 if (OrderInputsWithConstantOnTheRight()) {
2889 return;
2890 }
2891 // Order according to instruction id.
2892 if (left->GetId() > right->GetId()) {
2893 ReplaceInput(right, 0);
2894 ReplaceInput(left, 1);
2895 }
2896 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002897
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002898 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002899 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002900 return true;
2901 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002902
Roland Levillain31dd3d62016-02-16 12:21:02 +00002903 // Try to statically evaluate `this` and return a HConstant
2904 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002905 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002906 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002907
2908 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002909 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2910 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002911 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
2912 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00002913 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002914 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2915 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01002916 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2917 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00002918 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
2919 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01002920 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002921 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2922 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002923
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002924 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002925 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002926 HConstant* GetConstantRight() const;
2927
2928 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002929 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002930 HInstruction* GetLeastConstantLeft() const;
2931
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002932 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002933
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002934 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002935 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2936};
2937
Mark Mendellc4701932015-04-10 13:18:51 -04002938// The comparison bias applies for floating point operations and indicates how NaN
2939// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002940enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002941 kNoBias, // bias is not applicable (i.e. for long operation)
2942 kGtBias, // return 1 for NaN comparisons
2943 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00002944 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04002945};
2946
Roland Levillain31dd3d62016-02-16 12:21:02 +00002947std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
2948
Dave Allison20dfc792014-06-16 20:44:29 -07002949class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002950 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002951 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002952 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
2953 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
2954 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002955
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002956 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002957 // `instruction`, and disregard moves in between.
2958 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002959
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002960 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002961
2962 virtual IfCondition GetCondition() const = 0;
2963
Mark Mendellc4701932015-04-10 13:18:51 -04002964 virtual IfCondition GetOppositeCondition() const = 0;
2965
Vladimir Markoa1de9182016-02-25 11:37:38 +00002966 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
2967 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
2968 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04002969
2970 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00002971 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04002972 }
2973
Roland Levillain4fa13f62015-07-06 18:11:54 +01002974 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002975 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002976 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002977 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002978 }
2979
2980 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002981 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002982 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002983 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002984 }
2985
Roland Levillain31dd3d62016-02-16 12:21:02 +00002986 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002987 // Needed if we merge a HCompare into a HCondition.
2988 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
2989 static constexpr size_t kFieldComparisonBiasSize =
2990 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
2991 static constexpr size_t kNumberOfConditionPackedBits =
2992 kFieldComparisonBias + kFieldComparisonBiasSize;
2993 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2994 using ComparisonBiasField =
2995 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
2996
Roland Levillain31dd3d62016-02-16 12:21:02 +00002997 template <typename T>
2998 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
2999
3000 template <typename T>
3001 int32_t CompareFP(T x, T y) const {
3002 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3003 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3004 // Handle the bias.
3005 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3006 }
3007
3008 // Return an integer constant containing the result of a condition evaluated at compile time.
3009 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3010 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3011 }
3012
Dave Allison20dfc792014-06-16 20:44:29 -07003013 private:
3014 DISALLOW_COPY_AND_ASSIGN(HCondition);
3015};
3016
3017// Instruction to check if two inputs are equal to each other.
3018class HEqual : public HCondition {
3019 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003020 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3021 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003022
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003023 bool IsCommutative() const OVERRIDE { return true; }
3024
Vladimir Marko9e23df52015-11-10 17:14:35 +00003025 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3026 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003027 return MakeConstantCondition(true, GetDexPc());
3028 }
3029 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3030 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3031 }
3032 // In the following Evaluate methods, a HCompare instruction has
3033 // been merged into this HEqual instruction; evaluate it as
3034 // `Compare(x, y) == 0`.
3035 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3036 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3037 GetDexPc());
3038 }
3039 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3040 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3041 }
3042 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3043 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003044 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003045
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003046 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003047
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003048 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003049 return kCondEQ;
3050 }
3051
Mark Mendellc4701932015-04-10 13:18:51 -04003052 IfCondition GetOppositeCondition() const OVERRIDE {
3053 return kCondNE;
3054 }
3055
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003056 private:
Aart Bike9f37602015-10-09 11:15:55 -07003057 template <typename T> bool Compute(T x, T y) const { return x == y; }
3058
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003059 DISALLOW_COPY_AND_ASSIGN(HEqual);
3060};
3061
Dave Allison20dfc792014-06-16 20:44:29 -07003062class HNotEqual : public HCondition {
3063 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003064 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3065 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003066
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003067 bool IsCommutative() const OVERRIDE { return true; }
3068
Vladimir Marko9e23df52015-11-10 17:14:35 +00003069 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3070 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003071 return MakeConstantCondition(false, GetDexPc());
3072 }
3073 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3074 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3075 }
3076 // In the following Evaluate methods, a HCompare instruction has
3077 // been merged into this HNotEqual instruction; evaluate it as
3078 // `Compare(x, y) != 0`.
3079 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3080 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3081 }
3082 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3083 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3084 }
3085 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3086 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003087 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003088
Dave Allison20dfc792014-06-16 20:44:29 -07003089 DECLARE_INSTRUCTION(NotEqual);
3090
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003091 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003092 return kCondNE;
3093 }
3094
Mark Mendellc4701932015-04-10 13:18:51 -04003095 IfCondition GetOppositeCondition() const OVERRIDE {
3096 return kCondEQ;
3097 }
3098
Dave Allison20dfc792014-06-16 20:44:29 -07003099 private:
Aart Bike9f37602015-10-09 11:15:55 -07003100 template <typename T> bool Compute(T x, T y) const { return x != y; }
3101
Dave Allison20dfc792014-06-16 20:44:29 -07003102 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3103};
3104
3105class HLessThan : public HCondition {
3106 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003107 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3108 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003109
Roland Levillain9867bc72015-08-05 10:21:34 +01003110 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003111 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003112 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003113 // In the following Evaluate methods, a HCompare instruction has
3114 // been merged into this HLessThan instruction; evaluate it as
3115 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003116 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003117 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3118 }
3119 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3120 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3121 }
3122 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3123 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003124 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003125
Dave Allison20dfc792014-06-16 20:44:29 -07003126 DECLARE_INSTRUCTION(LessThan);
3127
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003128 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003129 return kCondLT;
3130 }
3131
Mark Mendellc4701932015-04-10 13:18:51 -04003132 IfCondition GetOppositeCondition() const OVERRIDE {
3133 return kCondGE;
3134 }
3135
Dave Allison20dfc792014-06-16 20:44:29 -07003136 private:
Aart Bike9f37602015-10-09 11:15:55 -07003137 template <typename T> bool Compute(T x, T y) const { return x < y; }
3138
Dave Allison20dfc792014-06-16 20:44:29 -07003139 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3140};
3141
3142class HLessThanOrEqual : public HCondition {
3143 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003144 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3145 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003146
Roland Levillain9867bc72015-08-05 10:21:34 +01003147 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003148 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003149 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003150 // In the following Evaluate methods, a HCompare instruction has
3151 // been merged into this HLessThanOrEqual instruction; evaluate it as
3152 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003153 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003154 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3155 }
3156 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3157 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3158 }
3159 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3160 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003161 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003162
Dave Allison20dfc792014-06-16 20:44:29 -07003163 DECLARE_INSTRUCTION(LessThanOrEqual);
3164
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003165 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003166 return kCondLE;
3167 }
3168
Mark Mendellc4701932015-04-10 13:18:51 -04003169 IfCondition GetOppositeCondition() const OVERRIDE {
3170 return kCondGT;
3171 }
3172
Dave Allison20dfc792014-06-16 20:44:29 -07003173 private:
Aart Bike9f37602015-10-09 11:15:55 -07003174 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3175
Dave Allison20dfc792014-06-16 20:44:29 -07003176 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3177};
3178
3179class HGreaterThan : public HCondition {
3180 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003181 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3182 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003183
Roland Levillain9867bc72015-08-05 10:21:34 +01003184 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003185 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003186 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003187 // In the following Evaluate methods, a HCompare instruction has
3188 // been merged into this HGreaterThan instruction; evaluate it as
3189 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003190 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003191 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3192 }
3193 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3194 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3195 }
3196 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3197 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003198 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003199
Dave Allison20dfc792014-06-16 20:44:29 -07003200 DECLARE_INSTRUCTION(GreaterThan);
3201
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003202 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003203 return kCondGT;
3204 }
3205
Mark Mendellc4701932015-04-10 13:18:51 -04003206 IfCondition GetOppositeCondition() const OVERRIDE {
3207 return kCondLE;
3208 }
3209
Dave Allison20dfc792014-06-16 20:44:29 -07003210 private:
Aart Bike9f37602015-10-09 11:15:55 -07003211 template <typename T> bool Compute(T x, T y) const { return x > y; }
3212
Dave Allison20dfc792014-06-16 20:44:29 -07003213 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3214};
3215
3216class HGreaterThanOrEqual : public HCondition {
3217 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003218 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3219 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003220
Roland Levillain9867bc72015-08-05 10:21:34 +01003221 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003222 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003223 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003224 // In the following Evaluate methods, a HCompare instruction has
3225 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3226 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003227 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003228 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3229 }
3230 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3231 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3232 }
3233 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3234 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003235 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003236
Dave Allison20dfc792014-06-16 20:44:29 -07003237 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3238
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003239 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003240 return kCondGE;
3241 }
3242
Mark Mendellc4701932015-04-10 13:18:51 -04003243 IfCondition GetOppositeCondition() const OVERRIDE {
3244 return kCondLT;
3245 }
3246
Dave Allison20dfc792014-06-16 20:44:29 -07003247 private:
Aart Bike9f37602015-10-09 11:15:55 -07003248 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3249
Dave Allison20dfc792014-06-16 20:44:29 -07003250 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3251};
3252
Aart Bike9f37602015-10-09 11:15:55 -07003253class HBelow : public HCondition {
3254 public:
3255 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3256 : HCondition(first, second, dex_pc) {}
3257
3258 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003259 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003260 }
3261 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003262 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3263 }
3264 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3265 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3266 LOG(FATAL) << DebugName() << " is not defined for float values";
3267 UNREACHABLE();
3268 }
3269 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3270 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3271 LOG(FATAL) << DebugName() << " is not defined for double values";
3272 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003273 }
3274
3275 DECLARE_INSTRUCTION(Below);
3276
3277 IfCondition GetCondition() const OVERRIDE {
3278 return kCondB;
3279 }
3280
3281 IfCondition GetOppositeCondition() const OVERRIDE {
3282 return kCondAE;
3283 }
3284
3285 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003286 template <typename T> bool Compute(T x, T y) const {
3287 return MakeUnsigned(x) < MakeUnsigned(y);
3288 }
Aart Bike9f37602015-10-09 11:15:55 -07003289
3290 DISALLOW_COPY_AND_ASSIGN(HBelow);
3291};
3292
3293class HBelowOrEqual : public HCondition {
3294 public:
3295 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3296 : HCondition(first, second, dex_pc) {}
3297
3298 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003299 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003300 }
3301 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003302 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3303 }
3304 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3305 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3306 LOG(FATAL) << DebugName() << " is not defined for float values";
3307 UNREACHABLE();
3308 }
3309 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3310 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3311 LOG(FATAL) << DebugName() << " is not defined for double values";
3312 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003313 }
3314
3315 DECLARE_INSTRUCTION(BelowOrEqual);
3316
3317 IfCondition GetCondition() const OVERRIDE {
3318 return kCondBE;
3319 }
3320
3321 IfCondition GetOppositeCondition() const OVERRIDE {
3322 return kCondA;
3323 }
3324
3325 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003326 template <typename T> bool Compute(T x, T y) const {
3327 return MakeUnsigned(x) <= MakeUnsigned(y);
3328 }
Aart Bike9f37602015-10-09 11:15:55 -07003329
3330 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3331};
3332
3333class HAbove : public HCondition {
3334 public:
3335 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3336 : HCondition(first, second, dex_pc) {}
3337
3338 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003339 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003340 }
3341 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003342 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3343 }
3344 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3345 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3346 LOG(FATAL) << DebugName() << " is not defined for float values";
3347 UNREACHABLE();
3348 }
3349 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3350 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3351 LOG(FATAL) << DebugName() << " is not defined for double values";
3352 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003353 }
3354
3355 DECLARE_INSTRUCTION(Above);
3356
3357 IfCondition GetCondition() const OVERRIDE {
3358 return kCondA;
3359 }
3360
3361 IfCondition GetOppositeCondition() const OVERRIDE {
3362 return kCondBE;
3363 }
3364
3365 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003366 template <typename T> bool Compute(T x, T y) const {
3367 return MakeUnsigned(x) > MakeUnsigned(y);
3368 }
Aart Bike9f37602015-10-09 11:15:55 -07003369
3370 DISALLOW_COPY_AND_ASSIGN(HAbove);
3371};
3372
3373class HAboveOrEqual : public HCondition {
3374 public:
3375 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3376 : HCondition(first, second, dex_pc) {}
3377
3378 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003379 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003380 }
3381 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003382 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3383 }
3384 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3385 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3386 LOG(FATAL) << DebugName() << " is not defined for float values";
3387 UNREACHABLE();
3388 }
3389 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3390 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3391 LOG(FATAL) << DebugName() << " is not defined for double values";
3392 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003393 }
3394
3395 DECLARE_INSTRUCTION(AboveOrEqual);
3396
3397 IfCondition GetCondition() const OVERRIDE {
3398 return kCondAE;
3399 }
3400
3401 IfCondition GetOppositeCondition() const OVERRIDE {
3402 return kCondB;
3403 }
3404
3405 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003406 template <typename T> bool Compute(T x, T y) const {
3407 return MakeUnsigned(x) >= MakeUnsigned(y);
3408 }
Aart Bike9f37602015-10-09 11:15:55 -07003409
3410 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3411};
Dave Allison20dfc792014-06-16 20:44:29 -07003412
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003413// Instruction to check how two inputs compare to each other.
3414// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3415class HCompare : public HBinaryOperation {
3416 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003417 HCompare(Primitive::Type type,
3418 HInstruction* first,
3419 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003420 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003421 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003422 : HBinaryOperation(Primitive::kPrimInt,
3423 first,
3424 second,
3425 SideEffectsForArchRuntimeCalls(type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003426 dex_pc) {
3427 SetPackedField<ComparisonBiasField>(bias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003428 DCHECK_EQ(type, first->GetType());
3429 DCHECK_EQ(type, second->GetType());
3430 }
3431
Roland Levillain9867bc72015-08-05 10:21:34 +01003432 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003433 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3434
3435 template <typename T>
3436 int32_t ComputeFP(T x, T y) const {
3437 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3438 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3439 // Handle the bias.
3440 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3441 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003442
Roland Levillain9867bc72015-08-05 10:21:34 +01003443 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003444 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3445 // reach this code path when processing a freshly built HIR
3446 // graph. However HCompare integer instructions can be synthesized
3447 // by the instruction simplifier to implement IntegerCompare and
3448 // IntegerSignum intrinsics, so we have to handle this case.
3449 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003450 }
3451 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003452 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3453 }
3454 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3455 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3456 }
3457 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3458 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003459 }
3460
Calin Juravleddb7df22014-11-25 20:56:51 +00003461 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003462 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003463 }
3464
Vladimir Markoa1de9182016-02-25 11:37:38 +00003465 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003466
Roland Levillain31dd3d62016-02-16 12:21:02 +00003467 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003468 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003469 bool IsGtBias() const {
3470 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003471 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003472 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003473
3474 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3475 // MIPS64 uses a runtime call for FP comparisons.
3476 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3477 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003478
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003479 DECLARE_INSTRUCTION(Compare);
3480
Roland Levillain31dd3d62016-02-16 12:21:02 +00003481 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003482 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3483 static constexpr size_t kFieldComparisonBiasSize =
3484 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3485 static constexpr size_t kNumberOfComparePackedBits =
3486 kFieldComparisonBias + kFieldComparisonBiasSize;
3487 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3488 using ComparisonBiasField =
3489 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3490
Roland Levillain31dd3d62016-02-16 12:21:02 +00003491 // Return an integer constant containing the result of a comparison evaluated at compile time.
3492 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3493 DCHECK(value == -1 || value == 0 || value == 1) << value;
3494 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3495 }
3496
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003497 private:
3498 DISALLOW_COPY_AND_ASSIGN(HCompare);
3499};
3500
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003501// A local in the graph. Corresponds to a Dex register.
3502class HLocal : public HTemplateInstruction<0> {
3503 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003504 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003505 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003506
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003507 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003508
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003509 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003510
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003511 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003512 // The Dex register number.
3513 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003514
3515 DISALLOW_COPY_AND_ASSIGN(HLocal);
3516};
3517
3518// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003519class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003520 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003521 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3522 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003523 SetRawInputAt(0, local);
3524 }
3525
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003526 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3527
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003528 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003529
3530 private:
3531 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3532};
3533
3534// Store a value in a given local. This instruction has two inputs: the value
3535// and the local.
3536class HStoreLocal : public HTemplateInstruction<2> {
3537 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003538 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3539 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003540 SetRawInputAt(0, local);
3541 SetRawInputAt(1, value);
3542 }
3543
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003544 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3545
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003546 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003547
3548 private:
3549 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3550};
3551
David Brazdil6de19382016-01-08 17:37:10 +00003552class HNewInstance : public HExpression<2> {
3553 public:
3554 HNewInstance(HInstruction* cls,
3555 HCurrentMethod* current_method,
3556 uint32_t dex_pc,
3557 uint16_t type_index,
3558 const DexFile& dex_file,
3559 bool can_throw,
3560 bool finalizable,
3561 QuickEntrypointEnum entrypoint)
3562 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3563 type_index_(type_index),
3564 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003565 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003566 SetPackedFlag<kFlagCanThrow>(can_throw);
3567 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003568 SetRawInputAt(0, cls);
3569 SetRawInputAt(1, current_method);
3570 }
3571
3572 uint16_t GetTypeIndex() const { return type_index_; }
3573 const DexFile& GetDexFile() const { return dex_file_; }
3574
3575 // Calls runtime so needs an environment.
3576 bool NeedsEnvironment() const OVERRIDE { return true; }
3577
3578 // It may throw when called on type that's not instantiable/accessible.
3579 // It can throw OOME.
3580 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003581 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>() || true; }
David Brazdil6de19382016-01-08 17:37:10 +00003582
Vladimir Markoa1de9182016-02-25 11:37:38 +00003583 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003584
3585 bool CanBeNull() const OVERRIDE { return false; }
3586
3587 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3588
3589 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3590 entrypoint_ = entrypoint;
3591 }
3592
3593 bool IsStringAlloc() const;
3594
3595 DECLARE_INSTRUCTION(NewInstance);
3596
3597 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003598 static constexpr size_t kFlagCanThrow = kNumberOfExpressionPackedBits;
3599 static constexpr size_t kFlagFinalizable = kFlagCanThrow + 1;
3600 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3601 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3602 "Too many packed fields.");
3603
David Brazdil6de19382016-01-08 17:37:10 +00003604 const uint16_t type_index_;
3605 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003606 QuickEntrypointEnum entrypoint_;
3607
3608 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3609};
3610
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003611enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003612#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3613 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003614#include "intrinsics_list.h"
3615 kNone,
3616 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3617#undef INTRINSICS_LIST
3618#undef OPTIMIZING_INTRINSICS
3619};
3620std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3621
Agi Csaki05f20562015-08-19 14:58:14 -07003622enum IntrinsicNeedsEnvironmentOrCache {
3623 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3624 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003625};
3626
Aart Bik5d75afe2015-12-14 11:57:01 -08003627enum IntrinsicSideEffects {
3628 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3629 kReadSideEffects, // Intrinsic may read heap memory.
3630 kWriteSideEffects, // Intrinsic may write heap memory.
3631 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3632};
3633
3634enum IntrinsicExceptions {
3635 kNoThrow, // Intrinsic does not throw any exceptions.
3636 kCanThrow // Intrinsic may throw exceptions.
3637};
3638
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003639class HInvoke : public HInstruction {
3640 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003641 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003642
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003643 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003644
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003645 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003646 SetRawInputAt(index, argument);
3647 }
3648
Roland Levillain3e3d7332015-04-28 11:00:54 +01003649 // Return the number of arguments. This number can be lower than
3650 // the number of inputs returned by InputCount(), as some invoke
3651 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3652 // inputs at the end of their list of inputs.
3653 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3654
Vladimir Markoa1de9182016-02-25 11:37:38 +00003655 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003656
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003657 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003658 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003659
Vladimir Markoa1de9182016-02-25 11:37:38 +00003660 InvokeType GetOriginalInvokeType() const {
3661 return GetPackedField<OriginalInvokeTypeField>();
3662 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003663
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003664 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003665 return intrinsic_;
3666 }
3667
Aart Bik5d75afe2015-12-14 11:57:01 -08003668 void SetIntrinsic(Intrinsics intrinsic,
3669 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3670 IntrinsicSideEffects side_effects,
3671 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003672
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003673 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003674 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003675 }
3676
Vladimir Markoa1de9182016-02-25 11:37:38 +00003677 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003678
3679 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3680
3681 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3682 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3683 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003684
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003685 uint32_t* GetIntrinsicOptimizations() {
3686 return &intrinsic_optimizations_;
3687 }
3688
3689 const uint32_t* GetIntrinsicOptimizations() const {
3690 return &intrinsic_optimizations_;
3691 }
3692
3693 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3694
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003695 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003696
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003697 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003698 static constexpr size_t kFieldOriginalInvokeType = kNumberOfGenericPackedBits;
3699 static constexpr size_t kFieldOriginalInvokeTypeSize =
3700 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3701 static constexpr size_t kFieldReturnType =
3702 kFieldOriginalInvokeType + kFieldOriginalInvokeTypeSize;
3703 static constexpr size_t kFieldReturnTypeSize =
3704 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3705 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3706 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3707 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3708 using OriginalInvokeTypeField =
3709 BitField<InvokeType, kFieldOriginalInvokeType, kFieldOriginalInvokeTypeSize>;
3710 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3711
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003712 HInvoke(ArenaAllocator* arena,
3713 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003714 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003715 Primitive::Type return_type,
3716 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003717 uint32_t dex_method_index,
3718 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003719 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003720 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003721 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003722 inputs_(number_of_arguments + number_of_other_inputs,
3723 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003724 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003725 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003726 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003727 SetPackedField<ReturnTypeField>(return_type);
3728 SetPackedField<OriginalInvokeTypeField>(original_invoke_type);
3729 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003730 }
3731
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003732 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003733 return inputs_[index];
3734 }
3735
David Brazdil1abb4192015-02-17 18:33:36 +00003736 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003737 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003738 }
3739
Vladimir Markoa1de9182016-02-25 11:37:38 +00003740 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003741
Roland Levillain3e3d7332015-04-28 11:00:54 +01003742 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003743 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003744 const uint32_t dex_method_index_;
3745 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003746
3747 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3748 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003749
3750 private:
3751 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3752};
3753
Calin Juravle175dc732015-08-25 15:42:32 +01003754class HInvokeUnresolved : public HInvoke {
3755 public:
3756 HInvokeUnresolved(ArenaAllocator* arena,
3757 uint32_t number_of_arguments,
3758 Primitive::Type return_type,
3759 uint32_t dex_pc,
3760 uint32_t dex_method_index,
3761 InvokeType invoke_type)
3762 : HInvoke(arena,
3763 number_of_arguments,
3764 0u /* number_of_other_inputs */,
3765 return_type,
3766 dex_pc,
3767 dex_method_index,
3768 invoke_type) {
3769 }
3770
3771 DECLARE_INSTRUCTION(InvokeUnresolved);
3772
3773 private:
3774 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3775};
3776
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003777class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003778 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003779 // Requirements of this method call regarding the class
3780 // initialization (clinit) check of its declaring class.
3781 enum class ClinitCheckRequirement {
3782 kNone, // Class already initialized.
3783 kExplicit, // Static call having explicit clinit check as last input.
3784 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003785 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003786 };
3787
Vladimir Marko58155012015-08-19 12:49:41 +00003788 // Determines how to load the target ArtMethod*.
3789 enum class MethodLoadKind {
3790 // Use a String init ArtMethod* loaded from Thread entrypoints.
3791 kStringInit,
3792
3793 // Use the method's own ArtMethod* loaded by the register allocator.
3794 kRecursive,
3795
3796 // Use ArtMethod* at a known address, embed the direct address in the code.
3797 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3798 kDirectAddress,
3799
3800 // Use ArtMethod* at an address that will be known at link time, embed the direct
3801 // address in the code. If the image is relocatable, emit .patch_oat entry.
3802 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3803 // the image relocatable or not.
3804 kDirectAddressWithFixup,
3805
Vladimir Markoa1de9182016-02-25 11:37:38 +00003806 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003807 // Used when we need to use the dex cache, for example for invoke-static that
3808 // may cause class initialization (the entry may point to a resolution method),
3809 // and we know that we can access the dex cache arrays using a PC-relative load.
3810 kDexCachePcRelative,
3811
3812 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3813 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3814 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3815 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3816 kDexCacheViaMethod,
3817 };
3818
3819 // Determines the location of the code pointer.
3820 enum class CodePtrLocation {
3821 // Recursive call, use local PC-relative call instruction.
3822 kCallSelf,
3823
3824 // Use PC-relative call instruction patched at link time.
3825 // Used for calls within an oat file, boot->boot or app->app.
3826 kCallPCRelative,
3827
3828 // Call to a known target address, embed the direct address in code.
3829 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3830 kCallDirect,
3831
3832 // Call to a target address that will be known at link time, embed the direct
3833 // address in code. If the image is relocatable, emit .patch_oat entry.
3834 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3835 // the image relocatable or not.
3836 kCallDirectWithFixup,
3837
3838 // Use code pointer from the ArtMethod*.
3839 // Used when we don't know the target code. This is also the last-resort-kind used when
3840 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3841 kCallArtMethod,
3842 };
3843
3844 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003845 MethodLoadKind method_load_kind;
3846 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003847 // The method load data holds
3848 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3849 // Note that there are multiple string init methods, each having its own offset.
3850 // - the method address for kDirectAddress
3851 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003852 uint64_t method_load_data;
3853 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003854 };
3855
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003856 HInvokeStaticOrDirect(ArenaAllocator* arena,
3857 uint32_t number_of_arguments,
3858 Primitive::Type return_type,
3859 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003860 uint32_t method_index,
3861 MethodReference target_method,
3862 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003863 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003864 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003865 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003866 : HInvoke(arena,
3867 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003868 // There is potentially one extra argument for the HCurrentMethod node, and
3869 // potentially one other if the clinit check is explicit, and potentially
3870 // one other if the method is a string factory.
3871 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3872 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3873 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003874 return_type,
3875 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003876 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003877 original_invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00003878 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003879 dispatch_info_(dispatch_info) {
3880 SetPackedField<OptimizedInvokeTypeField>(optimized_invoke_type);
3881 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
3882 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003883
Vladimir Markodc151b22015-10-15 18:02:30 +01003884 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003885 bool had_current_method_input = HasCurrentMethodInput();
3886 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3887
3888 // Using the current method is the default and once we find a better
3889 // method load kind, we should not go back to using the current method.
3890 DCHECK(had_current_method_input || !needs_current_method_input);
3891
3892 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003893 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3894 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003895 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003896 dispatch_info_ = dispatch_info;
3897 }
3898
Vladimir Markoc53c0792015-11-19 15:48:33 +00003899 void AddSpecialInput(HInstruction* input) {
3900 // We allow only one special input.
3901 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3902 DCHECK(InputCount() == GetSpecialInputIndex() ||
3903 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3904 InsertInputAt(GetSpecialInputIndex(), input);
3905 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003906
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003907 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003908 // We access the method via the dex cache so we can't do an implicit null check.
3909 // TODO: for intrinsics we can generate implicit null checks.
3910 return false;
3911 }
3912
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003913 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003914 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003915 }
3916
Vladimir Markoc53c0792015-11-19 15:48:33 +00003917 // Get the index of the special input, if any.
3918 //
David Brazdil6de19382016-01-08 17:37:10 +00003919 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3920 // method pointer; otherwise there may be one platform-specific special input,
3921 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003922 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003923 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003924
Vladimir Markoa1de9182016-02-25 11:37:38 +00003925 InvokeType GetOptimizedInvokeType() const {
3926 return GetPackedField<OptimizedInvokeTypeField>();
3927 }
3928
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003929 void SetOptimizedInvokeType(InvokeType invoke_type) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003930 SetPackedField<OptimizedInvokeTypeField>(invoke_type);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003931 }
3932
Vladimir Marko58155012015-08-19 12:49:41 +00003933 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3934 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3935 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003936 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003937 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003938 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003939 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003940 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3941 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003942 bool HasCurrentMethodInput() const {
3943 // This function can be called only after the invoke has been fully initialized by the builder.
3944 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003945 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003946 return true;
3947 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003948 DCHECK(InputCount() == GetSpecialInputIndex() ||
3949 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003950 return false;
3951 }
3952 }
Vladimir Marko58155012015-08-19 12:49:41 +00003953 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3954 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003955 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003956
3957 int32_t GetStringInitOffset() const {
3958 DCHECK(IsStringInit());
3959 return dispatch_info_.method_load_data;
3960 }
3961
3962 uint64_t GetMethodAddress() const {
3963 DCHECK(HasMethodAddress());
3964 return dispatch_info_.method_load_data;
3965 }
3966
3967 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003968 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003969 return dispatch_info_.method_load_data;
3970 }
3971
3972 uint64_t GetDirectCodePtr() const {
3973 DCHECK(HasDirectCodePtr());
3974 return dispatch_info_.direct_code_ptr;
3975 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003976
Vladimir Markoa1de9182016-02-25 11:37:38 +00003977 ClinitCheckRequirement GetClinitCheckRequirement() const {
3978 return GetPackedField<ClinitCheckRequirementField>();
3979 }
Calin Juravle68ad6492015-08-18 17:08:12 +01003980
Roland Levillain4c0eb422015-04-24 16:43:49 +01003981 // Is this instruction a call to a static method?
3982 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003983 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003984 }
3985
Vladimir Markofbb184a2015-11-13 14:47:00 +00003986 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3987 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3988 // instruction; only relevant for static calls with explicit clinit check.
3989 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003990 DCHECK(IsStaticWithExplicitClinitCheck());
3991 size_t last_input_index = InputCount() - 1;
3992 HInstruction* last_input = InputAt(last_input_index);
3993 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003994 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003995 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003996 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003997 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003998 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003999 }
4000
David Brazdilbc9ab162016-01-20 14:50:19 +00004001 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00004002 DCHECK(IsStringInit());
4003 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00004004 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00004005 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004006 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00004007 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01004008 }
4009
Roland Levillain4c0eb422015-04-24 16:43:49 +01004010 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004011 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004012 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004013 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004014 }
4015
4016 // Is this a call to a static method whose declaring class has an
4017 // implicit intialization check requirement?
4018 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004019 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004020 }
4021
Vladimir Markob554b5a2015-11-06 12:57:55 +00004022 // Does this method load kind need the current method as an input?
4023 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4024 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4025 }
4026
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004027 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004028
Roland Levillain4c0eb422015-04-24 16:43:49 +01004029 protected:
4030 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
4031 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
4032 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
4033 HInstruction* input = input_record.GetInstruction();
4034 // `input` is the last input of a static invoke marked as having
4035 // an explicit clinit check. It must either be:
4036 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4037 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4038 DCHECK(input != nullptr);
4039 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
4040 }
4041 return input_record;
4042 }
4043
Vladimir Markoc53c0792015-11-19 15:48:33 +00004044 void InsertInputAt(size_t index, HInstruction* input);
4045 void RemoveInputAt(size_t index);
4046
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004047 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004048 static constexpr size_t kFieldOptimizedInvokeType = kNumberOfInvokePackedBits;
4049 static constexpr size_t kFieldOptimizedInvokeTypeSize =
4050 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4051 static constexpr size_t kFieldClinitCheckRequirement =
4052 kFieldOptimizedInvokeType + kFieldOptimizedInvokeTypeSize;
4053 static constexpr size_t kFieldClinitCheckRequirementSize =
4054 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4055 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4056 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4057 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4058 "Too many packed fields.");
4059 using OptimizedInvokeTypeField =
4060 BitField<InvokeType, kFieldOptimizedInvokeType, kFieldOptimizedInvokeTypeSize>;
4061 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4062 kFieldClinitCheckRequirement,
4063 kFieldClinitCheckRequirementSize>;
4064
Vladimir Marko58155012015-08-19 12:49:41 +00004065 // The target method may refer to different dex file or method index than the original
4066 // invoke. This happens for sharpened calls and for calls where a method was redeclared
4067 // in derived class to increase visibility.
4068 MethodReference target_method_;
4069 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004070
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004071 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004072};
Vladimir Markof64242a2015-12-01 14:58:23 +00004073std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004074std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004075
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004076class HInvokeVirtual : public HInvoke {
4077 public:
4078 HInvokeVirtual(ArenaAllocator* arena,
4079 uint32_t number_of_arguments,
4080 Primitive::Type return_type,
4081 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004082 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004083 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004084 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004085 vtable_index_(vtable_index) {}
4086
Calin Juravle641547a2015-04-21 22:08:51 +01004087 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004088 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004089 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004090 }
4091
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004092 uint32_t GetVTableIndex() const { return vtable_index_; }
4093
4094 DECLARE_INSTRUCTION(InvokeVirtual);
4095
4096 private:
4097 const uint32_t vtable_index_;
4098
4099 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4100};
4101
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004102class HInvokeInterface : public HInvoke {
4103 public:
4104 HInvokeInterface(ArenaAllocator* arena,
4105 uint32_t number_of_arguments,
4106 Primitive::Type return_type,
4107 uint32_t dex_pc,
4108 uint32_t dex_method_index,
4109 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004110 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004111 imt_index_(imt_index) {}
4112
Calin Juravle641547a2015-04-21 22:08:51 +01004113 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004114 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004115 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004116 }
4117
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004118 uint32_t GetImtIndex() const { return imt_index_; }
4119 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4120
4121 DECLARE_INSTRUCTION(InvokeInterface);
4122
4123 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004124 const uint32_t imt_index_;
4125
4126 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4127};
4128
Roland Levillain88cb1752014-10-20 16:36:47 +01004129class HNeg : public HUnaryOperation {
4130 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004131 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4132 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01004133
Roland Levillain9867bc72015-08-05 10:21:34 +01004134 template <typename T> T Compute(T x) const { return -x; }
4135
4136 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004137 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004138 }
4139 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004140 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004141 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004142 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4143 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4144 }
4145 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4146 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4147 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004148
Roland Levillain88cb1752014-10-20 16:36:47 +01004149 DECLARE_INSTRUCTION(Neg);
4150
4151 private:
4152 DISALLOW_COPY_AND_ASSIGN(HNeg);
4153};
4154
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004155class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004156 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004157 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004158 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004159 uint32_t dex_pc,
4160 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004161 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004162 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004163 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004164 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004165 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004166 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004167 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004168 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004169 }
4170
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004171 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004172 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004173
4174 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004175 bool NeedsEnvironment() const OVERRIDE { return true; }
4176
Mingyao Yang0c365e62015-03-31 15:09:29 -07004177 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4178 bool CanThrow() const OVERRIDE { return true; }
4179
Calin Juravle10e244f2015-01-26 18:54:32 +00004180 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004181
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004182 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4183
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004184 DECLARE_INSTRUCTION(NewArray);
4185
4186 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004187 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004188 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004189 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004190
4191 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4192};
4193
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004194class HAdd : public HBinaryOperation {
4195 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004196 HAdd(Primitive::Type result_type,
4197 HInstruction* left,
4198 HInstruction* right,
4199 uint32_t dex_pc = kNoDexPc)
4200 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004201
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004202 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004203
Roland Levillain9867bc72015-08-05 10:21:34 +01004204 template <typename T> T Compute(T x, T y) const { return x + y; }
4205
4206 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004207 return GetBlock()->GetGraph()->GetIntConstant(
4208 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004209 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004210 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004211 return GetBlock()->GetGraph()->GetLongConstant(
4212 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004213 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004214 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4215 return GetBlock()->GetGraph()->GetFloatConstant(
4216 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4217 }
4218 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4219 return GetBlock()->GetGraph()->GetDoubleConstant(
4220 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4221 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004222
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004223 DECLARE_INSTRUCTION(Add);
4224
4225 private:
4226 DISALLOW_COPY_AND_ASSIGN(HAdd);
4227};
4228
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004229class HSub : public HBinaryOperation {
4230 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004231 HSub(Primitive::Type result_type,
4232 HInstruction* left,
4233 HInstruction* right,
4234 uint32_t dex_pc = kNoDexPc)
4235 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004236
Roland Levillain9867bc72015-08-05 10:21:34 +01004237 template <typename T> T Compute(T x, T y) const { return x - y; }
4238
4239 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004240 return GetBlock()->GetGraph()->GetIntConstant(
4241 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004242 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004243 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004244 return GetBlock()->GetGraph()->GetLongConstant(
4245 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004246 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004247 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4248 return GetBlock()->GetGraph()->GetFloatConstant(
4249 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4250 }
4251 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4252 return GetBlock()->GetGraph()->GetDoubleConstant(
4253 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4254 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004255
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004256 DECLARE_INSTRUCTION(Sub);
4257
4258 private:
4259 DISALLOW_COPY_AND_ASSIGN(HSub);
4260};
4261
Calin Juravle34bacdf2014-10-07 20:23:36 +01004262class HMul : public HBinaryOperation {
4263 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004264 HMul(Primitive::Type result_type,
4265 HInstruction* left,
4266 HInstruction* right,
4267 uint32_t dex_pc = kNoDexPc)
4268 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004269
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004270 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004271
Roland Levillain9867bc72015-08-05 10:21:34 +01004272 template <typename T> T Compute(T x, T y) const { return x * y; }
4273
4274 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004275 return GetBlock()->GetGraph()->GetIntConstant(
4276 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004277 }
4278 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004279 return GetBlock()->GetGraph()->GetLongConstant(
4280 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004281 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004282 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4283 return GetBlock()->GetGraph()->GetFloatConstant(
4284 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4285 }
4286 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4287 return GetBlock()->GetGraph()->GetDoubleConstant(
4288 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4289 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004290
4291 DECLARE_INSTRUCTION(Mul);
4292
4293 private:
4294 DISALLOW_COPY_AND_ASSIGN(HMul);
4295};
4296
Calin Juravle7c4954d2014-10-28 16:57:40 +00004297class HDiv : public HBinaryOperation {
4298 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004299 HDiv(Primitive::Type result_type,
4300 HInstruction* left,
4301 HInstruction* right,
4302 uint32_t dex_pc)
4303 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004304
Roland Levillain9867bc72015-08-05 10:21:34 +01004305 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004306 T ComputeIntegral(T x, T y) const {
4307 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004308 // Our graph structure ensures we never have 0 for `y` during
4309 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004310 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004311 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004312 return (y == -1) ? -x : x / y;
4313 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004314
Roland Levillain31dd3d62016-02-16 12:21:02 +00004315 template <typename T>
4316 T ComputeFP(T x, T y) const {
4317 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4318 return x / y;
4319 }
4320
Roland Levillain9867bc72015-08-05 10:21:34 +01004321 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004322 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004323 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004324 }
4325 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004326 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004327 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4328 }
4329 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4330 return GetBlock()->GetGraph()->GetFloatConstant(
4331 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4332 }
4333 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4334 return GetBlock()->GetGraph()->GetDoubleConstant(
4335 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004336 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004337
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004338 static SideEffects SideEffectsForArchRuntimeCalls() {
4339 // The generated code can use a runtime call.
4340 return SideEffects::CanTriggerGC();
4341 }
4342
Calin Juravle7c4954d2014-10-28 16:57:40 +00004343 DECLARE_INSTRUCTION(Div);
4344
4345 private:
4346 DISALLOW_COPY_AND_ASSIGN(HDiv);
4347};
4348
Calin Juravlebacfec32014-11-14 15:54:36 +00004349class HRem : public HBinaryOperation {
4350 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004351 HRem(Primitive::Type result_type,
4352 HInstruction* left,
4353 HInstruction* right,
4354 uint32_t dex_pc)
4355 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004356
Roland Levillain9867bc72015-08-05 10:21:34 +01004357 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004358 T ComputeIntegral(T x, T y) const {
4359 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004360 // Our graph structure ensures we never have 0 for `y` during
4361 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004362 DCHECK_NE(y, 0);
4363 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4364 return (y == -1) ? 0 : x % y;
4365 }
4366
Roland Levillain31dd3d62016-02-16 12:21:02 +00004367 template <typename T>
4368 T ComputeFP(T x, T y) const {
4369 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4370 return std::fmod(x, y);
4371 }
4372
Roland Levillain9867bc72015-08-05 10:21:34 +01004373 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004374 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004375 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004376 }
4377 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004378 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004379 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004380 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004381 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4382 return GetBlock()->GetGraph()->GetFloatConstant(
4383 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4384 }
4385 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4386 return GetBlock()->GetGraph()->GetDoubleConstant(
4387 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4388 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004389
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004390 static SideEffects SideEffectsForArchRuntimeCalls() {
4391 return SideEffects::CanTriggerGC();
4392 }
4393
Calin Juravlebacfec32014-11-14 15:54:36 +00004394 DECLARE_INSTRUCTION(Rem);
4395
4396 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004397 DISALLOW_COPY_AND_ASSIGN(HRem);
4398};
4399
Calin Juravled0d48522014-11-04 16:40:20 +00004400class HDivZeroCheck : public HExpression<1> {
4401 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004402 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4403 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004404 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004405 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004406 SetRawInputAt(0, value);
4407 }
4408
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004409 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4410
Calin Juravled0d48522014-11-04 16:40:20 +00004411 bool CanBeMoved() const OVERRIDE { return true; }
4412
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004413 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004414 return true;
4415 }
4416
4417 bool NeedsEnvironment() const OVERRIDE { return true; }
4418 bool CanThrow() const OVERRIDE { return true; }
4419
Calin Juravled0d48522014-11-04 16:40:20 +00004420 DECLARE_INSTRUCTION(DivZeroCheck);
4421
4422 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004423 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4424};
4425
Calin Juravle9aec02f2014-11-18 23:06:35 +00004426class HShl : public HBinaryOperation {
4427 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004428 HShl(Primitive::Type result_type,
4429 HInstruction* left,
4430 HInstruction* right,
4431 uint32_t dex_pc = kNoDexPc)
4432 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004433
Roland Levillain9867bc72015-08-05 10:21:34 +01004434 template <typename T, typename U, typename V>
4435 T Compute(T x, U y, V max_shift_value) const {
4436 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4437 "V is not the unsigned integer type corresponding to T");
4438 return x << (y & max_shift_value);
4439 }
4440
4441 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4442 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004443 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004444 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004445 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4446 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004447 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004448 }
4449 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4450 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004451 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004452 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004453 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4454 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4455 LOG(FATAL) << DebugName() << " is not defined for float values";
4456 UNREACHABLE();
4457 }
4458 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4459 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4460 LOG(FATAL) << DebugName() << " is not defined for double values";
4461 UNREACHABLE();
4462 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004463
4464 DECLARE_INSTRUCTION(Shl);
4465
4466 private:
4467 DISALLOW_COPY_AND_ASSIGN(HShl);
4468};
4469
4470class HShr : public HBinaryOperation {
4471 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004472 HShr(Primitive::Type result_type,
4473 HInstruction* left,
4474 HInstruction* right,
4475 uint32_t dex_pc = kNoDexPc)
4476 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004477
Roland Levillain9867bc72015-08-05 10:21:34 +01004478 template <typename T, typename U, typename V>
4479 T Compute(T x, U y, V max_shift_value) const {
4480 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4481 "V is not the unsigned integer type corresponding to T");
4482 return x >> (y & max_shift_value);
4483 }
4484
4485 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4486 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004487 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004488 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004489 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4490 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004491 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004492 }
4493 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4494 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004495 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004496 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004497 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4498 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4499 LOG(FATAL) << DebugName() << " is not defined for float values";
4500 UNREACHABLE();
4501 }
4502 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4503 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4504 LOG(FATAL) << DebugName() << " is not defined for double values";
4505 UNREACHABLE();
4506 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004507
4508 DECLARE_INSTRUCTION(Shr);
4509
4510 private:
4511 DISALLOW_COPY_AND_ASSIGN(HShr);
4512};
4513
4514class HUShr : public HBinaryOperation {
4515 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004516 HUShr(Primitive::Type result_type,
4517 HInstruction* left,
4518 HInstruction* right,
4519 uint32_t dex_pc = kNoDexPc)
4520 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004521
Roland Levillain9867bc72015-08-05 10:21:34 +01004522 template <typename T, typename U, typename V>
4523 T Compute(T x, U y, V max_shift_value) const {
4524 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4525 "V is not the unsigned integer type corresponding to T");
4526 V ux = static_cast<V>(x);
4527 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004528 }
4529
Roland Levillain9867bc72015-08-05 10:21:34 +01004530 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4531 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004532 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004533 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004534 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4535 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004536 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004537 }
4538 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4539 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004540 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004541 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004542 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4543 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4544 LOG(FATAL) << DebugName() << " is not defined for float values";
4545 UNREACHABLE();
4546 }
4547 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4548 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4549 LOG(FATAL) << DebugName() << " is not defined for double values";
4550 UNREACHABLE();
4551 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004552
4553 DECLARE_INSTRUCTION(UShr);
4554
4555 private:
4556 DISALLOW_COPY_AND_ASSIGN(HUShr);
4557};
4558
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004559class HAnd : public HBinaryOperation {
4560 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004561 HAnd(Primitive::Type result_type,
4562 HInstruction* left,
4563 HInstruction* right,
4564 uint32_t dex_pc = kNoDexPc)
4565 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004566
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004567 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004568
Roland Levillaine53bd812016-02-24 14:54:18 +00004569 template <typename T> T Compute(T x, T y) const { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004570
4571 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004572 return GetBlock()->GetGraph()->GetIntConstant(
4573 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004574 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004575 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004576 return GetBlock()->GetGraph()->GetLongConstant(
4577 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004578 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004579 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4580 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4581 LOG(FATAL) << DebugName() << " is not defined for float values";
4582 UNREACHABLE();
4583 }
4584 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4585 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4586 LOG(FATAL) << DebugName() << " is not defined for double values";
4587 UNREACHABLE();
4588 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004589
4590 DECLARE_INSTRUCTION(And);
4591
4592 private:
4593 DISALLOW_COPY_AND_ASSIGN(HAnd);
4594};
4595
4596class HOr : public HBinaryOperation {
4597 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004598 HOr(Primitive::Type result_type,
4599 HInstruction* left,
4600 HInstruction* right,
4601 uint32_t dex_pc = kNoDexPc)
4602 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004603
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004604 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004605
Roland Levillaine53bd812016-02-24 14:54:18 +00004606 template <typename T> T Compute(T x, T y) const { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004607
4608 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004609 return GetBlock()->GetGraph()->GetIntConstant(
4610 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004611 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004612 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004613 return GetBlock()->GetGraph()->GetLongConstant(
4614 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004615 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004616 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4617 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4618 LOG(FATAL) << DebugName() << " is not defined for float values";
4619 UNREACHABLE();
4620 }
4621 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4622 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4623 LOG(FATAL) << DebugName() << " is not defined for double values";
4624 UNREACHABLE();
4625 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004626
4627 DECLARE_INSTRUCTION(Or);
4628
4629 private:
4630 DISALLOW_COPY_AND_ASSIGN(HOr);
4631};
4632
4633class HXor : public HBinaryOperation {
4634 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004635 HXor(Primitive::Type result_type,
4636 HInstruction* left,
4637 HInstruction* right,
4638 uint32_t dex_pc = kNoDexPc)
4639 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004640
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004641 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004642
Roland Levillaine53bd812016-02-24 14:54:18 +00004643 template <typename T> T Compute(T x, T y) const { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004644
4645 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004646 return GetBlock()->GetGraph()->GetIntConstant(
4647 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004648 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004649 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004650 return GetBlock()->GetGraph()->GetLongConstant(
4651 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004652 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004653 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4654 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4655 LOG(FATAL) << DebugName() << " is not defined for float values";
4656 UNREACHABLE();
4657 }
4658 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4659 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4660 LOG(FATAL) << DebugName() << " is not defined for double values";
4661 UNREACHABLE();
4662 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004663
4664 DECLARE_INSTRUCTION(Xor);
4665
4666 private:
4667 DISALLOW_COPY_AND_ASSIGN(HXor);
4668};
4669
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004670class HRor : public HBinaryOperation {
4671 public:
4672 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4673 : HBinaryOperation(result_type, value, distance) {}
4674
4675 template <typename T, typename U, typename V>
4676 T Compute(T x, U y, V max_shift_value) const {
4677 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4678 "V is not the unsigned integer type corresponding to T");
4679 V ux = static_cast<V>(x);
4680 if ((y & max_shift_value) == 0) {
4681 return static_cast<T>(ux);
4682 } else {
4683 const V reg_bits = sizeof(T) * 8;
4684 return static_cast<T>(ux >> (y & max_shift_value)) |
4685 (x << (reg_bits - (y & max_shift_value)));
4686 }
4687 }
4688
4689 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4690 return GetBlock()->GetGraph()->GetIntConstant(
4691 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4692 }
4693 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4694 return GetBlock()->GetGraph()->GetLongConstant(
4695 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4696 }
4697 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4698 return GetBlock()->GetGraph()->GetLongConstant(
4699 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4700 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004701 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4702 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4703 LOG(FATAL) << DebugName() << " is not defined for float values";
4704 UNREACHABLE();
4705 }
4706 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4707 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4708 LOG(FATAL) << DebugName() << " is not defined for double values";
4709 UNREACHABLE();
4710 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004711
4712 DECLARE_INSTRUCTION(Ror);
4713
4714 private:
4715 DISALLOW_COPY_AND_ASSIGN(HRor);
4716};
4717
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004718// The value of a parameter in this method. Its location depends on
4719// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004720class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004721 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004722 HParameterValue(const DexFile& dex_file,
4723 uint16_t type_index,
4724 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004725 Primitive::Type parameter_type,
4726 bool is_this = false)
4727 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004728 dex_file_(dex_file),
4729 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004730 index_(index) {
4731 SetPackedFlag<kFlagIsThis>(is_this);
4732 SetPackedFlag<kFlagCanBeNull>(!is_this);
4733 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004734
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004735 const DexFile& GetDexFile() const { return dex_file_; }
4736 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004737 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004738 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004739
Vladimir Markoa1de9182016-02-25 11:37:38 +00004740 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4741 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004742
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004743 DECLARE_INSTRUCTION(ParameterValue);
4744
4745 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004746 // Whether or not the parameter value corresponds to 'this' argument.
4747 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4748 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4749 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4750 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4751 "Too many packed fields.");
4752
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004753 const DexFile& dex_file_;
4754 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004755 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004756 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004757 const uint8_t index_;
4758
4759 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4760};
4761
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004762class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004763 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004764 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4765 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004766
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004767 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004768 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004769 return true;
4770 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004771
Roland Levillain9867bc72015-08-05 10:21:34 +01004772 template <typename T> T Compute(T x) const { return ~x; }
4773
4774 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004775 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004776 }
4777 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004778 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004779 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004780 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4781 LOG(FATAL) << DebugName() << " is not defined for float values";
4782 UNREACHABLE();
4783 }
4784 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4785 LOG(FATAL) << DebugName() << " is not defined for double values";
4786 UNREACHABLE();
4787 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004788
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004789 DECLARE_INSTRUCTION(Not);
4790
4791 private:
4792 DISALLOW_COPY_AND_ASSIGN(HNot);
4793};
4794
David Brazdil66d126e2015-04-03 16:02:44 +01004795class HBooleanNot : public HUnaryOperation {
4796 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004797 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4798 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004799
4800 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004801 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004802 return true;
4803 }
4804
Roland Levillain9867bc72015-08-05 10:21:34 +01004805 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004806 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004807 return !x;
4808 }
4809
Roland Levillain9867bc72015-08-05 10:21:34 +01004810 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004811 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004812 }
4813 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4814 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004815 UNREACHABLE();
4816 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004817 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4818 LOG(FATAL) << DebugName() << " is not defined for float values";
4819 UNREACHABLE();
4820 }
4821 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4822 LOG(FATAL) << DebugName() << " is not defined for double values";
4823 UNREACHABLE();
4824 }
David Brazdil66d126e2015-04-03 16:02:44 +01004825
4826 DECLARE_INSTRUCTION(BooleanNot);
4827
4828 private:
4829 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4830};
4831
Roland Levillaindff1f282014-11-05 14:15:05 +00004832class HTypeConversion : public HExpression<1> {
4833 public:
4834 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004835 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004836 : HExpression(result_type,
4837 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4838 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004839 SetRawInputAt(0, input);
4840 DCHECK_NE(input->GetType(), result_type);
4841 }
4842
4843 HInstruction* GetInput() const { return InputAt(0); }
4844 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4845 Primitive::Type GetResultType() const { return GetType(); }
4846
4847 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004848 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004849
Mark Mendelle82549b2015-05-06 10:55:34 -04004850 // Try to statically evaluate the conversion and return a HConstant
4851 // containing the result. If the input cannot be converted, return nullptr.
4852 HConstant* TryStaticEvaluation() const;
4853
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004854 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4855 Primitive::Type result_type) {
4856 // Some architectures may not require the 'GC' side effects, but at this point
4857 // in the compilation process we do not know what architecture we will
4858 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004859 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4860 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004861 return SideEffects::CanTriggerGC();
4862 }
4863 return SideEffects::None();
4864 }
4865
Roland Levillaindff1f282014-11-05 14:15:05 +00004866 DECLARE_INSTRUCTION(TypeConversion);
4867
4868 private:
4869 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4870};
4871
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004872static constexpr uint32_t kNoRegNumber = -1;
4873
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004874class HPhi : public HInstruction {
4875 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004876 HPhi(ArenaAllocator* arena,
4877 uint32_t reg_number,
4878 size_t number_of_inputs,
4879 Primitive::Type type,
4880 uint32_t dex_pc = kNoDexPc)
4881 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004882 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004883 reg_number_(reg_number) {
4884 SetPackedField<TypeField>(ToPhiType(type));
4885 DCHECK_NE(GetType(), Primitive::kPrimVoid);
4886 // Phis are constructed live and marked dead if conflicting or unused.
4887 // Individual steps of SsaBuilder should assume that if a phi has been
4888 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4889 SetPackedFlag<kFlagIsLive>(true);
4890 SetPackedFlag<kFlagCanBeNull>(true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004891 }
4892
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004893 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4894 static Primitive::Type ToPhiType(Primitive::Type type) {
4895 switch (type) {
4896 case Primitive::kPrimBoolean:
4897 case Primitive::kPrimByte:
4898 case Primitive::kPrimShort:
4899 case Primitive::kPrimChar:
4900 return Primitive::kPrimInt;
4901 default:
4902 return type;
4903 }
4904 }
4905
David Brazdilffee3d32015-07-06 11:48:53 +01004906 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4907
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004908 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004909
4910 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004911 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004912
Vladimir Markoa1de9182016-02-25 11:37:38 +00004913 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
David Brazdil4833f5a2015-12-16 10:37:39 +00004914 void SetType(Primitive::Type new_type) {
4915 // Make sure that only valid type changes occur. The following are allowed:
4916 // (1) int -> float/ref (primitive type propagation),
4917 // (2) long -> double (primitive type propagation).
Vladimir Markoa1de9182016-02-25 11:37:38 +00004918 DCHECK(GetType() == new_type ||
4919 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4920 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4921 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4922 SetPackedField<TypeField>(new_type);
David Brazdil4833f5a2015-12-16 10:37:39 +00004923 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004924
Vladimir Markoa1de9182016-02-25 11:37:38 +00004925 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4926 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004927
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004928 uint32_t GetRegNumber() const { return reg_number_; }
4929
Vladimir Markoa1de9182016-02-25 11:37:38 +00004930 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
4931 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
4932 bool IsDead() const { return !IsLive(); }
4933 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004934
David Brazdil77a48ae2015-09-15 12:34:04 +00004935 bool IsVRegEquivalentOf(HInstruction* other) const {
4936 return other != nullptr
4937 && other->IsPhi()
4938 && other->AsPhi()->GetBlock() == GetBlock()
4939 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4940 }
4941
Calin Juravlea4f88312015-04-16 12:57:19 +01004942 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4943 // An equivalent phi is a phi having the same dex register and type.
4944 // It assumes that phis with the same dex register are adjacent.
4945 HPhi* GetNextEquivalentPhiWithSameType() {
4946 HInstruction* next = GetNext();
4947 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4948 if (next->GetType() == GetType()) {
4949 return next->AsPhi();
4950 }
4951 next = next->GetNext();
4952 }
4953 return nullptr;
4954 }
4955
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004956 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004957
David Brazdil1abb4192015-02-17 18:33:36 +00004958 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004959 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004960 return inputs_[index];
4961 }
David Brazdil1abb4192015-02-17 18:33:36 +00004962
4963 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004964 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004965 }
4966
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004967 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004968 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
4969 static constexpr size_t kFieldTypeSize =
4970 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4971 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
4972 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
4973 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
4974 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4975 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
4976
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004977 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004978 const uint32_t reg_number_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004979
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004980 DISALLOW_COPY_AND_ASSIGN(HPhi);
4981};
4982
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004983class HNullCheck : public HExpression<1> {
4984 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004985 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4986 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004987 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004988 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004989 SetRawInputAt(0, value);
4990 }
4991
Calin Juravle10e244f2015-01-26 18:54:32 +00004992 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004993 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004994 return true;
4995 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004996
Calin Juravle10e244f2015-01-26 18:54:32 +00004997 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004998
Calin Juravle10e244f2015-01-26 18:54:32 +00004999 bool CanThrow() const OVERRIDE { return true; }
5000
5001 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005002
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005003
5004 DECLARE_INSTRUCTION(NullCheck);
5005
5006 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005007 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5008};
5009
5010class FieldInfo : public ValueObject {
5011 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005012 FieldInfo(MemberOffset field_offset,
5013 Primitive::Type field_type,
5014 bool is_volatile,
5015 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005016 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005017 const DexFile& dex_file,
5018 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005019 : field_offset_(field_offset),
5020 field_type_(field_type),
5021 is_volatile_(is_volatile),
5022 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005023 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005024 dex_file_(dex_file),
5025 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005026
5027 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005028 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005029 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005030 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005031 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005032 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005033 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005034
5035 private:
5036 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005037 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005038 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005039 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005040 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005041 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005042 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005043};
5044
5045class HInstanceFieldGet : public HExpression<1> {
5046 public:
5047 HInstanceFieldGet(HInstruction* value,
5048 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005049 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005050 bool is_volatile,
5051 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005052 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005053 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005054 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005055 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005056 : HExpression(field_type,
5057 SideEffects::FieldReadOfType(field_type, is_volatile),
5058 dex_pc),
5059 field_info_(field_offset,
5060 field_type,
5061 is_volatile,
5062 field_idx,
5063 declaring_class_def_index,
5064 dex_file,
5065 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005066 SetRawInputAt(0, value);
5067 }
5068
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005069 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005070
5071 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5072 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
5073 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005074 }
5075
Calin Juravle641547a2015-04-21 22:08:51 +01005076 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5077 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005078 }
5079
5080 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005081 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5082 }
5083
Calin Juravle52c48962014-12-16 17:02:57 +00005084 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005085 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005086 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005087 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005088
5089 DECLARE_INSTRUCTION(InstanceFieldGet);
5090
5091 private:
5092 const FieldInfo field_info_;
5093
5094 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5095};
5096
5097class HInstanceFieldSet : public HTemplateInstruction<2> {
5098 public:
5099 HInstanceFieldSet(HInstruction* object,
5100 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005101 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005102 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005103 bool is_volatile,
5104 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005105 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005106 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005107 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005108 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005109 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5110 dex_pc),
5111 field_info_(field_offset,
5112 field_type,
5113 is_volatile,
5114 field_idx,
5115 declaring_class_def_index,
5116 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005117 dex_cache) {
5118 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005119 SetRawInputAt(0, object);
5120 SetRawInputAt(1, value);
5121 }
5122
Calin Juravle641547a2015-04-21 22:08:51 +01005123 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5124 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005125 }
5126
Calin Juravle52c48962014-12-16 17:02:57 +00005127 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005128 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005129 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005130 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005131 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005132 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5133 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005134
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005135 DECLARE_INSTRUCTION(InstanceFieldSet);
5136
5137 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005138 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5139 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5140 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5141 "Too many packed fields.");
5142
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005143 const FieldInfo field_info_;
5144
5145 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5146};
5147
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005148class HArrayGet : public HExpression<2> {
5149 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005150 HArrayGet(HInstruction* array,
5151 HInstruction* index,
5152 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005153 uint32_t dex_pc,
5154 SideEffects additional_side_effects = SideEffects::None())
5155 : HExpression(type,
5156 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00005157 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005158 SetRawInputAt(0, array);
5159 SetRawInputAt(1, index);
5160 }
5161
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005162 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005163 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005164 return true;
5165 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005166 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005167 // TODO: We can be smarter here.
5168 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5169 // which generates the implicit null check. There are cases when these can be removed
5170 // to produce better code. If we ever add optimizations to do so we should allow an
5171 // implicit check here (as long as the address falls in the first page).
5172 return false;
5173 }
5174
David Brazdil4833f5a2015-12-16 10:37:39 +00005175 bool IsEquivalentOf(HArrayGet* other) const {
5176 bool result = (GetDexPc() == other->GetDexPc());
5177 if (kIsDebugBuild && result) {
5178 DCHECK_EQ(GetBlock(), other->GetBlock());
5179 DCHECK_EQ(GetArray(), other->GetArray());
5180 DCHECK_EQ(GetIndex(), other->GetIndex());
5181 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005182 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005183 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005184 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5185 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005186 }
5187 }
5188 return result;
5189 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005190
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005191 HInstruction* GetArray() const { return InputAt(0); }
5192 HInstruction* GetIndex() const { return InputAt(1); }
5193
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005194 DECLARE_INSTRUCTION(ArrayGet);
5195
5196 private:
5197 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5198};
5199
5200class HArraySet : public HTemplateInstruction<3> {
5201 public:
5202 HArraySet(HInstruction* array,
5203 HInstruction* index,
5204 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005205 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005206 uint32_t dex_pc,
5207 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005208 : HTemplateInstruction(
5209 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005210 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
5211 additional_side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005212 dex_pc) {
5213 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5214 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5215 SetPackedFlag<kFlagValueCanBeNull>(true);
5216 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005217 SetRawInputAt(0, array);
5218 SetRawInputAt(1, index);
5219 SetRawInputAt(2, value);
5220 }
5221
Calin Juravle77520bc2015-01-12 18:45:46 +00005222 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005223 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005224 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005225 }
5226
Mingyao Yang81014cb2015-06-02 03:16:27 -07005227 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005228 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005229
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005230 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005231 // TODO: Same as for ArrayGet.
5232 return false;
5233 }
5234
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005235 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005236 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005237 }
5238
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005239 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005240 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005241 }
5242
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005243 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005244 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005245 }
5246
Vladimir Markoa1de9182016-02-25 11:37:38 +00005247 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5248 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5249 bool StaticTypeOfArrayIsObjectArray() const {
5250 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5251 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005252
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005253 HInstruction* GetArray() const { return InputAt(0); }
5254 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005255 HInstruction* GetValue() const { return InputAt(2); }
5256
5257 Primitive::Type GetComponentType() const {
5258 // The Dex format does not type floating point index operations. Since the
5259 // `expected_component_type_` is set during building and can therefore not
5260 // be correct, we also check what is the value type. If it is a floating
5261 // point type, we must use that type.
5262 Primitive::Type value_type = GetValue()->GetType();
5263 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5264 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005265 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005266 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005267
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005268 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005269 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005270 }
5271
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005272 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5273 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5274 }
5275
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005276 DECLARE_INSTRUCTION(ArraySet);
5277
5278 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005279 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5280 static constexpr size_t kFieldExpectedComponentTypeSize =
5281 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5282 static constexpr size_t kFlagNeedsTypeCheck =
5283 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5284 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005285 // Cached information for the reference_type_info_ so that codegen
5286 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005287 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5288 static constexpr size_t kNumberOfArraySetPackedBits =
5289 kFlagStaticTypeOfArrayIsObjectArray + 1;
5290 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5291 using ExpectedComponentTypeField =
5292 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005293
5294 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5295};
5296
5297class HArrayLength : public HExpression<1> {
5298 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005299 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005300 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005301 // Note that arrays do not change length, so the instruction does not
5302 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005303 SetRawInputAt(0, array);
5304 }
5305
Calin Juravle77520bc2015-01-12 18:45:46 +00005306 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005307 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005308 return true;
5309 }
Calin Juravle641547a2015-04-21 22:08:51 +01005310 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5311 return obj == InputAt(0);
5312 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005313
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005314 DECLARE_INSTRUCTION(ArrayLength);
5315
5316 private:
5317 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5318};
5319
5320class HBoundsCheck : public HExpression<2> {
5321 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005322 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5323 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005324 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005325 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005326 DCHECK(index->GetType() == Primitive::kPrimInt);
5327 SetRawInputAt(0, index);
5328 SetRawInputAt(1, length);
5329 }
5330
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005331 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005332 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005333 return true;
5334 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005335
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005336 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005337
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005338 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005339
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005340 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005341
5342 DECLARE_INSTRUCTION(BoundsCheck);
5343
5344 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005345 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5346};
5347
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005348class HSuspendCheck : public HTemplateInstruction<0> {
5349 public:
5350 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005351 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005352
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005353 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005354 return true;
5355 }
5356
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005357 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5358 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005359
5360 DECLARE_INSTRUCTION(SuspendCheck);
5361
5362 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005363 // Only used for code generation, in order to share the same slow path between back edges
5364 // of a same loop.
5365 SlowPathCode* slow_path_;
5366
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005367 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5368};
5369
David Srbecky0cf44932015-12-09 14:09:59 +00005370// Pseudo-instruction which provides the native debugger with mapping information.
5371// It ensures that we can generate line number and local variables at this point.
5372class HNativeDebugInfo : public HTemplateInstruction<0> {
5373 public:
5374 explicit HNativeDebugInfo(uint32_t dex_pc)
5375 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5376
5377 bool NeedsEnvironment() const OVERRIDE {
5378 return true;
5379 }
5380
5381 DECLARE_INSTRUCTION(NativeDebugInfo);
5382
5383 private:
5384 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5385};
5386
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005387/**
5388 * Instruction to load a Class object.
5389 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005390class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005391 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005392 HLoadClass(HCurrentMethod* current_method,
5393 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005394 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005395 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005396 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005397 bool needs_access_check,
5398 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005399 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005400 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005401 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005402 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005403 // Referrers class should not need access check. We never inline unverified
5404 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005405 DCHECK(!is_referrers_class || !needs_access_check);
5406
5407 SetPackedFlag<kFlagIsReferrersClass>(is_referrers_class);
5408 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5409 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
5410 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005411 SetRawInputAt(0, current_method);
5412 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005413
5414 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005415
5416 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005417 // Note that we don't need to test for generate_clinit_check_.
5418 // Whether or not we need to generate the clinit check is processed in
5419 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005420 return other->AsLoadClass()->type_index_ == type_index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00005421 other->AsLoadClass()->GetPackedFields() == GetPackedFields();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005422 }
5423
5424 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5425
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005426 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005427 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005428
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005429 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005430 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005431 }
5432
Calin Juravle0ba218d2015-05-19 18:46:01 +01005433 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005434 // The entrypoint the code generator is going to call does not do
5435 // clinit of the class.
5436 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005437 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005438 }
5439
5440 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005441 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005442 (!IsReferrersClass() && !IsInDexCache()) ||
5443 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005444 }
5445
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005446
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005447 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005448 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005449 }
5450
Calin Juravleacf735c2015-02-12 15:25:22 +00005451 ReferenceTypeInfo GetLoadedClassRTI() {
5452 return loaded_class_rti_;
5453 }
5454
5455 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5456 // Make sure we only set exact types (the loaded class should never be merged).
5457 DCHECK(rti.IsExact());
5458 loaded_class_rti_ = rti;
5459 }
5460
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005461 const DexFile& GetDexFile() { return dex_file_; }
5462
Vladimir Markoa1de9182016-02-25 11:37:38 +00005463 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !IsReferrersClass(); }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005464
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005465 static SideEffects SideEffectsForArchRuntimeCalls() {
5466 return SideEffects::CanTriggerGC();
5467 }
5468
Vladimir Markoa1de9182016-02-25 11:37:38 +00005469 bool IsReferrersClass() const { return GetPackedFlag<kFlagIsReferrersClass>(); }
5470 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5471 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5472 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005473
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005474 DECLARE_INSTRUCTION(LoadClass);
5475
5476 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005477 static constexpr size_t kFlagIsReferrersClass = kNumberOfExpressionPackedBits;
5478 static constexpr size_t kFlagNeedsAccessCheck = kFlagIsReferrersClass + 1;
5479 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005480 // Whether this instruction must generate the initialization check.
5481 // Used for code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005482 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInDexCache + 1;
5483 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagGenerateClInitCheck + 1;
5484 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
5485
5486 const uint16_t type_index_;
5487 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005488
Calin Juravleacf735c2015-02-12 15:25:22 +00005489 ReferenceTypeInfo loaded_class_rti_;
5490
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005491 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5492};
5493
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005494class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005495 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005496 HLoadString(HCurrentMethod* current_method,
5497 uint32_t string_index,
5498 uint32_t dex_pc,
5499 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005500 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005501 string_index_(string_index) {
5502 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005503 SetRawInputAt(0, current_method);
5504 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005505
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005506 bool CanBeMoved() const OVERRIDE { return true; }
5507
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005508 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5509 return other->AsLoadString()->string_index_ == string_index_;
5510 }
5511
5512 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5513
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005514 uint32_t GetStringIndex() const { return string_index_; }
5515
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005516 // Will call the runtime if the string is not already in the dex cache.
5517 bool NeedsEnvironment() const OVERRIDE { return !IsInDexCache(); }
5518
Vladimir Markodc151b22015-10-15 18:02:30 +01005519 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005520 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005521 bool CanThrow() const OVERRIDE { return !IsInDexCache(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005522
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005523 static SideEffects SideEffectsForArchRuntimeCalls() {
5524 return SideEffects::CanTriggerGC();
5525 }
5526
Vladimir Markoa1de9182016-02-25 11:37:38 +00005527 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5528
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005529 DECLARE_INSTRUCTION(LoadString);
5530
5531 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005532 static constexpr size_t kFlagIsInDexCache = kNumberOfExpressionPackedBits;
5533 static constexpr size_t kNumberOfLoadStringPackedBits = kFlagIsInDexCache + 1;
5534 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5535
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005536 const uint32_t string_index_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005537
5538 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5539};
5540
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005541/**
5542 * Performs an initialization check on its Class object input.
5543 */
5544class HClinitCheck : public HExpression<1> {
5545 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005546 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005547 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005548 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005549 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5550 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005551 SetRawInputAt(0, constant);
5552 }
5553
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005554 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005555 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005556 return true;
5557 }
5558
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005559 bool NeedsEnvironment() const OVERRIDE {
5560 // May call runtime to initialize the class.
5561 return true;
5562 }
5563
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005564 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005565
5566 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5567
5568 DECLARE_INSTRUCTION(ClinitCheck);
5569
5570 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005571 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5572};
5573
5574class HStaticFieldGet : public HExpression<1> {
5575 public:
5576 HStaticFieldGet(HInstruction* cls,
5577 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005578 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005579 bool is_volatile,
5580 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005581 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005582 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005583 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005584 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005585 : HExpression(field_type,
5586 SideEffects::FieldReadOfType(field_type, is_volatile),
5587 dex_pc),
5588 field_info_(field_offset,
5589 field_type,
5590 is_volatile,
5591 field_idx,
5592 declaring_class_def_index,
5593 dex_file,
5594 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005595 SetRawInputAt(0, cls);
5596 }
5597
Calin Juravle52c48962014-12-16 17:02:57 +00005598
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005599 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005600
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005601 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005602 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5603 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005604 }
5605
5606 size_t ComputeHashCode() const OVERRIDE {
5607 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5608 }
5609
Calin Juravle52c48962014-12-16 17:02:57 +00005610 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005611 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5612 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005613 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005614
5615 DECLARE_INSTRUCTION(StaticFieldGet);
5616
5617 private:
5618 const FieldInfo field_info_;
5619
5620 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5621};
5622
5623class HStaticFieldSet : public HTemplateInstruction<2> {
5624 public:
5625 HStaticFieldSet(HInstruction* cls,
5626 HInstruction* value,
5627 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005628 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005629 bool is_volatile,
5630 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005631 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005632 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005633 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005634 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005635 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5636 dex_pc),
5637 field_info_(field_offset,
5638 field_type,
5639 is_volatile,
5640 field_idx,
5641 declaring_class_def_index,
5642 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005643 dex_cache) {
5644 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005645 SetRawInputAt(0, cls);
5646 SetRawInputAt(1, value);
5647 }
5648
Calin Juravle52c48962014-12-16 17:02:57 +00005649 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005650 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5651 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005652 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005653
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005654 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005655 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5656 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005657
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005658 DECLARE_INSTRUCTION(StaticFieldSet);
5659
5660 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005661 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5662 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
5663 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
5664 "Too many packed fields.");
5665
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005666 const FieldInfo field_info_;
5667
5668 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5669};
5670
Calin Juravlee460d1d2015-09-29 04:52:17 +01005671class HUnresolvedInstanceFieldGet : public HExpression<1> {
5672 public:
5673 HUnresolvedInstanceFieldGet(HInstruction* obj,
5674 Primitive::Type field_type,
5675 uint32_t field_index,
5676 uint32_t dex_pc)
5677 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5678 field_index_(field_index) {
5679 SetRawInputAt(0, obj);
5680 }
5681
5682 bool NeedsEnvironment() const OVERRIDE { return true; }
5683 bool CanThrow() const OVERRIDE { return true; }
5684
5685 Primitive::Type GetFieldType() const { return GetType(); }
5686 uint32_t GetFieldIndex() const { return field_index_; }
5687
5688 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5689
5690 private:
5691 const uint32_t field_index_;
5692
5693 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5694};
5695
5696class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5697 public:
5698 HUnresolvedInstanceFieldSet(HInstruction* obj,
5699 HInstruction* value,
5700 Primitive::Type field_type,
5701 uint32_t field_index,
5702 uint32_t dex_pc)
5703 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005704 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005705 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005706 DCHECK_EQ(field_type, value->GetType());
5707 SetRawInputAt(0, obj);
5708 SetRawInputAt(1, value);
5709 }
5710
5711 bool NeedsEnvironment() const OVERRIDE { return true; }
5712 bool CanThrow() const OVERRIDE { return true; }
5713
Vladimir Markoa1de9182016-02-25 11:37:38 +00005714 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005715 uint32_t GetFieldIndex() const { return field_index_; }
5716
5717 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5718
5719 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005720 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5721 static constexpr size_t kFieldFieldTypeSize =
5722 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5723 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5724 kFieldFieldType + kFieldFieldTypeSize;
5725 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5726 "Too many packed fields.");
5727 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5728
Calin Juravlee460d1d2015-09-29 04:52:17 +01005729 const uint32_t field_index_;
5730
5731 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5732};
5733
5734class HUnresolvedStaticFieldGet : public HExpression<0> {
5735 public:
5736 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5737 uint32_t field_index,
5738 uint32_t dex_pc)
5739 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5740 field_index_(field_index) {
5741 }
5742
5743 bool NeedsEnvironment() const OVERRIDE { return true; }
5744 bool CanThrow() const OVERRIDE { return true; }
5745
5746 Primitive::Type GetFieldType() const { return GetType(); }
5747 uint32_t GetFieldIndex() const { return field_index_; }
5748
5749 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5750
5751 private:
5752 const uint32_t field_index_;
5753
5754 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5755};
5756
5757class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5758 public:
5759 HUnresolvedStaticFieldSet(HInstruction* value,
5760 Primitive::Type field_type,
5761 uint32_t field_index,
5762 uint32_t dex_pc)
5763 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01005764 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005765 SetPackedField<FieldTypeField>(field_type);
Calin Juravlee460d1d2015-09-29 04:52:17 +01005766 DCHECK_EQ(field_type, value->GetType());
5767 SetRawInputAt(0, value);
5768 }
5769
5770 bool NeedsEnvironment() const OVERRIDE { return true; }
5771 bool CanThrow() const OVERRIDE { return true; }
5772
Vladimir Markoa1de9182016-02-25 11:37:38 +00005773 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01005774 uint32_t GetFieldIndex() const { return field_index_; }
5775
5776 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5777
5778 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005779 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
5780 static constexpr size_t kFieldFieldTypeSize =
5781 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5782 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
5783 kFieldFieldType + kFieldFieldTypeSize;
5784 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5785 "Too many packed fields.");
5786 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
5787
Calin Juravlee460d1d2015-09-29 04:52:17 +01005788 const uint32_t field_index_;
5789
5790 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5791};
5792
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005793// Implement the move-exception DEX instruction.
5794class HLoadException : public HExpression<0> {
5795 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005796 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5797 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005798
David Brazdilbbd733e2015-08-18 17:48:17 +01005799 bool CanBeNull() const OVERRIDE { return false; }
5800
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005801 DECLARE_INSTRUCTION(LoadException);
5802
5803 private:
5804 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5805};
5806
David Brazdilcb1c0552015-08-04 16:22:25 +01005807// Implicit part of move-exception which clears thread-local exception storage.
5808// Must not be removed because the runtime expects the TLS to get cleared.
5809class HClearException : public HTemplateInstruction<0> {
5810 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005811 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5812 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005813
5814 DECLARE_INSTRUCTION(ClearException);
5815
5816 private:
5817 DISALLOW_COPY_AND_ASSIGN(HClearException);
5818};
5819
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005820class HThrow : public HTemplateInstruction<1> {
5821 public:
5822 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005823 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005824 SetRawInputAt(0, exception);
5825 }
5826
5827 bool IsControlFlow() const OVERRIDE { return true; }
5828
5829 bool NeedsEnvironment() const OVERRIDE { return true; }
5830
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005831 bool CanThrow() const OVERRIDE { return true; }
5832
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005833
5834 DECLARE_INSTRUCTION(Throw);
5835
5836 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005837 DISALLOW_COPY_AND_ASSIGN(HThrow);
5838};
5839
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005840/**
5841 * Implementation strategies for the code generator of a HInstanceOf
5842 * or `HCheckCast`.
5843 */
5844enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005845 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005846 kExactCheck, // Can do a single class compare.
5847 kClassHierarchyCheck, // Can just walk the super class chain.
5848 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5849 kInterfaceCheck, // No optimization yet when checking against an interface.
5850 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005851 kArrayCheck, // No optimization yet when checking against a generic array.
5852 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005853};
5854
Roland Levillain86503782016-02-11 19:07:30 +00005855std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5856
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005857class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005858 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005859 HInstanceOf(HInstruction* object,
5860 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005861 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005862 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005863 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005864 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005865 dex_pc) {
5866 SetPackedField<TypeCheckKindField>(check_kind);
5867 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005868 SetRawInputAt(0, object);
5869 SetRawInputAt(1, constant);
5870 }
5871
5872 bool CanBeMoved() const OVERRIDE { return true; }
5873
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005874 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005875 return true;
5876 }
5877
5878 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005879 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005880 }
5881
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005882 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005883 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5884 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5885 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5886 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005887
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005888 static bool CanCallRuntime(TypeCheckKind check_kind) {
5889 // Mips currently does runtime calls for any other checks.
5890 return check_kind != TypeCheckKind::kExactCheck;
5891 }
5892
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005893 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005894 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005895 }
5896
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005897 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005898
5899 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005900 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
5901 static constexpr size_t kFieldTypeCheckKindSize =
5902 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
5903 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
5904 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
5905 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5906 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005907
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005908 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5909};
5910
Calin Juravleb1498f62015-02-16 13:13:29 +00005911class HBoundType : public HExpression<1> {
5912 public:
David Brazdilf5552582015-12-27 13:36:12 +00005913 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005914 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005915 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
5916 SetPackedFlag<kFlagUpperCanBeNull>(true);
5917 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00005918 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005919 SetRawInputAt(0, input);
5920 }
5921
David Brazdilf5552582015-12-27 13:36:12 +00005922 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005923 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005924 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00005925 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005926
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005927 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005928 DCHECK(GetUpperCanBeNull() || !can_be_null);
5929 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005930 }
5931
Vladimir Markoa1de9182016-02-25 11:37:38 +00005932 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005933
Calin Juravleb1498f62015-02-16 13:13:29 +00005934 DECLARE_INSTRUCTION(BoundType);
5935
5936 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005937 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5938 // is false then CanBeNull() cannot be true).
5939 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
5940 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
5941 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
5942 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5943
Calin Juravleb1498f62015-02-16 13:13:29 +00005944 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005945 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5946 // It is used to bound the type in cases like:
5947 // if (x instanceof ClassX) {
5948 // // uper_bound_ will be ClassX
5949 // }
David Brazdilf5552582015-12-27 13:36:12 +00005950 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005951
5952 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5953};
5954
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005955class HCheckCast : public HTemplateInstruction<2> {
5956 public:
5957 HCheckCast(HInstruction* object,
5958 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005959 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005960 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00005961 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
5962 SetPackedField<TypeCheckKindField>(check_kind);
5963 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005964 SetRawInputAt(0, object);
5965 SetRawInputAt(1, constant);
5966 }
5967
5968 bool CanBeMoved() const OVERRIDE { return true; }
5969
5970 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5971 return true;
5972 }
5973
5974 bool NeedsEnvironment() const OVERRIDE {
5975 // Instruction may throw a CheckCastError.
5976 return true;
5977 }
5978
5979 bool CanThrow() const OVERRIDE { return true; }
5980
Vladimir Markoa1de9182016-02-25 11:37:38 +00005981 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
5982 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
5983 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
5984 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005985
5986 DECLARE_INSTRUCTION(CheckCast);
5987
5988 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005989 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
5990 static constexpr size_t kFieldTypeCheckKindSize =
5991 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
5992 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
5993 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
5994 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5995 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005996
5997 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005998};
5999
Calin Juravle27df7582015-04-17 19:12:31 +01006000class HMemoryBarrier : public HTemplateInstruction<0> {
6001 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006002 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006003 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006004 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6005 SetPackedField<BarrierKindField>(barrier_kind);
6006 }
Calin Juravle27df7582015-04-17 19:12:31 +01006007
Vladimir Markoa1de9182016-02-25 11:37:38 +00006008 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006009
6010 DECLARE_INSTRUCTION(MemoryBarrier);
6011
6012 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006013 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6014 static constexpr size_t kFieldBarrierKindSize =
6015 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6016 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6017 kFieldBarrierKind + kFieldBarrierKindSize;
6018 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6019 "Too many packed fields.");
6020 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006021
6022 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6023};
6024
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006025class HMonitorOperation : public HTemplateInstruction<1> {
6026 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006027 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006028 kEnter,
6029 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006030 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006031 };
6032
6033 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006034 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006035 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6036 dex_pc) {
6037 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006038 SetRawInputAt(0, object);
6039 }
6040
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006041 // Instruction may go into runtime, so we need an environment.
6042 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006043
6044 bool CanThrow() const OVERRIDE {
6045 // Verifier guarantees that monitor-exit cannot throw.
6046 // This is important because it allows the HGraphBuilder to remove
6047 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6048 return IsEnter();
6049 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006050
Vladimir Markoa1de9182016-02-25 11:37:38 +00006051 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6052 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006053
6054 DECLARE_INSTRUCTION(MonitorOperation);
6055
Calin Juravle52c48962014-12-16 17:02:57 +00006056 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006057 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6058 static constexpr size_t kFieldOperationKindSize =
6059 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6060 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6061 kFieldOperationKind + kFieldOperationKindSize;
6062 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6063 "Too many packed fields.");
6064 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006065
6066 private:
6067 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6068};
6069
David Brazdil74eb1b22015-12-14 11:44:01 +00006070class HSelect : public HExpression<3> {
6071 public:
6072 HSelect(HInstruction* condition,
6073 HInstruction* true_value,
6074 HInstruction* false_value,
6075 uint32_t dex_pc)
6076 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6077 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6078
6079 // First input must be `true_value` or `false_value` to allow codegens to
6080 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6081 // that architectures which implement HSelect as a conditional move also
6082 // will not need to invert the condition.
6083 SetRawInputAt(0, false_value);
6084 SetRawInputAt(1, true_value);
6085 SetRawInputAt(2, condition);
6086 }
6087
6088 HInstruction* GetFalseValue() const { return InputAt(0); }
6089 HInstruction* GetTrueValue() const { return InputAt(1); }
6090 HInstruction* GetCondition() const { return InputAt(2); }
6091
6092 bool CanBeMoved() const OVERRIDE { return true; }
6093 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
6094
6095 bool CanBeNull() const OVERRIDE {
6096 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6097 }
6098
6099 DECLARE_INSTRUCTION(Select);
6100
6101 private:
6102 DISALLOW_COPY_AND_ASSIGN(HSelect);
6103};
6104
Vladimir Markof9f64412015-09-02 14:05:49 +01006105class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006106 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006107 MoveOperands(Location source,
6108 Location destination,
6109 Primitive::Type type,
6110 HInstruction* instruction)
6111 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006112
6113 Location GetSource() const { return source_; }
6114 Location GetDestination() const { return destination_; }
6115
6116 void SetSource(Location value) { source_ = value; }
6117 void SetDestination(Location value) { destination_ = value; }
6118
6119 // The parallel move resolver marks moves as "in-progress" by clearing the
6120 // destination (but not the source).
6121 Location MarkPending() {
6122 DCHECK(!IsPending());
6123 Location dest = destination_;
6124 destination_ = Location::NoLocation();
6125 return dest;
6126 }
6127
6128 void ClearPending(Location dest) {
6129 DCHECK(IsPending());
6130 destination_ = dest;
6131 }
6132
6133 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006134 DCHECK(source_.IsValid() || destination_.IsInvalid());
6135 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006136 }
6137
6138 // True if this blocks a move from the given location.
6139 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006140 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006141 }
6142
6143 // A move is redundant if it's been eliminated, if its source and
6144 // destination are the same, or if its destination is unneeded.
6145 bool IsRedundant() const {
6146 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6147 }
6148
6149 // We clear both operands to indicate move that's been eliminated.
6150 void Eliminate() {
6151 source_ = destination_ = Location::NoLocation();
6152 }
6153
6154 bool IsEliminated() const {
6155 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6156 return source_.IsInvalid();
6157 }
6158
Alexey Frunze4dda3372015-06-01 18:31:49 -07006159 Primitive::Type GetType() const { return type_; }
6160
Nicolas Geoffray90218252015-04-15 11:56:51 +01006161 bool Is64BitMove() const {
6162 return Primitive::Is64BitType(type_);
6163 }
6164
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006165 HInstruction* GetInstruction() const { return instruction_; }
6166
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006167 private:
6168 Location source_;
6169 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006170 // The type this move is for.
6171 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006172 // The instruction this move is assocatied with. Null when this move is
6173 // for moving an input in the expected locations of user (including a phi user).
6174 // This is only used in debug mode, to ensure we do not connect interval siblings
6175 // in the same parallel move.
6176 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006177};
6178
Roland Levillainc9285912015-12-18 10:38:42 +00006179std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6180
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006181static constexpr size_t kDefaultNumberOfMoves = 4;
6182
6183class HParallelMove : public HTemplateInstruction<0> {
6184 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006185 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006186 : HTemplateInstruction(SideEffects::None(), dex_pc),
6187 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6188 moves_.reserve(kDefaultNumberOfMoves);
6189 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006190
Nicolas Geoffray90218252015-04-15 11:56:51 +01006191 void AddMove(Location source,
6192 Location destination,
6193 Primitive::Type type,
6194 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006195 DCHECK(source.IsValid());
6196 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006197 if (kIsDebugBuild) {
6198 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006199 for (const MoveOperands& move : moves_) {
6200 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006201 // Special case the situation where the move is for the spill slot
6202 // of the instruction.
6203 if ((GetPrevious() == instruction)
6204 || ((GetPrevious() == nullptr)
6205 && instruction->IsPhi()
6206 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006207 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006208 << "Doing parallel moves for the same instruction.";
6209 } else {
6210 DCHECK(false) << "Doing parallel moves for the same instruction.";
6211 }
6212 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006213 }
6214 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006215 for (const MoveOperands& move : moves_) {
6216 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006217 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006218 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006219 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006220 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006221 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006222 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006223 }
6224
Vladimir Marko225b6462015-09-28 12:17:40 +01006225 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006226 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006227 }
6228
Vladimir Marko225b6462015-09-28 12:17:40 +01006229 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006230
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006231 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006232
6233 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006234 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006235
6236 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6237};
6238
Mark Mendell0616ae02015-04-17 12:49:27 -04006239} // namespace art
6240
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006241#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6242#include "nodes_shared.h"
6243#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006244#ifdef ART_ENABLE_CODEGEN_arm
6245#include "nodes_arm.h"
6246#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006247#ifdef ART_ENABLE_CODEGEN_arm64
6248#include "nodes_arm64.h"
6249#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006250#ifdef ART_ENABLE_CODEGEN_x86
6251#include "nodes_x86.h"
6252#endif
6253
6254namespace art {
6255
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006256class HGraphVisitor : public ValueObject {
6257 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006258 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6259 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006260
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006261 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006262 virtual void VisitBasicBlock(HBasicBlock* block);
6263
Roland Levillain633021e2014-10-01 14:12:25 +01006264 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006265 void VisitInsertionOrder();
6266
Roland Levillain633021e2014-10-01 14:12:25 +01006267 // Visit the graph following dominator tree reverse post-order.
6268 void VisitReversePostOrder();
6269
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006270 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006271
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006272 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006273#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006274 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6275
6276 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6277
6278#undef DECLARE_VISIT_INSTRUCTION
6279
6280 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006281 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006282
6283 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6284};
6285
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006286class HGraphDelegateVisitor : public HGraphVisitor {
6287 public:
6288 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6289 virtual ~HGraphDelegateVisitor() {}
6290
6291 // Visit functions that delegate to to super class.
6292#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006293 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006294
6295 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6296
6297#undef DECLARE_VISIT_INSTRUCTION
6298
6299 private:
6300 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6301};
6302
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006303class HInsertionOrderIterator : public ValueObject {
6304 public:
6305 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6306
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006307 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006308 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006309 void Advance() { ++index_; }
6310
6311 private:
6312 const HGraph& graph_;
6313 size_t index_;
6314
6315 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6316};
6317
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006318class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006319 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006320 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6321 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006322 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006323 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006324
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006325 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6326 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006327 void Advance() { ++index_; }
6328
6329 private:
6330 const HGraph& graph_;
6331 size_t index_;
6332
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006333 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006334};
6335
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006336class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006337 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006338 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006339 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006340 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006341 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006342 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006343
6344 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006345 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006346 void Advance() { --index_; }
6347
6348 private:
6349 const HGraph& graph_;
6350 size_t index_;
6351
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006352 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006353};
6354
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006355class HLinearPostOrderIterator : public ValueObject {
6356 public:
6357 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006358 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006359
6360 bool Done() const { return index_ == 0; }
6361
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006362 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006363
6364 void Advance() {
6365 --index_;
6366 DCHECK_GE(index_, 0U);
6367 }
6368
6369 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006370 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006371 size_t index_;
6372
6373 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6374};
6375
6376class HLinearOrderIterator : public ValueObject {
6377 public:
6378 explicit HLinearOrderIterator(const HGraph& graph)
6379 : order_(graph.GetLinearOrder()), index_(0) {}
6380
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006381 bool Done() const { return index_ == order_.size(); }
6382 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006383 void Advance() { ++index_; }
6384
6385 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006386 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006387 size_t index_;
6388
6389 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6390};
6391
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006392// Iterator over the blocks that art part of the loop. Includes blocks part
6393// of an inner loop. The order in which the blocks are iterated is on their
6394// block id.
6395class HBlocksInLoopIterator : public ValueObject {
6396 public:
6397 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6398 : blocks_in_loop_(info.GetBlocks()),
6399 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6400 index_(0) {
6401 if (!blocks_in_loop_.IsBitSet(index_)) {
6402 Advance();
6403 }
6404 }
6405
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006406 bool Done() const { return index_ == blocks_.size(); }
6407 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006408 void Advance() {
6409 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006410 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006411 if (blocks_in_loop_.IsBitSet(index_)) {
6412 break;
6413 }
6414 }
6415 }
6416
6417 private:
6418 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006419 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006420 size_t index_;
6421
6422 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6423};
6424
Mingyao Yang3584bce2015-05-19 16:01:59 -07006425// Iterator over the blocks that art part of the loop. Includes blocks part
6426// of an inner loop. The order in which the blocks are iterated is reverse
6427// post order.
6428class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6429 public:
6430 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6431 : blocks_in_loop_(info.GetBlocks()),
6432 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6433 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006434 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006435 Advance();
6436 }
6437 }
6438
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006439 bool Done() const { return index_ == blocks_.size(); }
6440 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006441 void Advance() {
6442 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006443 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6444 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006445 break;
6446 }
6447 }
6448 }
6449
6450 private:
6451 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006452 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006453 size_t index_;
6454
6455 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6456};
6457
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006458inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006459 if (constant->IsIntConstant()) {
6460 return constant->AsIntConstant()->GetValue();
6461 } else if (constant->IsLongConstant()) {
6462 return constant->AsLongConstant()->GetValue();
6463 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006464 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006465 return 0;
6466 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006467}
6468
Vladimir Marko58155012015-08-19 12:49:41 +00006469inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6470 // For the purposes of the compiler, the dex files must actually be the same object
6471 // if we want to safely treat them as the same. This is especially important for JIT
6472 // as custom class loaders can open the same underlying file (or memory) multiple
6473 // times and provide different class resolution but no two class loaders should ever
6474 // use the same DexFile object - doing so is an unsupported hack that can lead to
6475 // all sorts of weird failures.
6476 return &lhs == &rhs;
6477}
6478
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006479#define INSTRUCTION_TYPE_CHECK(type, super) \
6480 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6481 inline const H##type* HInstruction::As##type() const { \
6482 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6483 } \
6484 inline H##type* HInstruction::As##type() { \
6485 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6486 }
6487
6488 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6489#undef INSTRUCTION_TYPE_CHECK
6490
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00006491class SwitchTable : public ValueObject {
6492 public:
6493 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
6494 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
6495 int32_t table_offset = instruction.VRegB_31t();
6496 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
6497 if (sparse) {
6498 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
6499 } else {
6500 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
6501 }
6502 num_entries_ = table[1];
6503 values_ = reinterpret_cast<const int32_t*>(&table[2]);
6504 }
6505
6506 uint16_t GetNumEntries() const {
6507 return num_entries_;
6508 }
6509
6510 void CheckIndex(size_t index) const {
6511 if (sparse_) {
6512 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6513 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
6514 } else {
6515 // In a packed table, we have the starting key and num_entries_ values.
6516 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
6517 }
6518 }
6519
6520 int32_t GetEntryAt(size_t index) const {
6521 CheckIndex(index);
6522 return values_[index];
6523 }
6524
6525 uint32_t GetDexPcForIndex(size_t index) const {
6526 CheckIndex(index);
6527 return dex_pc_ +
6528 (reinterpret_cast<const int16_t*>(values_ + index) -
6529 reinterpret_cast<const int16_t*>(&instruction_));
6530 }
6531
6532 // Index of the first value in the table.
6533 size_t GetFirstValueIndex() const {
6534 if (sparse_) {
6535 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6536 return num_entries_;
6537 } else {
6538 // In a packed table, we have the starting key and num_entries_ values.
6539 return 1;
6540 }
6541 }
6542
6543 private:
6544 const Instruction& instruction_;
6545 const uint32_t dex_pc_;
6546
6547 // Whether this is a sparse-switch table (or a packed-switch one).
6548 const bool sparse_;
6549
6550 // This can't be const as it needs to be computed off of the given instruction, and complicated
6551 // expressions in the initializer list seemed very ugly.
6552 uint16_t num_entries_;
6553
6554 const int32_t* values_;
6555
6556 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
6557};
6558
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006559// Create space in `blocks` for adding `number_of_new_blocks` entries
6560// starting at location `at`. Blocks after `at` are moved accordingly.
6561inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6562 size_t number_of_new_blocks,
6563 size_t after) {
6564 DCHECK_LT(after, blocks->size());
6565 size_t old_size = blocks->size();
6566 size_t new_size = old_size + number_of_new_blocks;
6567 blocks->resize(new_size);
6568 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6569}
6570
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006571} // namespace art
6572
6573#endif // ART_COMPILER_OPTIMIZING_NODES_H_