blob: 4de3f8abb50d72a7f6b1e0e35bb91f40d8591d8e [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"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000029#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "handle.h"
31#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "primitive.h"
David Brazdild26a4112015-11-10 11:07:31 +000038#include "utils/array_ref.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000039
40namespace art {
41
David Brazdil1abb4192015-02-17 18:33:36 +000042class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010046class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000048class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000051class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000052class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000054class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010056class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010057class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000059class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010060class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062
Mathieu Chartier736b5602015-09-02 14:54:11 -070063namespace mirror {
64class DexCache;
65} // namespace mirror
66
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067static const int kDefaultNumberOfBlocks = 8;
68static const int kDefaultNumberOfSuccessors = 2;
69static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010070static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010071static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000072static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Calin Juravle9aec02f2014-11-18 23:06:35 +000074static constexpr uint32_t kMaxIntShiftValue = 0x1f;
75static constexpr uint64_t kMaxLongShiftValue = 0x3f;
76
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010077static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070078static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010079
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010080static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
81
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060082static constexpr uint32_t kNoDexPc = -1;
83
Dave Allison20dfc792014-06-16 20:44:29 -070084enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070085 // All types.
86 kCondEQ, // ==
87 kCondNE, // !=
88 // Signed integers and floating-point numbers.
89 kCondLT, // <
90 kCondLE, // <=
91 kCondGT, // >
92 kCondGE, // >=
93 // Unsigned integers.
94 kCondB, // <
95 kCondBE, // <=
96 kCondA, // >
97 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -070098};
99
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000100enum GraphAnalysisResult {
101 kAnalysisFailThrowCatchLoop,
102 kAnalysisFailAmbiguousArrayOp,
103 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000104};
105
Vladimir Markof9f64412015-09-02 14:05:49 +0100106class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100107 public:
108 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
109
110 void AddInstruction(HInstruction* instruction);
111 void RemoveInstruction(HInstruction* instruction);
112
David Brazdilc3d743f2015-04-22 13:40:50 +0100113 // Insert `instruction` before/after an existing instruction `cursor`.
114 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
115 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
116
Roland Levillain6b469232014-09-25 10:10:38 +0100117 // Return true if this list contains `instruction`.
118 bool Contains(HInstruction* instruction) const;
119
Roland Levillainccc07a92014-09-16 14:48:16 +0100120 // Return true if `instruction1` is found before `instruction2` in
121 // this instruction list and false otherwise. Abort if none
122 // of these instructions is found.
123 bool FoundBefore(const HInstruction* instruction1,
124 const HInstruction* instruction2) const;
125
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000126 bool IsEmpty() const { return first_instruction_ == nullptr; }
127 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
128
129 // Update the block of all instructions to be `block`.
130 void SetBlockOfInstructions(HBasicBlock* block) const;
131
132 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
133 void Add(const HInstructionList& instruction_list);
134
David Brazdil2d7352b2015-04-20 14:52:42 +0100135 // Return the number of instructions in the list. This is an expensive operation.
136 size_t CountSize() const;
137
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100138 private:
139 HInstruction* first_instruction_;
140 HInstruction* last_instruction_;
141
142 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000143 friend class HGraph;
144 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100145 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100146 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100147
148 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
149};
150
David Brazdil4833f5a2015-12-16 10:37:39 +0000151class ReferenceTypeInfo : ValueObject {
152 public:
153 typedef Handle<mirror::Class> TypeHandle;
154
155 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
156 // The constructor will check that the type_handle is valid.
157 return ReferenceTypeInfo(type_handle, is_exact);
158 }
159
160 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
161
Vladimir Markof39745e2016-01-26 12:16:55 +0000162 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000163 return handle.GetReference() != nullptr;
164 }
165
166 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
167 return IsValidHandle(type_handle_);
168 }
169
170 bool IsExact() const { return is_exact_; }
171
172 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
173 DCHECK(IsValid());
174 return GetTypeHandle()->IsObjectClass();
175 }
176
177 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
178 DCHECK(IsValid());
179 return GetTypeHandle()->IsStringClass();
180 }
181
182 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
183 DCHECK(IsValid());
184 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
185 }
186
187 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
188 DCHECK(IsValid());
189 return GetTypeHandle()->IsInterface();
190 }
191
192 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
193 DCHECK(IsValid());
194 return GetTypeHandle()->IsArrayClass();
195 }
196
197 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
198 DCHECK(IsValid());
199 return GetTypeHandle()->IsPrimitiveArray();
200 }
201
202 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
203 DCHECK(IsValid());
204 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
205 }
206
207 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
208 DCHECK(IsValid());
209 if (!IsExact()) return false;
210 if (!IsArrayClass()) return false;
211 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
212 }
213
214 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
215 DCHECK(IsValid());
216 if (!IsExact()) return false;
217 if (!IsArrayClass()) return false;
218 if (!rti.IsArrayClass()) return false;
219 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
220 rti.GetTypeHandle()->GetComponentType());
221 }
222
223 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
224
225 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
226 DCHECK(IsValid());
227 DCHECK(rti.IsValid());
228 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
229 }
230
231 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
232 DCHECK(IsValid());
233 DCHECK(rti.IsValid());
234 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
235 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
236 }
237
238 // Returns true if the type information provide the same amount of details.
239 // Note that it does not mean that the instructions have the same actual type
240 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000241 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000242 if (!IsValid() && !rti.IsValid()) {
243 // Invalid types are equal.
244 return true;
245 }
246 if (!IsValid() || !rti.IsValid()) {
247 // One is valid, the other not.
248 return false;
249 }
250 return IsExact() == rti.IsExact()
251 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
252 }
253
254 private:
255 ReferenceTypeInfo();
256 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
257
258 // The class of the object.
259 TypeHandle type_handle_;
260 // Whether or not the type is exact or a superclass of the actual type.
261 // Whether or not we have any information about this type.
262 bool is_exact_;
263};
264
265std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
266
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000267// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100268class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000269 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100270 HGraph(ArenaAllocator* arena,
271 const DexFile& dex_file,
272 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100273 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700274 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100275 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100276 bool debuggable = false,
277 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000278 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100279 blocks_(arena->Adapter(kArenaAllocBlockList)),
280 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
281 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700282 entry_block_(nullptr),
283 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100284 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100285 number_of_vregs_(0),
286 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000287 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400288 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000289 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000290 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000291 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000292 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100293 dex_file_(dex_file),
294 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100295 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100296 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100297 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700298 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000299 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100300 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
301 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
302 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
303 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000304 cached_current_method_(nullptr),
305 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100306 blocks_.reserve(kDefaultNumberOfBlocks);
307 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000308
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000309 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100310 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
311
David Brazdil69ba7b72015-06-23 18:27:30 +0100312 bool IsInSsaForm() const { return in_ssa_form_; }
313
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000314 HBasicBlock* GetEntryBlock() const { return entry_block_; }
315 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100316 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000317
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000318 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
319 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000320
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000321 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100322
David Brazdil4833f5a2015-12-16 10:37:39 +0000323 // Try building the SSA form of this graph, with dominance computation and
324 // loop recognition. Returns a code specifying that it was successful or the
325 // reason for failure.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000326 GraphAnalysisResult TryBuildingSsa(StackHandleScopeCollection* handles);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000327
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100328 void ComputeDominanceInformation();
329 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000330 void ClearLoopInformation();
331 void FindBackEdges(ArenaBitVector* visited);
332 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100333 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100334 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000335
David Brazdil4833f5a2015-12-16 10:37:39 +0000336 // Analyze all natural loops in this graph. Returns a code specifying that it
337 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000338 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000339 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100340
David Brazdilffee3d32015-07-06 11:48:53 +0100341 // Iterate over blocks to compute try block membership. Needs reverse post
342 // order and loop information.
343 void ComputeTryBlockInformation();
344
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000345 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000346 // Returns the instruction used to replace the invoke expression or null if the
347 // invoke is for a void method.
348 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000349
Mingyao Yang3584bce2015-05-19 16:01:59 -0700350 // Need to add a couple of blocks to test if the loop body is entered and
351 // put deoptimization instructions, etc.
352 void TransformLoopHeaderForBCE(HBasicBlock* header);
353
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000354 // Removes `block` from the graph. Assumes `block` has been disconnected from
355 // other blocks and has no instructions or phis.
356 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000357
David Brazdilfc6a86a2015-06-26 10:33:45 +0000358 // Splits the edge between `block` and `successor` while preserving the
359 // indices in the predecessor/successor lists. If there are multiple edges
360 // between the blocks, the lowest indices are used.
361 // Returns the new block which is empty and has the same dex pc as `successor`.
362 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
363
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100364 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
365 void SimplifyLoop(HBasicBlock* header);
366
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000367 int32_t GetNextInstructionId() {
368 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000369 return current_instruction_id_++;
370 }
371
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000372 int32_t GetCurrentInstructionId() const {
373 return current_instruction_id_;
374 }
375
376 void SetCurrentInstructionId(int32_t id) {
377 current_instruction_id_ = id;
378 }
379
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100380 uint16_t GetMaximumNumberOfOutVRegs() const {
381 return maximum_number_of_out_vregs_;
382 }
383
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000384 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
385 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100386 }
387
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100388 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
389 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
390 }
391
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000392 void UpdateTemporariesVRegSlots(size_t slots) {
393 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100394 }
395
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000396 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100397 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000398 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100399 }
400
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100401 void SetNumberOfVRegs(uint16_t number_of_vregs) {
402 number_of_vregs_ = number_of_vregs;
403 }
404
405 uint16_t GetNumberOfVRegs() const {
406 return number_of_vregs_;
407 }
408
409 void SetNumberOfInVRegs(uint16_t value) {
410 number_of_in_vregs_ = value;
411 }
412
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100413 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100414 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100415 return number_of_vregs_ - number_of_in_vregs_;
416 }
417
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100418 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100419 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100420 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100421
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100422 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100423 return linear_order_;
424 }
425
Mark Mendell1152c922015-04-24 17:06:35 -0400426 bool HasBoundsChecks() const {
427 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800428 }
429
Mark Mendell1152c922015-04-24 17:06:35 -0400430 void SetHasBoundsChecks(bool value) {
431 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800432 }
433
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100434 bool ShouldGenerateConstructorBarrier() const {
435 return should_generate_constructor_barrier_;
436 }
437
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000438 bool IsDebuggable() const { return debuggable_; }
439
David Brazdil8d5b8b22015-03-24 10:51:52 +0000440 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000441 // already, it is created and inserted into the graph. This method is only for
442 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600443 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000444
445 // TODO: This is problematic for the consistency of reference type propagation
446 // because it can be created anytime after the pass and thus it will be left
447 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600448 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000449
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600450 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
451 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000452 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600453 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
454 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000455 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600456 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
457 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000458 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600459 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
460 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000461 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000462
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100463 HCurrentMethod* GetCurrentMethod();
464
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100465 const DexFile& GetDexFile() const {
466 return dex_file_;
467 }
468
469 uint32_t GetMethodIdx() const {
470 return method_idx_;
471 }
472
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100473 InvokeType GetInvokeType() const {
474 return invoke_type_;
475 }
476
Mark Mendellc4701932015-04-10 13:18:51 -0400477 InstructionSet GetInstructionSet() const {
478 return instruction_set_;
479 }
480
David Brazdil77a48ae2015-09-15 12:34:04 +0000481 bool HasTryCatch() const { return has_try_catch_; }
482 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100483
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000484 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
485 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
486
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100487 ArtMethod* GetArtMethod() const { return art_method_; }
488 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
489
Mark Mendellf6529172015-11-17 11:16:56 -0500490 // Returns an instruction with the opposite boolean value from 'cond'.
491 // The instruction has been inserted into the graph, either as a constant, or
492 // before cursor.
493 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
494
David Brazdil2d7352b2015-04-20 14:52:42 +0100495 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000496 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100497 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000498
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000499 template <class InstructionType, typename ValueType>
500 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600501 ArenaSafeMap<ValueType, InstructionType*>* cache,
502 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000503 // Try to find an existing constant of the given value.
504 InstructionType* constant = nullptr;
505 auto cached_constant = cache->find(value);
506 if (cached_constant != cache->end()) {
507 constant = cached_constant->second;
508 }
509
510 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100511 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000512 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600513 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000514 cache->Overwrite(value, constant);
515 InsertConstant(constant);
516 }
517 return constant;
518 }
519
David Brazdil8d5b8b22015-03-24 10:51:52 +0000520 void InsertConstant(HConstant* instruction);
521
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000522 // Cache a float constant into the graph. This method should only be
523 // called by the SsaBuilder when creating "equivalent" instructions.
524 void CacheFloatConstant(HFloatConstant* constant);
525
526 // See CacheFloatConstant comment.
527 void CacheDoubleConstant(HDoubleConstant* constant);
528
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000529 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000530
531 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100532 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000533
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100534 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100535 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000536
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100537 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100538 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100539
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000540 HBasicBlock* entry_block_;
541 HBasicBlock* exit_block_;
542
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100543 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100544 uint16_t maximum_number_of_out_vregs_;
545
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100546 // The number of virtual registers in this method. Contains the parameters.
547 uint16_t number_of_vregs_;
548
549 // The number of virtual registers used by parameters of this method.
550 uint16_t number_of_in_vregs_;
551
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000552 // Number of vreg size slots that the temporaries use (used in baseline compiler).
553 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100554
Mark Mendell1152c922015-04-24 17:06:35 -0400555 // Has bounds checks. We can totally skip BCE if it's false.
556 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800557
David Brazdil77a48ae2015-09-15 12:34:04 +0000558 // Flag whether there are any try/catch blocks in the graph. We will skip
559 // try/catch-related passes if false.
560 bool has_try_catch_;
561
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000562 // Flag whether there are any irreducible loops in the graph.
563 bool has_irreducible_loops_;
564
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000565 // Indicates whether the graph should be compiled in a way that
566 // ensures full debuggability. If false, we can apply more
567 // aggressive optimizations that may limit the level of debugging.
568 const bool debuggable_;
569
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000570 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000571 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000572
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100573 // The dex file from which the method is from.
574 const DexFile& dex_file_;
575
576 // The method index in the dex file.
577 const uint32_t method_idx_;
578
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100579 // If inlined, this encodes how the callee is being invoked.
580 const InvokeType invoke_type_;
581
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100582 // Whether the graph has been transformed to SSA form. Only used
583 // in debug mode to ensure we are not using properties only valid
584 // for non-SSA form (like the number of temporaries).
585 bool in_ssa_form_;
586
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100587 const bool should_generate_constructor_barrier_;
588
Mathieu Chartiere401d142015-04-22 13:56:20 -0700589 const InstructionSet instruction_set_;
590
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000591 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000592 HNullConstant* cached_null_constant_;
593 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000594 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000595 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000596 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000597
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100598 HCurrentMethod* cached_current_method_;
599
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100600 // The ArtMethod this graph is for. Note that for AOT, it may be null,
601 // for example for methods whose declaring class could not be resolved
602 // (such as when the superclass could not be found).
603 ArtMethod* art_method_;
604
David Brazdil4833f5a2015-12-16 10:37:39 +0000605 // Keep the RTI of inexact Object to avoid having to pass stack handle
606 // collection pointer to passes which may create NullConstant.
607 ReferenceTypeInfo inexact_object_rti_;
608
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000609 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100610 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000611 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000612 DISALLOW_COPY_AND_ASSIGN(HGraph);
613};
614
Vladimir Markof9f64412015-09-02 14:05:49 +0100615class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000616 public:
617 HLoopInformation(HBasicBlock* header, HGraph* graph)
618 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100619 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000620 irreducible_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100621 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100622 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100623 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
624 back_edges_.reserve(kDefaultNumberOfBackEdges);
625 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100626
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000627 bool IsIrreducible() const { return irreducible_; }
628
629 void Dump(std::ostream& os);
630
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100631 HBasicBlock* GetHeader() const {
632 return header_;
633 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000634
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000635 void SetHeader(HBasicBlock* block) {
636 header_ = block;
637 }
638
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100639 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
640 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
641 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
642
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000643 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100644 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000645 }
646
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100647 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100648 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100649 }
650
David Brazdil46e2a392015-03-16 17:31:52 +0000651 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100652 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100653 }
654
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000655 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100656 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000657 }
658
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100659 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100660
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100661 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100662 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100663 }
664
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100665 // Returns the lifetime position of the back edge that has the
666 // greatest lifetime position.
667 size_t GetLifetimeEnd() const;
668
669 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100670 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100671 }
672
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000673 // Finds blocks that are part of this loop.
674 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100675
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100676 // Returns whether this loop information contains `block`.
677 // Note that this loop information *must* be populated before entering this function.
678 bool Contains(const HBasicBlock& block) const;
679
680 // Returns whether this loop information is an inner loop of `other`.
681 // Note that `other` *must* be populated before entering this function.
682 bool IsIn(const HLoopInformation& other) const;
683
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800684 // Returns true if instruction is not defined within this loop.
685 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700686
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100687 const ArenaBitVector& GetBlocks() const { return blocks_; }
688
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000689 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000690 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000691
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000692 void ClearAllBlocks() {
693 blocks_.ClearAllBits();
694 }
695
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000696 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100697 // Internal recursive implementation of `Populate`.
698 void PopulateRecursive(HBasicBlock* block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000699 void PopulateIrreducibleRecursive(HBasicBlock* block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100700
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000701 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100702 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000703 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100704 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100705 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000706
707 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
708};
709
David Brazdilec16f792015-08-19 15:04:01 +0100710// Stores try/catch information for basic blocks.
711// Note that HGraph is constructed so that catch blocks cannot simultaneously
712// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100713class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100714 public:
715 // Try block information constructor.
716 explicit TryCatchInformation(const HTryBoundary& try_entry)
717 : try_entry_(&try_entry),
718 catch_dex_file_(nullptr),
719 catch_type_index_(DexFile::kDexNoIndex16) {
720 DCHECK(try_entry_ != nullptr);
721 }
722
723 // Catch block information constructor.
724 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
725 : try_entry_(nullptr),
726 catch_dex_file_(&dex_file),
727 catch_type_index_(catch_type_index) {}
728
729 bool IsTryBlock() const { return try_entry_ != nullptr; }
730
731 const HTryBoundary& GetTryEntry() const {
732 DCHECK(IsTryBlock());
733 return *try_entry_;
734 }
735
736 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
737
738 bool IsCatchAllTypeIndex() const {
739 DCHECK(IsCatchBlock());
740 return catch_type_index_ == DexFile::kDexNoIndex16;
741 }
742
743 uint16_t GetCatchTypeIndex() const {
744 DCHECK(IsCatchBlock());
745 return catch_type_index_;
746 }
747
748 const DexFile& GetCatchDexFile() const {
749 DCHECK(IsCatchBlock());
750 return *catch_dex_file_;
751 }
752
753 private:
754 // One of possibly several TryBoundary instructions entering the block's try.
755 // Only set for try blocks.
756 const HTryBoundary* try_entry_;
757
758 // Exception type information. Only set for catch blocks.
759 const DexFile* catch_dex_file_;
760 const uint16_t catch_type_index_;
761};
762
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100763static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100764static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100765
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000766// A block in a method. Contains the list of instructions represented
767// as a double linked list. Each block knows its predecessors and
768// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100769
Vladimir Markof9f64412015-09-02 14:05:49 +0100770class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000771 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600772 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000773 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000774 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
775 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000776 loop_information_(nullptr),
777 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000778 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100779 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100780 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100781 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000782 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000783 try_catch_information_(nullptr) {
784 predecessors_.reserve(kDefaultNumberOfPredecessors);
785 successors_.reserve(kDefaultNumberOfSuccessors);
786 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
787 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000788
Vladimir Marko60584552015-09-03 13:35:12 +0000789 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100790 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000791 }
792
Vladimir Marko60584552015-09-03 13:35:12 +0000793 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100794 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000795 }
796
David Brazdild26a4112015-11-10 11:07:31 +0000797 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
798 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
799
Vladimir Marko60584552015-09-03 13:35:12 +0000800 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
801 return ContainsElement(successors_, block, start_from);
802 }
803
804 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100805 return dominated_blocks_;
806 }
807
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100808 bool IsEntryBlock() const {
809 return graph_->GetEntryBlock() == this;
810 }
811
812 bool IsExitBlock() const {
813 return graph_->GetExitBlock() == this;
814 }
815
David Brazdil46e2a392015-03-16 17:31:52 +0000816 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000817 bool IsSingleTryBoundary() const;
818
819 // Returns true if this block emits nothing but a jump.
820 bool IsSingleJump() const {
821 HLoopInformation* loop_info = GetLoopInformation();
822 return (IsSingleGoto() || IsSingleTryBoundary())
823 // Back edges generate a suspend check.
824 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
825 }
David Brazdil46e2a392015-03-16 17:31:52 +0000826
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000827 void AddBackEdge(HBasicBlock* back_edge) {
828 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000829 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000830 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100831 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000832 loop_information_->AddBackEdge(back_edge);
833 }
834
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000835 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000836 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000837
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100838 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000839 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600840 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000841
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000842 HBasicBlock* GetDominator() const { return dominator_; }
843 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000844 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
845
846 void RemoveDominatedBlock(HBasicBlock* block) {
847 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100848 }
Vladimir Marko60584552015-09-03 13:35:12 +0000849
850 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
851 ReplaceElement(dominated_blocks_, existing, new_block);
852 }
853
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100854 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000855
856 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100857 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000858 }
859
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100860 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
861 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100862 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100863 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100864 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
865 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000866
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000867 HInstruction* GetFirstInstructionDisregardMoves() const;
868
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000869 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000870 successors_.push_back(block);
871 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000872 }
873
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100874 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
875 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100876 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000877 new_block->predecessors_.push_back(this);
878 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000879 }
880
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000881 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
882 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000883 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000884 new_block->successors_.push_back(this);
885 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000886 }
887
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100888 // Insert `this` between `predecessor` and `successor. This method
889 // preserves the indicies, and will update the first edge found between
890 // `predecessor` and `successor`.
891 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
892 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100893 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000894 successor->predecessors_[predecessor_index] = this;
895 predecessor->successors_[successor_index] = this;
896 successors_.push_back(successor);
897 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100898 }
899
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100900 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000901 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100902 }
903
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000904 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000905 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000906 }
907
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100908 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000909 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100910 }
911
912 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000913 predecessors_.push_back(block);
914 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100915 }
916
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100917 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000918 DCHECK_EQ(predecessors_.size(), 2u);
919 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100920 }
921
David Brazdil769c9e52015-04-27 13:54:09 +0100922 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000923 DCHECK_EQ(successors_.size(), 2u);
924 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100925 }
926
David Brazdilfc6a86a2015-06-26 10:33:45 +0000927 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000928 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100929 }
930
David Brazdilfc6a86a2015-06-26 10:33:45 +0000931 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000932 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100933 }
934
David Brazdilfc6a86a2015-06-26 10:33:45 +0000935 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000936 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100937 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000938 }
939
940 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000941 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100942 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000943 }
944
945 // Returns whether the first occurrence of `predecessor` in the list of
946 // predecessors is at index `idx`.
947 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100948 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000949 return GetPredecessorIndexOf(predecessor) == idx;
950 }
951
David Brazdild7558da2015-09-22 13:04:14 +0100952 // Create a new block between this block and its predecessors. The new block
953 // is added to the graph, all predecessor edges are relinked to it and an edge
954 // is created to `this`. Returns the new empty block. Reverse post order or
955 // loop and try/catch information are not updated.
956 HBasicBlock* CreateImmediateDominator();
957
David Brazdilfc6a86a2015-06-26 10:33:45 +0000958 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100959 // created, latter block. Note that this method will add the block to the
960 // graph, create a Goto at the end of the former block and will create an edge
961 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100962 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000963 HBasicBlock* SplitBefore(HInstruction* cursor);
964
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000965 // Split the block into two blocks just after `cursor`. Returns the newly
966 // created block. Note that this method just updates raw block information,
967 // like predecessors, successors, dominators, and instruction list. It does not
968 // update the graph, reverse post order, loop information, nor make sure the
969 // blocks are consistent (for example ending with a control flow instruction).
970 HBasicBlock* SplitAfter(HInstruction* cursor);
971
David Brazdil9bc43612015-11-05 21:25:24 +0000972 // Split catch block into two blocks after the original move-exception bytecode
973 // instruction, or at the beginning if not present. Returns the newly created,
974 // latter block, or nullptr if such block could not be created (must be dead
975 // in that case). Note that this method just updates raw block information,
976 // like predecessors, successors, dominators, and instruction list. It does not
977 // update the graph, reverse post order, loop information, nor make sure the
978 // blocks are consistent (for example ending with a control flow instruction).
979 HBasicBlock* SplitCatchBlockAfterMoveException();
980
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000981 // Merge `other` at the end of `this`. Successors and dominated blocks of
982 // `other` are changed to be successors and dominated blocks of `this`. Note
983 // that this method does not update the graph, reverse post order, loop
984 // information, nor make sure the blocks are consistent (for example ending
985 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100986 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000987
988 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
989 // of `this` are moved to `other`.
990 // Note that this method does not update the graph, reverse post order, loop
991 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000992 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000993 void ReplaceWith(HBasicBlock* other);
994
David Brazdil2d7352b2015-04-20 14:52:42 +0100995 // Merge `other` at the end of `this`. This method updates loops, reverse post
996 // order, links to predecessors, successors, dominators and deletes the block
997 // from the graph. The two blocks must be successive, i.e. `this` the only
998 // predecessor of `other` and vice versa.
999 void MergeWith(HBasicBlock* other);
1000
1001 // Disconnects `this` from all its predecessors, successors and dominator,
1002 // removes it from all loops it is included in and eventually from the graph.
1003 // The block must not dominate any other block. Predecessors and successors
1004 // are safely updated.
1005 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001006
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001007 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001008 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001009 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001010 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001011 // Replace instruction `initial` with `replacement` within this block.
1012 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1013 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001014 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001015 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001016 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001017 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1018 // instruction list. With 'ensure_safety' set to true, it verifies that the
1019 // instruction is not in use and removes it from the use lists of its inputs.
1020 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1021 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001022 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001023
1024 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001025 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001026 }
1027
Roland Levillain6b879dd2014-09-22 17:13:44 +01001028 bool IsLoopPreHeaderFirstPredecessor() const {
1029 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001030 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001031 }
1032
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001033 bool IsFirstPredecessorBackEdge() const {
1034 DCHECK(IsLoopHeader());
1035 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1036 }
1037
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001038 HLoopInformation* GetLoopInformation() const {
1039 return loop_information_;
1040 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001041
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001042 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001043 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001044 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001045 void SetInLoop(HLoopInformation* info) {
1046 if (IsLoopHeader()) {
1047 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001048 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001049 loop_information_ = info;
1050 } else if (loop_information_->Contains(*info->GetHeader())) {
1051 // Block is currently part of an outer loop. Make it part of this inner loop.
1052 // Note that a non loop header having a loop information means this loop information
1053 // has already been populated
1054 loop_information_ = info;
1055 } else {
1056 // Block is part of an inner loop. Do not update the loop information.
1057 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1058 // at this point, because this method is being called while populating `info`.
1059 }
1060 }
1061
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001062 // Raw update of the loop information.
1063 void SetLoopInformation(HLoopInformation* info) {
1064 loop_information_ = info;
1065 }
1066
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001067 bool IsInLoop() const { return loop_information_ != nullptr; }
1068
David Brazdilec16f792015-08-19 15:04:01 +01001069 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1070
1071 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1072 try_catch_information_ = try_catch_information;
1073 }
1074
1075 bool IsTryBlock() const {
1076 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1077 }
1078
1079 bool IsCatchBlock() const {
1080 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1081 }
David Brazdilffee3d32015-07-06 11:48:53 +01001082
1083 // Returns the try entry that this block's successors should have. They will
1084 // be in the same try, unless the block ends in a try boundary. In that case,
1085 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001086 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001087
David Brazdild7558da2015-09-22 13:04:14 +01001088 bool HasThrowingInstructions() const;
1089
David Brazdila4b8c212015-05-07 09:59:30 +01001090 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001091 bool Dominates(HBasicBlock* block) const;
1092
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001093 size_t GetLifetimeStart() const { return lifetime_start_; }
1094 size_t GetLifetimeEnd() const { return lifetime_end_; }
1095
1096 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1097 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1098
David Brazdil8d5b8b22015-03-24 10:51:52 +00001099 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001100 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001101 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001102 bool HasSinglePhi() const;
1103
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001104 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001105 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001106 ArenaVector<HBasicBlock*> predecessors_;
1107 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001108 HInstructionList instructions_;
1109 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001110 HLoopInformation* loop_information_;
1111 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001112 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001113 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001114 // The dex program counter of the first instruction of this block.
1115 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001116 size_t lifetime_start_;
1117 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001118 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001119
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001120 friend class HGraph;
1121 friend class HInstruction;
1122
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001123 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1124};
1125
David Brazdilb2bd1c52015-03-25 11:17:37 +00001126// Iterates over the LoopInformation of all loops which contain 'block'
1127// from the innermost to the outermost.
1128class HLoopInformationOutwardIterator : public ValueObject {
1129 public:
1130 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1131 : current_(block.GetLoopInformation()) {}
1132
1133 bool Done() const { return current_ == nullptr; }
1134
1135 void Advance() {
1136 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001137 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001138 }
1139
1140 HLoopInformation* Current() const {
1141 DCHECK(!Done());
1142 return current_;
1143 }
1144
1145 private:
1146 HLoopInformation* current_;
1147
1148 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1149};
1150
Alexandre Ramesef20f712015-06-09 10:29:30 +01001151#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001152 M(Above, Condition) \
1153 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001154 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001155 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001156 M(ArrayGet, Instruction) \
1157 M(ArrayLength, Instruction) \
1158 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001159 M(Below, Condition) \
1160 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001161 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001162 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001163 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001164 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001165 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001166 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001167 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001168 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001169 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001170 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001171 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001172 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001173 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001174 M(Exit, Instruction) \
1175 M(FloatConstant, Constant) \
1176 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001177 M(GreaterThan, Condition) \
1178 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001179 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001180 M(InstanceFieldGet, Instruction) \
1181 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001182 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001183 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001184 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001185 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001186 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001187 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001188 M(LessThan, Condition) \
1189 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001190 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001191 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001192 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001193 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001194 M(Local, Instruction) \
1195 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001196 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001197 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001198 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001199 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001200 M(Neg, UnaryOperation) \
1201 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001202 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001203 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001204 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001205 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001206 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001207 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001208 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001209 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001210 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001211 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001212 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001213 M(Return, Instruction) \
1214 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001215 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001216 M(Shl, BinaryOperation) \
1217 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001218 M(StaticFieldGet, Instruction) \
1219 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001220 M(UnresolvedInstanceFieldGet, Instruction) \
1221 M(UnresolvedInstanceFieldSet, Instruction) \
1222 M(UnresolvedStaticFieldGet, Instruction) \
1223 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001224 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001225 M(StoreLocal, Instruction) \
1226 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001227 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001228 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001229 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001230 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001231 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001232 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001233 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001234
Vladimir Markob4536b72015-11-24 13:45:23 +00001235#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001236#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001237#else
1238#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1239 M(ArmDexCacheArraysBase, Instruction)
1240#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001241
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001242#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001243#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001244#else
1245#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001246 M(Arm64DataProcWithShifterOp, Instruction) \
Nicolas Geoffray6b5afdd2016-01-22 09:31:52 +00001247 M(Arm64IntermediateAddress, Instruction) \
1248 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001249#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001250
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001251#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1252
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001253#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1254
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001255#ifndef ART_ENABLE_CODEGEN_x86
1256#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1257#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001258#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1259 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001260 M(X86LoadFromConstantTable, Instruction) \
1261 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001262#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001263
1264#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1265
1266#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1267 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1268 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1269 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001270 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001271 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001272 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1273 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1274
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001275#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1276 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001277 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001278 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001279 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001280 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001281
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001282#define FOR_EACH_INSTRUCTION(M) \
1283 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1284 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1285
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001286#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001287FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1288#undef FORWARD_DECLARATION
1289
Roland Levillainccc07a92014-09-16 14:48:16 +01001290#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001291 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001292 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001293 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001294 return other->Is##type(); \
1295 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001296 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001297
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001298#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1299 bool Is##type() const { return As##type() != nullptr; } \
1300 const H##type* As##type() const { return this; } \
1301 H##type* As##type() { return this; }
1302
David Brazdiled596192015-01-23 10:39:45 +00001303template <typename T> class HUseList;
1304
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001305template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001306class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001307 public:
David Brazdiled596192015-01-23 10:39:45 +00001308 HUseListNode* GetPrevious() const { return prev_; }
1309 HUseListNode* GetNext() const { return next_; }
1310 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001311 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001312 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001313
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001314 private:
David Brazdiled596192015-01-23 10:39:45 +00001315 HUseListNode(T user, size_t index)
1316 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1317
1318 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001319 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001320 HUseListNode<T>* prev_;
1321 HUseListNode<T>* next_;
1322
1323 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001324
1325 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1326};
1327
David Brazdiled596192015-01-23 10:39:45 +00001328template <typename T>
1329class HUseList : public ValueObject {
1330 public:
1331 HUseList() : first_(nullptr) {}
1332
1333 void Clear() {
1334 first_ = nullptr;
1335 }
1336
1337 // Adds a new entry at the beginning of the use list and returns
1338 // the newly created node.
1339 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001340 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001341 if (IsEmpty()) {
1342 first_ = new_node;
1343 } else {
1344 first_->prev_ = new_node;
1345 new_node->next_ = first_;
1346 first_ = new_node;
1347 }
1348 return new_node;
1349 }
1350
1351 HUseListNode<T>* GetFirst() const {
1352 return first_;
1353 }
1354
1355 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001356 DCHECK(node != nullptr);
1357 DCHECK(Contains(node));
1358
David Brazdiled596192015-01-23 10:39:45 +00001359 if (node->prev_ != nullptr) {
1360 node->prev_->next_ = node->next_;
1361 }
1362 if (node->next_ != nullptr) {
1363 node->next_->prev_ = node->prev_;
1364 }
1365 if (node == first_) {
1366 first_ = node->next_;
1367 }
1368 }
1369
David Brazdil1abb4192015-02-17 18:33:36 +00001370 bool Contains(const HUseListNode<T>* node) const {
1371 if (node == nullptr) {
1372 return false;
1373 }
1374 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1375 if (current == node) {
1376 return true;
1377 }
1378 }
1379 return false;
1380 }
1381
David Brazdiled596192015-01-23 10:39:45 +00001382 bool IsEmpty() const {
1383 return first_ == nullptr;
1384 }
1385
1386 bool HasOnlyOneUse() const {
1387 return first_ != nullptr && first_->next_ == nullptr;
1388 }
1389
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001390 size_t SizeSlow() const {
1391 size_t count = 0;
1392 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1393 ++count;
1394 }
1395 return count;
1396 }
1397
David Brazdiled596192015-01-23 10:39:45 +00001398 private:
1399 HUseListNode<T>* first_;
1400};
1401
1402template<typename T>
1403class HUseIterator : public ValueObject {
1404 public:
1405 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1406
1407 bool Done() const { return current_ == nullptr; }
1408
1409 void Advance() {
1410 DCHECK(!Done());
1411 current_ = current_->GetNext();
1412 }
1413
1414 HUseListNode<T>* Current() const {
1415 DCHECK(!Done());
1416 return current_;
1417 }
1418
1419 private:
1420 HUseListNode<T>* current_;
1421
1422 friend class HValue;
1423};
1424
David Brazdil1abb4192015-02-17 18:33:36 +00001425// This class is used by HEnvironment and HInstruction classes to record the
1426// instructions they use and pointers to the corresponding HUseListNodes kept
1427// by the used instructions.
1428template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001429class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001430 public:
1431 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1432 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1433
1434 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1435 : instruction_(old_record.instruction_), use_node_(use_node) {
1436 DCHECK(instruction_ != nullptr);
1437 DCHECK(use_node_ != nullptr);
1438 DCHECK(old_record.use_node_ == nullptr);
1439 }
1440
1441 HInstruction* GetInstruction() const { return instruction_; }
1442 HUseListNode<T>* GetUseNode() const { return use_node_; }
1443
1444 private:
1445 // Instruction used by the user.
1446 HInstruction* instruction_;
1447
1448 // Corresponding entry in the use list kept by 'instruction_'.
1449 HUseListNode<T>* use_node_;
1450};
1451
Aart Bik854a02b2015-07-14 16:07:00 -07001452/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001453 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001454 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001455 * For write/read dependences on fields/arrays, the dependence analysis uses
1456 * type disambiguation (e.g. a float field write cannot modify the value of an
1457 * integer field read) and the access type (e.g. a reference array write cannot
1458 * modify the value of a reference field read [although it may modify the
1459 * reference fetch prior to reading the field, which is represented by its own
1460 * write/read dependence]). The analysis makes conservative points-to
1461 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1462 * the same, and any reference read depends on any reference read without
1463 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001464 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001465 * The internal representation uses 38-bit and is described in the table below.
1466 * The first line indicates the side effect, and for field/array accesses the
1467 * second line indicates the type of the access (in the order of the
1468 * Primitive::Type enum).
1469 * The two numbered lines below indicate the bit position in the bitfield (read
1470 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001471 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001472 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1473 * +-------------+---------+---------+--------------+---------+---------+
1474 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1475 * | 3 |333333322|222222221| 1 |111111110|000000000|
1476 * | 7 |654321098|765432109| 8 |765432109|876543210|
1477 *
1478 * Note that, to ease the implementation, 'changes' bits are least significant
1479 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001480 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001481class SideEffects : public ValueObject {
1482 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001483 SideEffects() : flags_(0) {}
1484
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001485 static SideEffects None() {
1486 return SideEffects(0);
1487 }
1488
1489 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001490 return SideEffects(kAllChangeBits | kAllDependOnBits);
1491 }
1492
1493 static SideEffects AllChanges() {
1494 return SideEffects(kAllChangeBits);
1495 }
1496
1497 static SideEffects AllDependencies() {
1498 return SideEffects(kAllDependOnBits);
1499 }
1500
1501 static SideEffects AllExceptGCDependency() {
1502 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1503 }
1504
1505 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001506 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001507 }
1508
Aart Bik34c3ba92015-07-20 14:08:59 -07001509 static SideEffects AllWrites() {
1510 return SideEffects(kAllWrites);
1511 }
1512
1513 static SideEffects AllReads() {
1514 return SideEffects(kAllReads);
1515 }
1516
1517 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1518 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001519 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001520 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001521 }
1522
Aart Bik854a02b2015-07-14 16:07:00 -07001523 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1524 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001525 }
1526
Aart Bik34c3ba92015-07-20 14:08:59 -07001527 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1528 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001529 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001530 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001531 }
1532
1533 static SideEffects ArrayReadOfType(Primitive::Type type) {
1534 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1535 }
1536
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001537 static SideEffects CanTriggerGC() {
1538 return SideEffects(1ULL << kCanTriggerGCBit);
1539 }
1540
1541 static SideEffects DependsOnGC() {
1542 return SideEffects(1ULL << kDependsOnGCBit);
1543 }
1544
Aart Bik854a02b2015-07-14 16:07:00 -07001545 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001546 SideEffects Union(SideEffects other) const {
1547 return SideEffects(flags_ | other.flags_);
1548 }
1549
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001550 SideEffects Exclusion(SideEffects other) const {
1551 return SideEffects(flags_ & ~other.flags_);
1552 }
1553
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001554 void Add(SideEffects other) {
1555 flags_ |= other.flags_;
1556 }
1557
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001558 bool Includes(SideEffects other) const {
1559 return (other.flags_ & flags_) == other.flags_;
1560 }
1561
1562 bool HasSideEffects() const {
1563 return (flags_ & kAllChangeBits);
1564 }
1565
1566 bool HasDependencies() const {
1567 return (flags_ & kAllDependOnBits);
1568 }
1569
1570 // Returns true if there are no side effects or dependencies.
1571 bool DoesNothing() const {
1572 return flags_ == 0;
1573 }
1574
Aart Bik854a02b2015-07-14 16:07:00 -07001575 // Returns true if something is written.
1576 bool DoesAnyWrite() const {
1577 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001578 }
1579
Aart Bik854a02b2015-07-14 16:07:00 -07001580 // Returns true if something is read.
1581 bool DoesAnyRead() const {
1582 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001583 }
1584
Aart Bik854a02b2015-07-14 16:07:00 -07001585 // Returns true if potentially everything is written and read
1586 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001587 bool DoesAllReadWrite() const {
1588 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1589 }
1590
Aart Bik854a02b2015-07-14 16:07:00 -07001591 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001592 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001593 }
1594
Roland Levillain0d5a2812015-11-13 10:07:31 +00001595 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001596 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001597 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1598 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001599 }
1600
1601 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001602 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001603 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001604 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001605 for (int s = kLastBit; s >= 0; s--) {
1606 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1607 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1608 // This is a bit for the GC side effect.
1609 if (current_bit_is_set) {
1610 flags += "GC";
1611 }
Aart Bik854a02b2015-07-14 16:07:00 -07001612 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001613 } else {
1614 // This is a bit for the array/field analysis.
1615 // The underscore character stands for the 'can trigger GC' bit.
1616 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1617 if (current_bit_is_set) {
1618 flags += kDebug[s];
1619 }
1620 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1621 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1622 flags += "|";
1623 }
1624 }
Aart Bik854a02b2015-07-14 16:07:00 -07001625 }
1626 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001627 }
1628
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001629 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001630
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001631 private:
1632 static constexpr int kFieldArrayAnalysisBits = 9;
1633
1634 static constexpr int kFieldWriteOffset = 0;
1635 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1636 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1637 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1638
1639 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1640
1641 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1642 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1643 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1644 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1645
1646 static constexpr int kLastBit = kDependsOnGCBit;
1647 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1648
1649 // Aliases.
1650
1651 static_assert(kChangeBits == kDependOnBits,
1652 "the 'change' bits should match the 'depend on' bits.");
1653
1654 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1655 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1656 static constexpr uint64_t kAllWrites =
1657 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1658 static constexpr uint64_t kAllReads =
1659 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001660
Aart Bik854a02b2015-07-14 16:07:00 -07001661 // Work around the fact that HIR aliases I/F and J/D.
1662 // TODO: remove this interceptor once HIR types are clean
1663 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1664 switch (type) {
1665 case Primitive::kPrimInt:
1666 case Primitive::kPrimFloat:
1667 return TypeFlag(Primitive::kPrimInt, offset) |
1668 TypeFlag(Primitive::kPrimFloat, offset);
1669 case Primitive::kPrimLong:
1670 case Primitive::kPrimDouble:
1671 return TypeFlag(Primitive::kPrimLong, offset) |
1672 TypeFlag(Primitive::kPrimDouble, offset);
1673 default:
1674 return TypeFlag(type, offset);
1675 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001676 }
1677
Aart Bik854a02b2015-07-14 16:07:00 -07001678 // Translates type to bit flag.
1679 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1680 CHECK_NE(type, Primitive::kPrimVoid);
1681 const uint64_t one = 1;
1682 const int shift = type; // 0-based consecutive enum
1683 DCHECK_LE(kFieldWriteOffset, shift);
1684 DCHECK_LT(shift, kArrayWriteOffset);
1685 return one << (type + offset);
1686 }
1687
1688 // Private constructor on direct flags value.
1689 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1690
1691 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001692};
1693
David Brazdiled596192015-01-23 10:39:45 +00001694// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001695class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001696 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001697 HEnvironment(ArenaAllocator* arena,
1698 size_t number_of_vregs,
1699 const DexFile& dex_file,
1700 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001701 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001702 InvokeType invoke_type,
1703 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001704 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1705 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001706 parent_(nullptr),
1707 dex_file_(dex_file),
1708 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001709 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001710 invoke_type_(invoke_type),
1711 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001712 }
1713
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001714 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001715 : HEnvironment(arena,
1716 to_copy.Size(),
1717 to_copy.GetDexFile(),
1718 to_copy.GetMethodIdx(),
1719 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001720 to_copy.GetInvokeType(),
1721 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001722
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001723 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001724 if (parent_ != nullptr) {
1725 parent_->SetAndCopyParentChain(allocator, parent);
1726 } else {
1727 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1728 parent_->CopyFrom(parent);
1729 if (parent->GetParent() != nullptr) {
1730 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1731 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001732 }
David Brazdiled596192015-01-23 10:39:45 +00001733 }
1734
Vladimir Marko71bf8092015-09-15 15:33:14 +01001735 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001736 void CopyFrom(HEnvironment* environment);
1737
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001738 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1739 // input to the loop phi instead. This is for inserting instructions that
1740 // require an environment (like HDeoptimization) in the loop pre-header.
1741 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001742
1743 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001744 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001745 }
1746
1747 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001748 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001749 }
1750
David Brazdil1abb4192015-02-17 18:33:36 +00001751 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001752
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001753 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001754
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001755 HEnvironment* GetParent() const { return parent_; }
1756
1757 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001758 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001759 }
1760
1761 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001762 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001763 }
1764
1765 uint32_t GetDexPc() const {
1766 return dex_pc_;
1767 }
1768
1769 uint32_t GetMethodIdx() const {
1770 return method_idx_;
1771 }
1772
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001773 InvokeType GetInvokeType() const {
1774 return invoke_type_;
1775 }
1776
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001777 const DexFile& GetDexFile() const {
1778 return dex_file_;
1779 }
1780
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001781 HInstruction* GetHolder() const {
1782 return holder_;
1783 }
1784
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001785
1786 bool IsFromInlinedInvoke() const {
1787 return GetParent() != nullptr;
1788 }
1789
David Brazdiled596192015-01-23 10:39:45 +00001790 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001791 // Record instructions' use entries of this environment for constant-time removal.
1792 // It should only be called by HInstruction when a new environment use is added.
1793 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1794 DCHECK(env_use->GetUser() == this);
1795 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001796 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001797 }
David Brazdiled596192015-01-23 10:39:45 +00001798
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001799 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1800 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001801 HEnvironment* parent_;
1802 const DexFile& dex_file_;
1803 const uint32_t method_idx_;
1804 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001805 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001806
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001807 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001808 HInstruction* const holder_;
1809
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001810 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001811
1812 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1813};
1814
Vladimir Markof9f64412015-09-02 14:05:49 +01001815class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001816 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001817 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001818 : previous_(nullptr),
1819 next_(nullptr),
1820 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001821 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001822 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001823 ssa_index_(-1),
David Brazdilb3e773e2016-01-26 11:28:37 +00001824 emitted_at_use_site_(false),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001825 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001826 locations_(nullptr),
1827 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001828 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001829 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001830 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001831
Dave Allison20dfc792014-06-16 20:44:29 -07001832 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001833
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001834#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001835 enum InstructionKind {
1836 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1837 };
1838#undef DECLARE_KIND
1839
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001840 HInstruction* GetNext() const { return next_; }
1841 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001842
Calin Juravle77520bc2015-01-12 18:45:46 +00001843 HInstruction* GetNextDisregardingMoves() const;
1844 HInstruction* GetPreviousDisregardingMoves() const;
1845
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001846 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001847 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001848 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001849 bool IsInBlock() const { return block_ != nullptr; }
1850 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001851 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1852 bool IsIrreducibleLoopHeaderPhi() const {
1853 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1854 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001855
Roland Levillain6b879dd2014-09-22 17:13:44 +01001856 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001857 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001858
1859 virtual void Accept(HGraphVisitor* visitor) = 0;
1860 virtual const char* DebugName() const = 0;
1861
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001862 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001863 void SetRawInputAt(size_t index, HInstruction* input) {
1864 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1865 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001866
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001867 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001868
1869 uint32_t GetDexPc() const { return dex_pc_; }
1870
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001871 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001872
Roland Levillaine161a2a2014-10-03 12:45:18 +01001873 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001874 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001875
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001876 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001877 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001878
Calin Juravle10e244f2015-01-26 18:54:32 +00001879 // Does not apply for all instructions, but having this at top level greatly
1880 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001881 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001882 virtual bool CanBeNull() const {
1883 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1884 return true;
1885 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001886
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001887 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001888 return false;
1889 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001890
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001891 virtual bool IsActualObject() const {
1892 return GetType() == Primitive::kPrimNot;
1893 }
1894
Calin Juravle2e768302015-07-28 14:41:11 +00001895 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001896
Calin Juravle61d544b2015-02-23 16:46:57 +00001897 ReferenceTypeInfo GetReferenceTypeInfo() const {
1898 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1899 return reference_type_info_;
1900 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001901
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001902 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001903 DCHECK(user != nullptr);
1904 HUseListNode<HInstruction*>* use =
1905 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1906 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001907 }
1908
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001909 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001910 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001911 HUseListNode<HEnvironment*>* env_use =
1912 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1913 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001914 }
1915
David Brazdil1abb4192015-02-17 18:33:36 +00001916 void RemoveAsUserOfInput(size_t input) {
1917 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1918 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1919 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001920
David Brazdil1abb4192015-02-17 18:33:36 +00001921 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1922 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001923
David Brazdiled596192015-01-23 10:39:45 +00001924 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1925 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001926 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001927 bool HasOnlyOneNonEnvironmentUse() const {
1928 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1929 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001930
Roland Levillain6c82d402014-10-13 16:10:27 +01001931 // Does this instruction strictly dominate `other_instruction`?
1932 // Returns false if this instruction and `other_instruction` are the same.
1933 // Aborts if this instruction and `other_instruction` are both phis.
1934 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001935
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001936 int GetId() const { return id_; }
1937 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001938
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001939 int GetSsaIndex() const { return ssa_index_; }
1940 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1941 bool HasSsaIndex() const { return ssa_index_ != -1; }
1942
1943 bool HasEnvironment() const { return environment_ != nullptr; }
1944 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001945 // Set the `environment_` field. Raw because this method does not
1946 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001947 void SetRawEnvironment(HEnvironment* environment) {
1948 DCHECK(environment_ == nullptr);
1949 DCHECK_EQ(environment->GetHolder(), this);
1950 environment_ = environment;
1951 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001952
1953 // Set the environment of this instruction, copying it from `environment`. While
1954 // copying, the uses lists are being updated.
1955 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001956 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001957 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001958 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001959 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001960 if (environment->GetParent() != nullptr) {
1961 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1962 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001963 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001964
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001965 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1966 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001967 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001968 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001969 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001970 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001971 if (environment->GetParent() != nullptr) {
1972 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1973 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001974 }
1975
Nicolas Geoffray39468442014-09-02 15:17:15 +01001976 // Returns the number of entries in the environment. Typically, that is the
1977 // number of dex registers in a method. It could be more in case of inlining.
1978 size_t EnvironmentSize() const;
1979
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001980 LocationSummary* GetLocations() const { return locations_; }
1981 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001982
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001983 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001984 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001985
Alexandre Rames188d4312015-04-09 18:30:21 +01001986 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1987 // uses of this instruction by `other` are *not* updated.
1988 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1989 ReplaceWith(other);
1990 other->ReplaceInput(this, use_index);
1991 }
1992
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001993 // Move `this` instruction before `cursor`.
1994 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001995
Vladimir Markofb337ea2015-11-25 15:25:10 +00001996 // Move `this` before its first user and out of any loops. If there is no
1997 // out-of-loop user that dominates all other users, move the instruction
1998 // to the end of the out-of-loop common dominator of the user's blocks.
1999 //
2000 // This can be used only on non-throwing instructions with no side effects that
2001 // have at least one use but no environment uses.
2002 void MoveBeforeFirstUserAndOutOfLoops();
2003
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002004#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002005 bool Is##type() const; \
2006 const H##type* As##type() const; \
2007 H##type* As##type();
2008
2009 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2010#undef INSTRUCTION_TYPE_CHECK
2011
2012#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002013 bool Is##type() const { return (As##type() != nullptr); } \
2014 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002015 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002016 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002017#undef INSTRUCTION_TYPE_CHECK
2018
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002019 // Returns whether the instruction can be moved within the graph.
2020 virtual bool CanBeMoved() const { return false; }
2021
2022 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002023 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002024 return false;
2025 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002026
2027 // Returns whether any data encoded in the two instructions is equal.
2028 // This method does not look at the inputs. Both instructions must be
2029 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002030 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002031 return false;
2032 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002033
2034 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002035 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002036 // 2) Their inputs are identical.
2037 bool Equals(HInstruction* other) const;
2038
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002039 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2040 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2041 // the virtual function because the __attribute__((__pure__)) doesn't really
2042 // apply the strong requirement for virtual functions, preventing optimizations.
2043 InstructionKind GetKind() const PURE;
2044 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002045
2046 virtual size_t ComputeHashCode() const {
2047 size_t result = GetKind();
2048 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2049 result = (result * 31) + InputAt(i)->GetId();
2050 }
2051 return result;
2052 }
2053
2054 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002055 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002056
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002057 size_t GetLifetimePosition() const { return lifetime_position_; }
2058 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2059 LiveInterval* GetLiveInterval() const { return live_interval_; }
2060 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2061 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2062
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002063 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2064
2065 // Returns whether the code generation of the instruction will require to have access
2066 // to the current method. Such instructions are:
2067 // (1): Instructions that require an environment, as calling the runtime requires
2068 // to walk the stack and have the current method stored at a specific stack address.
2069 // (2): Object literals like classes and strings, that are loaded from the dex cache
2070 // fields of the current method.
2071 bool NeedsCurrentMethod() const {
2072 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2073 }
2074
Vladimir Markodc151b22015-10-15 18:02:30 +01002075 // Returns whether the code generation of the instruction will require to have access
2076 // to the dex cache of the current method's declaring class via the current method.
2077 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002078
Mark Mendellc4701932015-04-10 13:18:51 -04002079 // Does this instruction have any use in an environment before
2080 // control flow hits 'other'?
2081 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2082
2083 // Remove all references to environment uses of this instruction.
2084 // The caller must ensure that this is safe to do.
2085 void RemoveEnvironmentUsers();
2086
David Brazdilb3e773e2016-01-26 11:28:37 +00002087 bool IsEmittedAtUseSite() const { return emitted_at_use_site_; }
2088 void MarkEmittedAtUseSite() { emitted_at_use_site_ = true; }
2089
David Brazdil1abb4192015-02-17 18:33:36 +00002090 protected:
2091 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2092 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
Aart Bik5d75afe2015-12-14 11:57:01 -08002093 void SetSideEffects(SideEffects other) { side_effects_ = other; }
David Brazdil1abb4192015-02-17 18:33:36 +00002094
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002095 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002096 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2097
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002098 HInstruction* previous_;
2099 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002100 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002101 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002102
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002103 // An instruction gets an id when it is added to the graph.
2104 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002105 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002106 int id_;
2107
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002108 // When doing liveness analysis, instructions that have uses get an SSA index.
2109 int ssa_index_;
2110
David Brazdilb3e773e2016-01-26 11:28:37 +00002111 // If set, the machine code for this instruction is assumed to be generated by
2112 // its users. Used by liveness analysis to compute use positions accordingly.
2113 bool emitted_at_use_site_;
2114
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002115 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002116 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002117
2118 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002119 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002120
Nicolas Geoffray39468442014-09-02 15:17:15 +01002121 // The environment associated with this instruction. Not null if the instruction
2122 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002123 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002124
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002125 // Set by the code generator.
2126 LocationSummary* locations_;
2127
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002128 // Set by the liveness analysis.
2129 LiveInterval* live_interval_;
2130
2131 // Set by the liveness analysis, this is the position in a linear
2132 // order of blocks where this instruction's live interval start.
2133 size_t lifetime_position_;
2134
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002135 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002136
Calin Juravleacf735c2015-02-12 15:25:22 +00002137 // TODO: for primitive types this should be marked as invalid.
2138 ReferenceTypeInfo reference_type_info_;
2139
David Brazdil1abb4192015-02-17 18:33:36 +00002140 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002141 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002142 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002143 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002144 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002145
2146 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2147};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002148std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002149
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002150class HInputIterator : public ValueObject {
2151 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002152 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002153
2154 bool Done() const { return index_ == instruction_->InputCount(); }
2155 HInstruction* Current() const { return instruction_->InputAt(index_); }
2156 void Advance() { index_++; }
2157
2158 private:
2159 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002160 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002161
2162 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2163};
2164
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002165class HInstructionIterator : public ValueObject {
2166 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002167 explicit HInstructionIterator(const HInstructionList& instructions)
2168 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002169 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002170 }
2171
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002172 bool Done() const { return instruction_ == nullptr; }
2173 HInstruction* Current() const { return instruction_; }
2174 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002175 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002176 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002177 }
2178
2179 private:
2180 HInstruction* instruction_;
2181 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002182
2183 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002184};
2185
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002186class HBackwardInstructionIterator : public ValueObject {
2187 public:
2188 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2189 : instruction_(instructions.last_instruction_) {
2190 next_ = Done() ? nullptr : instruction_->GetPrevious();
2191 }
2192
2193 bool Done() const { return instruction_ == nullptr; }
2194 HInstruction* Current() const { return instruction_; }
2195 void Advance() {
2196 instruction_ = next_;
2197 next_ = Done() ? nullptr : instruction_->GetPrevious();
2198 }
2199
2200 private:
2201 HInstruction* instruction_;
2202 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002203
2204 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002205};
2206
Vladimir Markof9f64412015-09-02 14:05:49 +01002207template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002208class HTemplateInstruction: public HInstruction {
2209 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002210 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002211 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002212 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002213
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002214 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002215
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002216 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002217 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2218 DCHECK_LT(i, N);
2219 return inputs_[i];
2220 }
David Brazdil1abb4192015-02-17 18:33:36 +00002221
2222 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002223 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002224 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002225 }
2226
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002227 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002228 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002229
2230 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002231};
2232
Vladimir Markof9f64412015-09-02 14:05:49 +01002233// HTemplateInstruction specialization for N=0.
2234template<>
2235class HTemplateInstruction<0>: public HInstruction {
2236 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002237 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002238 : HInstruction(side_effects, dex_pc) {}
2239
Vladimir Markof9f64412015-09-02 14:05:49 +01002240 virtual ~HTemplateInstruction() {}
2241
2242 size_t InputCount() const OVERRIDE { return 0; }
2243
2244 protected:
2245 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2246 LOG(FATAL) << "Unreachable";
2247 UNREACHABLE();
2248 }
2249
2250 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2251 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2252 LOG(FATAL) << "Unreachable";
2253 UNREACHABLE();
2254 }
2255
2256 private:
2257 friend class SsaBuilder;
2258};
2259
Dave Allison20dfc792014-06-16 20:44:29 -07002260template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002261class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002262 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002263 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002264 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002265 virtual ~HExpression() {}
2266
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002267 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002268
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002269 protected:
2270 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002271};
2272
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002273// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2274// instruction that branches to the exit block.
2275class HReturnVoid : public HTemplateInstruction<0> {
2276 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002277 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2278 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002279
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002280 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002281
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002282 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002283
2284 private:
2285 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2286};
2287
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002288// Represents dex's RETURN opcodes. A HReturn is a control flow
2289// instruction that branches to the exit block.
2290class HReturn : public HTemplateInstruction<1> {
2291 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002292 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2293 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002294 SetRawInputAt(0, value);
2295 }
2296
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002297 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002298
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002299 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002300
2301 private:
2302 DISALLOW_COPY_AND_ASSIGN(HReturn);
2303};
2304
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002305// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002306// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002307// exit block.
2308class HExit : public HTemplateInstruction<0> {
2309 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002310 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002311
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002312 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002313
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002314 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002315
2316 private:
2317 DISALLOW_COPY_AND_ASSIGN(HExit);
2318};
2319
2320// Jumps from one block to another.
2321class HGoto : public HTemplateInstruction<0> {
2322 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002323 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002324
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002325 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002326
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002327 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002328 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002329 }
2330
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002331 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002332
2333 private:
2334 DISALLOW_COPY_AND_ASSIGN(HGoto);
2335};
2336
Roland Levillain9867bc72015-08-05 10:21:34 +01002337class HConstant : public HExpression<0> {
2338 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002339 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2340 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002341
2342 bool CanBeMoved() const OVERRIDE { return true; }
2343
2344 virtual bool IsMinusOne() const { return false; }
2345 virtual bool IsZero() const { return false; }
2346 virtual bool IsOne() const { return false; }
2347
David Brazdil77a48ae2015-09-15 12:34:04 +00002348 virtual uint64_t GetValueAsUint64() const = 0;
2349
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002350 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002351
2352 private:
2353 DISALLOW_COPY_AND_ASSIGN(HConstant);
2354};
2355
2356class HNullConstant : public HConstant {
2357 public:
2358 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2359 return true;
2360 }
2361
David Brazdil77a48ae2015-09-15 12:34:04 +00002362 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2363
Roland Levillain9867bc72015-08-05 10:21:34 +01002364 size_t ComputeHashCode() const OVERRIDE { return 0; }
2365
2366 DECLARE_INSTRUCTION(NullConstant);
2367
2368 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002369 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002370
2371 friend class HGraph;
2372 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2373};
2374
2375// Constants of the type int. Those can be from Dex instructions, or
2376// synthesized (for example with the if-eqz instruction).
2377class HIntConstant : public HConstant {
2378 public:
2379 int32_t GetValue() const { return value_; }
2380
David Brazdil9f389d42015-10-01 14:32:56 +01002381 uint64_t GetValueAsUint64() const OVERRIDE {
2382 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2383 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002384
Roland Levillain9867bc72015-08-05 10:21:34 +01002385 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2386 DCHECK(other->IsIntConstant());
2387 return other->AsIntConstant()->value_ == value_;
2388 }
2389
2390 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2391
2392 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2393 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2394 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2395
2396 DECLARE_INSTRUCTION(IntConstant);
2397
2398 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002399 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2400 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2401 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2402 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002403
2404 const int32_t value_;
2405
2406 friend class HGraph;
2407 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2408 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2409 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2410};
2411
2412class HLongConstant : public HConstant {
2413 public:
2414 int64_t GetValue() const { return value_; }
2415
David Brazdil77a48ae2015-09-15 12:34:04 +00002416 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2417
Roland Levillain9867bc72015-08-05 10:21:34 +01002418 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2419 DCHECK(other->IsLongConstant());
2420 return other->AsLongConstant()->value_ == value_;
2421 }
2422
2423 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2424
2425 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2426 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2427 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2428
2429 DECLARE_INSTRUCTION(LongConstant);
2430
2431 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002432 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2433 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002434
2435 const int64_t value_;
2436
2437 friend class HGraph;
2438 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2439};
Dave Allison20dfc792014-06-16 20:44:29 -07002440
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002441// Conditional branch. A block ending with an HIf instruction must have
2442// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002443class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002444 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002445 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2446 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002447 SetRawInputAt(0, input);
2448 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002449
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002450 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002451
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002452 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002453 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002454 }
2455
2456 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002457 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002458 }
2459
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002460 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002461
2462 private:
2463 DISALLOW_COPY_AND_ASSIGN(HIf);
2464};
2465
David Brazdilfc6a86a2015-06-26 10:33:45 +00002466
2467// Abstract instruction which marks the beginning and/or end of a try block and
2468// links it to the respective exception handlers. Behaves the same as a Goto in
2469// non-exceptional control flow.
2470// Normal-flow successor is stored at index zero, exception handlers under
2471// higher indices in no particular order.
2472class HTryBoundary : public HTemplateInstruction<0> {
2473 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002474 enum BoundaryKind {
2475 kEntry,
2476 kExit,
2477 };
2478
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002479 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2480 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002481
2482 bool IsControlFlow() const OVERRIDE { return true; }
2483
2484 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002485 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002486
David Brazdild26a4112015-11-10 11:07:31 +00002487 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2488 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2489 }
2490
David Brazdilfc6a86a2015-06-26 10:33:45 +00002491 // Returns whether `handler` is among its exception handlers (non-zero index
2492 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002493 bool HasExceptionHandler(const HBasicBlock& handler) const {
2494 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002495 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002496 }
2497
2498 // If not present already, adds `handler` to its block's list of exception
2499 // handlers.
2500 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002501 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002502 GetBlock()->AddSuccessor(handler);
2503 }
2504 }
2505
David Brazdil56e1acc2015-06-30 15:41:36 +01002506 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002507
David Brazdilffee3d32015-07-06 11:48:53 +01002508 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2509
David Brazdilfc6a86a2015-06-26 10:33:45 +00002510 DECLARE_INSTRUCTION(TryBoundary);
2511
2512 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002513 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002514
2515 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2516};
2517
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002518// Deoptimize to interpreter, upon checking a condition.
2519class HDeoptimize : public HTemplateInstruction<1> {
2520 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002521 // We set CanTriggerGC to prevent any intermediate address to be live
2522 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002523 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002524 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002525 SetRawInputAt(0, cond);
2526 }
2527
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002528 bool CanBeMoved() const OVERRIDE { return true; }
2529 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2530 return true;
2531 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002532 bool NeedsEnvironment() const OVERRIDE { return true; }
2533 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002534
2535 DECLARE_INSTRUCTION(Deoptimize);
2536
2537 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002538 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2539};
2540
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002541// Represents the ArtMethod that was passed as a first argument to
2542// the method. It is used by instructions that depend on it, like
2543// instructions that work with the dex cache.
2544class HCurrentMethod : public HExpression<0> {
2545 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002546 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2547 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002548
2549 DECLARE_INSTRUCTION(CurrentMethod);
2550
2551 private:
2552 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2553};
2554
Mark Mendellfe57faa2015-09-18 09:26:15 -04002555// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2556// have one successor for each entry in the switch table, and the final successor
2557// will be the block containing the next Dex opcode.
2558class HPackedSwitch : public HTemplateInstruction<1> {
2559 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002560 HPackedSwitch(int32_t start_value,
2561 uint32_t num_entries,
2562 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002563 uint32_t dex_pc = kNoDexPc)
2564 : HTemplateInstruction(SideEffects::None(), dex_pc),
2565 start_value_(start_value),
2566 num_entries_(num_entries) {
2567 SetRawInputAt(0, input);
2568 }
2569
2570 bool IsControlFlow() const OVERRIDE { return true; }
2571
2572 int32_t GetStartValue() const { return start_value_; }
2573
Vladimir Marko211c2112015-09-24 16:52:33 +01002574 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002575
2576 HBasicBlock* GetDefaultBlock() const {
2577 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002578 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002579 }
2580 DECLARE_INSTRUCTION(PackedSwitch);
2581
2582 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002583 const int32_t start_value_;
2584 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002585
2586 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2587};
2588
Roland Levillain88cb1752014-10-20 16:36:47 +01002589class HUnaryOperation : public HExpression<1> {
2590 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002591 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2592 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002593 SetRawInputAt(0, input);
2594 }
2595
2596 HInstruction* GetInput() const { return InputAt(0); }
2597 Primitive::Type GetResultType() const { return GetType(); }
2598
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002599 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002600 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002601 return true;
2602 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002603
Roland Levillain9240d6a2014-10-20 16:47:04 +01002604 // Try to statically evaluate `operation` and return a HConstant
2605 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002606 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002607 HConstant* TryStaticEvaluation() const;
2608
2609 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002610 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2611 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002612
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002613 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002614
2615 private:
2616 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2617};
2618
Dave Allison20dfc792014-06-16 20:44:29 -07002619class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002620 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002621 HBinaryOperation(Primitive::Type result_type,
2622 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002623 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002624 SideEffects side_effects = SideEffects::None(),
2625 uint32_t dex_pc = kNoDexPc)
2626 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002627 SetRawInputAt(0, left);
2628 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002629 }
2630
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002631 HInstruction* GetLeft() const { return InputAt(0); }
2632 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002633 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002634
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002635 virtual bool IsCommutative() const { return false; }
2636
2637 // Put constant on the right.
2638 // Returns whether order is changed.
2639 bool OrderInputsWithConstantOnTheRight() {
2640 HInstruction* left = InputAt(0);
2641 HInstruction* right = InputAt(1);
2642 if (left->IsConstant() && !right->IsConstant()) {
2643 ReplaceInput(right, 0);
2644 ReplaceInput(left, 1);
2645 return true;
2646 }
2647 return false;
2648 }
2649
2650 // Order inputs by instruction id, but favor constant on the right side.
2651 // This helps GVN for commutative ops.
2652 void OrderInputs() {
2653 DCHECK(IsCommutative());
2654 HInstruction* left = InputAt(0);
2655 HInstruction* right = InputAt(1);
2656 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2657 return;
2658 }
2659 if (OrderInputsWithConstantOnTheRight()) {
2660 return;
2661 }
2662 // Order according to instruction id.
2663 if (left->GetId() > right->GetId()) {
2664 ReplaceInput(right, 0);
2665 ReplaceInput(left, 1);
2666 }
2667 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002668
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002669 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002670 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002671 return true;
2672 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002673
Roland Levillain9240d6a2014-10-20 16:47:04 +01002674 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002675 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002676 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002677 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002678
2679 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002680 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2681 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2682 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2683 HLongConstant* y ATTRIBUTE_UNUSED) const {
2684 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2685 return nullptr;
2686 }
2687 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2688 HIntConstant* y ATTRIBUTE_UNUSED) const {
2689 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2690 return nullptr;
2691 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002692 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2693 HNullConstant* y ATTRIBUTE_UNUSED) const {
2694 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2695 return nullptr;
2696 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002697
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002698 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002699 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002700 HConstant* GetConstantRight() const;
2701
2702 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002703 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002704 HInstruction* GetLeastConstantLeft() const;
2705
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002706 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002707
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002708 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002709 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2710};
2711
Mark Mendellc4701932015-04-10 13:18:51 -04002712// The comparison bias applies for floating point operations and indicates how NaN
2713// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002714enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002715 kNoBias, // bias is not applicable (i.e. for long operation)
2716 kGtBias, // return 1 for NaN comparisons
2717 kLtBias, // return -1 for NaN comparisons
2718};
2719
Dave Allison20dfc792014-06-16 20:44:29 -07002720class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002721 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002722 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2723 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002724 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002725
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002726 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002727 // `instruction`, and disregard moves in between.
2728 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002729
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002730 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002731
2732 virtual IfCondition GetCondition() const = 0;
2733
Mark Mendellc4701932015-04-10 13:18:51 -04002734 virtual IfCondition GetOppositeCondition() const = 0;
2735
Roland Levillain4fa13f62015-07-06 18:11:54 +01002736 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002737
2738 void SetBias(ComparisonBias bias) { bias_ = bias; }
2739
2740 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2741 return bias_ == other->AsCondition()->bias_;
2742 }
2743
Roland Levillain4fa13f62015-07-06 18:11:54 +01002744 bool IsFPConditionTrueIfNaN() const {
2745 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2746 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002747 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002748 }
2749
2750 bool IsFPConditionFalseIfNaN() const {
2751 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2752 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002753 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002754 }
2755
Dave Allison20dfc792014-06-16 20:44:29 -07002756 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002757 // Needed if we merge a HCompare into a HCondition.
2758 ComparisonBias bias_;
2759
Dave Allison20dfc792014-06-16 20:44:29 -07002760 DISALLOW_COPY_AND_ASSIGN(HCondition);
2761};
2762
2763// Instruction to check if two inputs are equal to each other.
2764class HEqual : public HCondition {
2765 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002766 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2767 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002768
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002769 bool IsCommutative() const OVERRIDE { return true; }
2770
Roland Levillain9867bc72015-08-05 10:21:34 +01002771 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002772 return GetBlock()->GetGraph()->GetIntConstant(
2773 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002774 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002775 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002776 return GetBlock()->GetGraph()->GetIntConstant(
2777 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002778 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002779 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2780 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002781 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002782 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002783
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002784 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002785
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002786 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002787 return kCondEQ;
2788 }
2789
Mark Mendellc4701932015-04-10 13:18:51 -04002790 IfCondition GetOppositeCondition() const OVERRIDE {
2791 return kCondNE;
2792 }
2793
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002794 private:
Aart Bike9f37602015-10-09 11:15:55 -07002795 template <typename T> bool Compute(T x, T y) const { return x == y; }
2796
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002797 DISALLOW_COPY_AND_ASSIGN(HEqual);
2798};
2799
Dave Allison20dfc792014-06-16 20:44:29 -07002800class HNotEqual : public HCondition {
2801 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002802 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2803 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002804
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002805 bool IsCommutative() const OVERRIDE { return true; }
2806
Roland Levillain9867bc72015-08-05 10:21:34 +01002807 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002808 return GetBlock()->GetGraph()->GetIntConstant(
2809 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002810 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002811 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002812 return GetBlock()->GetGraph()->GetIntConstant(
2813 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002814 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002815 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2816 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002817 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002818 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002819
Dave Allison20dfc792014-06-16 20:44:29 -07002820 DECLARE_INSTRUCTION(NotEqual);
2821
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002822 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002823 return kCondNE;
2824 }
2825
Mark Mendellc4701932015-04-10 13:18:51 -04002826 IfCondition GetOppositeCondition() const OVERRIDE {
2827 return kCondEQ;
2828 }
2829
Dave Allison20dfc792014-06-16 20:44:29 -07002830 private:
Aart Bike9f37602015-10-09 11:15:55 -07002831 template <typename T> bool Compute(T x, T y) const { return x != y; }
2832
Dave Allison20dfc792014-06-16 20:44:29 -07002833 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2834};
2835
2836class HLessThan : public HCondition {
2837 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002838 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2839 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002840
Roland Levillain9867bc72015-08-05 10:21:34 +01002841 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002842 return GetBlock()->GetGraph()->GetIntConstant(
2843 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002844 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002845 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002846 return GetBlock()->GetGraph()->GetIntConstant(
2847 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002848 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002849
Dave Allison20dfc792014-06-16 20:44:29 -07002850 DECLARE_INSTRUCTION(LessThan);
2851
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002852 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002853 return kCondLT;
2854 }
2855
Mark Mendellc4701932015-04-10 13:18:51 -04002856 IfCondition GetOppositeCondition() const OVERRIDE {
2857 return kCondGE;
2858 }
2859
Dave Allison20dfc792014-06-16 20:44:29 -07002860 private:
Aart Bike9f37602015-10-09 11:15:55 -07002861 template <typename T> bool Compute(T x, T y) const { return x < y; }
2862
Dave Allison20dfc792014-06-16 20:44:29 -07002863 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2864};
2865
2866class HLessThanOrEqual : public HCondition {
2867 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002868 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2869 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002870
Roland Levillain9867bc72015-08-05 10:21:34 +01002871 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002872 return GetBlock()->GetGraph()->GetIntConstant(
2873 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002874 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002875 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002876 return GetBlock()->GetGraph()->GetIntConstant(
2877 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002878 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002879
Dave Allison20dfc792014-06-16 20:44:29 -07002880 DECLARE_INSTRUCTION(LessThanOrEqual);
2881
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002882 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002883 return kCondLE;
2884 }
2885
Mark Mendellc4701932015-04-10 13:18:51 -04002886 IfCondition GetOppositeCondition() const OVERRIDE {
2887 return kCondGT;
2888 }
2889
Dave Allison20dfc792014-06-16 20:44:29 -07002890 private:
Aart Bike9f37602015-10-09 11:15:55 -07002891 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2892
Dave Allison20dfc792014-06-16 20:44:29 -07002893 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2894};
2895
2896class HGreaterThan : public HCondition {
2897 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002898 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2899 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002900
Roland Levillain9867bc72015-08-05 10:21:34 +01002901 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002902 return GetBlock()->GetGraph()->GetIntConstant(
2903 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002904 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002905 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002906 return GetBlock()->GetGraph()->GetIntConstant(
2907 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002908 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002909
Dave Allison20dfc792014-06-16 20:44:29 -07002910 DECLARE_INSTRUCTION(GreaterThan);
2911
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002912 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002913 return kCondGT;
2914 }
2915
Mark Mendellc4701932015-04-10 13:18:51 -04002916 IfCondition GetOppositeCondition() const OVERRIDE {
2917 return kCondLE;
2918 }
2919
Dave Allison20dfc792014-06-16 20:44:29 -07002920 private:
Aart Bike9f37602015-10-09 11:15:55 -07002921 template <typename T> bool Compute(T x, T y) const { return x > y; }
2922
Dave Allison20dfc792014-06-16 20:44:29 -07002923 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2924};
2925
2926class HGreaterThanOrEqual : public HCondition {
2927 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002928 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2929 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002930
Roland Levillain9867bc72015-08-05 10:21:34 +01002931 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002932 return GetBlock()->GetGraph()->GetIntConstant(
2933 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002934 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002935 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002936 return GetBlock()->GetGraph()->GetIntConstant(
2937 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002938 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002939
Dave Allison20dfc792014-06-16 20:44:29 -07002940 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2941
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002942 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002943 return kCondGE;
2944 }
2945
Mark Mendellc4701932015-04-10 13:18:51 -04002946 IfCondition GetOppositeCondition() const OVERRIDE {
2947 return kCondLT;
2948 }
2949
Dave Allison20dfc792014-06-16 20:44:29 -07002950 private:
Aart Bike9f37602015-10-09 11:15:55 -07002951 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2952
Dave Allison20dfc792014-06-16 20:44:29 -07002953 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2954};
2955
Aart Bike9f37602015-10-09 11:15:55 -07002956class HBelow : public HCondition {
2957 public:
2958 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2959 : HCondition(first, second, dex_pc) {}
2960
2961 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2962 return GetBlock()->GetGraph()->GetIntConstant(
2963 Compute(static_cast<uint32_t>(x->GetValue()),
2964 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2965 }
2966 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2967 return GetBlock()->GetGraph()->GetIntConstant(
2968 Compute(static_cast<uint64_t>(x->GetValue()),
2969 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2970 }
2971
2972 DECLARE_INSTRUCTION(Below);
2973
2974 IfCondition GetCondition() const OVERRIDE {
2975 return kCondB;
2976 }
2977
2978 IfCondition GetOppositeCondition() const OVERRIDE {
2979 return kCondAE;
2980 }
2981
2982 private:
2983 template <typename T> bool Compute(T x, T y) const { return x < y; }
2984
2985 DISALLOW_COPY_AND_ASSIGN(HBelow);
2986};
2987
2988class HBelowOrEqual : public HCondition {
2989 public:
2990 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2991 : HCondition(first, second, dex_pc) {}
2992
2993 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2994 return GetBlock()->GetGraph()->GetIntConstant(
2995 Compute(static_cast<uint32_t>(x->GetValue()),
2996 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2997 }
2998 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2999 return GetBlock()->GetGraph()->GetIntConstant(
3000 Compute(static_cast<uint64_t>(x->GetValue()),
3001 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3002 }
3003
3004 DECLARE_INSTRUCTION(BelowOrEqual);
3005
3006 IfCondition GetCondition() const OVERRIDE {
3007 return kCondBE;
3008 }
3009
3010 IfCondition GetOppositeCondition() const OVERRIDE {
3011 return kCondA;
3012 }
3013
3014 private:
3015 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3016
3017 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3018};
3019
3020class HAbove : public HCondition {
3021 public:
3022 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3023 : HCondition(first, second, dex_pc) {}
3024
3025 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3026 return GetBlock()->GetGraph()->GetIntConstant(
3027 Compute(static_cast<uint32_t>(x->GetValue()),
3028 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3029 }
3030 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3031 return GetBlock()->GetGraph()->GetIntConstant(
3032 Compute(static_cast<uint64_t>(x->GetValue()),
3033 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3034 }
3035
3036 DECLARE_INSTRUCTION(Above);
3037
3038 IfCondition GetCondition() const OVERRIDE {
3039 return kCondA;
3040 }
3041
3042 IfCondition GetOppositeCondition() const OVERRIDE {
3043 return kCondBE;
3044 }
3045
3046 private:
3047 template <typename T> bool Compute(T x, T y) const { return x > y; }
3048
3049 DISALLOW_COPY_AND_ASSIGN(HAbove);
3050};
3051
3052class HAboveOrEqual : public HCondition {
3053 public:
3054 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3055 : HCondition(first, second, dex_pc) {}
3056
3057 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3058 return GetBlock()->GetGraph()->GetIntConstant(
3059 Compute(static_cast<uint32_t>(x->GetValue()),
3060 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3061 }
3062 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3063 return GetBlock()->GetGraph()->GetIntConstant(
3064 Compute(static_cast<uint64_t>(x->GetValue()),
3065 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3066 }
3067
3068 DECLARE_INSTRUCTION(AboveOrEqual);
3069
3070 IfCondition GetCondition() const OVERRIDE {
3071 return kCondAE;
3072 }
3073
3074 IfCondition GetOppositeCondition() const OVERRIDE {
3075 return kCondB;
3076 }
3077
3078 private:
3079 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3080
3081 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3082};
Dave Allison20dfc792014-06-16 20:44:29 -07003083
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003084// Instruction to check how two inputs compare to each other.
3085// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3086class HCompare : public HBinaryOperation {
3087 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003088 HCompare(Primitive::Type type,
3089 HInstruction* first,
3090 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003091 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003092 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003093 : HBinaryOperation(Primitive::kPrimInt,
3094 first,
3095 second,
3096 SideEffectsForArchRuntimeCalls(type),
3097 dex_pc),
3098 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003099 DCHECK_EQ(type, first->GetType());
3100 DCHECK_EQ(type, second->GetType());
3101 }
3102
Roland Levillain9867bc72015-08-05 10:21:34 +01003103 template <typename T>
3104 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003105
Roland Levillain9867bc72015-08-05 10:21:34 +01003106 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003107 return GetBlock()->GetGraph()->GetIntConstant(
3108 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003109 }
3110 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003111 return GetBlock()->GetGraph()->GetIntConstant(
3112 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003113 }
3114
Calin Juravleddb7df22014-11-25 20:56:51 +00003115 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3116 return bias_ == other->AsCompare()->bias_;
3117 }
3118
Mark Mendellc4701932015-04-10 13:18:51 -04003119 ComparisonBias GetBias() const { return bias_; }
3120
Roland Levillain4fa13f62015-07-06 18:11:54 +01003121 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003122
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003123
3124 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3125 // MIPS64 uses a runtime call for FP comparisons.
3126 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3127 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003128
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003129 DECLARE_INSTRUCTION(Compare);
3130
3131 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003132 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003133
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003134 DISALLOW_COPY_AND_ASSIGN(HCompare);
3135};
3136
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003137// A local in the graph. Corresponds to a Dex register.
3138class HLocal : public HTemplateInstruction<0> {
3139 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003140 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003141 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003142
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003143 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003144
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003145 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003146
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003147 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003148 // The Dex register number.
3149 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003150
3151 DISALLOW_COPY_AND_ASSIGN(HLocal);
3152};
3153
3154// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003155class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003156 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003157 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3158 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003159 SetRawInputAt(0, local);
3160 }
3161
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003162 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3163
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003164 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003165
3166 private:
3167 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3168};
3169
3170// Store a value in a given local. This instruction has two inputs: the value
3171// and the local.
3172class HStoreLocal : public HTemplateInstruction<2> {
3173 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003174 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3175 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003176 SetRawInputAt(0, local);
3177 SetRawInputAt(1, value);
3178 }
3179
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003180 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3181
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003182 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003183
3184 private:
3185 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3186};
3187
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003188class HFloatConstant : public HConstant {
3189 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003190 float GetValue() const { return value_; }
3191
David Brazdil77a48ae2015-09-15 12:34:04 +00003192 uint64_t GetValueAsUint64() const OVERRIDE {
3193 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3194 }
3195
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003196 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003197 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003198 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003199 }
3200
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003201 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003202
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003203 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003204 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003205 }
3206 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003207 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003208 }
3209 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003210 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3211 }
3212 bool IsNaN() const {
3213 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003214 }
3215
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003216 DECLARE_INSTRUCTION(FloatConstant);
3217
3218 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003219 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3220 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3221 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3222 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003223
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003224 const float value_;
3225
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003226 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003227 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003228 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003229 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3230};
3231
3232class HDoubleConstant : public HConstant {
3233 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003234 double GetValue() const { return value_; }
3235
David Brazdil77a48ae2015-09-15 12:34:04 +00003236 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3237
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003238 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003239 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003240 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003241 }
3242
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003243 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003244
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003245 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003246 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003247 }
3248 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003249 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003250 }
3251 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003252 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3253 }
3254 bool IsNaN() const {
3255 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003256 }
3257
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003258 DECLARE_INSTRUCTION(DoubleConstant);
3259
3260 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003261 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3262 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3263 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3264 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003265
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003266 const double value_;
3267
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003268 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003269 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003270 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003271 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3272};
3273
David Brazdil6de19382016-01-08 17:37:10 +00003274class HNewInstance : public HExpression<2> {
3275 public:
3276 HNewInstance(HInstruction* cls,
3277 HCurrentMethod* current_method,
3278 uint32_t dex_pc,
3279 uint16_t type_index,
3280 const DexFile& dex_file,
3281 bool can_throw,
3282 bool finalizable,
3283 QuickEntrypointEnum entrypoint)
3284 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3285 type_index_(type_index),
3286 dex_file_(dex_file),
3287 can_throw_(can_throw),
3288 finalizable_(finalizable),
3289 entrypoint_(entrypoint) {
3290 SetRawInputAt(0, cls);
3291 SetRawInputAt(1, current_method);
3292 }
3293
3294 uint16_t GetTypeIndex() const { return type_index_; }
3295 const DexFile& GetDexFile() const { return dex_file_; }
3296
3297 // Calls runtime so needs an environment.
3298 bool NeedsEnvironment() const OVERRIDE { return true; }
3299
3300 // It may throw when called on type that's not instantiable/accessible.
3301 // It can throw OOME.
3302 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3303 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3304
3305 bool IsFinalizable() const { return finalizable_; }
3306
3307 bool CanBeNull() const OVERRIDE { return false; }
3308
3309 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3310
3311 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3312 entrypoint_ = entrypoint;
3313 }
3314
3315 bool IsStringAlloc() const;
3316
3317 DECLARE_INSTRUCTION(NewInstance);
3318
3319 private:
3320 const uint16_t type_index_;
3321 const DexFile& dex_file_;
3322 const bool can_throw_;
3323 const bool finalizable_;
3324 QuickEntrypointEnum entrypoint_;
3325
3326 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3327};
3328
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003329enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003330#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3331 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003332#include "intrinsics_list.h"
3333 kNone,
3334 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3335#undef INTRINSICS_LIST
3336#undef OPTIMIZING_INTRINSICS
3337};
3338std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3339
Agi Csaki05f20562015-08-19 14:58:14 -07003340enum IntrinsicNeedsEnvironmentOrCache {
3341 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3342 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003343};
3344
Aart Bik5d75afe2015-12-14 11:57:01 -08003345enum IntrinsicSideEffects {
3346 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3347 kReadSideEffects, // Intrinsic may read heap memory.
3348 kWriteSideEffects, // Intrinsic may write heap memory.
3349 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3350};
3351
3352enum IntrinsicExceptions {
3353 kNoThrow, // Intrinsic does not throw any exceptions.
3354 kCanThrow // Intrinsic may throw exceptions.
3355};
3356
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003357class HInvoke : public HInstruction {
3358 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003359 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003360
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003361 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003362
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003363 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003364 SetRawInputAt(index, argument);
3365 }
3366
Roland Levillain3e3d7332015-04-28 11:00:54 +01003367 // Return the number of arguments. This number can be lower than
3368 // the number of inputs returned by InputCount(), as some invoke
3369 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3370 // inputs at the end of their list of inputs.
3371 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3372
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003373 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003374
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003375 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003376 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003377
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003378 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3379
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003380 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003381 return intrinsic_;
3382 }
3383
Aart Bik5d75afe2015-12-14 11:57:01 -08003384 void SetIntrinsic(Intrinsics intrinsic,
3385 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3386 IntrinsicSideEffects side_effects,
3387 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003388
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003389 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003390 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003391 }
3392
Aart Bik5d75afe2015-12-14 11:57:01 -08003393 bool CanThrow() const OVERRIDE { return can_throw_; }
3394
3395 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3396
3397 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3398 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3399 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003400
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003401 uint32_t* GetIntrinsicOptimizations() {
3402 return &intrinsic_optimizations_;
3403 }
3404
3405 const uint32_t* GetIntrinsicOptimizations() const {
3406 return &intrinsic_optimizations_;
3407 }
3408
3409 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3410
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003411 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003412
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003413 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003414 HInvoke(ArenaAllocator* arena,
3415 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003416 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003417 Primitive::Type return_type,
3418 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003419 uint32_t dex_method_index,
3420 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003421 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003422 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003423 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003424 inputs_(number_of_arguments + number_of_other_inputs,
3425 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003426 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003427 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003428 original_invoke_type_(original_invoke_type),
Aart Bik5d75afe2015-12-14 11:57:01 -08003429 can_throw_(true),
agicsaki57b81ec2015-08-11 17:39:37 -07003430 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003431 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003432 }
3433
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003434 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003435 return inputs_[index];
3436 }
3437
David Brazdil1abb4192015-02-17 18:33:36 +00003438 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003439 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003440 }
3441
Aart Bik5d75afe2015-12-14 11:57:01 -08003442 void SetCanThrow(bool can_throw) { can_throw_ = can_throw; }
3443
Roland Levillain3e3d7332015-04-28 11:00:54 +01003444 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003445 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003446 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003447 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003448 const InvokeType original_invoke_type_;
Aart Bik5d75afe2015-12-14 11:57:01 -08003449 bool can_throw_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003450 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003451
3452 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3453 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003454
3455 private:
3456 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3457};
3458
Calin Juravle175dc732015-08-25 15:42:32 +01003459class HInvokeUnresolved : public HInvoke {
3460 public:
3461 HInvokeUnresolved(ArenaAllocator* arena,
3462 uint32_t number_of_arguments,
3463 Primitive::Type return_type,
3464 uint32_t dex_pc,
3465 uint32_t dex_method_index,
3466 InvokeType invoke_type)
3467 : HInvoke(arena,
3468 number_of_arguments,
3469 0u /* number_of_other_inputs */,
3470 return_type,
3471 dex_pc,
3472 dex_method_index,
3473 invoke_type) {
3474 }
3475
3476 DECLARE_INSTRUCTION(InvokeUnresolved);
3477
3478 private:
3479 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3480};
3481
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003482class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003483 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003484 // Requirements of this method call regarding the class
3485 // initialization (clinit) check of its declaring class.
3486 enum class ClinitCheckRequirement {
3487 kNone, // Class already initialized.
3488 kExplicit, // Static call having explicit clinit check as last input.
3489 kImplicit, // Static call implicitly requiring a clinit check.
3490 };
3491
Vladimir Marko58155012015-08-19 12:49:41 +00003492 // Determines how to load the target ArtMethod*.
3493 enum class MethodLoadKind {
3494 // Use a String init ArtMethod* loaded from Thread entrypoints.
3495 kStringInit,
3496
3497 // Use the method's own ArtMethod* loaded by the register allocator.
3498 kRecursive,
3499
3500 // Use ArtMethod* at a known address, embed the direct address in the code.
3501 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3502 kDirectAddress,
3503
3504 // Use ArtMethod* at an address that will be known at link time, embed the direct
3505 // address in the code. If the image is relocatable, emit .patch_oat entry.
3506 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3507 // the image relocatable or not.
3508 kDirectAddressWithFixup,
3509
3510 // Load from resoved methods array in the dex cache using a PC-relative load.
3511 // Used when we need to use the dex cache, for example for invoke-static that
3512 // may cause class initialization (the entry may point to a resolution method),
3513 // and we know that we can access the dex cache arrays using a PC-relative load.
3514 kDexCachePcRelative,
3515
3516 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3517 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3518 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3519 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3520 kDexCacheViaMethod,
3521 };
3522
3523 // Determines the location of the code pointer.
3524 enum class CodePtrLocation {
3525 // Recursive call, use local PC-relative call instruction.
3526 kCallSelf,
3527
3528 // Use PC-relative call instruction patched at link time.
3529 // Used for calls within an oat file, boot->boot or app->app.
3530 kCallPCRelative,
3531
3532 // Call to a known target address, embed the direct address in code.
3533 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3534 kCallDirect,
3535
3536 // Call to a target address that will be known at link time, embed the direct
3537 // address in code. If the image is relocatable, emit .patch_oat entry.
3538 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3539 // the image relocatable or not.
3540 kCallDirectWithFixup,
3541
3542 // Use code pointer from the ArtMethod*.
3543 // Used when we don't know the target code. This is also the last-resort-kind used when
3544 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3545 kCallArtMethod,
3546 };
3547
3548 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003549 MethodLoadKind method_load_kind;
3550 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003551 // The method load data holds
3552 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3553 // Note that there are multiple string init methods, each having its own offset.
3554 // - the method address for kDirectAddress
3555 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003556 uint64_t method_load_data;
3557 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003558 };
3559
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003560 HInvokeStaticOrDirect(ArenaAllocator* arena,
3561 uint32_t number_of_arguments,
3562 Primitive::Type return_type,
3563 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003564 uint32_t method_index,
3565 MethodReference target_method,
3566 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003567 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003568 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003569 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003570 : HInvoke(arena,
3571 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003572 // There is potentially one extra argument for the HCurrentMethod node, and
3573 // potentially one other if the clinit check is explicit, and potentially
3574 // one other if the method is a string factory.
3575 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3576 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3577 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003578 return_type,
3579 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003580 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003581 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003582 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003583 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003584 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003585 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003586
Vladimir Markodc151b22015-10-15 18:02:30 +01003587 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003588 bool had_current_method_input = HasCurrentMethodInput();
3589 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3590
3591 // Using the current method is the default and once we find a better
3592 // method load kind, we should not go back to using the current method.
3593 DCHECK(had_current_method_input || !needs_current_method_input);
3594
3595 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003596 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3597 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003598 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003599 dispatch_info_ = dispatch_info;
3600 }
3601
Vladimir Markoc53c0792015-11-19 15:48:33 +00003602 void AddSpecialInput(HInstruction* input) {
3603 // We allow only one special input.
3604 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3605 DCHECK(InputCount() == GetSpecialInputIndex() ||
3606 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3607 InsertInputAt(GetSpecialInputIndex(), input);
3608 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003609
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003610 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003611 // We access the method via the dex cache so we can't do an implicit null check.
3612 // TODO: for intrinsics we can generate implicit null checks.
3613 return false;
3614 }
3615
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003616 bool CanBeNull() const OVERRIDE {
3617 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3618 }
3619
Vladimir Markoc53c0792015-11-19 15:48:33 +00003620 // Get the index of the special input, if any.
3621 //
David Brazdil6de19382016-01-08 17:37:10 +00003622 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3623 // method pointer; otherwise there may be one platform-specific special input,
3624 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003625 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3626
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003627 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3628 void SetOptimizedInvokeType(InvokeType invoke_type) {
3629 optimized_invoke_type_ = invoke_type;
3630 }
3631
Vladimir Marko58155012015-08-19 12:49:41 +00003632 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3633 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3634 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003635 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003636 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003637 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003638 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003639 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3640 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003641 bool HasCurrentMethodInput() const {
3642 // This function can be called only after the invoke has been fully initialized by the builder.
3643 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003644 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003645 return true;
3646 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003647 DCHECK(InputCount() == GetSpecialInputIndex() ||
3648 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003649 return false;
3650 }
3651 }
Vladimir Marko58155012015-08-19 12:49:41 +00003652 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3653 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003654 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003655
3656 int32_t GetStringInitOffset() const {
3657 DCHECK(IsStringInit());
3658 return dispatch_info_.method_load_data;
3659 }
3660
3661 uint64_t GetMethodAddress() const {
3662 DCHECK(HasMethodAddress());
3663 return dispatch_info_.method_load_data;
3664 }
3665
3666 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003667 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003668 return dispatch_info_.method_load_data;
3669 }
3670
3671 uint64_t GetDirectCodePtr() const {
3672 DCHECK(HasDirectCodePtr());
3673 return dispatch_info_.direct_code_ptr;
3674 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003675
Calin Juravle68ad6492015-08-18 17:08:12 +01003676 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3677
Roland Levillain4c0eb422015-04-24 16:43:49 +01003678 // Is this instruction a call to a static method?
3679 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003680 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003681 }
3682
Vladimir Markofbb184a2015-11-13 14:47:00 +00003683 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3684 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3685 // instruction; only relevant for static calls with explicit clinit check.
3686 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003687 DCHECK(IsStaticWithExplicitClinitCheck());
3688 size_t last_input_index = InputCount() - 1;
3689 HInstruction* last_input = InputAt(last_input_index);
3690 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003691 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003692 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003693 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003694 clinit_check_requirement_ = new_requirement;
3695 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003696 }
3697
David Brazdilbc9ab162016-01-20 14:50:19 +00003698 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00003699 DCHECK(IsStringInit());
3700 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00003701 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00003702 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003703 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00003704 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003705 }
3706
Roland Levillain4c0eb422015-04-24 16:43:49 +01003707 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003708 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003709 bool IsStaticWithExplicitClinitCheck() const {
3710 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3711 }
3712
3713 // Is this a call to a static method whose declaring class has an
3714 // implicit intialization check requirement?
3715 bool IsStaticWithImplicitClinitCheck() const {
3716 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3717 }
3718
Vladimir Markob554b5a2015-11-06 12:57:55 +00003719 // Does this method load kind need the current method as an input?
3720 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3721 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3722 }
3723
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003724 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003725
Roland Levillain4c0eb422015-04-24 16:43:49 +01003726 protected:
3727 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3728 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3729 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3730 HInstruction* input = input_record.GetInstruction();
3731 // `input` is the last input of a static invoke marked as having
3732 // an explicit clinit check. It must either be:
3733 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3734 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3735 DCHECK(input != nullptr);
3736 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3737 }
3738 return input_record;
3739 }
3740
Vladimir Markoc53c0792015-11-19 15:48:33 +00003741 void InsertInputAt(size_t index, HInstruction* input);
3742 void RemoveInputAt(size_t index);
3743
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003744 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003745 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003746 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003747 // The target method may refer to different dex file or method index than the original
3748 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3749 // in derived class to increase visibility.
3750 MethodReference target_method_;
3751 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003752
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003753 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003754};
Vladimir Markof64242a2015-12-01 14:58:23 +00003755std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003756std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003757
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003758class HInvokeVirtual : public HInvoke {
3759 public:
3760 HInvokeVirtual(ArenaAllocator* arena,
3761 uint32_t number_of_arguments,
3762 Primitive::Type return_type,
3763 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003764 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003765 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003766 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003767 vtable_index_(vtable_index) {}
3768
Calin Juravle641547a2015-04-21 22:08:51 +01003769 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003770 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003771 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003772 }
3773
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003774 uint32_t GetVTableIndex() const { return vtable_index_; }
3775
3776 DECLARE_INSTRUCTION(InvokeVirtual);
3777
3778 private:
3779 const uint32_t vtable_index_;
3780
3781 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3782};
3783
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003784class HInvokeInterface : public HInvoke {
3785 public:
3786 HInvokeInterface(ArenaAllocator* arena,
3787 uint32_t number_of_arguments,
3788 Primitive::Type return_type,
3789 uint32_t dex_pc,
3790 uint32_t dex_method_index,
3791 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003792 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003793 imt_index_(imt_index) {}
3794
Calin Juravle641547a2015-04-21 22:08:51 +01003795 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003796 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003797 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003798 }
3799
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003800 uint32_t GetImtIndex() const { return imt_index_; }
3801 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3802
3803 DECLARE_INSTRUCTION(InvokeInterface);
3804
3805 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003806 const uint32_t imt_index_;
3807
3808 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3809};
3810
Roland Levillain88cb1752014-10-20 16:36:47 +01003811class HNeg : public HUnaryOperation {
3812 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003813 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3814 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003815
Roland Levillain9867bc72015-08-05 10:21:34 +01003816 template <typename T> T Compute(T x) const { return -x; }
3817
3818 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003819 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003820 }
3821 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003822 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003823 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003824
Roland Levillain88cb1752014-10-20 16:36:47 +01003825 DECLARE_INSTRUCTION(Neg);
3826
3827 private:
3828 DISALLOW_COPY_AND_ASSIGN(HNeg);
3829};
3830
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003831class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003832 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003833 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003834 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003835 uint32_t dex_pc,
3836 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003837 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003838 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003839 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003840 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003841 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003842 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003843 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003844 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003845 }
3846
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003847 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003848 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003849
3850 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003851 bool NeedsEnvironment() const OVERRIDE { return true; }
3852
Mingyao Yang0c365e62015-03-31 15:09:29 -07003853 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3854 bool CanThrow() const OVERRIDE { return true; }
3855
Calin Juravle10e244f2015-01-26 18:54:32 +00003856 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003857
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003858 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3859
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003860 DECLARE_INSTRUCTION(NewArray);
3861
3862 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003863 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003864 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003865 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003866
3867 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3868};
3869
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003870class HAdd : public HBinaryOperation {
3871 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003872 HAdd(Primitive::Type result_type,
3873 HInstruction* left,
3874 HInstruction* right,
3875 uint32_t dex_pc = kNoDexPc)
3876 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003877
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003878 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003879
Roland Levillain9867bc72015-08-05 10:21:34 +01003880 template <typename T> T Compute(T x, T y) const { return x + y; }
3881
3882 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003883 return GetBlock()->GetGraph()->GetIntConstant(
3884 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003885 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003886 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003887 return GetBlock()->GetGraph()->GetLongConstant(
3888 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003889 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003890
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003891 DECLARE_INSTRUCTION(Add);
3892
3893 private:
3894 DISALLOW_COPY_AND_ASSIGN(HAdd);
3895};
3896
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003897class HSub : public HBinaryOperation {
3898 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003899 HSub(Primitive::Type result_type,
3900 HInstruction* left,
3901 HInstruction* right,
3902 uint32_t dex_pc = kNoDexPc)
3903 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003904
Roland Levillain9867bc72015-08-05 10:21:34 +01003905 template <typename T> T Compute(T x, T y) const { return x - y; }
3906
3907 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003908 return GetBlock()->GetGraph()->GetIntConstant(
3909 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003910 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003911 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003912 return GetBlock()->GetGraph()->GetLongConstant(
3913 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003914 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003915
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003916 DECLARE_INSTRUCTION(Sub);
3917
3918 private:
3919 DISALLOW_COPY_AND_ASSIGN(HSub);
3920};
3921
Calin Juravle34bacdf2014-10-07 20:23:36 +01003922class HMul : public HBinaryOperation {
3923 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003924 HMul(Primitive::Type result_type,
3925 HInstruction* left,
3926 HInstruction* right,
3927 uint32_t dex_pc = kNoDexPc)
3928 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003929
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003930 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003931
Roland Levillain9867bc72015-08-05 10:21:34 +01003932 template <typename T> T Compute(T x, T y) const { return x * y; }
3933
3934 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003935 return GetBlock()->GetGraph()->GetIntConstant(
3936 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003937 }
3938 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003939 return GetBlock()->GetGraph()->GetLongConstant(
3940 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003941 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003942
3943 DECLARE_INSTRUCTION(Mul);
3944
3945 private:
3946 DISALLOW_COPY_AND_ASSIGN(HMul);
3947};
3948
Calin Juravle7c4954d2014-10-28 16:57:40 +00003949class HDiv : public HBinaryOperation {
3950 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003951 HDiv(Primitive::Type result_type,
3952 HInstruction* left,
3953 HInstruction* right,
3954 uint32_t dex_pc)
3955 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003956
Roland Levillain9867bc72015-08-05 10:21:34 +01003957 template <typename T>
3958 T Compute(T x, T y) const {
3959 // Our graph structure ensures we never have 0 for `y` during
3960 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003961 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003962 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003963 return (y == -1) ? -x : x / y;
3964 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003965
Roland Levillain9867bc72015-08-05 10:21:34 +01003966 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003967 return GetBlock()->GetGraph()->GetIntConstant(
3968 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003969 }
3970 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003971 return GetBlock()->GetGraph()->GetLongConstant(
3972 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003973 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003974
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003975 static SideEffects SideEffectsForArchRuntimeCalls() {
3976 // The generated code can use a runtime call.
3977 return SideEffects::CanTriggerGC();
3978 }
3979
Calin Juravle7c4954d2014-10-28 16:57:40 +00003980 DECLARE_INSTRUCTION(Div);
3981
3982 private:
3983 DISALLOW_COPY_AND_ASSIGN(HDiv);
3984};
3985
Calin Juravlebacfec32014-11-14 15:54:36 +00003986class HRem : public HBinaryOperation {
3987 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003988 HRem(Primitive::Type result_type,
3989 HInstruction* left,
3990 HInstruction* right,
3991 uint32_t dex_pc)
3992 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003993
Roland Levillain9867bc72015-08-05 10:21:34 +01003994 template <typename T>
3995 T Compute(T x, T y) const {
3996 // Our graph structure ensures we never have 0 for `y` during
3997 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003998 DCHECK_NE(y, 0);
3999 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4000 return (y == -1) ? 0 : x % y;
4001 }
4002
Roland Levillain9867bc72015-08-05 10:21:34 +01004003 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004004 return GetBlock()->GetGraph()->GetIntConstant(
4005 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004006 }
4007 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004008 return GetBlock()->GetGraph()->GetLongConstant(
4009 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004010 }
4011
Calin Juravlebacfec32014-11-14 15:54:36 +00004012
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004013 static SideEffects SideEffectsForArchRuntimeCalls() {
4014 return SideEffects::CanTriggerGC();
4015 }
4016
Calin Juravlebacfec32014-11-14 15:54:36 +00004017 DECLARE_INSTRUCTION(Rem);
4018
4019 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004020 DISALLOW_COPY_AND_ASSIGN(HRem);
4021};
4022
Calin Juravled0d48522014-11-04 16:40:20 +00004023class HDivZeroCheck : public HExpression<1> {
4024 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004025 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4026 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004027 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004028 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004029 SetRawInputAt(0, value);
4030 }
4031
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004032 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4033
Calin Juravled0d48522014-11-04 16:40:20 +00004034 bool CanBeMoved() const OVERRIDE { return true; }
4035
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004036 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004037 return true;
4038 }
4039
4040 bool NeedsEnvironment() const OVERRIDE { return true; }
4041 bool CanThrow() const OVERRIDE { return true; }
4042
Calin Juravled0d48522014-11-04 16:40:20 +00004043 DECLARE_INSTRUCTION(DivZeroCheck);
4044
4045 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004046 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4047};
4048
Calin Juravle9aec02f2014-11-18 23:06:35 +00004049class HShl : public HBinaryOperation {
4050 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004051 HShl(Primitive::Type result_type,
4052 HInstruction* left,
4053 HInstruction* right,
4054 uint32_t dex_pc = kNoDexPc)
4055 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004056
Roland Levillain9867bc72015-08-05 10:21:34 +01004057 template <typename T, typename U, typename V>
4058 T Compute(T x, U y, V max_shift_value) const {
4059 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4060 "V is not the unsigned integer type corresponding to T");
4061 return x << (y & max_shift_value);
4062 }
4063
4064 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4065 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004066 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004067 }
4068 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4069 // case is handled as `x << static_cast<int>(y)`.
4070 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4071 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004072 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004073 }
4074 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4075 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004076 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004077 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004078
4079 DECLARE_INSTRUCTION(Shl);
4080
4081 private:
4082 DISALLOW_COPY_AND_ASSIGN(HShl);
4083};
4084
4085class HShr : public HBinaryOperation {
4086 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004087 HShr(Primitive::Type result_type,
4088 HInstruction* left,
4089 HInstruction* right,
4090 uint32_t dex_pc = kNoDexPc)
4091 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004092
Roland Levillain9867bc72015-08-05 10:21:34 +01004093 template <typename T, typename U, typename V>
4094 T Compute(T x, U y, V max_shift_value) const {
4095 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4096 "V is not the unsigned integer type corresponding to T");
4097 return x >> (y & max_shift_value);
4098 }
4099
4100 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4101 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004102 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004103 }
4104 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4105 // case is handled as `x >> static_cast<int>(y)`.
4106 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4107 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004108 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004109 }
4110 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4111 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004112 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004113 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004114
4115 DECLARE_INSTRUCTION(Shr);
4116
4117 private:
4118 DISALLOW_COPY_AND_ASSIGN(HShr);
4119};
4120
4121class HUShr : public HBinaryOperation {
4122 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004123 HUShr(Primitive::Type result_type,
4124 HInstruction* left,
4125 HInstruction* right,
4126 uint32_t dex_pc = kNoDexPc)
4127 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004128
Roland Levillain9867bc72015-08-05 10:21:34 +01004129 template <typename T, typename U, typename V>
4130 T Compute(T x, U y, V max_shift_value) const {
4131 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4132 "V is not the unsigned integer type corresponding to T");
4133 V ux = static_cast<V>(x);
4134 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004135 }
4136
Roland Levillain9867bc72015-08-05 10:21:34 +01004137 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4138 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004139 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004140 }
4141 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4142 // case is handled as `x >>> static_cast<int>(y)`.
4143 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4144 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004145 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004146 }
4147 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4148 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004149 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004150 }
4151
4152 DECLARE_INSTRUCTION(UShr);
4153
4154 private:
4155 DISALLOW_COPY_AND_ASSIGN(HUShr);
4156};
4157
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004158class HAnd : public HBinaryOperation {
4159 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004160 HAnd(Primitive::Type result_type,
4161 HInstruction* left,
4162 HInstruction* right,
4163 uint32_t dex_pc = kNoDexPc)
4164 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004165
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004166 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004167
Roland Levillain9867bc72015-08-05 10:21:34 +01004168 template <typename T, typename U>
4169 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4170
4171 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004172 return GetBlock()->GetGraph()->GetIntConstant(
4173 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004174 }
4175 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004176 return GetBlock()->GetGraph()->GetLongConstant(
4177 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004178 }
4179 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004180 return GetBlock()->GetGraph()->GetLongConstant(
4181 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004182 }
4183 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004184 return GetBlock()->GetGraph()->GetLongConstant(
4185 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004186 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004187
4188 DECLARE_INSTRUCTION(And);
4189
4190 private:
4191 DISALLOW_COPY_AND_ASSIGN(HAnd);
4192};
4193
4194class HOr : public HBinaryOperation {
4195 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004196 HOr(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 Geoffray9574c4b2014-11-12 13:19:37 +00004201
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004202 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004203
Roland Levillain9867bc72015-08-05 10:21:34 +01004204 template <typename T, typename U>
4205 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4206
4207 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004208 return GetBlock()->GetGraph()->GetIntConstant(
4209 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004210 }
4211 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004212 return GetBlock()->GetGraph()->GetLongConstant(
4213 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004214 }
4215 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004216 return GetBlock()->GetGraph()->GetLongConstant(
4217 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004218 }
4219 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004220 return GetBlock()->GetGraph()->GetLongConstant(
4221 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004222 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004223
4224 DECLARE_INSTRUCTION(Or);
4225
4226 private:
4227 DISALLOW_COPY_AND_ASSIGN(HOr);
4228};
4229
4230class HXor : public HBinaryOperation {
4231 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004232 HXor(Primitive::Type result_type,
4233 HInstruction* left,
4234 HInstruction* right,
4235 uint32_t dex_pc = kNoDexPc)
4236 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004237
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004238 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004239
Roland Levillain9867bc72015-08-05 10:21:34 +01004240 template <typename T, typename U>
4241 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4242
4243 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004244 return GetBlock()->GetGraph()->GetIntConstant(
4245 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004246 }
4247 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004248 return GetBlock()->GetGraph()->GetLongConstant(
4249 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004250 }
4251 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004252 return GetBlock()->GetGraph()->GetLongConstant(
4253 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004254 }
4255 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004256 return GetBlock()->GetGraph()->GetLongConstant(
4257 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004258 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004259
4260 DECLARE_INSTRUCTION(Xor);
4261
4262 private:
4263 DISALLOW_COPY_AND_ASSIGN(HXor);
4264};
4265
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004266class HRor : public HBinaryOperation {
4267 public:
4268 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4269 : HBinaryOperation(result_type, value, distance) {}
4270
4271 template <typename T, typename U, typename V>
4272 T Compute(T x, U y, V max_shift_value) const {
4273 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4274 "V is not the unsigned integer type corresponding to T");
4275 V ux = static_cast<V>(x);
4276 if ((y & max_shift_value) == 0) {
4277 return static_cast<T>(ux);
4278 } else {
4279 const V reg_bits = sizeof(T) * 8;
4280 return static_cast<T>(ux >> (y & max_shift_value)) |
4281 (x << (reg_bits - (y & max_shift_value)));
4282 }
4283 }
4284
4285 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4286 return GetBlock()->GetGraph()->GetIntConstant(
4287 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4288 }
4289 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4290 return GetBlock()->GetGraph()->GetLongConstant(
4291 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4292 }
4293 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4294 return GetBlock()->GetGraph()->GetLongConstant(
4295 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4296 }
4297
4298 DECLARE_INSTRUCTION(Ror);
4299
4300 private:
4301 DISALLOW_COPY_AND_ASSIGN(HRor);
4302};
4303
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004304// The value of a parameter in this method. Its location depends on
4305// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004306class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004307 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004308 HParameterValue(const DexFile& dex_file,
4309 uint16_t type_index,
4310 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004311 Primitive::Type parameter_type,
4312 bool is_this = false)
4313 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004314 dex_file_(dex_file),
4315 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004316 index_(index),
4317 is_this_(is_this),
4318 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004319
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004320 const DexFile& GetDexFile() const { return dex_file_; }
4321 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004322 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004323 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004324
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004325 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4326 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004327
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004328 DECLARE_INSTRUCTION(ParameterValue);
4329
4330 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004331 const DexFile& dex_file_;
4332 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004333 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004334 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004335 const uint8_t index_;
4336
Calin Juravle10e244f2015-01-26 18:54:32 +00004337 // Whether or not the parameter value corresponds to 'this' argument.
4338 const bool is_this_;
4339
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004340 bool can_be_null_;
4341
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004342 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4343};
4344
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004345class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004346 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004347 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4348 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004349
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004350 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004351 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004352 return true;
4353 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004354
Roland Levillain9867bc72015-08-05 10:21:34 +01004355 template <typename T> T Compute(T x) const { return ~x; }
4356
4357 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004358 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004359 }
4360 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004361 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004362 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004363
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004364 DECLARE_INSTRUCTION(Not);
4365
4366 private:
4367 DISALLOW_COPY_AND_ASSIGN(HNot);
4368};
4369
David Brazdil66d126e2015-04-03 16:02:44 +01004370class HBooleanNot : public HUnaryOperation {
4371 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004372 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4373 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004374
4375 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004376 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004377 return true;
4378 }
4379
Roland Levillain9867bc72015-08-05 10:21:34 +01004380 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004381 DCHECK(IsUint<1>(x));
4382 return !x;
4383 }
4384
Roland Levillain9867bc72015-08-05 10:21:34 +01004385 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004386 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004387 }
4388 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4389 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004390 UNREACHABLE();
4391 }
4392
4393 DECLARE_INSTRUCTION(BooleanNot);
4394
4395 private:
4396 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4397};
4398
Roland Levillaindff1f282014-11-05 14:15:05 +00004399class HTypeConversion : public HExpression<1> {
4400 public:
4401 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004402 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004403 : HExpression(result_type,
4404 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4405 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004406 SetRawInputAt(0, input);
4407 DCHECK_NE(input->GetType(), result_type);
4408 }
4409
4410 HInstruction* GetInput() const { return InputAt(0); }
4411 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4412 Primitive::Type GetResultType() const { return GetType(); }
4413
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004414 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004415 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004416
Roland Levillaindff1f282014-11-05 14:15:05 +00004417 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004418 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004419
Mark Mendelle82549b2015-05-06 10:55:34 -04004420 // Try to statically evaluate the conversion and return a HConstant
4421 // containing the result. If the input cannot be converted, return nullptr.
4422 HConstant* TryStaticEvaluation() const;
4423
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004424 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4425 Primitive::Type result_type) {
4426 // Some architectures may not require the 'GC' side effects, but at this point
4427 // in the compilation process we do not know what architecture we will
4428 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004429 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4430 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004431 return SideEffects::CanTriggerGC();
4432 }
4433 return SideEffects::None();
4434 }
4435
Roland Levillaindff1f282014-11-05 14:15:05 +00004436 DECLARE_INSTRUCTION(TypeConversion);
4437
4438 private:
4439 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4440};
4441
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004442static constexpr uint32_t kNoRegNumber = -1;
4443
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004444class HPhi : public HInstruction {
4445 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004446 HPhi(ArenaAllocator* arena,
4447 uint32_t reg_number,
4448 size_t number_of_inputs,
4449 Primitive::Type type,
4450 uint32_t dex_pc = kNoDexPc)
4451 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004452 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004453 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004454 type_(ToPhiType(type)),
4455 // Phis are constructed live and marked dead if conflicting or unused.
4456 // Individual steps of SsaBuilder should assume that if a phi has been
4457 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4458 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004459 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004460 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004461 }
4462
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004463 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4464 static Primitive::Type ToPhiType(Primitive::Type type) {
4465 switch (type) {
4466 case Primitive::kPrimBoolean:
4467 case Primitive::kPrimByte:
4468 case Primitive::kPrimShort:
4469 case Primitive::kPrimChar:
4470 return Primitive::kPrimInt;
4471 default:
4472 return type;
4473 }
4474 }
4475
David Brazdilffee3d32015-07-06 11:48:53 +01004476 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4477
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004478 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004479
4480 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004481 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004482
Calin Juravle10e244f2015-01-26 18:54:32 +00004483 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil4833f5a2015-12-16 10:37:39 +00004484 void SetType(Primitive::Type new_type) {
4485 // Make sure that only valid type changes occur. The following are allowed:
4486 // (1) int -> float/ref (primitive type propagation),
4487 // (2) long -> double (primitive type propagation).
4488 DCHECK(type_ == new_type ||
4489 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4490 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4491 (type_ == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4492 type_ = new_type;
4493 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004494
Calin Juravle10e244f2015-01-26 18:54:32 +00004495 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4496 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4497
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004498 uint32_t GetRegNumber() const { return reg_number_; }
4499
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004500 void SetDead() { is_live_ = false; }
4501 void SetLive() { is_live_ = true; }
4502 bool IsDead() const { return !is_live_; }
4503 bool IsLive() const { return is_live_; }
4504
David Brazdil77a48ae2015-09-15 12:34:04 +00004505 bool IsVRegEquivalentOf(HInstruction* other) const {
4506 return other != nullptr
4507 && other->IsPhi()
4508 && other->AsPhi()->GetBlock() == GetBlock()
4509 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4510 }
4511
Calin Juravlea4f88312015-04-16 12:57:19 +01004512 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4513 // An equivalent phi is a phi having the same dex register and type.
4514 // It assumes that phis with the same dex register are adjacent.
4515 HPhi* GetNextEquivalentPhiWithSameType() {
4516 HInstruction* next = GetNext();
4517 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4518 if (next->GetType() == GetType()) {
4519 return next->AsPhi();
4520 }
4521 next = next->GetNext();
4522 }
4523 return nullptr;
4524 }
4525
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004526 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004527
David Brazdil1abb4192015-02-17 18:33:36 +00004528 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004529 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004530 return inputs_[index];
4531 }
David Brazdil1abb4192015-02-17 18:33:36 +00004532
4533 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004534 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004535 }
4536
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004537 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004538 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004539 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004540 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004541 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004542 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004543
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004544 DISALLOW_COPY_AND_ASSIGN(HPhi);
4545};
4546
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004547class HNullCheck : public HExpression<1> {
4548 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004549 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4550 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004551 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004552 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004553 SetRawInputAt(0, value);
4554 }
4555
Calin Juravle10e244f2015-01-26 18:54:32 +00004556 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004557 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004558 return true;
4559 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004560
Calin Juravle10e244f2015-01-26 18:54:32 +00004561 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004562
Calin Juravle10e244f2015-01-26 18:54:32 +00004563 bool CanThrow() const OVERRIDE { return true; }
4564
4565 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004566
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004567
4568 DECLARE_INSTRUCTION(NullCheck);
4569
4570 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004571 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4572};
4573
4574class FieldInfo : public ValueObject {
4575 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004576 FieldInfo(MemberOffset field_offset,
4577 Primitive::Type field_type,
4578 bool is_volatile,
4579 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004580 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004581 const DexFile& dex_file,
4582 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004583 : field_offset_(field_offset),
4584 field_type_(field_type),
4585 is_volatile_(is_volatile),
4586 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004587 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004588 dex_file_(dex_file),
4589 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004590
4591 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004592 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004593 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004594 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004595 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004596 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004597 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004598
4599 private:
4600 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004601 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004602 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004603 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004604 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004605 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004606 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004607};
4608
4609class HInstanceFieldGet : public HExpression<1> {
4610 public:
4611 HInstanceFieldGet(HInstruction* value,
4612 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004613 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004614 bool is_volatile,
4615 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004616 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004617 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004618 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004619 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004620 : HExpression(field_type,
4621 SideEffects::FieldReadOfType(field_type, is_volatile),
4622 dex_pc),
4623 field_info_(field_offset,
4624 field_type,
4625 is_volatile,
4626 field_idx,
4627 declaring_class_def_index,
4628 dex_file,
4629 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004630 SetRawInputAt(0, value);
4631 }
4632
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004633 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004634
4635 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4636 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4637 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004638 }
4639
Calin Juravle641547a2015-04-21 22:08:51 +01004640 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4641 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004642 }
4643
4644 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004645 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4646 }
4647
Calin Juravle52c48962014-12-16 17:02:57 +00004648 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004649 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004650 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004651 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004652
4653 DECLARE_INSTRUCTION(InstanceFieldGet);
4654
4655 private:
4656 const FieldInfo field_info_;
4657
4658 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4659};
4660
4661class HInstanceFieldSet : public HTemplateInstruction<2> {
4662 public:
4663 HInstanceFieldSet(HInstruction* object,
4664 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004665 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004666 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004667 bool is_volatile,
4668 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004669 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004670 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004671 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004672 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004673 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4674 dex_pc),
4675 field_info_(field_offset,
4676 field_type,
4677 is_volatile,
4678 field_idx,
4679 declaring_class_def_index,
4680 dex_file,
4681 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004682 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004683 SetRawInputAt(0, object);
4684 SetRawInputAt(1, value);
4685 }
4686
Calin Juravle641547a2015-04-21 22:08:51 +01004687 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4688 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004689 }
4690
Calin Juravle52c48962014-12-16 17:02:57 +00004691 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004692 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004693 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004694 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004695 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004696 bool GetValueCanBeNull() const { return value_can_be_null_; }
4697 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004698
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004699 DECLARE_INSTRUCTION(InstanceFieldSet);
4700
4701 private:
4702 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004703 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004704
4705 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4706};
4707
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004708class HArrayGet : public HExpression<2> {
4709 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004710 HArrayGet(HInstruction* array,
4711 HInstruction* index,
4712 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004713 uint32_t dex_pc,
4714 SideEffects additional_side_effects = SideEffects::None())
4715 : HExpression(type,
4716 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004717 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004718 SetRawInputAt(0, array);
4719 SetRawInputAt(1, index);
4720 }
4721
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004722 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004723 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004724 return true;
4725 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004726 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004727 // TODO: We can be smarter here.
4728 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4729 // which generates the implicit null check. There are cases when these can be removed
4730 // to produce better code. If we ever add optimizations to do so we should allow an
4731 // implicit check here (as long as the address falls in the first page).
4732 return false;
4733 }
4734
David Brazdil4833f5a2015-12-16 10:37:39 +00004735 bool IsEquivalentOf(HArrayGet* other) const {
4736 bool result = (GetDexPc() == other->GetDexPc());
4737 if (kIsDebugBuild && result) {
4738 DCHECK_EQ(GetBlock(), other->GetBlock());
4739 DCHECK_EQ(GetArray(), other->GetArray());
4740 DCHECK_EQ(GetIndex(), other->GetIndex());
4741 if (Primitive::IsIntOrLongType(GetType())) {
4742 DCHECK(Primitive::IsFloatingPointType(other->GetType()));
4743 } else {
4744 DCHECK(Primitive::IsFloatingPointType(GetType()));
4745 DCHECK(Primitive::IsIntOrLongType(other->GetType()));
4746 }
4747 }
4748 return result;
4749 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004750
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004751 HInstruction* GetArray() const { return InputAt(0); }
4752 HInstruction* GetIndex() const { return InputAt(1); }
4753
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004754 DECLARE_INSTRUCTION(ArrayGet);
4755
4756 private:
4757 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4758};
4759
4760class HArraySet : public HTemplateInstruction<3> {
4761 public:
4762 HArraySet(HInstruction* array,
4763 HInstruction* index,
4764 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004765 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004766 uint32_t dex_pc,
4767 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004768 : HTemplateInstruction(
4769 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004770 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4771 additional_side_effects),
4772 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004773 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004774 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004775 value_can_be_null_(true),
4776 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004777 SetRawInputAt(0, array);
4778 SetRawInputAt(1, index);
4779 SetRawInputAt(2, value);
4780 }
4781
Calin Juravle77520bc2015-01-12 18:45:46 +00004782 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004783 // We currently always call a runtime method to catch array store
4784 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004785 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004786 }
4787
Mingyao Yang81014cb2015-06-02 03:16:27 -07004788 // Can throw ArrayStoreException.
4789 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4790
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004791 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004792 // TODO: Same as for ArrayGet.
4793 return false;
4794 }
4795
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004796 void ClearNeedsTypeCheck() {
4797 needs_type_check_ = false;
4798 }
4799
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004800 void ClearValueCanBeNull() {
4801 value_can_be_null_ = false;
4802 }
4803
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004804 void SetStaticTypeOfArrayIsObjectArray() {
4805 static_type_of_array_is_object_array_ = true;
4806 }
4807
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004808 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004809 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004810 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004811
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004812 HInstruction* GetArray() const { return InputAt(0); }
4813 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004814 HInstruction* GetValue() const { return InputAt(2); }
4815
4816 Primitive::Type GetComponentType() const {
4817 // The Dex format does not type floating point index operations. Since the
4818 // `expected_component_type_` is set during building and can therefore not
4819 // be correct, we also check what is the value type. If it is a floating
4820 // point type, we must use that type.
4821 Primitive::Type value_type = GetValue()->GetType();
4822 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4823 ? value_type
4824 : expected_component_type_;
4825 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004826
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004827 Primitive::Type GetRawExpectedComponentType() const {
4828 return expected_component_type_;
4829 }
4830
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004831 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4832 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4833 }
4834
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004835 DECLARE_INSTRUCTION(ArraySet);
4836
4837 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004838 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004839 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004840 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004841 // Cached information for the reference_type_info_ so that codegen
4842 // does not need to inspect the static type.
4843 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004844
4845 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4846};
4847
4848class HArrayLength : public HExpression<1> {
4849 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004850 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004851 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004852 // Note that arrays do not change length, so the instruction does not
4853 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004854 SetRawInputAt(0, array);
4855 }
4856
Calin Juravle77520bc2015-01-12 18:45:46 +00004857 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004858 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004859 return true;
4860 }
Calin Juravle641547a2015-04-21 22:08:51 +01004861 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4862 return obj == InputAt(0);
4863 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004864
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004865 DECLARE_INSTRUCTION(ArrayLength);
4866
4867 private:
4868 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4869};
4870
4871class HBoundsCheck : public HExpression<2> {
4872 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004873 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
4874 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004875 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004876 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004877 DCHECK(index->GetType() == Primitive::kPrimInt);
4878 SetRawInputAt(0, index);
4879 SetRawInputAt(1, length);
4880 }
4881
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004882 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004883 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004884 return true;
4885 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004886
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004887 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004888
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004889 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004890
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004891 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004892
4893 DECLARE_INSTRUCTION(BoundsCheck);
4894
4895 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004896 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4897};
4898
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004899/**
4900 * Some DEX instructions are folded into multiple HInstructions that need
4901 * to stay live until the last HInstruction. This class
4902 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004903 * HInstruction stays live. `index` represents the stack location index of the
4904 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004905 */
4906class HTemporary : public HTemplateInstruction<0> {
4907 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004908 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4909 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004910
4911 size_t GetIndex() const { return index_; }
4912
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004913 Primitive::Type GetType() const OVERRIDE {
4914 // The previous instruction is the one that will be stored in the temporary location.
4915 DCHECK(GetPrevious() != nullptr);
4916 return GetPrevious()->GetType();
4917 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004918
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004919 DECLARE_INSTRUCTION(Temporary);
4920
4921 private:
4922 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004923 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4924};
4925
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004926class HSuspendCheck : public HTemplateInstruction<0> {
4927 public:
4928 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004929 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004930
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004931 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004932 return true;
4933 }
4934
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004935 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4936 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004937
4938 DECLARE_INSTRUCTION(SuspendCheck);
4939
4940 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004941 // Only used for code generation, in order to share the same slow path between back edges
4942 // of a same loop.
4943 SlowPathCode* slow_path_;
4944
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004945 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4946};
4947
David Srbecky0cf44932015-12-09 14:09:59 +00004948// Pseudo-instruction which provides the native debugger with mapping information.
4949// It ensures that we can generate line number and local variables at this point.
4950class HNativeDebugInfo : public HTemplateInstruction<0> {
4951 public:
4952 explicit HNativeDebugInfo(uint32_t dex_pc)
4953 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
4954
4955 bool NeedsEnvironment() const OVERRIDE {
4956 return true;
4957 }
4958
4959 DECLARE_INSTRUCTION(NativeDebugInfo);
4960
4961 private:
4962 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
4963};
4964
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004965/**
4966 * Instruction to load a Class object.
4967 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004968class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004969 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004970 HLoadClass(HCurrentMethod* current_method,
4971 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004972 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004973 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004974 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004975 bool needs_access_check,
4976 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004977 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004978 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004979 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004980 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004981 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004982 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004983 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00004984 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004985 // Referrers class should not need access check. We never inline unverified
4986 // methods so we can't possibly end up in this situation.
4987 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004988 SetRawInputAt(0, current_method);
4989 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004990
4991 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004992
4993 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004994 // Note that we don't need to test for generate_clinit_check_.
4995 // Whether or not we need to generate the clinit check is processed in
4996 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004997 return other->AsLoadClass()->type_index_ == type_index_ &&
4998 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004999 }
5000
5001 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5002
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005003 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005004 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005005 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005006
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005007 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005008 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005009 }
5010
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005011 bool MustGenerateClinitCheck() const {
5012 return generate_clinit_check_;
5013 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005014
Calin Juravle0ba218d2015-05-19 18:46:01 +01005015 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005016 // The entrypoint the code generator is going to call does not do
5017 // clinit of the class.
5018 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01005019 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005020 }
5021
5022 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005023 return MustGenerateClinitCheck() ||
5024 (!is_referrers_class_ && !is_in_dex_cache_) ||
5025 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01005026 }
5027
5028 bool NeedsAccessCheck() const {
5029 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005030 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005031
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005032 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005033 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005034 }
5035
Calin Juravleacf735c2015-02-12 15:25:22 +00005036 ReferenceTypeInfo GetLoadedClassRTI() {
5037 return loaded_class_rti_;
5038 }
5039
5040 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5041 // Make sure we only set exact types (the loaded class should never be merged).
5042 DCHECK(rti.IsExact());
5043 loaded_class_rti_ = rti;
5044 }
5045
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005046 const DexFile& GetDexFile() { return dex_file_; }
5047
Vladimir Markodc151b22015-10-15 18:02:30 +01005048 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005049
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005050 static SideEffects SideEffectsForArchRuntimeCalls() {
5051 return SideEffects::CanTriggerGC();
5052 }
5053
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005054 bool IsInDexCache() const { return is_in_dex_cache_; }
5055
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005056 DECLARE_INSTRUCTION(LoadClass);
5057
5058 private:
5059 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005060 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005061 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005062 // Whether this instruction must generate the initialization check.
5063 // Used for code generation.
5064 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005065 const bool needs_access_check_;
5066 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005067
Calin Juravleacf735c2015-02-12 15:25:22 +00005068 ReferenceTypeInfo loaded_class_rti_;
5069
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005070 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5071};
5072
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005073class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005074 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005075 HLoadString(HCurrentMethod* current_method,
5076 uint32_t string_index,
5077 uint32_t dex_pc,
5078 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005079 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005080 string_index_(string_index),
5081 is_in_dex_cache_(is_in_dex_cache) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005082 SetRawInputAt(0, current_method);
5083 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005084
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005085 bool CanBeMoved() const OVERRIDE { return true; }
5086
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005087 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5088 return other->AsLoadString()->string_index_ == string_index_;
5089 }
5090
5091 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5092
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005093 uint32_t GetStringIndex() const { return string_index_; }
5094
5095 // TODO: Can we deopt or debug when we resolve a string?
5096 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01005097 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005098 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005099 bool IsInDexCache() const { return is_in_dex_cache_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005100
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005101 static SideEffects SideEffectsForArchRuntimeCalls() {
5102 return SideEffects::CanTriggerGC();
5103 }
5104
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005105 DECLARE_INSTRUCTION(LoadString);
5106
5107 private:
5108 const uint32_t string_index_;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005109 const bool is_in_dex_cache_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005110
5111 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5112};
5113
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005114/**
5115 * Performs an initialization check on its Class object input.
5116 */
5117class HClinitCheck : public HExpression<1> {
5118 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005119 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005120 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005121 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005122 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5123 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005124 SetRawInputAt(0, constant);
5125 }
5126
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005127 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005128 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005129 return true;
5130 }
5131
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005132 bool NeedsEnvironment() const OVERRIDE {
5133 // May call runtime to initialize the class.
5134 return true;
5135 }
5136
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005137 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005138
5139 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5140
5141 DECLARE_INSTRUCTION(ClinitCheck);
5142
5143 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005144 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5145};
5146
5147class HStaticFieldGet : public HExpression<1> {
5148 public:
5149 HStaticFieldGet(HInstruction* cls,
5150 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005151 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005152 bool is_volatile,
5153 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005154 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005155 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005156 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005157 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005158 : HExpression(field_type,
5159 SideEffects::FieldReadOfType(field_type, is_volatile),
5160 dex_pc),
5161 field_info_(field_offset,
5162 field_type,
5163 is_volatile,
5164 field_idx,
5165 declaring_class_def_index,
5166 dex_file,
5167 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005168 SetRawInputAt(0, cls);
5169 }
5170
Calin Juravle52c48962014-12-16 17:02:57 +00005171
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005172 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005173
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005174 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005175 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5176 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005177 }
5178
5179 size_t ComputeHashCode() const OVERRIDE {
5180 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5181 }
5182
Calin Juravle52c48962014-12-16 17:02:57 +00005183 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005184 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5185 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005186 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005187
5188 DECLARE_INSTRUCTION(StaticFieldGet);
5189
5190 private:
5191 const FieldInfo field_info_;
5192
5193 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5194};
5195
5196class HStaticFieldSet : public HTemplateInstruction<2> {
5197 public:
5198 HStaticFieldSet(HInstruction* cls,
5199 HInstruction* value,
5200 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005201 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005202 bool is_volatile,
5203 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005204 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005205 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005206 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005207 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005208 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5209 dex_pc),
5210 field_info_(field_offset,
5211 field_type,
5212 is_volatile,
5213 field_idx,
5214 declaring_class_def_index,
5215 dex_file,
5216 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005217 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005218 SetRawInputAt(0, cls);
5219 SetRawInputAt(1, value);
5220 }
5221
Calin Juravle52c48962014-12-16 17:02:57 +00005222 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005223 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5224 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005225 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005226
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005227 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005228 bool GetValueCanBeNull() const { return value_can_be_null_; }
5229 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005230
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005231 DECLARE_INSTRUCTION(StaticFieldSet);
5232
5233 private:
5234 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005235 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005236
5237 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5238};
5239
Calin Juravlee460d1d2015-09-29 04:52:17 +01005240class HUnresolvedInstanceFieldGet : public HExpression<1> {
5241 public:
5242 HUnresolvedInstanceFieldGet(HInstruction* obj,
5243 Primitive::Type field_type,
5244 uint32_t field_index,
5245 uint32_t dex_pc)
5246 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5247 field_index_(field_index) {
5248 SetRawInputAt(0, obj);
5249 }
5250
5251 bool NeedsEnvironment() const OVERRIDE { return true; }
5252 bool CanThrow() const OVERRIDE { return true; }
5253
5254 Primitive::Type GetFieldType() const { return GetType(); }
5255 uint32_t GetFieldIndex() const { return field_index_; }
5256
5257 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5258
5259 private:
5260 const uint32_t field_index_;
5261
5262 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5263};
5264
5265class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5266 public:
5267 HUnresolvedInstanceFieldSet(HInstruction* obj,
5268 HInstruction* value,
5269 Primitive::Type field_type,
5270 uint32_t field_index,
5271 uint32_t dex_pc)
5272 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5273 field_type_(field_type),
5274 field_index_(field_index) {
5275 DCHECK_EQ(field_type, value->GetType());
5276 SetRawInputAt(0, obj);
5277 SetRawInputAt(1, value);
5278 }
5279
5280 bool NeedsEnvironment() const OVERRIDE { return true; }
5281 bool CanThrow() const OVERRIDE { return true; }
5282
5283 Primitive::Type GetFieldType() const { return field_type_; }
5284 uint32_t GetFieldIndex() const { return field_index_; }
5285
5286 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5287
5288 private:
5289 const Primitive::Type field_type_;
5290 const uint32_t field_index_;
5291
5292 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5293};
5294
5295class HUnresolvedStaticFieldGet : public HExpression<0> {
5296 public:
5297 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5298 uint32_t field_index,
5299 uint32_t dex_pc)
5300 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5301 field_index_(field_index) {
5302 }
5303
5304 bool NeedsEnvironment() const OVERRIDE { return true; }
5305 bool CanThrow() const OVERRIDE { return true; }
5306
5307 Primitive::Type GetFieldType() const { return GetType(); }
5308 uint32_t GetFieldIndex() const { return field_index_; }
5309
5310 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5311
5312 private:
5313 const uint32_t field_index_;
5314
5315 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5316};
5317
5318class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5319 public:
5320 HUnresolvedStaticFieldSet(HInstruction* value,
5321 Primitive::Type field_type,
5322 uint32_t field_index,
5323 uint32_t dex_pc)
5324 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5325 field_type_(field_type),
5326 field_index_(field_index) {
5327 DCHECK_EQ(field_type, value->GetType());
5328 SetRawInputAt(0, value);
5329 }
5330
5331 bool NeedsEnvironment() const OVERRIDE { return true; }
5332 bool CanThrow() const OVERRIDE { return true; }
5333
5334 Primitive::Type GetFieldType() const { return field_type_; }
5335 uint32_t GetFieldIndex() const { return field_index_; }
5336
5337 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5338
5339 private:
5340 const Primitive::Type field_type_;
5341 const uint32_t field_index_;
5342
5343 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5344};
5345
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005346// Implement the move-exception DEX instruction.
5347class HLoadException : public HExpression<0> {
5348 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005349 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5350 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005351
David Brazdilbbd733e2015-08-18 17:48:17 +01005352 bool CanBeNull() const OVERRIDE { return false; }
5353
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005354 DECLARE_INSTRUCTION(LoadException);
5355
5356 private:
5357 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5358};
5359
David Brazdilcb1c0552015-08-04 16:22:25 +01005360// Implicit part of move-exception which clears thread-local exception storage.
5361// Must not be removed because the runtime expects the TLS to get cleared.
5362class HClearException : public HTemplateInstruction<0> {
5363 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005364 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5365 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005366
5367 DECLARE_INSTRUCTION(ClearException);
5368
5369 private:
5370 DISALLOW_COPY_AND_ASSIGN(HClearException);
5371};
5372
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005373class HThrow : public HTemplateInstruction<1> {
5374 public:
5375 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005376 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005377 SetRawInputAt(0, exception);
5378 }
5379
5380 bool IsControlFlow() const OVERRIDE { return true; }
5381
5382 bool NeedsEnvironment() const OVERRIDE { return true; }
5383
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005384 bool CanThrow() const OVERRIDE { return true; }
5385
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005386
5387 DECLARE_INSTRUCTION(Throw);
5388
5389 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005390 DISALLOW_COPY_AND_ASSIGN(HThrow);
5391};
5392
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005393/**
5394 * Implementation strategies for the code generator of a HInstanceOf
5395 * or `HCheckCast`.
5396 */
5397enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005398 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005399 kExactCheck, // Can do a single class compare.
5400 kClassHierarchyCheck, // Can just walk the super class chain.
5401 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5402 kInterfaceCheck, // No optimization yet when checking against an interface.
5403 kArrayObjectCheck, // Can just check if the array is not primitive.
5404 kArrayCheck // No optimization yet when checking against a generic array.
5405};
5406
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005407class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005408 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005409 HInstanceOf(HInstruction* object,
5410 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005411 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005412 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005413 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005414 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005415 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005416 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005417 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005418 SetRawInputAt(0, object);
5419 SetRawInputAt(1, constant);
5420 }
5421
5422 bool CanBeMoved() const OVERRIDE { return true; }
5423
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005424 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005425 return true;
5426 }
5427
5428 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005429 return false;
5430 }
5431
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005432 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5433
5434 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005435
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005436 // Used only in code generation.
5437 bool MustDoNullCheck() const { return must_do_null_check_; }
5438 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5439
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005440 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5441 return (check_kind == TypeCheckKind::kExactCheck)
5442 ? SideEffects::None()
5443 // Mips currently does runtime calls for any other checks.
5444 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005445 }
5446
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005447 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005448
5449 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005450 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005451 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005452
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005453 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5454};
5455
Calin Juravleb1498f62015-02-16 13:13:29 +00005456class HBoundType : public HExpression<1> {
5457 public:
David Brazdilf5552582015-12-27 13:36:12 +00005458 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005459 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
David Brazdilf5552582015-12-27 13:36:12 +00005460 upper_bound_(ReferenceTypeInfo::CreateInvalid()),
5461 upper_can_be_null_(true),
5462 can_be_null_(true) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005463 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005464 SetRawInputAt(0, input);
5465 }
5466
David Brazdilf5552582015-12-27 13:36:12 +00005467 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005468 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5469 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
David Brazdilf5552582015-12-27 13:36:12 +00005470 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005471
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005472 void SetCanBeNull(bool can_be_null) {
5473 DCHECK(upper_can_be_null_ || !can_be_null);
5474 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005475 }
5476
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005477 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5478
Calin Juravleb1498f62015-02-16 13:13:29 +00005479 DECLARE_INSTRUCTION(BoundType);
5480
5481 private:
5482 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005483 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5484 // It is used to bound the type in cases like:
5485 // if (x instanceof ClassX) {
5486 // // uper_bound_ will be ClassX
5487 // }
David Brazdilf5552582015-12-27 13:36:12 +00005488 ReferenceTypeInfo upper_bound_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005489 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5490 // is false then can_be_null_ cannot be true).
David Brazdilf5552582015-12-27 13:36:12 +00005491 bool upper_can_be_null_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005492 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005493
5494 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5495};
5496
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005497class HCheckCast : public HTemplateInstruction<2> {
5498 public:
5499 HCheckCast(HInstruction* object,
5500 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005501 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005502 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005503 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005504 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005505 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005506 SetRawInputAt(0, object);
5507 SetRawInputAt(1, constant);
5508 }
5509
5510 bool CanBeMoved() const OVERRIDE { return true; }
5511
5512 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5513 return true;
5514 }
5515
5516 bool NeedsEnvironment() const OVERRIDE {
5517 // Instruction may throw a CheckCastError.
5518 return true;
5519 }
5520
5521 bool CanThrow() const OVERRIDE { return true; }
5522
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005523 bool MustDoNullCheck() const { return must_do_null_check_; }
5524 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005525 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005526
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005527 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005528
5529 DECLARE_INSTRUCTION(CheckCast);
5530
5531 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005532 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005533 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005534
5535 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005536};
5537
Calin Juravle27df7582015-04-17 19:12:31 +01005538class HMemoryBarrier : public HTemplateInstruction<0> {
5539 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005540 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005541 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005542 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005543 barrier_kind_(barrier_kind) {}
5544
5545 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5546
5547 DECLARE_INSTRUCTION(MemoryBarrier);
5548
5549 private:
5550 const MemBarrierKind barrier_kind_;
5551
5552 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5553};
5554
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005555class HMonitorOperation : public HTemplateInstruction<1> {
5556 public:
5557 enum OperationKind {
5558 kEnter,
5559 kExit,
5560 };
5561
5562 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005563 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005564 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5565 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005566 SetRawInputAt(0, object);
5567 }
5568
5569 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005570 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5571
5572 bool CanThrow() const OVERRIDE {
5573 // Verifier guarantees that monitor-exit cannot throw.
5574 // This is important because it allows the HGraphBuilder to remove
5575 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5576 return IsEnter();
5577 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005578
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005579
5580 bool IsEnter() const { return kind_ == kEnter; }
5581
5582 DECLARE_INSTRUCTION(MonitorOperation);
5583
Calin Juravle52c48962014-12-16 17:02:57 +00005584 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005585 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005586
5587 private:
5588 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5589};
5590
David Brazdil74eb1b22015-12-14 11:44:01 +00005591class HSelect : public HExpression<3> {
5592 public:
5593 HSelect(HInstruction* condition,
5594 HInstruction* true_value,
5595 HInstruction* false_value,
5596 uint32_t dex_pc)
5597 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
5598 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
5599
5600 // First input must be `true_value` or `false_value` to allow codegens to
5601 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
5602 // that architectures which implement HSelect as a conditional move also
5603 // will not need to invert the condition.
5604 SetRawInputAt(0, false_value);
5605 SetRawInputAt(1, true_value);
5606 SetRawInputAt(2, condition);
5607 }
5608
5609 HInstruction* GetFalseValue() const { return InputAt(0); }
5610 HInstruction* GetTrueValue() const { return InputAt(1); }
5611 HInstruction* GetCondition() const { return InputAt(2); }
5612
5613 bool CanBeMoved() const OVERRIDE { return true; }
5614 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
5615
5616 bool CanBeNull() const OVERRIDE {
5617 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
5618 }
5619
5620 DECLARE_INSTRUCTION(Select);
5621
5622 private:
5623 DISALLOW_COPY_AND_ASSIGN(HSelect);
5624};
5625
Vladimir Markof9f64412015-09-02 14:05:49 +01005626class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005627 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005628 MoveOperands(Location source,
5629 Location destination,
5630 Primitive::Type type,
5631 HInstruction* instruction)
5632 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005633
5634 Location GetSource() const { return source_; }
5635 Location GetDestination() const { return destination_; }
5636
5637 void SetSource(Location value) { source_ = value; }
5638 void SetDestination(Location value) { destination_ = value; }
5639
5640 // The parallel move resolver marks moves as "in-progress" by clearing the
5641 // destination (but not the source).
5642 Location MarkPending() {
5643 DCHECK(!IsPending());
5644 Location dest = destination_;
5645 destination_ = Location::NoLocation();
5646 return dest;
5647 }
5648
5649 void ClearPending(Location dest) {
5650 DCHECK(IsPending());
5651 destination_ = dest;
5652 }
5653
5654 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00005655 DCHECK(source_.IsValid() || destination_.IsInvalid());
5656 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005657 }
5658
5659 // True if this blocks a move from the given location.
5660 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005661 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005662 }
5663
5664 // A move is redundant if it's been eliminated, if its source and
5665 // destination are the same, or if its destination is unneeded.
5666 bool IsRedundant() const {
5667 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5668 }
5669
5670 // We clear both operands to indicate move that's been eliminated.
5671 void Eliminate() {
5672 source_ = destination_ = Location::NoLocation();
5673 }
5674
5675 bool IsEliminated() const {
5676 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5677 return source_.IsInvalid();
5678 }
5679
Alexey Frunze4dda3372015-06-01 18:31:49 -07005680 Primitive::Type GetType() const { return type_; }
5681
Nicolas Geoffray90218252015-04-15 11:56:51 +01005682 bool Is64BitMove() const {
5683 return Primitive::Is64BitType(type_);
5684 }
5685
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005686 HInstruction* GetInstruction() const { return instruction_; }
5687
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005688 private:
5689 Location source_;
5690 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005691 // The type this move is for.
5692 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005693 // The instruction this move is assocatied with. Null when this move is
5694 // for moving an input in the expected locations of user (including a phi user).
5695 // This is only used in debug mode, to ensure we do not connect interval siblings
5696 // in the same parallel move.
5697 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005698};
5699
Roland Levillainc9285912015-12-18 10:38:42 +00005700std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
5701
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005702static constexpr size_t kDefaultNumberOfMoves = 4;
5703
5704class HParallelMove : public HTemplateInstruction<0> {
5705 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005706 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005707 : HTemplateInstruction(SideEffects::None(), dex_pc),
5708 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5709 moves_.reserve(kDefaultNumberOfMoves);
5710 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005711
Nicolas Geoffray90218252015-04-15 11:56:51 +01005712 void AddMove(Location source,
5713 Location destination,
5714 Primitive::Type type,
5715 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005716 DCHECK(source.IsValid());
5717 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005718 if (kIsDebugBuild) {
5719 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005720 for (const MoveOperands& move : moves_) {
5721 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005722 // Special case the situation where the move is for the spill slot
5723 // of the instruction.
5724 if ((GetPrevious() == instruction)
5725 || ((GetPrevious() == nullptr)
5726 && instruction->IsPhi()
5727 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005728 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005729 << "Doing parallel moves for the same instruction.";
5730 } else {
5731 DCHECK(false) << "Doing parallel moves for the same instruction.";
5732 }
5733 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005734 }
5735 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005736 for (const MoveOperands& move : moves_) {
5737 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005738 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005739 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005740 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005741 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005742 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005743 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005744 }
5745
Vladimir Marko225b6462015-09-28 12:17:40 +01005746 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005747 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005748 }
5749
Vladimir Marko225b6462015-09-28 12:17:40 +01005750 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005751
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005752 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005753
5754 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005755 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005756
5757 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5758};
5759
Mark Mendell0616ae02015-04-17 12:49:27 -04005760} // namespace art
5761
Vladimir Markob4536b72015-11-24 13:45:23 +00005762#ifdef ART_ENABLE_CODEGEN_arm
5763#include "nodes_arm.h"
5764#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005765#ifdef ART_ENABLE_CODEGEN_arm64
5766#include "nodes_arm64.h"
5767#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005768#ifdef ART_ENABLE_CODEGEN_x86
5769#include "nodes_x86.h"
5770#endif
5771
5772namespace art {
5773
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005774class HGraphVisitor : public ValueObject {
5775 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005776 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5777 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005778
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005779 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005780 virtual void VisitBasicBlock(HBasicBlock* block);
5781
Roland Levillain633021e2014-10-01 14:12:25 +01005782 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005783 void VisitInsertionOrder();
5784
Roland Levillain633021e2014-10-01 14:12:25 +01005785 // Visit the graph following dominator tree reverse post-order.
5786 void VisitReversePostOrder();
5787
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005788 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005789
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005790 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005791#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005792 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5793
5794 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5795
5796#undef DECLARE_VISIT_INSTRUCTION
5797
5798 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005799 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005800
5801 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5802};
5803
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005804class HGraphDelegateVisitor : public HGraphVisitor {
5805 public:
5806 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5807 virtual ~HGraphDelegateVisitor() {}
5808
5809 // Visit functions that delegate to to super class.
5810#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005811 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005812
5813 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5814
5815#undef DECLARE_VISIT_INSTRUCTION
5816
5817 private:
5818 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5819};
5820
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005821class HInsertionOrderIterator : public ValueObject {
5822 public:
5823 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5824
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005825 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005826 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005827 void Advance() { ++index_; }
5828
5829 private:
5830 const HGraph& graph_;
5831 size_t index_;
5832
5833 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5834};
5835
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005836class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005837 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005838 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5839 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005840 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005841 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005842
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005843 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5844 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005845 void Advance() { ++index_; }
5846
5847 private:
5848 const HGraph& graph_;
5849 size_t index_;
5850
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005851 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005852};
5853
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005854class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005855 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005856 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005857 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005858 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005859 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005860 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005861
5862 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005863 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005864 void Advance() { --index_; }
5865
5866 private:
5867 const HGraph& graph_;
5868 size_t index_;
5869
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005870 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005871};
5872
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005873class HLinearPostOrderIterator : public ValueObject {
5874 public:
5875 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005876 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005877
5878 bool Done() const { return index_ == 0; }
5879
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005880 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005881
5882 void Advance() {
5883 --index_;
5884 DCHECK_GE(index_, 0U);
5885 }
5886
5887 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005888 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005889 size_t index_;
5890
5891 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5892};
5893
5894class HLinearOrderIterator : public ValueObject {
5895 public:
5896 explicit HLinearOrderIterator(const HGraph& graph)
5897 : order_(graph.GetLinearOrder()), index_(0) {}
5898
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005899 bool Done() const { return index_ == order_.size(); }
5900 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005901 void Advance() { ++index_; }
5902
5903 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005904 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005905 size_t index_;
5906
5907 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5908};
5909
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005910// Iterator over the blocks that art part of the loop. Includes blocks part
5911// of an inner loop. The order in which the blocks are iterated is on their
5912// block id.
5913class HBlocksInLoopIterator : public ValueObject {
5914 public:
5915 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5916 : blocks_in_loop_(info.GetBlocks()),
5917 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5918 index_(0) {
5919 if (!blocks_in_loop_.IsBitSet(index_)) {
5920 Advance();
5921 }
5922 }
5923
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005924 bool Done() const { return index_ == blocks_.size(); }
5925 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005926 void Advance() {
5927 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005928 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005929 if (blocks_in_loop_.IsBitSet(index_)) {
5930 break;
5931 }
5932 }
5933 }
5934
5935 private:
5936 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005937 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005938 size_t index_;
5939
5940 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5941};
5942
Mingyao Yang3584bce2015-05-19 16:01:59 -07005943// Iterator over the blocks that art part of the loop. Includes blocks part
5944// of an inner loop. The order in which the blocks are iterated is reverse
5945// post order.
5946class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5947 public:
5948 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5949 : blocks_in_loop_(info.GetBlocks()),
5950 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5951 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005952 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005953 Advance();
5954 }
5955 }
5956
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005957 bool Done() const { return index_ == blocks_.size(); }
5958 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005959 void Advance() {
5960 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005961 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5962 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005963 break;
5964 }
5965 }
5966 }
5967
5968 private:
5969 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005970 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005971 size_t index_;
5972
5973 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5974};
5975
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005976inline int64_t Int64FromConstant(HConstant* constant) {
5977 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5978 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5979 : constant->AsLongConstant()->GetValue();
5980}
5981
Vladimir Marko58155012015-08-19 12:49:41 +00005982inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5983 // For the purposes of the compiler, the dex files must actually be the same object
5984 // if we want to safely treat them as the same. This is especially important for JIT
5985 // as custom class loaders can open the same underlying file (or memory) multiple
5986 // times and provide different class resolution but no two class loaders should ever
5987 // use the same DexFile object - doing so is an unsupported hack that can lead to
5988 // all sorts of weird failures.
5989 return &lhs == &rhs;
5990}
5991
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005992#define INSTRUCTION_TYPE_CHECK(type, super) \
5993 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
5994 inline const H##type* HInstruction::As##type() const { \
5995 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
5996 } \
5997 inline H##type* HInstruction::As##type() { \
5998 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
5999 }
6000
6001 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6002#undef INSTRUCTION_TYPE_CHECK
6003
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006004} // namespace art
6005
6006#endif // ART_COMPILER_OPTIMIZING_NODES_H_