blob: 922696bb5d04d2231600cdeb7bea99337ae6fd45 [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);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001014 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001015 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001016 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1017 // instruction list. With 'ensure_safety' set to true, it verifies that the
1018 // instruction is not in use and removes it from the use lists of its inputs.
1019 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1020 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001021 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001022
1023 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001024 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001025 }
1026
Roland Levillain6b879dd2014-09-22 17:13:44 +01001027 bool IsLoopPreHeaderFirstPredecessor() const {
1028 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001029 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001030 }
1031
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001032 bool IsFirstPredecessorBackEdge() const {
1033 DCHECK(IsLoopHeader());
1034 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1035 }
1036
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001037 HLoopInformation* GetLoopInformation() const {
1038 return loop_information_;
1039 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001040
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001041 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001042 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001043 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001044 void SetInLoop(HLoopInformation* info) {
1045 if (IsLoopHeader()) {
1046 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001047 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001048 loop_information_ = info;
1049 } else if (loop_information_->Contains(*info->GetHeader())) {
1050 // Block is currently part of an outer loop. Make it part of this inner loop.
1051 // Note that a non loop header having a loop information means this loop information
1052 // has already been populated
1053 loop_information_ = info;
1054 } else {
1055 // Block is part of an inner loop. Do not update the loop information.
1056 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1057 // at this point, because this method is being called while populating `info`.
1058 }
1059 }
1060
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001061 // Raw update of the loop information.
1062 void SetLoopInformation(HLoopInformation* info) {
1063 loop_information_ = info;
1064 }
1065
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001066 bool IsInLoop() const { return loop_information_ != nullptr; }
1067
David Brazdilec16f792015-08-19 15:04:01 +01001068 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1069
1070 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1071 try_catch_information_ = try_catch_information;
1072 }
1073
1074 bool IsTryBlock() const {
1075 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1076 }
1077
1078 bool IsCatchBlock() const {
1079 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1080 }
David Brazdilffee3d32015-07-06 11:48:53 +01001081
1082 // Returns the try entry that this block's successors should have. They will
1083 // be in the same try, unless the block ends in a try boundary. In that case,
1084 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001085 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001086
David Brazdild7558da2015-09-22 13:04:14 +01001087 bool HasThrowingInstructions() const;
1088
David Brazdila4b8c212015-05-07 09:59:30 +01001089 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001090 bool Dominates(HBasicBlock* block) const;
1091
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001092 size_t GetLifetimeStart() const { return lifetime_start_; }
1093 size_t GetLifetimeEnd() const { return lifetime_end_; }
1094
1095 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1096 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1097
David Brazdil8d5b8b22015-03-24 10:51:52 +00001098 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001099 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001100 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001101 bool HasSinglePhi() const;
1102
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001103 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001104 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001105 ArenaVector<HBasicBlock*> predecessors_;
1106 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001107 HInstructionList instructions_;
1108 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001109 HLoopInformation* loop_information_;
1110 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001111 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001112 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001113 // The dex program counter of the first instruction of this block.
1114 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001115 size_t lifetime_start_;
1116 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001117 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001118
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001119 friend class HGraph;
1120 friend class HInstruction;
1121
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001122 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1123};
1124
David Brazdilb2bd1c52015-03-25 11:17:37 +00001125// Iterates over the LoopInformation of all loops which contain 'block'
1126// from the innermost to the outermost.
1127class HLoopInformationOutwardIterator : public ValueObject {
1128 public:
1129 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1130 : current_(block.GetLoopInformation()) {}
1131
1132 bool Done() const { return current_ == nullptr; }
1133
1134 void Advance() {
1135 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001136 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001137 }
1138
1139 HLoopInformation* Current() const {
1140 DCHECK(!Done());
1141 return current_;
1142 }
1143
1144 private:
1145 HLoopInformation* current_;
1146
1147 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1148};
1149
Alexandre Ramesef20f712015-06-09 10:29:30 +01001150#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001151 M(Above, Condition) \
1152 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001153 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001154 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001155 M(ArrayGet, Instruction) \
1156 M(ArrayLength, Instruction) \
1157 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001158 M(Below, Condition) \
1159 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001160 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001161 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001162 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001163 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001164 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001165 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001166 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001167 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001168 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001169 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001170 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001171 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001172 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001173 M(Exit, Instruction) \
1174 M(FloatConstant, Constant) \
1175 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001176 M(GreaterThan, Condition) \
1177 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001178 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001179 M(InstanceFieldGet, Instruction) \
1180 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001181 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001182 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001183 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001184 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001185 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001186 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001187 M(LessThan, Condition) \
1188 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001189 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001190 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001191 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001192 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001193 M(Local, Instruction) \
1194 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001195 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001196 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001197 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001198 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001199 M(Neg, UnaryOperation) \
1200 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001201 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001202 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001203 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001204 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001205 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001206 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001207 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001208 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001209 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001210 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001211 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001212 M(Return, Instruction) \
1213 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001214 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001215 M(Shl, BinaryOperation) \
1216 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001217 M(StaticFieldGet, Instruction) \
1218 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001219 M(UnresolvedInstanceFieldGet, Instruction) \
1220 M(UnresolvedInstanceFieldSet, Instruction) \
1221 M(UnresolvedStaticFieldGet, Instruction) \
1222 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001223 M(StoreLocal, Instruction) \
1224 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001225 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001226 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001227 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001228 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001229 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001230 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001231 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001232
Vladimir Markob4536b72015-11-24 13:45:23 +00001233#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001234#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001235#else
1236#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1237 M(ArmDexCacheArraysBase, Instruction)
1238#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001239
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001240#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001241#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001242#else
1243#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001244 M(Arm64DataProcWithShifterOp, Instruction) \
Nicolas Geoffray6b5afdd2016-01-22 09:31:52 +00001245 M(Arm64IntermediateAddress, Instruction) \
1246 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001247#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001248
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001249#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1250
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001251#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1252
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001253#ifndef ART_ENABLE_CODEGEN_x86
1254#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1255#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001256#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1257 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001258 M(X86LoadFromConstantTable, Instruction) \
1259 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001260#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001261
1262#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1263
1264#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1265 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1266 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1267 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001268 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001269 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001270 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1271 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1272
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001273#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1274 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001275 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001276 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001277 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001278 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001279
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001280#define FOR_EACH_INSTRUCTION(M) \
1281 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1282 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1283
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001284#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001285FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1286#undef FORWARD_DECLARATION
1287
Roland Levillainccc07a92014-09-16 14:48:16 +01001288#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001289 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001290 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001291 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001292 return other->Is##type(); \
1293 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001294 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001295
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001296#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1297 bool Is##type() const { return As##type() != nullptr; } \
1298 const H##type* As##type() const { return this; } \
1299 H##type* As##type() { return this; }
1300
David Brazdiled596192015-01-23 10:39:45 +00001301template <typename T> class HUseList;
1302
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001303template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001304class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001305 public:
David Brazdiled596192015-01-23 10:39:45 +00001306 HUseListNode* GetPrevious() const { return prev_; }
1307 HUseListNode* GetNext() const { return next_; }
1308 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001309 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001310 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001311
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001312 private:
David Brazdiled596192015-01-23 10:39:45 +00001313 HUseListNode(T user, size_t index)
1314 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1315
1316 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001317 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001318 HUseListNode<T>* prev_;
1319 HUseListNode<T>* next_;
1320
1321 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001322
1323 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1324};
1325
David Brazdiled596192015-01-23 10:39:45 +00001326template <typename T>
1327class HUseList : public ValueObject {
1328 public:
1329 HUseList() : first_(nullptr) {}
1330
1331 void Clear() {
1332 first_ = nullptr;
1333 }
1334
1335 // Adds a new entry at the beginning of the use list and returns
1336 // the newly created node.
1337 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001338 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001339 if (IsEmpty()) {
1340 first_ = new_node;
1341 } else {
1342 first_->prev_ = new_node;
1343 new_node->next_ = first_;
1344 first_ = new_node;
1345 }
1346 return new_node;
1347 }
1348
1349 HUseListNode<T>* GetFirst() const {
1350 return first_;
1351 }
1352
1353 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001354 DCHECK(node != nullptr);
1355 DCHECK(Contains(node));
1356
David Brazdiled596192015-01-23 10:39:45 +00001357 if (node->prev_ != nullptr) {
1358 node->prev_->next_ = node->next_;
1359 }
1360 if (node->next_ != nullptr) {
1361 node->next_->prev_ = node->prev_;
1362 }
1363 if (node == first_) {
1364 first_ = node->next_;
1365 }
1366 }
1367
David Brazdil1abb4192015-02-17 18:33:36 +00001368 bool Contains(const HUseListNode<T>* node) const {
1369 if (node == nullptr) {
1370 return false;
1371 }
1372 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1373 if (current == node) {
1374 return true;
1375 }
1376 }
1377 return false;
1378 }
1379
David Brazdiled596192015-01-23 10:39:45 +00001380 bool IsEmpty() const {
1381 return first_ == nullptr;
1382 }
1383
1384 bool HasOnlyOneUse() const {
1385 return first_ != nullptr && first_->next_ == nullptr;
1386 }
1387
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001388 size_t SizeSlow() const {
1389 size_t count = 0;
1390 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1391 ++count;
1392 }
1393 return count;
1394 }
1395
David Brazdiled596192015-01-23 10:39:45 +00001396 private:
1397 HUseListNode<T>* first_;
1398};
1399
1400template<typename T>
1401class HUseIterator : public ValueObject {
1402 public:
1403 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1404
1405 bool Done() const { return current_ == nullptr; }
1406
1407 void Advance() {
1408 DCHECK(!Done());
1409 current_ = current_->GetNext();
1410 }
1411
1412 HUseListNode<T>* Current() const {
1413 DCHECK(!Done());
1414 return current_;
1415 }
1416
1417 private:
1418 HUseListNode<T>* current_;
1419
1420 friend class HValue;
1421};
1422
David Brazdil1abb4192015-02-17 18:33:36 +00001423// This class is used by HEnvironment and HInstruction classes to record the
1424// instructions they use and pointers to the corresponding HUseListNodes kept
1425// by the used instructions.
1426template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001427class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001428 public:
1429 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1430 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1431
1432 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1433 : instruction_(old_record.instruction_), use_node_(use_node) {
1434 DCHECK(instruction_ != nullptr);
1435 DCHECK(use_node_ != nullptr);
1436 DCHECK(old_record.use_node_ == nullptr);
1437 }
1438
1439 HInstruction* GetInstruction() const { return instruction_; }
1440 HUseListNode<T>* GetUseNode() const { return use_node_; }
1441
1442 private:
1443 // Instruction used by the user.
1444 HInstruction* instruction_;
1445
1446 // Corresponding entry in the use list kept by 'instruction_'.
1447 HUseListNode<T>* use_node_;
1448};
1449
Aart Bik854a02b2015-07-14 16:07:00 -07001450/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001451 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001452 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001453 * For write/read dependences on fields/arrays, the dependence analysis uses
1454 * type disambiguation (e.g. a float field write cannot modify the value of an
1455 * integer field read) and the access type (e.g. a reference array write cannot
1456 * modify the value of a reference field read [although it may modify the
1457 * reference fetch prior to reading the field, which is represented by its own
1458 * write/read dependence]). The analysis makes conservative points-to
1459 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1460 * the same, and any reference read depends on any reference read without
1461 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001462 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001463 * The internal representation uses 38-bit and is described in the table below.
1464 * The first line indicates the side effect, and for field/array accesses the
1465 * second line indicates the type of the access (in the order of the
1466 * Primitive::Type enum).
1467 * The two numbered lines below indicate the bit position in the bitfield (read
1468 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001469 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001470 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1471 * +-------------+---------+---------+--------------+---------+---------+
1472 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1473 * | 3 |333333322|222222221| 1 |111111110|000000000|
1474 * | 7 |654321098|765432109| 8 |765432109|876543210|
1475 *
1476 * Note that, to ease the implementation, 'changes' bits are least significant
1477 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001478 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001479class SideEffects : public ValueObject {
1480 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001481 SideEffects() : flags_(0) {}
1482
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001483 static SideEffects None() {
1484 return SideEffects(0);
1485 }
1486
1487 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001488 return SideEffects(kAllChangeBits | kAllDependOnBits);
1489 }
1490
1491 static SideEffects AllChanges() {
1492 return SideEffects(kAllChangeBits);
1493 }
1494
1495 static SideEffects AllDependencies() {
1496 return SideEffects(kAllDependOnBits);
1497 }
1498
1499 static SideEffects AllExceptGCDependency() {
1500 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1501 }
1502
1503 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001504 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001505 }
1506
Aart Bik34c3ba92015-07-20 14:08:59 -07001507 static SideEffects AllWrites() {
1508 return SideEffects(kAllWrites);
1509 }
1510
1511 static SideEffects AllReads() {
1512 return SideEffects(kAllReads);
1513 }
1514
1515 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1516 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001517 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001518 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001519 }
1520
Aart Bik854a02b2015-07-14 16:07:00 -07001521 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1522 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001523 }
1524
Aart Bik34c3ba92015-07-20 14:08:59 -07001525 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1526 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001527 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001528 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001529 }
1530
1531 static SideEffects ArrayReadOfType(Primitive::Type type) {
1532 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1533 }
1534
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001535 static SideEffects CanTriggerGC() {
1536 return SideEffects(1ULL << kCanTriggerGCBit);
1537 }
1538
1539 static SideEffects DependsOnGC() {
1540 return SideEffects(1ULL << kDependsOnGCBit);
1541 }
1542
Aart Bik854a02b2015-07-14 16:07:00 -07001543 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001544 SideEffects Union(SideEffects other) const {
1545 return SideEffects(flags_ | other.flags_);
1546 }
1547
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001548 SideEffects Exclusion(SideEffects other) const {
1549 return SideEffects(flags_ & ~other.flags_);
1550 }
1551
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001552 void Add(SideEffects other) {
1553 flags_ |= other.flags_;
1554 }
1555
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001556 bool Includes(SideEffects other) const {
1557 return (other.flags_ & flags_) == other.flags_;
1558 }
1559
1560 bool HasSideEffects() const {
1561 return (flags_ & kAllChangeBits);
1562 }
1563
1564 bool HasDependencies() const {
1565 return (flags_ & kAllDependOnBits);
1566 }
1567
1568 // Returns true if there are no side effects or dependencies.
1569 bool DoesNothing() const {
1570 return flags_ == 0;
1571 }
1572
Aart Bik854a02b2015-07-14 16:07:00 -07001573 // Returns true if something is written.
1574 bool DoesAnyWrite() const {
1575 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001576 }
1577
Aart Bik854a02b2015-07-14 16:07:00 -07001578 // Returns true if something is read.
1579 bool DoesAnyRead() const {
1580 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001581 }
1582
Aart Bik854a02b2015-07-14 16:07:00 -07001583 // Returns true if potentially everything is written and read
1584 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001585 bool DoesAllReadWrite() const {
1586 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1587 }
1588
Aart Bik854a02b2015-07-14 16:07:00 -07001589 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001590 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001591 }
1592
Roland Levillain0d5a2812015-11-13 10:07:31 +00001593 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001594 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001595 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1596 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001597 }
1598
1599 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001600 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001601 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001602 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001603 for (int s = kLastBit; s >= 0; s--) {
1604 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1605 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1606 // This is a bit for the GC side effect.
1607 if (current_bit_is_set) {
1608 flags += "GC";
1609 }
Aart Bik854a02b2015-07-14 16:07:00 -07001610 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001611 } else {
1612 // This is a bit for the array/field analysis.
1613 // The underscore character stands for the 'can trigger GC' bit.
1614 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1615 if (current_bit_is_set) {
1616 flags += kDebug[s];
1617 }
1618 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1619 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1620 flags += "|";
1621 }
1622 }
Aart Bik854a02b2015-07-14 16:07:00 -07001623 }
1624 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001625 }
1626
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001627 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001628
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001629 private:
1630 static constexpr int kFieldArrayAnalysisBits = 9;
1631
1632 static constexpr int kFieldWriteOffset = 0;
1633 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1634 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1635 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1636
1637 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1638
1639 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1640 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1641 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1642 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1643
1644 static constexpr int kLastBit = kDependsOnGCBit;
1645 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1646
1647 // Aliases.
1648
1649 static_assert(kChangeBits == kDependOnBits,
1650 "the 'change' bits should match the 'depend on' bits.");
1651
1652 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1653 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1654 static constexpr uint64_t kAllWrites =
1655 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1656 static constexpr uint64_t kAllReads =
1657 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001658
Aart Bik854a02b2015-07-14 16:07:00 -07001659 // Work around the fact that HIR aliases I/F and J/D.
1660 // TODO: remove this interceptor once HIR types are clean
1661 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1662 switch (type) {
1663 case Primitive::kPrimInt:
1664 case Primitive::kPrimFloat:
1665 return TypeFlag(Primitive::kPrimInt, offset) |
1666 TypeFlag(Primitive::kPrimFloat, offset);
1667 case Primitive::kPrimLong:
1668 case Primitive::kPrimDouble:
1669 return TypeFlag(Primitive::kPrimLong, offset) |
1670 TypeFlag(Primitive::kPrimDouble, offset);
1671 default:
1672 return TypeFlag(type, offset);
1673 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001674 }
1675
Aart Bik854a02b2015-07-14 16:07:00 -07001676 // Translates type to bit flag.
1677 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1678 CHECK_NE(type, Primitive::kPrimVoid);
1679 const uint64_t one = 1;
1680 const int shift = type; // 0-based consecutive enum
1681 DCHECK_LE(kFieldWriteOffset, shift);
1682 DCHECK_LT(shift, kArrayWriteOffset);
1683 return one << (type + offset);
1684 }
1685
1686 // Private constructor on direct flags value.
1687 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1688
1689 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001690};
1691
David Brazdiled596192015-01-23 10:39:45 +00001692// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001693class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001694 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001695 HEnvironment(ArenaAllocator* arena,
1696 size_t number_of_vregs,
1697 const DexFile& dex_file,
1698 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001699 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001700 InvokeType invoke_type,
1701 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001702 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1703 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001704 parent_(nullptr),
1705 dex_file_(dex_file),
1706 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001707 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001708 invoke_type_(invoke_type),
1709 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001710 }
1711
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001712 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001713 : HEnvironment(arena,
1714 to_copy.Size(),
1715 to_copy.GetDexFile(),
1716 to_copy.GetMethodIdx(),
1717 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001718 to_copy.GetInvokeType(),
1719 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001720
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001721 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001722 if (parent_ != nullptr) {
1723 parent_->SetAndCopyParentChain(allocator, parent);
1724 } else {
1725 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1726 parent_->CopyFrom(parent);
1727 if (parent->GetParent() != nullptr) {
1728 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1729 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001730 }
David Brazdiled596192015-01-23 10:39:45 +00001731 }
1732
Vladimir Marko71bf8092015-09-15 15:33:14 +01001733 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001734 void CopyFrom(HEnvironment* environment);
1735
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001736 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1737 // input to the loop phi instead. This is for inserting instructions that
1738 // require an environment (like HDeoptimization) in the loop pre-header.
1739 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001740
1741 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001742 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001743 }
1744
1745 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001746 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001747 }
1748
David Brazdil1abb4192015-02-17 18:33:36 +00001749 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001750
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001751 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001752
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001753 HEnvironment* GetParent() const { return parent_; }
1754
1755 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001756 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001757 }
1758
1759 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001760 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001761 }
1762
1763 uint32_t GetDexPc() const {
1764 return dex_pc_;
1765 }
1766
1767 uint32_t GetMethodIdx() const {
1768 return method_idx_;
1769 }
1770
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001771 InvokeType GetInvokeType() const {
1772 return invoke_type_;
1773 }
1774
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001775 const DexFile& GetDexFile() const {
1776 return dex_file_;
1777 }
1778
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001779 HInstruction* GetHolder() const {
1780 return holder_;
1781 }
1782
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001783
1784 bool IsFromInlinedInvoke() const {
1785 return GetParent() != nullptr;
1786 }
1787
David Brazdiled596192015-01-23 10:39:45 +00001788 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001789 // Record instructions' use entries of this environment for constant-time removal.
1790 // It should only be called by HInstruction when a new environment use is added.
1791 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1792 DCHECK(env_use->GetUser() == this);
1793 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001794 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001795 }
David Brazdiled596192015-01-23 10:39:45 +00001796
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001797 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1798 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001799 HEnvironment* parent_;
1800 const DexFile& dex_file_;
1801 const uint32_t method_idx_;
1802 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001803 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001804
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001805 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001806 HInstruction* const holder_;
1807
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001808 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001809
1810 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1811};
1812
Vladimir Markof9f64412015-09-02 14:05:49 +01001813class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001814 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001815 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001816 : previous_(nullptr),
1817 next_(nullptr),
1818 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001819 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001820 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001821 ssa_index_(-1),
David Brazdilb3e773e2016-01-26 11:28:37 +00001822 emitted_at_use_site_(false),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001823 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001824 locations_(nullptr),
1825 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001826 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001827 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001828 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001829
Dave Allison20dfc792014-06-16 20:44:29 -07001830 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001831
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001832#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001833 enum InstructionKind {
1834 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1835 };
1836#undef DECLARE_KIND
1837
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001838 HInstruction* GetNext() const { return next_; }
1839 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001840
Calin Juravle77520bc2015-01-12 18:45:46 +00001841 HInstruction* GetNextDisregardingMoves() const;
1842 HInstruction* GetPreviousDisregardingMoves() const;
1843
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001844 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001845 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001846 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001847 bool IsInBlock() const { return block_ != nullptr; }
1848 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001849 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1850 bool IsIrreducibleLoopHeaderPhi() const {
1851 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1852 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001853
Roland Levillain6b879dd2014-09-22 17:13:44 +01001854 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001855 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001856
1857 virtual void Accept(HGraphVisitor* visitor) = 0;
1858 virtual const char* DebugName() const = 0;
1859
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001860 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001861 void SetRawInputAt(size_t index, HInstruction* input) {
1862 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1863 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001864
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001865 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001866
1867 uint32_t GetDexPc() const { return dex_pc_; }
1868
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001869 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001870
Roland Levillaine161a2a2014-10-03 12:45:18 +01001871 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001872 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001873
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001874 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001875 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001876
Calin Juravle10e244f2015-01-26 18:54:32 +00001877 // Does not apply for all instructions, but having this at top level greatly
1878 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001879 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001880 virtual bool CanBeNull() const {
1881 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1882 return true;
1883 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001884
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001885 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001886 return false;
1887 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001888
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001889 virtual bool IsActualObject() const {
1890 return GetType() == Primitive::kPrimNot;
1891 }
1892
Calin Juravle2e768302015-07-28 14:41:11 +00001893 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001894
Calin Juravle61d544b2015-02-23 16:46:57 +00001895 ReferenceTypeInfo GetReferenceTypeInfo() const {
1896 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1897 return reference_type_info_;
1898 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001899
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001900 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001901 DCHECK(user != nullptr);
1902 HUseListNode<HInstruction*>* use =
1903 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1904 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001905 }
1906
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001907 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001908 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001909 HUseListNode<HEnvironment*>* env_use =
1910 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1911 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001912 }
1913
David Brazdil1abb4192015-02-17 18:33:36 +00001914 void RemoveAsUserOfInput(size_t input) {
1915 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1916 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1917 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001918
David Brazdil1abb4192015-02-17 18:33:36 +00001919 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1920 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001921
David Brazdiled596192015-01-23 10:39:45 +00001922 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1923 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001924 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001925 bool HasOnlyOneNonEnvironmentUse() const {
1926 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1927 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001928
Roland Levillain6c82d402014-10-13 16:10:27 +01001929 // Does this instruction strictly dominate `other_instruction`?
1930 // Returns false if this instruction and `other_instruction` are the same.
1931 // Aborts if this instruction and `other_instruction` are both phis.
1932 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001933
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001934 int GetId() const { return id_; }
1935 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001936
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001937 int GetSsaIndex() const { return ssa_index_; }
1938 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1939 bool HasSsaIndex() const { return ssa_index_ != -1; }
1940
1941 bool HasEnvironment() const { return environment_ != nullptr; }
1942 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001943 // Set the `environment_` field. Raw because this method does not
1944 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001945 void SetRawEnvironment(HEnvironment* environment) {
1946 DCHECK(environment_ == nullptr);
1947 DCHECK_EQ(environment->GetHolder(), this);
1948 environment_ = environment;
1949 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001950
1951 // Set the environment of this instruction, copying it from `environment`. While
1952 // copying, the uses lists are being updated.
1953 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001954 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001955 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001956 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001957 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001958 if (environment->GetParent() != nullptr) {
1959 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1960 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001961 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001962
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001963 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1964 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001965 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001966 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001967 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001968 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001969 if (environment->GetParent() != nullptr) {
1970 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1971 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001972 }
1973
Nicolas Geoffray39468442014-09-02 15:17:15 +01001974 // Returns the number of entries in the environment. Typically, that is the
1975 // number of dex registers in a method. It could be more in case of inlining.
1976 size_t EnvironmentSize() const;
1977
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001978 LocationSummary* GetLocations() const { return locations_; }
1979 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001980
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001981 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001982 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001983
Alexandre Rames188d4312015-04-09 18:30:21 +01001984 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1985 // uses of this instruction by `other` are *not* updated.
1986 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1987 ReplaceWith(other);
1988 other->ReplaceInput(this, use_index);
1989 }
1990
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001991 // Move `this` instruction before `cursor`.
1992 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001993
Vladimir Markofb337ea2015-11-25 15:25:10 +00001994 // Move `this` before its first user and out of any loops. If there is no
1995 // out-of-loop user that dominates all other users, move the instruction
1996 // to the end of the out-of-loop common dominator of the user's blocks.
1997 //
1998 // This can be used only on non-throwing instructions with no side effects that
1999 // have at least one use but no environment uses.
2000 void MoveBeforeFirstUserAndOutOfLoops();
2001
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002002#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002003 bool Is##type() const; \
2004 const H##type* As##type() const; \
2005 H##type* As##type();
2006
2007 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2008#undef INSTRUCTION_TYPE_CHECK
2009
2010#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002011 bool Is##type() const { return (As##type() != nullptr); } \
2012 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002013 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002014 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002015#undef INSTRUCTION_TYPE_CHECK
2016
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002017 // Returns whether the instruction can be moved within the graph.
2018 virtual bool CanBeMoved() const { return false; }
2019
2020 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002021 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002022 return false;
2023 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002024
2025 // Returns whether any data encoded in the two instructions is equal.
2026 // This method does not look at the inputs. Both instructions must be
2027 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002028 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002029 return false;
2030 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002031
2032 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002033 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002034 // 2) Their inputs are identical.
2035 bool Equals(HInstruction* other) const;
2036
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002037 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2038 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2039 // the virtual function because the __attribute__((__pure__)) doesn't really
2040 // apply the strong requirement for virtual functions, preventing optimizations.
2041 InstructionKind GetKind() const PURE;
2042 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002043
2044 virtual size_t ComputeHashCode() const {
2045 size_t result = GetKind();
2046 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2047 result = (result * 31) + InputAt(i)->GetId();
2048 }
2049 return result;
2050 }
2051
2052 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002053 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002054
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002055 size_t GetLifetimePosition() const { return lifetime_position_; }
2056 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2057 LiveInterval* GetLiveInterval() const { return live_interval_; }
2058 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2059 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2060
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002061 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2062
2063 // Returns whether the code generation of the instruction will require to have access
2064 // to the current method. Such instructions are:
2065 // (1): Instructions that require an environment, as calling the runtime requires
2066 // to walk the stack and have the current method stored at a specific stack address.
2067 // (2): Object literals like classes and strings, that are loaded from the dex cache
2068 // fields of the current method.
2069 bool NeedsCurrentMethod() const {
2070 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2071 }
2072
Vladimir Markodc151b22015-10-15 18:02:30 +01002073 // Returns whether the code generation of the instruction will require to have access
2074 // to the dex cache of the current method's declaring class via the current method.
2075 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002076
Mark Mendellc4701932015-04-10 13:18:51 -04002077 // Does this instruction have any use in an environment before
2078 // control flow hits 'other'?
2079 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2080
2081 // Remove all references to environment uses of this instruction.
2082 // The caller must ensure that this is safe to do.
2083 void RemoveEnvironmentUsers();
2084
David Brazdilb3e773e2016-01-26 11:28:37 +00002085 bool IsEmittedAtUseSite() const { return emitted_at_use_site_; }
2086 void MarkEmittedAtUseSite() { emitted_at_use_site_ = true; }
2087
David Brazdil1abb4192015-02-17 18:33:36 +00002088 protected:
2089 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2090 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
Aart Bik5d75afe2015-12-14 11:57:01 -08002091 void SetSideEffects(SideEffects other) { side_effects_ = other; }
David Brazdil1abb4192015-02-17 18:33:36 +00002092
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002093 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002094 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2095
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002096 HInstruction* previous_;
2097 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002098 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002099 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002100
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002101 // An instruction gets an id when it is added to the graph.
2102 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002103 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002104 int id_;
2105
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002106 // When doing liveness analysis, instructions that have uses get an SSA index.
2107 int ssa_index_;
2108
David Brazdilb3e773e2016-01-26 11:28:37 +00002109 // If set, the machine code for this instruction is assumed to be generated by
2110 // its users. Used by liveness analysis to compute use positions accordingly.
2111 bool emitted_at_use_site_;
2112
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002113 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002114 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002115
2116 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002117 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002118
Nicolas Geoffray39468442014-09-02 15:17:15 +01002119 // The environment associated with this instruction. Not null if the instruction
2120 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002121 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002122
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002123 // Set by the code generator.
2124 LocationSummary* locations_;
2125
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002126 // Set by the liveness analysis.
2127 LiveInterval* live_interval_;
2128
2129 // Set by the liveness analysis, this is the position in a linear
2130 // order of blocks where this instruction's live interval start.
2131 size_t lifetime_position_;
2132
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002133 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002134
Calin Juravleacf735c2015-02-12 15:25:22 +00002135 // TODO: for primitive types this should be marked as invalid.
2136 ReferenceTypeInfo reference_type_info_;
2137
David Brazdil1abb4192015-02-17 18:33:36 +00002138 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002139 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002140 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002141 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002142 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002143
2144 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2145};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002146std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002147
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002148class HInputIterator : public ValueObject {
2149 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002150 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002151
2152 bool Done() const { return index_ == instruction_->InputCount(); }
2153 HInstruction* Current() const { return instruction_->InputAt(index_); }
2154 void Advance() { index_++; }
2155
2156 private:
2157 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002158 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002159
2160 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2161};
2162
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002163class HInstructionIterator : public ValueObject {
2164 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002165 explicit HInstructionIterator(const HInstructionList& instructions)
2166 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002167 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002168 }
2169
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002170 bool Done() const { return instruction_ == nullptr; }
2171 HInstruction* Current() const { return instruction_; }
2172 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002173 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002174 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002175 }
2176
2177 private:
2178 HInstruction* instruction_;
2179 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002180
2181 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002182};
2183
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002184class HBackwardInstructionIterator : public ValueObject {
2185 public:
2186 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2187 : instruction_(instructions.last_instruction_) {
2188 next_ = Done() ? nullptr : instruction_->GetPrevious();
2189 }
2190
2191 bool Done() const { return instruction_ == nullptr; }
2192 HInstruction* Current() const { return instruction_; }
2193 void Advance() {
2194 instruction_ = next_;
2195 next_ = Done() ? nullptr : instruction_->GetPrevious();
2196 }
2197
2198 private:
2199 HInstruction* instruction_;
2200 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002201
2202 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002203};
2204
Vladimir Markof9f64412015-09-02 14:05:49 +01002205template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002206class HTemplateInstruction: public HInstruction {
2207 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002208 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002209 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002210 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002211
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002212 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002213
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002214 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002215 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2216 DCHECK_LT(i, N);
2217 return inputs_[i];
2218 }
David Brazdil1abb4192015-02-17 18:33:36 +00002219
2220 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002221 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002222 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002223 }
2224
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002225 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002226 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002227
2228 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002229};
2230
Vladimir Markof9f64412015-09-02 14:05:49 +01002231// HTemplateInstruction specialization for N=0.
2232template<>
2233class HTemplateInstruction<0>: public HInstruction {
2234 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002235 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002236 : HInstruction(side_effects, dex_pc) {}
2237
Vladimir Markof9f64412015-09-02 14:05:49 +01002238 virtual ~HTemplateInstruction() {}
2239
2240 size_t InputCount() const OVERRIDE { return 0; }
2241
2242 protected:
2243 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2244 LOG(FATAL) << "Unreachable";
2245 UNREACHABLE();
2246 }
2247
2248 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2249 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2250 LOG(FATAL) << "Unreachable";
2251 UNREACHABLE();
2252 }
2253
2254 private:
2255 friend class SsaBuilder;
2256};
2257
Dave Allison20dfc792014-06-16 20:44:29 -07002258template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002259class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002260 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002261 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002262 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002263 virtual ~HExpression() {}
2264
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002265 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002266
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002267 protected:
2268 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002269};
2270
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002271// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2272// instruction that branches to the exit block.
2273class HReturnVoid : public HTemplateInstruction<0> {
2274 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002275 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2276 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002277
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002278 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002279
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002280 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002281
2282 private:
2283 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2284};
2285
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002286// Represents dex's RETURN opcodes. A HReturn is a control flow
2287// instruction that branches to the exit block.
2288class HReturn : public HTemplateInstruction<1> {
2289 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002290 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2291 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002292 SetRawInputAt(0, value);
2293 }
2294
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002295 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002296
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002297 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002298
2299 private:
2300 DISALLOW_COPY_AND_ASSIGN(HReturn);
2301};
2302
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002303// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002304// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002305// exit block.
2306class HExit : public HTemplateInstruction<0> {
2307 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002308 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002309
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002310 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002311
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002312 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002313
2314 private:
2315 DISALLOW_COPY_AND_ASSIGN(HExit);
2316};
2317
2318// Jumps from one block to another.
2319class HGoto : public HTemplateInstruction<0> {
2320 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002321 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002322
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002323 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002324
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002325 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002326 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002327 }
2328
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002329 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002330
2331 private:
2332 DISALLOW_COPY_AND_ASSIGN(HGoto);
2333};
2334
Roland Levillain9867bc72015-08-05 10:21:34 +01002335class HConstant : public HExpression<0> {
2336 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002337 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2338 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002339
2340 bool CanBeMoved() const OVERRIDE { return true; }
2341
2342 virtual bool IsMinusOne() const { return false; }
2343 virtual bool IsZero() const { return false; }
2344 virtual bool IsOne() const { return false; }
2345
David Brazdil77a48ae2015-09-15 12:34:04 +00002346 virtual uint64_t GetValueAsUint64() const = 0;
2347
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002348 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002349
2350 private:
2351 DISALLOW_COPY_AND_ASSIGN(HConstant);
2352};
2353
2354class HNullConstant : public HConstant {
2355 public:
2356 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2357 return true;
2358 }
2359
David Brazdil77a48ae2015-09-15 12:34:04 +00002360 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2361
Roland Levillain9867bc72015-08-05 10:21:34 +01002362 size_t ComputeHashCode() const OVERRIDE { return 0; }
2363
2364 DECLARE_INSTRUCTION(NullConstant);
2365
2366 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002367 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002368
2369 friend class HGraph;
2370 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2371};
2372
2373// Constants of the type int. Those can be from Dex instructions, or
2374// synthesized (for example with the if-eqz instruction).
2375class HIntConstant : public HConstant {
2376 public:
2377 int32_t GetValue() const { return value_; }
2378
David Brazdil9f389d42015-10-01 14:32:56 +01002379 uint64_t GetValueAsUint64() const OVERRIDE {
2380 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2381 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002382
Roland Levillain9867bc72015-08-05 10:21:34 +01002383 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2384 DCHECK(other->IsIntConstant());
2385 return other->AsIntConstant()->value_ == value_;
2386 }
2387
2388 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2389
2390 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2391 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2392 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2393
2394 DECLARE_INSTRUCTION(IntConstant);
2395
2396 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002397 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2398 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2399 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2400 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002401
2402 const int32_t value_;
2403
2404 friend class HGraph;
2405 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2406 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2407 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2408};
2409
2410class HLongConstant : public HConstant {
2411 public:
2412 int64_t GetValue() const { return value_; }
2413
David Brazdil77a48ae2015-09-15 12:34:04 +00002414 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2415
Roland Levillain9867bc72015-08-05 10:21:34 +01002416 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2417 DCHECK(other->IsLongConstant());
2418 return other->AsLongConstant()->value_ == value_;
2419 }
2420
2421 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2422
2423 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2424 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2425 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2426
2427 DECLARE_INSTRUCTION(LongConstant);
2428
2429 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002430 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2431 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002432
2433 const int64_t value_;
2434
2435 friend class HGraph;
2436 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2437};
Dave Allison20dfc792014-06-16 20:44:29 -07002438
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002439// Conditional branch. A block ending with an HIf instruction must have
2440// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002441class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002442 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002443 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2444 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002445 SetRawInputAt(0, input);
2446 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002447
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002448 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002449
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002450 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002451 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002452 }
2453
2454 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002455 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002456 }
2457
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002458 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002459
2460 private:
2461 DISALLOW_COPY_AND_ASSIGN(HIf);
2462};
2463
David Brazdilfc6a86a2015-06-26 10:33:45 +00002464
2465// Abstract instruction which marks the beginning and/or end of a try block and
2466// links it to the respective exception handlers. Behaves the same as a Goto in
2467// non-exceptional control flow.
2468// Normal-flow successor is stored at index zero, exception handlers under
2469// higher indices in no particular order.
2470class HTryBoundary : public HTemplateInstruction<0> {
2471 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002472 enum BoundaryKind {
2473 kEntry,
2474 kExit,
2475 };
2476
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002477 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2478 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002479
2480 bool IsControlFlow() const OVERRIDE { return true; }
2481
2482 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002483 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002484
David Brazdild26a4112015-11-10 11:07:31 +00002485 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2486 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2487 }
2488
David Brazdilfc6a86a2015-06-26 10:33:45 +00002489 // Returns whether `handler` is among its exception handlers (non-zero index
2490 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002491 bool HasExceptionHandler(const HBasicBlock& handler) const {
2492 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002493 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002494 }
2495
2496 // If not present already, adds `handler` to its block's list of exception
2497 // handlers.
2498 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002499 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002500 GetBlock()->AddSuccessor(handler);
2501 }
2502 }
2503
David Brazdil56e1acc2015-06-30 15:41:36 +01002504 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002505
David Brazdilffee3d32015-07-06 11:48:53 +01002506 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2507
David Brazdilfc6a86a2015-06-26 10:33:45 +00002508 DECLARE_INSTRUCTION(TryBoundary);
2509
2510 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002511 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002512
2513 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2514};
2515
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002516// Deoptimize to interpreter, upon checking a condition.
2517class HDeoptimize : public HTemplateInstruction<1> {
2518 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002519 // We set CanTriggerGC to prevent any intermediate address to be live
2520 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002521 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002522 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002523 SetRawInputAt(0, cond);
2524 }
2525
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002526 bool CanBeMoved() const OVERRIDE { return true; }
2527 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2528 return true;
2529 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002530 bool NeedsEnvironment() const OVERRIDE { return true; }
2531 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002532
2533 DECLARE_INSTRUCTION(Deoptimize);
2534
2535 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002536 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2537};
2538
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002539// Represents the ArtMethod that was passed as a first argument to
2540// the method. It is used by instructions that depend on it, like
2541// instructions that work with the dex cache.
2542class HCurrentMethod : public HExpression<0> {
2543 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002544 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2545 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002546
2547 DECLARE_INSTRUCTION(CurrentMethod);
2548
2549 private:
2550 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2551};
2552
Mark Mendellfe57faa2015-09-18 09:26:15 -04002553// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2554// have one successor for each entry in the switch table, and the final successor
2555// will be the block containing the next Dex opcode.
2556class HPackedSwitch : public HTemplateInstruction<1> {
2557 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002558 HPackedSwitch(int32_t start_value,
2559 uint32_t num_entries,
2560 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002561 uint32_t dex_pc = kNoDexPc)
2562 : HTemplateInstruction(SideEffects::None(), dex_pc),
2563 start_value_(start_value),
2564 num_entries_(num_entries) {
2565 SetRawInputAt(0, input);
2566 }
2567
2568 bool IsControlFlow() const OVERRIDE { return true; }
2569
2570 int32_t GetStartValue() const { return start_value_; }
2571
Vladimir Marko211c2112015-09-24 16:52:33 +01002572 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002573
2574 HBasicBlock* GetDefaultBlock() const {
2575 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002576 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002577 }
2578 DECLARE_INSTRUCTION(PackedSwitch);
2579
2580 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002581 const int32_t start_value_;
2582 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002583
2584 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2585};
2586
Roland Levillain88cb1752014-10-20 16:36:47 +01002587class HUnaryOperation : public HExpression<1> {
2588 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002589 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2590 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002591 SetRawInputAt(0, input);
2592 }
2593
2594 HInstruction* GetInput() const { return InputAt(0); }
2595 Primitive::Type GetResultType() const { return GetType(); }
2596
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002597 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002598 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002599 return true;
2600 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002601
Roland Levillain9240d6a2014-10-20 16:47:04 +01002602 // Try to statically evaluate `operation` and return a HConstant
2603 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002604 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002605 HConstant* TryStaticEvaluation() const;
2606
2607 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002608 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2609 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002610
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002611 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002612
2613 private:
2614 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2615};
2616
Dave Allison20dfc792014-06-16 20:44:29 -07002617class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002618 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002619 HBinaryOperation(Primitive::Type result_type,
2620 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002621 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002622 SideEffects side_effects = SideEffects::None(),
2623 uint32_t dex_pc = kNoDexPc)
2624 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002625 SetRawInputAt(0, left);
2626 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002627 }
2628
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002629 HInstruction* GetLeft() const { return InputAt(0); }
2630 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002631 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002632
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002633 virtual bool IsCommutative() const { return false; }
2634
2635 // Put constant on the right.
2636 // Returns whether order is changed.
2637 bool OrderInputsWithConstantOnTheRight() {
2638 HInstruction* left = InputAt(0);
2639 HInstruction* right = InputAt(1);
2640 if (left->IsConstant() && !right->IsConstant()) {
2641 ReplaceInput(right, 0);
2642 ReplaceInput(left, 1);
2643 return true;
2644 }
2645 return false;
2646 }
2647
2648 // Order inputs by instruction id, but favor constant on the right side.
2649 // This helps GVN for commutative ops.
2650 void OrderInputs() {
2651 DCHECK(IsCommutative());
2652 HInstruction* left = InputAt(0);
2653 HInstruction* right = InputAt(1);
2654 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2655 return;
2656 }
2657 if (OrderInputsWithConstantOnTheRight()) {
2658 return;
2659 }
2660 // Order according to instruction id.
2661 if (left->GetId() > right->GetId()) {
2662 ReplaceInput(right, 0);
2663 ReplaceInput(left, 1);
2664 }
2665 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002666
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002667 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002668 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002669 return true;
2670 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002671
Roland Levillain9240d6a2014-10-20 16:47:04 +01002672 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002673 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002674 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002675 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002676
2677 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002678 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2679 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2680 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2681 HLongConstant* y ATTRIBUTE_UNUSED) const {
2682 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2683 return nullptr;
2684 }
2685 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2686 HIntConstant* y ATTRIBUTE_UNUSED) const {
2687 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2688 return nullptr;
2689 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002690 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2691 HNullConstant* y ATTRIBUTE_UNUSED) const {
2692 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2693 return nullptr;
2694 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002695
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002696 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002697 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002698 HConstant* GetConstantRight() const;
2699
2700 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002701 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002702 HInstruction* GetLeastConstantLeft() const;
2703
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002704 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002705
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002706 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002707 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2708};
2709
Mark Mendellc4701932015-04-10 13:18:51 -04002710// The comparison bias applies for floating point operations and indicates how NaN
2711// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002712enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002713 kNoBias, // bias is not applicable (i.e. for long operation)
2714 kGtBias, // return 1 for NaN comparisons
2715 kLtBias, // return -1 for NaN comparisons
2716};
2717
Dave Allison20dfc792014-06-16 20:44:29 -07002718class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002719 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002720 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2721 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002722 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002723
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002724 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002725 // `instruction`, and disregard moves in between.
2726 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002727
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002728 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002729
2730 virtual IfCondition GetCondition() const = 0;
2731
Mark Mendellc4701932015-04-10 13:18:51 -04002732 virtual IfCondition GetOppositeCondition() const = 0;
2733
Roland Levillain4fa13f62015-07-06 18:11:54 +01002734 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002735
2736 void SetBias(ComparisonBias bias) { bias_ = bias; }
2737
2738 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2739 return bias_ == other->AsCondition()->bias_;
2740 }
2741
Roland Levillain4fa13f62015-07-06 18:11:54 +01002742 bool IsFPConditionTrueIfNaN() const {
2743 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2744 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002745 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002746 }
2747
2748 bool IsFPConditionFalseIfNaN() const {
2749 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2750 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002751 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002752 }
2753
Dave Allison20dfc792014-06-16 20:44:29 -07002754 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002755 // Needed if we merge a HCompare into a HCondition.
2756 ComparisonBias bias_;
2757
Dave Allison20dfc792014-06-16 20:44:29 -07002758 DISALLOW_COPY_AND_ASSIGN(HCondition);
2759};
2760
2761// Instruction to check if two inputs are equal to each other.
2762class HEqual : public HCondition {
2763 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002764 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2765 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002766
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002767 bool IsCommutative() const OVERRIDE { return true; }
2768
Roland Levillain9867bc72015-08-05 10:21:34 +01002769 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002770 return GetBlock()->GetGraph()->GetIntConstant(
2771 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002772 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002773 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002774 return GetBlock()->GetGraph()->GetIntConstant(
2775 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002776 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002777 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2778 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002779 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002780 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002781
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002782 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002783
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002784 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002785 return kCondEQ;
2786 }
2787
Mark Mendellc4701932015-04-10 13:18:51 -04002788 IfCondition GetOppositeCondition() const OVERRIDE {
2789 return kCondNE;
2790 }
2791
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002792 private:
Aart Bike9f37602015-10-09 11:15:55 -07002793 template <typename T> bool Compute(T x, T y) const { return x == y; }
2794
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002795 DISALLOW_COPY_AND_ASSIGN(HEqual);
2796};
2797
Dave Allison20dfc792014-06-16 20:44:29 -07002798class HNotEqual : public HCondition {
2799 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002800 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2801 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002802
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002803 bool IsCommutative() const OVERRIDE { return true; }
2804
Roland Levillain9867bc72015-08-05 10:21:34 +01002805 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002806 return GetBlock()->GetGraph()->GetIntConstant(
2807 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002808 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002809 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002810 return GetBlock()->GetGraph()->GetIntConstant(
2811 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002812 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002813 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2814 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002815 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002816 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002817
Dave Allison20dfc792014-06-16 20:44:29 -07002818 DECLARE_INSTRUCTION(NotEqual);
2819
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002820 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002821 return kCondNE;
2822 }
2823
Mark Mendellc4701932015-04-10 13:18:51 -04002824 IfCondition GetOppositeCondition() const OVERRIDE {
2825 return kCondEQ;
2826 }
2827
Dave Allison20dfc792014-06-16 20:44:29 -07002828 private:
Aart Bike9f37602015-10-09 11:15:55 -07002829 template <typename T> bool Compute(T x, T y) const { return x != y; }
2830
Dave Allison20dfc792014-06-16 20:44:29 -07002831 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2832};
2833
2834class HLessThan : public HCondition {
2835 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002836 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2837 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002838
Roland Levillain9867bc72015-08-05 10:21:34 +01002839 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002840 return GetBlock()->GetGraph()->GetIntConstant(
2841 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002842 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002843 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002844 return GetBlock()->GetGraph()->GetIntConstant(
2845 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002846 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002847
Dave Allison20dfc792014-06-16 20:44:29 -07002848 DECLARE_INSTRUCTION(LessThan);
2849
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002850 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002851 return kCondLT;
2852 }
2853
Mark Mendellc4701932015-04-10 13:18:51 -04002854 IfCondition GetOppositeCondition() const OVERRIDE {
2855 return kCondGE;
2856 }
2857
Dave Allison20dfc792014-06-16 20:44:29 -07002858 private:
Aart Bike9f37602015-10-09 11:15:55 -07002859 template <typename T> bool Compute(T x, T y) const { return x < y; }
2860
Dave Allison20dfc792014-06-16 20:44:29 -07002861 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2862};
2863
2864class HLessThanOrEqual : public HCondition {
2865 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002866 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2867 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002868
Roland Levillain9867bc72015-08-05 10:21:34 +01002869 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002870 return GetBlock()->GetGraph()->GetIntConstant(
2871 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002872 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002873 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002874 return GetBlock()->GetGraph()->GetIntConstant(
2875 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002876 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002877
Dave Allison20dfc792014-06-16 20:44:29 -07002878 DECLARE_INSTRUCTION(LessThanOrEqual);
2879
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002880 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002881 return kCondLE;
2882 }
2883
Mark Mendellc4701932015-04-10 13:18:51 -04002884 IfCondition GetOppositeCondition() const OVERRIDE {
2885 return kCondGT;
2886 }
2887
Dave Allison20dfc792014-06-16 20:44:29 -07002888 private:
Aart Bike9f37602015-10-09 11:15:55 -07002889 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2890
Dave Allison20dfc792014-06-16 20:44:29 -07002891 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2892};
2893
2894class HGreaterThan : public HCondition {
2895 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002896 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2897 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002898
Roland Levillain9867bc72015-08-05 10:21:34 +01002899 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002900 return GetBlock()->GetGraph()->GetIntConstant(
2901 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002902 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002903 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002904 return GetBlock()->GetGraph()->GetIntConstant(
2905 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002906 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002907
Dave Allison20dfc792014-06-16 20:44:29 -07002908 DECLARE_INSTRUCTION(GreaterThan);
2909
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002910 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002911 return kCondGT;
2912 }
2913
Mark Mendellc4701932015-04-10 13:18:51 -04002914 IfCondition GetOppositeCondition() const OVERRIDE {
2915 return kCondLE;
2916 }
2917
Dave Allison20dfc792014-06-16 20:44:29 -07002918 private:
Aart Bike9f37602015-10-09 11:15:55 -07002919 template <typename T> bool Compute(T x, T y) const { return x > y; }
2920
Dave Allison20dfc792014-06-16 20:44:29 -07002921 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2922};
2923
2924class HGreaterThanOrEqual : public HCondition {
2925 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002926 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2927 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002928
Roland Levillain9867bc72015-08-05 10:21:34 +01002929 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002930 return GetBlock()->GetGraph()->GetIntConstant(
2931 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002932 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002933 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002934 return GetBlock()->GetGraph()->GetIntConstant(
2935 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002936 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002937
Dave Allison20dfc792014-06-16 20:44:29 -07002938 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2939
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002940 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002941 return kCondGE;
2942 }
2943
Mark Mendellc4701932015-04-10 13:18:51 -04002944 IfCondition GetOppositeCondition() const OVERRIDE {
2945 return kCondLT;
2946 }
2947
Dave Allison20dfc792014-06-16 20:44:29 -07002948 private:
Aart Bike9f37602015-10-09 11:15:55 -07002949 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2950
Dave Allison20dfc792014-06-16 20:44:29 -07002951 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2952};
2953
Aart Bike9f37602015-10-09 11:15:55 -07002954class HBelow : public HCondition {
2955 public:
2956 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2957 : HCondition(first, second, dex_pc) {}
2958
2959 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2960 return GetBlock()->GetGraph()->GetIntConstant(
2961 Compute(static_cast<uint32_t>(x->GetValue()),
2962 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2963 }
2964 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2965 return GetBlock()->GetGraph()->GetIntConstant(
2966 Compute(static_cast<uint64_t>(x->GetValue()),
2967 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2968 }
2969
2970 DECLARE_INSTRUCTION(Below);
2971
2972 IfCondition GetCondition() const OVERRIDE {
2973 return kCondB;
2974 }
2975
2976 IfCondition GetOppositeCondition() const OVERRIDE {
2977 return kCondAE;
2978 }
2979
2980 private:
2981 template <typename T> bool Compute(T x, T y) const { return x < y; }
2982
2983 DISALLOW_COPY_AND_ASSIGN(HBelow);
2984};
2985
2986class HBelowOrEqual : public HCondition {
2987 public:
2988 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2989 : HCondition(first, second, dex_pc) {}
2990
2991 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2992 return GetBlock()->GetGraph()->GetIntConstant(
2993 Compute(static_cast<uint32_t>(x->GetValue()),
2994 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2995 }
2996 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2997 return GetBlock()->GetGraph()->GetIntConstant(
2998 Compute(static_cast<uint64_t>(x->GetValue()),
2999 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3000 }
3001
3002 DECLARE_INSTRUCTION(BelowOrEqual);
3003
3004 IfCondition GetCondition() const OVERRIDE {
3005 return kCondBE;
3006 }
3007
3008 IfCondition GetOppositeCondition() const OVERRIDE {
3009 return kCondA;
3010 }
3011
3012 private:
3013 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3014
3015 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3016};
3017
3018class HAbove : public HCondition {
3019 public:
3020 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3021 : HCondition(first, second, dex_pc) {}
3022
3023 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3024 return GetBlock()->GetGraph()->GetIntConstant(
3025 Compute(static_cast<uint32_t>(x->GetValue()),
3026 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3027 }
3028 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3029 return GetBlock()->GetGraph()->GetIntConstant(
3030 Compute(static_cast<uint64_t>(x->GetValue()),
3031 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3032 }
3033
3034 DECLARE_INSTRUCTION(Above);
3035
3036 IfCondition GetCondition() const OVERRIDE {
3037 return kCondA;
3038 }
3039
3040 IfCondition GetOppositeCondition() const OVERRIDE {
3041 return kCondBE;
3042 }
3043
3044 private:
3045 template <typename T> bool Compute(T x, T y) const { return x > y; }
3046
3047 DISALLOW_COPY_AND_ASSIGN(HAbove);
3048};
3049
3050class HAboveOrEqual : public HCondition {
3051 public:
3052 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3053 : HCondition(first, second, dex_pc) {}
3054
3055 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3056 return GetBlock()->GetGraph()->GetIntConstant(
3057 Compute(static_cast<uint32_t>(x->GetValue()),
3058 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3059 }
3060 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3061 return GetBlock()->GetGraph()->GetIntConstant(
3062 Compute(static_cast<uint64_t>(x->GetValue()),
3063 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3064 }
3065
3066 DECLARE_INSTRUCTION(AboveOrEqual);
3067
3068 IfCondition GetCondition() const OVERRIDE {
3069 return kCondAE;
3070 }
3071
3072 IfCondition GetOppositeCondition() const OVERRIDE {
3073 return kCondB;
3074 }
3075
3076 private:
3077 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3078
3079 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3080};
Dave Allison20dfc792014-06-16 20:44:29 -07003081
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003082// Instruction to check how two inputs compare to each other.
3083// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3084class HCompare : public HBinaryOperation {
3085 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003086 HCompare(Primitive::Type type,
3087 HInstruction* first,
3088 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003089 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003090 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003091 : HBinaryOperation(Primitive::kPrimInt,
3092 first,
3093 second,
3094 SideEffectsForArchRuntimeCalls(type),
3095 dex_pc),
3096 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003097 DCHECK_EQ(type, first->GetType());
3098 DCHECK_EQ(type, second->GetType());
3099 }
3100
Roland Levillain9867bc72015-08-05 10:21:34 +01003101 template <typename T>
3102 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003103
Roland Levillain9867bc72015-08-05 10:21:34 +01003104 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003105 return GetBlock()->GetGraph()->GetIntConstant(
3106 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003107 }
3108 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003109 return GetBlock()->GetGraph()->GetIntConstant(
3110 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003111 }
3112
Calin Juravleddb7df22014-11-25 20:56:51 +00003113 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3114 return bias_ == other->AsCompare()->bias_;
3115 }
3116
Mark Mendellc4701932015-04-10 13:18:51 -04003117 ComparisonBias GetBias() const { return bias_; }
3118
Roland Levillain4fa13f62015-07-06 18:11:54 +01003119 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003120
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003121
3122 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3123 // MIPS64 uses a runtime call for FP comparisons.
3124 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3125 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003126
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003127 DECLARE_INSTRUCTION(Compare);
3128
3129 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003130 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003131
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003132 DISALLOW_COPY_AND_ASSIGN(HCompare);
3133};
3134
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003135// A local in the graph. Corresponds to a Dex register.
3136class HLocal : public HTemplateInstruction<0> {
3137 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003138 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003139 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003140
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003141 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003142
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003143 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003144
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003145 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003146 // The Dex register number.
3147 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003148
3149 DISALLOW_COPY_AND_ASSIGN(HLocal);
3150};
3151
3152// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003153class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003154 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003155 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3156 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003157 SetRawInputAt(0, local);
3158 }
3159
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003160 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3161
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003162 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003163
3164 private:
3165 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3166};
3167
3168// Store a value in a given local. This instruction has two inputs: the value
3169// and the local.
3170class HStoreLocal : public HTemplateInstruction<2> {
3171 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003172 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3173 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003174 SetRawInputAt(0, local);
3175 SetRawInputAt(1, value);
3176 }
3177
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003178 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3179
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003180 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003181
3182 private:
3183 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3184};
3185
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003186class HFloatConstant : public HConstant {
3187 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003188 float GetValue() const { return value_; }
3189
David Brazdil77a48ae2015-09-15 12:34:04 +00003190 uint64_t GetValueAsUint64() const OVERRIDE {
3191 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3192 }
3193
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003194 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003195 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003196 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003197 }
3198
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003199 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003200
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003201 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003202 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003203 }
3204 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003205 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003206 }
3207 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003208 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3209 }
3210 bool IsNaN() const {
3211 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003212 }
3213
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003214 DECLARE_INSTRUCTION(FloatConstant);
3215
3216 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003217 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3218 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3219 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3220 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003221
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003222 const float value_;
3223
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003224 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003225 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003226 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003227 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3228};
3229
3230class HDoubleConstant : public HConstant {
3231 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003232 double GetValue() const { return value_; }
3233
David Brazdil77a48ae2015-09-15 12:34:04 +00003234 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3235
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003236 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003237 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003238 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003239 }
3240
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003241 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003242
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003243 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003244 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003245 }
3246 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003247 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003248 }
3249 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003250 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3251 }
3252 bool IsNaN() const {
3253 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003254 }
3255
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003256 DECLARE_INSTRUCTION(DoubleConstant);
3257
3258 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003259 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3260 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3261 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3262 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003263
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003264 const double value_;
3265
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003266 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003267 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003268 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003269 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3270};
3271
David Brazdil6de19382016-01-08 17:37:10 +00003272class HNewInstance : public HExpression<2> {
3273 public:
3274 HNewInstance(HInstruction* cls,
3275 HCurrentMethod* current_method,
3276 uint32_t dex_pc,
3277 uint16_t type_index,
3278 const DexFile& dex_file,
3279 bool can_throw,
3280 bool finalizable,
3281 QuickEntrypointEnum entrypoint)
3282 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3283 type_index_(type_index),
3284 dex_file_(dex_file),
3285 can_throw_(can_throw),
3286 finalizable_(finalizable),
3287 entrypoint_(entrypoint) {
3288 SetRawInputAt(0, cls);
3289 SetRawInputAt(1, current_method);
3290 }
3291
3292 uint16_t GetTypeIndex() const { return type_index_; }
3293 const DexFile& GetDexFile() const { return dex_file_; }
3294
3295 // Calls runtime so needs an environment.
3296 bool NeedsEnvironment() const OVERRIDE { return true; }
3297
3298 // It may throw when called on type that's not instantiable/accessible.
3299 // It can throw OOME.
3300 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3301 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3302
3303 bool IsFinalizable() const { return finalizable_; }
3304
3305 bool CanBeNull() const OVERRIDE { return false; }
3306
3307 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3308
3309 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3310 entrypoint_ = entrypoint;
3311 }
3312
3313 bool IsStringAlloc() const;
3314
3315 DECLARE_INSTRUCTION(NewInstance);
3316
3317 private:
3318 const uint16_t type_index_;
3319 const DexFile& dex_file_;
3320 const bool can_throw_;
3321 const bool finalizable_;
3322 QuickEntrypointEnum entrypoint_;
3323
3324 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3325};
3326
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003327enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003328#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3329 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003330#include "intrinsics_list.h"
3331 kNone,
3332 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3333#undef INTRINSICS_LIST
3334#undef OPTIMIZING_INTRINSICS
3335};
3336std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3337
Agi Csaki05f20562015-08-19 14:58:14 -07003338enum IntrinsicNeedsEnvironmentOrCache {
3339 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3340 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003341};
3342
Aart Bik5d75afe2015-12-14 11:57:01 -08003343enum IntrinsicSideEffects {
3344 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3345 kReadSideEffects, // Intrinsic may read heap memory.
3346 kWriteSideEffects, // Intrinsic may write heap memory.
3347 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3348};
3349
3350enum IntrinsicExceptions {
3351 kNoThrow, // Intrinsic does not throw any exceptions.
3352 kCanThrow // Intrinsic may throw exceptions.
3353};
3354
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003355class HInvoke : public HInstruction {
3356 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003357 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003358
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003359 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003360
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003361 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003362 SetRawInputAt(index, argument);
3363 }
3364
Roland Levillain3e3d7332015-04-28 11:00:54 +01003365 // Return the number of arguments. This number can be lower than
3366 // the number of inputs returned by InputCount(), as some invoke
3367 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3368 // inputs at the end of their list of inputs.
3369 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3370
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003371 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003372
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003373 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003374 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003375
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003376 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3377
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003378 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003379 return intrinsic_;
3380 }
3381
Aart Bik5d75afe2015-12-14 11:57:01 -08003382 void SetIntrinsic(Intrinsics intrinsic,
3383 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3384 IntrinsicSideEffects side_effects,
3385 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003386
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003387 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003388 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003389 }
3390
Aart Bik5d75afe2015-12-14 11:57:01 -08003391 bool CanThrow() const OVERRIDE { return can_throw_; }
3392
3393 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3394
3395 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3396 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3397 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003398
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003399 uint32_t* GetIntrinsicOptimizations() {
3400 return &intrinsic_optimizations_;
3401 }
3402
3403 const uint32_t* GetIntrinsicOptimizations() const {
3404 return &intrinsic_optimizations_;
3405 }
3406
3407 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3408
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003409 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003410
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003411 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003412 HInvoke(ArenaAllocator* arena,
3413 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003414 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003415 Primitive::Type return_type,
3416 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003417 uint32_t dex_method_index,
3418 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003419 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003420 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003421 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003422 inputs_(number_of_arguments + number_of_other_inputs,
3423 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003424 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003425 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003426 original_invoke_type_(original_invoke_type),
Aart Bik5d75afe2015-12-14 11:57:01 -08003427 can_throw_(true),
agicsaki57b81ec2015-08-11 17:39:37 -07003428 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003429 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003430 }
3431
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003432 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003433 return inputs_[index];
3434 }
3435
David Brazdil1abb4192015-02-17 18:33:36 +00003436 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003437 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003438 }
3439
Aart Bik5d75afe2015-12-14 11:57:01 -08003440 void SetCanThrow(bool can_throw) { can_throw_ = can_throw; }
3441
Roland Levillain3e3d7332015-04-28 11:00:54 +01003442 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003443 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003444 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003445 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003446 const InvokeType original_invoke_type_;
Aart Bik5d75afe2015-12-14 11:57:01 -08003447 bool can_throw_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003448 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003449
3450 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3451 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003452
3453 private:
3454 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3455};
3456
Calin Juravle175dc732015-08-25 15:42:32 +01003457class HInvokeUnresolved : public HInvoke {
3458 public:
3459 HInvokeUnresolved(ArenaAllocator* arena,
3460 uint32_t number_of_arguments,
3461 Primitive::Type return_type,
3462 uint32_t dex_pc,
3463 uint32_t dex_method_index,
3464 InvokeType invoke_type)
3465 : HInvoke(arena,
3466 number_of_arguments,
3467 0u /* number_of_other_inputs */,
3468 return_type,
3469 dex_pc,
3470 dex_method_index,
3471 invoke_type) {
3472 }
3473
3474 DECLARE_INSTRUCTION(InvokeUnresolved);
3475
3476 private:
3477 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3478};
3479
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003480class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003481 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003482 // Requirements of this method call regarding the class
3483 // initialization (clinit) check of its declaring class.
3484 enum class ClinitCheckRequirement {
3485 kNone, // Class already initialized.
3486 kExplicit, // Static call having explicit clinit check as last input.
3487 kImplicit, // Static call implicitly requiring a clinit check.
3488 };
3489
Vladimir Marko58155012015-08-19 12:49:41 +00003490 // Determines how to load the target ArtMethod*.
3491 enum class MethodLoadKind {
3492 // Use a String init ArtMethod* loaded from Thread entrypoints.
3493 kStringInit,
3494
3495 // Use the method's own ArtMethod* loaded by the register allocator.
3496 kRecursive,
3497
3498 // Use ArtMethod* at a known address, embed the direct address in the code.
3499 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3500 kDirectAddress,
3501
3502 // Use ArtMethod* at an address that will be known at link time, embed the direct
3503 // address in the code. If the image is relocatable, emit .patch_oat entry.
3504 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3505 // the image relocatable or not.
3506 kDirectAddressWithFixup,
3507
3508 // Load from resoved methods array in the dex cache using a PC-relative load.
3509 // Used when we need to use the dex cache, for example for invoke-static that
3510 // may cause class initialization (the entry may point to a resolution method),
3511 // and we know that we can access the dex cache arrays using a PC-relative load.
3512 kDexCachePcRelative,
3513
3514 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3515 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3516 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3517 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3518 kDexCacheViaMethod,
3519 };
3520
3521 // Determines the location of the code pointer.
3522 enum class CodePtrLocation {
3523 // Recursive call, use local PC-relative call instruction.
3524 kCallSelf,
3525
3526 // Use PC-relative call instruction patched at link time.
3527 // Used for calls within an oat file, boot->boot or app->app.
3528 kCallPCRelative,
3529
3530 // Call to a known target address, embed the direct address in code.
3531 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3532 kCallDirect,
3533
3534 // Call to a target address that will be known at link time, embed the direct
3535 // address in code. If the image is relocatable, emit .patch_oat entry.
3536 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3537 // the image relocatable or not.
3538 kCallDirectWithFixup,
3539
3540 // Use code pointer from the ArtMethod*.
3541 // Used when we don't know the target code. This is also the last-resort-kind used when
3542 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3543 kCallArtMethod,
3544 };
3545
3546 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003547 MethodLoadKind method_load_kind;
3548 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003549 // The method load data holds
3550 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3551 // Note that there are multiple string init methods, each having its own offset.
3552 // - the method address for kDirectAddress
3553 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003554 uint64_t method_load_data;
3555 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003556 };
3557
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003558 HInvokeStaticOrDirect(ArenaAllocator* arena,
3559 uint32_t number_of_arguments,
3560 Primitive::Type return_type,
3561 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003562 uint32_t method_index,
3563 MethodReference target_method,
3564 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003565 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003566 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003567 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003568 : HInvoke(arena,
3569 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003570 // There is potentially one extra argument for the HCurrentMethod node, and
3571 // potentially one other if the clinit check is explicit, and potentially
3572 // one other if the method is a string factory.
3573 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3574 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3575 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003576 return_type,
3577 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003578 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003579 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003580 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003581 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003582 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003583 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003584
Vladimir Markodc151b22015-10-15 18:02:30 +01003585 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003586 bool had_current_method_input = HasCurrentMethodInput();
3587 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3588
3589 // Using the current method is the default and once we find a better
3590 // method load kind, we should not go back to using the current method.
3591 DCHECK(had_current_method_input || !needs_current_method_input);
3592
3593 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003594 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3595 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003596 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003597 dispatch_info_ = dispatch_info;
3598 }
3599
Vladimir Markoc53c0792015-11-19 15:48:33 +00003600 void AddSpecialInput(HInstruction* input) {
3601 // We allow only one special input.
3602 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3603 DCHECK(InputCount() == GetSpecialInputIndex() ||
3604 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3605 InsertInputAt(GetSpecialInputIndex(), input);
3606 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003607
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003608 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003609 // We access the method via the dex cache so we can't do an implicit null check.
3610 // TODO: for intrinsics we can generate implicit null checks.
3611 return false;
3612 }
3613
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003614 bool CanBeNull() const OVERRIDE {
3615 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3616 }
3617
Vladimir Markoc53c0792015-11-19 15:48:33 +00003618 // Get the index of the special input, if any.
3619 //
David Brazdil6de19382016-01-08 17:37:10 +00003620 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3621 // method pointer; otherwise there may be one platform-specific special input,
3622 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003623 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3624
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003625 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3626 void SetOptimizedInvokeType(InvokeType invoke_type) {
3627 optimized_invoke_type_ = invoke_type;
3628 }
3629
Vladimir Marko58155012015-08-19 12:49:41 +00003630 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3631 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3632 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003633 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003634 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003635 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003636 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003637 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3638 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003639 bool HasCurrentMethodInput() const {
3640 // This function can be called only after the invoke has been fully initialized by the builder.
3641 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003642 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003643 return true;
3644 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003645 DCHECK(InputCount() == GetSpecialInputIndex() ||
3646 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003647 return false;
3648 }
3649 }
Vladimir Marko58155012015-08-19 12:49:41 +00003650 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3651 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003652 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003653
3654 int32_t GetStringInitOffset() const {
3655 DCHECK(IsStringInit());
3656 return dispatch_info_.method_load_data;
3657 }
3658
3659 uint64_t GetMethodAddress() const {
3660 DCHECK(HasMethodAddress());
3661 return dispatch_info_.method_load_data;
3662 }
3663
3664 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003665 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003666 return dispatch_info_.method_load_data;
3667 }
3668
3669 uint64_t GetDirectCodePtr() const {
3670 DCHECK(HasDirectCodePtr());
3671 return dispatch_info_.direct_code_ptr;
3672 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003673
Calin Juravle68ad6492015-08-18 17:08:12 +01003674 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3675
Roland Levillain4c0eb422015-04-24 16:43:49 +01003676 // Is this instruction a call to a static method?
3677 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003678 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003679 }
3680
Vladimir Markofbb184a2015-11-13 14:47:00 +00003681 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3682 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3683 // instruction; only relevant for static calls with explicit clinit check.
3684 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003685 DCHECK(IsStaticWithExplicitClinitCheck());
3686 size_t last_input_index = InputCount() - 1;
3687 HInstruction* last_input = InputAt(last_input_index);
3688 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003689 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003690 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003691 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003692 clinit_check_requirement_ = new_requirement;
3693 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003694 }
3695
David Brazdilbc9ab162016-01-20 14:50:19 +00003696 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00003697 DCHECK(IsStringInit());
3698 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00003699 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00003700 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003701 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00003702 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003703 }
3704
Roland Levillain4c0eb422015-04-24 16:43:49 +01003705 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003706 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003707 bool IsStaticWithExplicitClinitCheck() const {
3708 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3709 }
3710
3711 // Is this a call to a static method whose declaring class has an
3712 // implicit intialization check requirement?
3713 bool IsStaticWithImplicitClinitCheck() const {
3714 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3715 }
3716
Vladimir Markob554b5a2015-11-06 12:57:55 +00003717 // Does this method load kind need the current method as an input?
3718 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3719 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3720 }
3721
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003722 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003723
Roland Levillain4c0eb422015-04-24 16:43:49 +01003724 protected:
3725 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3726 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3727 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3728 HInstruction* input = input_record.GetInstruction();
3729 // `input` is the last input of a static invoke marked as having
3730 // an explicit clinit check. It must either be:
3731 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3732 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3733 DCHECK(input != nullptr);
3734 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3735 }
3736 return input_record;
3737 }
3738
Vladimir Markoc53c0792015-11-19 15:48:33 +00003739 void InsertInputAt(size_t index, HInstruction* input);
3740 void RemoveInputAt(size_t index);
3741
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003742 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003743 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003744 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003745 // The target method may refer to different dex file or method index than the original
3746 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3747 // in derived class to increase visibility.
3748 MethodReference target_method_;
3749 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003750
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003751 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003752};
Vladimir Markof64242a2015-12-01 14:58:23 +00003753std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003754std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003755
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003756class HInvokeVirtual : public HInvoke {
3757 public:
3758 HInvokeVirtual(ArenaAllocator* arena,
3759 uint32_t number_of_arguments,
3760 Primitive::Type return_type,
3761 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003762 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003763 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003764 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003765 vtable_index_(vtable_index) {}
3766
Calin Juravle641547a2015-04-21 22:08:51 +01003767 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003768 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003769 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003770 }
3771
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003772 uint32_t GetVTableIndex() const { return vtable_index_; }
3773
3774 DECLARE_INSTRUCTION(InvokeVirtual);
3775
3776 private:
3777 const uint32_t vtable_index_;
3778
3779 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3780};
3781
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003782class HInvokeInterface : public HInvoke {
3783 public:
3784 HInvokeInterface(ArenaAllocator* arena,
3785 uint32_t number_of_arguments,
3786 Primitive::Type return_type,
3787 uint32_t dex_pc,
3788 uint32_t dex_method_index,
3789 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003790 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003791 imt_index_(imt_index) {}
3792
Calin Juravle641547a2015-04-21 22:08:51 +01003793 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003794 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003795 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003796 }
3797
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003798 uint32_t GetImtIndex() const { return imt_index_; }
3799 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3800
3801 DECLARE_INSTRUCTION(InvokeInterface);
3802
3803 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003804 const uint32_t imt_index_;
3805
3806 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3807};
3808
Roland Levillain88cb1752014-10-20 16:36:47 +01003809class HNeg : public HUnaryOperation {
3810 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003811 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3812 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003813
Roland Levillain9867bc72015-08-05 10:21:34 +01003814 template <typename T> T Compute(T x) const { return -x; }
3815
3816 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003817 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003818 }
3819 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003820 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003821 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003822
Roland Levillain88cb1752014-10-20 16:36:47 +01003823 DECLARE_INSTRUCTION(Neg);
3824
3825 private:
3826 DISALLOW_COPY_AND_ASSIGN(HNeg);
3827};
3828
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003829class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003830 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003831 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003832 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003833 uint32_t dex_pc,
3834 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003835 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003836 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003837 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003838 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003839 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003840 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003841 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003842 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003843 }
3844
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003845 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003846 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003847
3848 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003849 bool NeedsEnvironment() const OVERRIDE { return true; }
3850
Mingyao Yang0c365e62015-03-31 15:09:29 -07003851 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3852 bool CanThrow() const OVERRIDE { return true; }
3853
Calin Juravle10e244f2015-01-26 18:54:32 +00003854 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003855
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003856 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3857
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003858 DECLARE_INSTRUCTION(NewArray);
3859
3860 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003861 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003862 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003863 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003864
3865 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3866};
3867
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003868class HAdd : public HBinaryOperation {
3869 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003870 HAdd(Primitive::Type result_type,
3871 HInstruction* left,
3872 HInstruction* right,
3873 uint32_t dex_pc = kNoDexPc)
3874 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003875
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003876 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003877
Roland Levillain9867bc72015-08-05 10:21:34 +01003878 template <typename T> T Compute(T x, T y) const { return x + y; }
3879
3880 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003881 return GetBlock()->GetGraph()->GetIntConstant(
3882 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003883 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003884 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003885 return GetBlock()->GetGraph()->GetLongConstant(
3886 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003887 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003888
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003889 DECLARE_INSTRUCTION(Add);
3890
3891 private:
3892 DISALLOW_COPY_AND_ASSIGN(HAdd);
3893};
3894
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003895class HSub : public HBinaryOperation {
3896 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003897 HSub(Primitive::Type result_type,
3898 HInstruction* left,
3899 HInstruction* right,
3900 uint32_t dex_pc = kNoDexPc)
3901 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003902
Roland Levillain9867bc72015-08-05 10:21:34 +01003903 template <typename T> T Compute(T x, T y) const { return x - y; }
3904
3905 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003906 return GetBlock()->GetGraph()->GetIntConstant(
3907 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003908 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003909 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003910 return GetBlock()->GetGraph()->GetLongConstant(
3911 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003912 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003913
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003914 DECLARE_INSTRUCTION(Sub);
3915
3916 private:
3917 DISALLOW_COPY_AND_ASSIGN(HSub);
3918};
3919
Calin Juravle34bacdf2014-10-07 20:23:36 +01003920class HMul : public HBinaryOperation {
3921 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003922 HMul(Primitive::Type result_type,
3923 HInstruction* left,
3924 HInstruction* right,
3925 uint32_t dex_pc = kNoDexPc)
3926 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003927
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003928 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003929
Roland Levillain9867bc72015-08-05 10:21:34 +01003930 template <typename T> T Compute(T x, T y) const { return x * y; }
3931
3932 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003933 return GetBlock()->GetGraph()->GetIntConstant(
3934 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003935 }
3936 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003937 return GetBlock()->GetGraph()->GetLongConstant(
3938 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003939 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003940
3941 DECLARE_INSTRUCTION(Mul);
3942
3943 private:
3944 DISALLOW_COPY_AND_ASSIGN(HMul);
3945};
3946
Calin Juravle7c4954d2014-10-28 16:57:40 +00003947class HDiv : public HBinaryOperation {
3948 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003949 HDiv(Primitive::Type result_type,
3950 HInstruction* left,
3951 HInstruction* right,
3952 uint32_t dex_pc)
3953 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003954
Roland Levillain9867bc72015-08-05 10:21:34 +01003955 template <typename T>
3956 T Compute(T x, T y) const {
3957 // Our graph structure ensures we never have 0 for `y` during
3958 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003959 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003960 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003961 return (y == -1) ? -x : x / y;
3962 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003963
Roland Levillain9867bc72015-08-05 10:21:34 +01003964 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003965 return GetBlock()->GetGraph()->GetIntConstant(
3966 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003967 }
3968 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003969 return GetBlock()->GetGraph()->GetLongConstant(
3970 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003971 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003972
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003973 static SideEffects SideEffectsForArchRuntimeCalls() {
3974 // The generated code can use a runtime call.
3975 return SideEffects::CanTriggerGC();
3976 }
3977
Calin Juravle7c4954d2014-10-28 16:57:40 +00003978 DECLARE_INSTRUCTION(Div);
3979
3980 private:
3981 DISALLOW_COPY_AND_ASSIGN(HDiv);
3982};
3983
Calin Juravlebacfec32014-11-14 15:54:36 +00003984class HRem : public HBinaryOperation {
3985 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003986 HRem(Primitive::Type result_type,
3987 HInstruction* left,
3988 HInstruction* right,
3989 uint32_t dex_pc)
3990 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003991
Roland Levillain9867bc72015-08-05 10:21:34 +01003992 template <typename T>
3993 T Compute(T x, T y) const {
3994 // Our graph structure ensures we never have 0 for `y` during
3995 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003996 DCHECK_NE(y, 0);
3997 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3998 return (y == -1) ? 0 : x % y;
3999 }
4000
Roland Levillain9867bc72015-08-05 10:21:34 +01004001 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004002 return GetBlock()->GetGraph()->GetIntConstant(
4003 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004004 }
4005 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004006 return GetBlock()->GetGraph()->GetLongConstant(
4007 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004008 }
4009
Calin Juravlebacfec32014-11-14 15:54:36 +00004010
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004011 static SideEffects SideEffectsForArchRuntimeCalls() {
4012 return SideEffects::CanTriggerGC();
4013 }
4014
Calin Juravlebacfec32014-11-14 15:54:36 +00004015 DECLARE_INSTRUCTION(Rem);
4016
4017 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004018 DISALLOW_COPY_AND_ASSIGN(HRem);
4019};
4020
Calin Juravled0d48522014-11-04 16:40:20 +00004021class HDivZeroCheck : public HExpression<1> {
4022 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004023 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4024 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004025 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004026 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004027 SetRawInputAt(0, value);
4028 }
4029
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004030 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4031
Calin Juravled0d48522014-11-04 16:40:20 +00004032 bool CanBeMoved() const OVERRIDE { return true; }
4033
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004034 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004035 return true;
4036 }
4037
4038 bool NeedsEnvironment() const OVERRIDE { return true; }
4039 bool CanThrow() const OVERRIDE { return true; }
4040
Calin Juravled0d48522014-11-04 16:40:20 +00004041 DECLARE_INSTRUCTION(DivZeroCheck);
4042
4043 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004044 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4045};
4046
Calin Juravle9aec02f2014-11-18 23:06:35 +00004047class HShl : public HBinaryOperation {
4048 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004049 HShl(Primitive::Type result_type,
4050 HInstruction* left,
4051 HInstruction* right,
4052 uint32_t dex_pc = kNoDexPc)
4053 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004054
Roland Levillain9867bc72015-08-05 10:21:34 +01004055 template <typename T, typename U, typename V>
4056 T Compute(T x, U y, V max_shift_value) const {
4057 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4058 "V is not the unsigned integer type corresponding to T");
4059 return x << (y & max_shift_value);
4060 }
4061
4062 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4063 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004064 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004065 }
4066 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4067 // case is handled as `x << static_cast<int>(y)`.
4068 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4069 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004070 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004071 }
4072 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4073 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004074 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004075 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004076
4077 DECLARE_INSTRUCTION(Shl);
4078
4079 private:
4080 DISALLOW_COPY_AND_ASSIGN(HShl);
4081};
4082
4083class HShr : public HBinaryOperation {
4084 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004085 HShr(Primitive::Type result_type,
4086 HInstruction* left,
4087 HInstruction* right,
4088 uint32_t dex_pc = kNoDexPc)
4089 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004090
Roland Levillain9867bc72015-08-05 10:21:34 +01004091 template <typename T, typename U, typename V>
4092 T Compute(T x, U y, V max_shift_value) const {
4093 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4094 "V is not the unsigned integer type corresponding to T");
4095 return x >> (y & max_shift_value);
4096 }
4097
4098 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4099 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004100 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004101 }
4102 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4103 // case is handled as `x >> static_cast<int>(y)`.
4104 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4105 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004106 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004107 }
4108 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4109 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004110 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004111 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004112
4113 DECLARE_INSTRUCTION(Shr);
4114
4115 private:
4116 DISALLOW_COPY_AND_ASSIGN(HShr);
4117};
4118
4119class HUShr : public HBinaryOperation {
4120 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004121 HUShr(Primitive::Type result_type,
4122 HInstruction* left,
4123 HInstruction* right,
4124 uint32_t dex_pc = kNoDexPc)
4125 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004126
Roland Levillain9867bc72015-08-05 10:21:34 +01004127 template <typename T, typename U, typename V>
4128 T Compute(T x, U y, V max_shift_value) const {
4129 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4130 "V is not the unsigned integer type corresponding to T");
4131 V ux = static_cast<V>(x);
4132 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004133 }
4134
Roland Levillain9867bc72015-08-05 10:21:34 +01004135 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4136 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004137 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004138 }
4139 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4140 // case is handled as `x >>> static_cast<int>(y)`.
4141 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4142 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004143 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004144 }
4145 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4146 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004147 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004148 }
4149
4150 DECLARE_INSTRUCTION(UShr);
4151
4152 private:
4153 DISALLOW_COPY_AND_ASSIGN(HUShr);
4154};
4155
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004156class HAnd : public HBinaryOperation {
4157 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004158 HAnd(Primitive::Type result_type,
4159 HInstruction* left,
4160 HInstruction* right,
4161 uint32_t dex_pc = kNoDexPc)
4162 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004163
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004164 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004165
Roland Levillain9867bc72015-08-05 10:21:34 +01004166 template <typename T, typename U>
4167 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4168
4169 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004170 return GetBlock()->GetGraph()->GetIntConstant(
4171 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004172 }
4173 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004174 return GetBlock()->GetGraph()->GetLongConstant(
4175 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004176 }
4177 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004178 return GetBlock()->GetGraph()->GetLongConstant(
4179 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004180 }
4181 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004182 return GetBlock()->GetGraph()->GetLongConstant(
4183 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004184 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004185
4186 DECLARE_INSTRUCTION(And);
4187
4188 private:
4189 DISALLOW_COPY_AND_ASSIGN(HAnd);
4190};
4191
4192class HOr : public HBinaryOperation {
4193 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004194 HOr(Primitive::Type result_type,
4195 HInstruction* left,
4196 HInstruction* right,
4197 uint32_t dex_pc = kNoDexPc)
4198 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004199
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004200 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004201
Roland Levillain9867bc72015-08-05 10:21:34 +01004202 template <typename T, typename U>
4203 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4204
4205 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004206 return GetBlock()->GetGraph()->GetIntConstant(
4207 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004208 }
4209 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004210 return GetBlock()->GetGraph()->GetLongConstant(
4211 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004212 }
4213 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004214 return GetBlock()->GetGraph()->GetLongConstant(
4215 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004216 }
4217 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004218 return GetBlock()->GetGraph()->GetLongConstant(
4219 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004220 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004221
4222 DECLARE_INSTRUCTION(Or);
4223
4224 private:
4225 DISALLOW_COPY_AND_ASSIGN(HOr);
4226};
4227
4228class HXor : public HBinaryOperation {
4229 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004230 HXor(Primitive::Type result_type,
4231 HInstruction* left,
4232 HInstruction* right,
4233 uint32_t dex_pc = kNoDexPc)
4234 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004235
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004236 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004237
Roland Levillain9867bc72015-08-05 10:21:34 +01004238 template <typename T, typename U>
4239 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4240
4241 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004242 return GetBlock()->GetGraph()->GetIntConstant(
4243 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004244 }
4245 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004246 return GetBlock()->GetGraph()->GetLongConstant(
4247 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004248 }
4249 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004250 return GetBlock()->GetGraph()->GetLongConstant(
4251 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004252 }
4253 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004254 return GetBlock()->GetGraph()->GetLongConstant(
4255 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004256 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004257
4258 DECLARE_INSTRUCTION(Xor);
4259
4260 private:
4261 DISALLOW_COPY_AND_ASSIGN(HXor);
4262};
4263
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004264class HRor : public HBinaryOperation {
4265 public:
4266 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4267 : HBinaryOperation(result_type, value, distance) {}
4268
4269 template <typename T, typename U, typename V>
4270 T Compute(T x, U y, V max_shift_value) const {
4271 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4272 "V is not the unsigned integer type corresponding to T");
4273 V ux = static_cast<V>(x);
4274 if ((y & max_shift_value) == 0) {
4275 return static_cast<T>(ux);
4276 } else {
4277 const V reg_bits = sizeof(T) * 8;
4278 return static_cast<T>(ux >> (y & max_shift_value)) |
4279 (x << (reg_bits - (y & max_shift_value)));
4280 }
4281 }
4282
4283 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4284 return GetBlock()->GetGraph()->GetIntConstant(
4285 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4286 }
4287 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4288 return GetBlock()->GetGraph()->GetLongConstant(
4289 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4290 }
4291 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4292 return GetBlock()->GetGraph()->GetLongConstant(
4293 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4294 }
4295
4296 DECLARE_INSTRUCTION(Ror);
4297
4298 private:
4299 DISALLOW_COPY_AND_ASSIGN(HRor);
4300};
4301
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004302// The value of a parameter in this method. Its location depends on
4303// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004304class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004305 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004306 HParameterValue(const DexFile& dex_file,
4307 uint16_t type_index,
4308 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004309 Primitive::Type parameter_type,
4310 bool is_this = false)
4311 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004312 dex_file_(dex_file),
4313 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004314 index_(index),
4315 is_this_(is_this),
4316 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004317
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004318 const DexFile& GetDexFile() const { return dex_file_; }
4319 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004320 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004321 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004322
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004323 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4324 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004325
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004326 DECLARE_INSTRUCTION(ParameterValue);
4327
4328 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004329 const DexFile& dex_file_;
4330 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004331 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004332 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004333 const uint8_t index_;
4334
Calin Juravle10e244f2015-01-26 18:54:32 +00004335 // Whether or not the parameter value corresponds to 'this' argument.
4336 const bool is_this_;
4337
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004338 bool can_be_null_;
4339
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004340 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4341};
4342
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004343class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004344 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004345 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4346 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004347
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004348 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004349 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004350 return true;
4351 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004352
Roland Levillain9867bc72015-08-05 10:21:34 +01004353 template <typename T> T Compute(T x) const { return ~x; }
4354
4355 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004356 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004357 }
4358 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004359 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004360 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004361
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004362 DECLARE_INSTRUCTION(Not);
4363
4364 private:
4365 DISALLOW_COPY_AND_ASSIGN(HNot);
4366};
4367
David Brazdil66d126e2015-04-03 16:02:44 +01004368class HBooleanNot : public HUnaryOperation {
4369 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004370 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4371 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004372
4373 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004374 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004375 return true;
4376 }
4377
Roland Levillain9867bc72015-08-05 10:21:34 +01004378 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004379 DCHECK(IsUint<1>(x));
4380 return !x;
4381 }
4382
Roland Levillain9867bc72015-08-05 10:21:34 +01004383 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004384 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004385 }
4386 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4387 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004388 UNREACHABLE();
4389 }
4390
4391 DECLARE_INSTRUCTION(BooleanNot);
4392
4393 private:
4394 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4395};
4396
Roland Levillaindff1f282014-11-05 14:15:05 +00004397class HTypeConversion : public HExpression<1> {
4398 public:
4399 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004400 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004401 : HExpression(result_type,
4402 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4403 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004404 SetRawInputAt(0, input);
4405 DCHECK_NE(input->GetType(), result_type);
4406 }
4407
4408 HInstruction* GetInput() const { return InputAt(0); }
4409 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4410 Primitive::Type GetResultType() const { return GetType(); }
4411
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004412 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004413 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004414
Roland Levillaindff1f282014-11-05 14:15:05 +00004415 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004416 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004417
Mark Mendelle82549b2015-05-06 10:55:34 -04004418 // Try to statically evaluate the conversion and return a HConstant
4419 // containing the result. If the input cannot be converted, return nullptr.
4420 HConstant* TryStaticEvaluation() const;
4421
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004422 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4423 Primitive::Type result_type) {
4424 // Some architectures may not require the 'GC' side effects, but at this point
4425 // in the compilation process we do not know what architecture we will
4426 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004427 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4428 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004429 return SideEffects::CanTriggerGC();
4430 }
4431 return SideEffects::None();
4432 }
4433
Roland Levillaindff1f282014-11-05 14:15:05 +00004434 DECLARE_INSTRUCTION(TypeConversion);
4435
4436 private:
4437 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4438};
4439
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004440static constexpr uint32_t kNoRegNumber = -1;
4441
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004442class HPhi : public HInstruction {
4443 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004444 HPhi(ArenaAllocator* arena,
4445 uint32_t reg_number,
4446 size_t number_of_inputs,
4447 Primitive::Type type,
4448 uint32_t dex_pc = kNoDexPc)
4449 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004450 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004451 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004452 type_(ToPhiType(type)),
4453 // Phis are constructed live and marked dead if conflicting or unused.
4454 // Individual steps of SsaBuilder should assume that if a phi has been
4455 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4456 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004457 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004458 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004459 }
4460
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004461 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4462 static Primitive::Type ToPhiType(Primitive::Type type) {
4463 switch (type) {
4464 case Primitive::kPrimBoolean:
4465 case Primitive::kPrimByte:
4466 case Primitive::kPrimShort:
4467 case Primitive::kPrimChar:
4468 return Primitive::kPrimInt;
4469 default:
4470 return type;
4471 }
4472 }
4473
David Brazdilffee3d32015-07-06 11:48:53 +01004474 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4475
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004476 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004477
4478 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004479 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004480
Calin Juravle10e244f2015-01-26 18:54:32 +00004481 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil4833f5a2015-12-16 10:37:39 +00004482 void SetType(Primitive::Type new_type) {
4483 // Make sure that only valid type changes occur. The following are allowed:
4484 // (1) int -> float/ref (primitive type propagation),
4485 // (2) long -> double (primitive type propagation).
4486 DCHECK(type_ == new_type ||
4487 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4488 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4489 (type_ == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4490 type_ = new_type;
4491 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004492
Calin Juravle10e244f2015-01-26 18:54:32 +00004493 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4494 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4495
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004496 uint32_t GetRegNumber() const { return reg_number_; }
4497
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004498 void SetDead() { is_live_ = false; }
4499 void SetLive() { is_live_ = true; }
4500 bool IsDead() const { return !is_live_; }
4501 bool IsLive() const { return is_live_; }
4502
David Brazdil77a48ae2015-09-15 12:34:04 +00004503 bool IsVRegEquivalentOf(HInstruction* other) const {
4504 return other != nullptr
4505 && other->IsPhi()
4506 && other->AsPhi()->GetBlock() == GetBlock()
4507 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4508 }
4509
Calin Juravlea4f88312015-04-16 12:57:19 +01004510 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4511 // An equivalent phi is a phi having the same dex register and type.
4512 // It assumes that phis with the same dex register are adjacent.
4513 HPhi* GetNextEquivalentPhiWithSameType() {
4514 HInstruction* next = GetNext();
4515 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4516 if (next->GetType() == GetType()) {
4517 return next->AsPhi();
4518 }
4519 next = next->GetNext();
4520 }
4521 return nullptr;
4522 }
4523
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004524 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004525
David Brazdil1abb4192015-02-17 18:33:36 +00004526 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004527 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004528 return inputs_[index];
4529 }
David Brazdil1abb4192015-02-17 18:33:36 +00004530
4531 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004532 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004533 }
4534
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004535 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004536 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004537 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004538 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004539 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004540 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004541
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004542 DISALLOW_COPY_AND_ASSIGN(HPhi);
4543};
4544
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004545class HNullCheck : public HExpression<1> {
4546 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004547 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4548 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004549 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004550 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004551 SetRawInputAt(0, value);
4552 }
4553
Calin Juravle10e244f2015-01-26 18:54:32 +00004554 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004555 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004556 return true;
4557 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004558
Calin Juravle10e244f2015-01-26 18:54:32 +00004559 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004560
Calin Juravle10e244f2015-01-26 18:54:32 +00004561 bool CanThrow() const OVERRIDE { return true; }
4562
4563 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004564
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004565
4566 DECLARE_INSTRUCTION(NullCheck);
4567
4568 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004569 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4570};
4571
4572class FieldInfo : public ValueObject {
4573 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004574 FieldInfo(MemberOffset field_offset,
4575 Primitive::Type field_type,
4576 bool is_volatile,
4577 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004578 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004579 const DexFile& dex_file,
4580 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004581 : field_offset_(field_offset),
4582 field_type_(field_type),
4583 is_volatile_(is_volatile),
4584 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004585 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004586 dex_file_(dex_file),
4587 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004588
4589 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004590 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004591 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004592 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004593 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004594 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004595 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004596
4597 private:
4598 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004599 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004600 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004601 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004602 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004603 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004604 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004605};
4606
4607class HInstanceFieldGet : public HExpression<1> {
4608 public:
4609 HInstanceFieldGet(HInstruction* value,
4610 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004611 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004612 bool is_volatile,
4613 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004614 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004615 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004616 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004617 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004618 : HExpression(field_type,
4619 SideEffects::FieldReadOfType(field_type, is_volatile),
4620 dex_pc),
4621 field_info_(field_offset,
4622 field_type,
4623 is_volatile,
4624 field_idx,
4625 declaring_class_def_index,
4626 dex_file,
4627 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004628 SetRawInputAt(0, value);
4629 }
4630
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004631 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004632
4633 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4634 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4635 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004636 }
4637
Calin Juravle641547a2015-04-21 22:08:51 +01004638 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4639 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004640 }
4641
4642 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004643 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4644 }
4645
Calin Juravle52c48962014-12-16 17:02:57 +00004646 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004647 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004648 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004649 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004650
4651 DECLARE_INSTRUCTION(InstanceFieldGet);
4652
4653 private:
4654 const FieldInfo field_info_;
4655
4656 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4657};
4658
4659class HInstanceFieldSet : public HTemplateInstruction<2> {
4660 public:
4661 HInstanceFieldSet(HInstruction* object,
4662 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004663 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004664 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004665 bool is_volatile,
4666 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004667 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004668 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004669 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004670 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004671 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4672 dex_pc),
4673 field_info_(field_offset,
4674 field_type,
4675 is_volatile,
4676 field_idx,
4677 declaring_class_def_index,
4678 dex_file,
4679 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004680 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004681 SetRawInputAt(0, object);
4682 SetRawInputAt(1, value);
4683 }
4684
Calin Juravle641547a2015-04-21 22:08:51 +01004685 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4686 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004687 }
4688
Calin Juravle52c48962014-12-16 17:02:57 +00004689 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004690 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004691 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004692 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004693 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004694 bool GetValueCanBeNull() const { return value_can_be_null_; }
4695 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004696
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004697 DECLARE_INSTRUCTION(InstanceFieldSet);
4698
4699 private:
4700 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004701 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004702
4703 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4704};
4705
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004706class HArrayGet : public HExpression<2> {
4707 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004708 HArrayGet(HInstruction* array,
4709 HInstruction* index,
4710 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004711 uint32_t dex_pc,
4712 SideEffects additional_side_effects = SideEffects::None())
4713 : HExpression(type,
4714 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004715 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004716 SetRawInputAt(0, array);
4717 SetRawInputAt(1, index);
4718 }
4719
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004720 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004721 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004722 return true;
4723 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004724 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004725 // TODO: We can be smarter here.
4726 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4727 // which generates the implicit null check. There are cases when these can be removed
4728 // to produce better code. If we ever add optimizations to do so we should allow an
4729 // implicit check here (as long as the address falls in the first page).
4730 return false;
4731 }
4732
David Brazdil4833f5a2015-12-16 10:37:39 +00004733 bool IsEquivalentOf(HArrayGet* other) const {
4734 bool result = (GetDexPc() == other->GetDexPc());
4735 if (kIsDebugBuild && result) {
4736 DCHECK_EQ(GetBlock(), other->GetBlock());
4737 DCHECK_EQ(GetArray(), other->GetArray());
4738 DCHECK_EQ(GetIndex(), other->GetIndex());
4739 if (Primitive::IsIntOrLongType(GetType())) {
4740 DCHECK(Primitive::IsFloatingPointType(other->GetType()));
4741 } else {
4742 DCHECK(Primitive::IsFloatingPointType(GetType()));
4743 DCHECK(Primitive::IsIntOrLongType(other->GetType()));
4744 }
4745 }
4746 return result;
4747 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004748
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004749 HInstruction* GetArray() const { return InputAt(0); }
4750 HInstruction* GetIndex() const { return InputAt(1); }
4751
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004752 DECLARE_INSTRUCTION(ArrayGet);
4753
4754 private:
4755 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4756};
4757
4758class HArraySet : public HTemplateInstruction<3> {
4759 public:
4760 HArraySet(HInstruction* array,
4761 HInstruction* index,
4762 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004763 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004764 uint32_t dex_pc,
4765 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004766 : HTemplateInstruction(
4767 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004768 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4769 additional_side_effects),
4770 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004771 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004772 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004773 value_can_be_null_(true),
4774 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004775 SetRawInputAt(0, array);
4776 SetRawInputAt(1, index);
4777 SetRawInputAt(2, value);
4778 }
4779
Calin Juravle77520bc2015-01-12 18:45:46 +00004780 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004781 // We currently always call a runtime method to catch array store
4782 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004783 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004784 }
4785
Mingyao Yang81014cb2015-06-02 03:16:27 -07004786 // Can throw ArrayStoreException.
4787 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4788
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004789 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004790 // TODO: Same as for ArrayGet.
4791 return false;
4792 }
4793
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004794 void ClearNeedsTypeCheck() {
4795 needs_type_check_ = false;
4796 }
4797
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004798 void ClearValueCanBeNull() {
4799 value_can_be_null_ = false;
4800 }
4801
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004802 void SetStaticTypeOfArrayIsObjectArray() {
4803 static_type_of_array_is_object_array_ = true;
4804 }
4805
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004806 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004807 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004808 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004809
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004810 HInstruction* GetArray() const { return InputAt(0); }
4811 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004812 HInstruction* GetValue() const { return InputAt(2); }
4813
4814 Primitive::Type GetComponentType() const {
4815 // The Dex format does not type floating point index operations. Since the
4816 // `expected_component_type_` is set during building and can therefore not
4817 // be correct, we also check what is the value type. If it is a floating
4818 // point type, we must use that type.
4819 Primitive::Type value_type = GetValue()->GetType();
4820 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4821 ? value_type
4822 : expected_component_type_;
4823 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004824
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004825 Primitive::Type GetRawExpectedComponentType() const {
4826 return expected_component_type_;
4827 }
4828
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004829 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4830 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4831 }
4832
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004833 DECLARE_INSTRUCTION(ArraySet);
4834
4835 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004836 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004837 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004838 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004839 // Cached information for the reference_type_info_ so that codegen
4840 // does not need to inspect the static type.
4841 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004842
4843 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4844};
4845
4846class HArrayLength : public HExpression<1> {
4847 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004848 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004849 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004850 // Note that arrays do not change length, so the instruction does not
4851 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004852 SetRawInputAt(0, array);
4853 }
4854
Calin Juravle77520bc2015-01-12 18:45:46 +00004855 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004856 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004857 return true;
4858 }
Calin Juravle641547a2015-04-21 22:08:51 +01004859 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4860 return obj == InputAt(0);
4861 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004862
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004863 DECLARE_INSTRUCTION(ArrayLength);
4864
4865 private:
4866 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4867};
4868
4869class HBoundsCheck : public HExpression<2> {
4870 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004871 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
4872 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004873 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004874 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004875 DCHECK(index->GetType() == Primitive::kPrimInt);
4876 SetRawInputAt(0, index);
4877 SetRawInputAt(1, length);
4878 }
4879
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004880 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004881 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004882 return true;
4883 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004884
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004885 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004886
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004887 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004888
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004889 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004890
4891 DECLARE_INSTRUCTION(BoundsCheck);
4892
4893 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004894 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4895};
4896
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004897/**
4898 * Some DEX instructions are folded into multiple HInstructions that need
4899 * to stay live until the last HInstruction. This class
4900 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004901 * HInstruction stays live. `index` represents the stack location index of the
4902 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004903 */
4904class HTemporary : public HTemplateInstruction<0> {
4905 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004906 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4907 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004908
4909 size_t GetIndex() const { return index_; }
4910
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004911 Primitive::Type GetType() const OVERRIDE {
4912 // The previous instruction is the one that will be stored in the temporary location.
4913 DCHECK(GetPrevious() != nullptr);
4914 return GetPrevious()->GetType();
4915 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004916
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004917 DECLARE_INSTRUCTION(Temporary);
4918
4919 private:
4920 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004921 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4922};
4923
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004924class HSuspendCheck : public HTemplateInstruction<0> {
4925 public:
4926 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004927 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004928
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004929 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004930 return true;
4931 }
4932
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004933 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4934 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004935
4936 DECLARE_INSTRUCTION(SuspendCheck);
4937
4938 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004939 // Only used for code generation, in order to share the same slow path between back edges
4940 // of a same loop.
4941 SlowPathCode* slow_path_;
4942
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004943 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4944};
4945
David Srbecky0cf44932015-12-09 14:09:59 +00004946// Pseudo-instruction which provides the native debugger with mapping information.
4947// It ensures that we can generate line number and local variables at this point.
4948class HNativeDebugInfo : public HTemplateInstruction<0> {
4949 public:
4950 explicit HNativeDebugInfo(uint32_t dex_pc)
4951 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
4952
4953 bool NeedsEnvironment() const OVERRIDE {
4954 return true;
4955 }
4956
4957 DECLARE_INSTRUCTION(NativeDebugInfo);
4958
4959 private:
4960 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
4961};
4962
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004963/**
4964 * Instruction to load a Class object.
4965 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004966class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004967 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004968 HLoadClass(HCurrentMethod* current_method,
4969 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004970 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004971 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004972 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004973 bool needs_access_check,
4974 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004975 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004976 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004977 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004978 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004979 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004980 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004981 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00004982 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004983 // Referrers class should not need access check. We never inline unverified
4984 // methods so we can't possibly end up in this situation.
4985 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004986 SetRawInputAt(0, current_method);
4987 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004988
4989 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004990
4991 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004992 // Note that we don't need to test for generate_clinit_check_.
4993 // Whether or not we need to generate the clinit check is processed in
4994 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004995 return other->AsLoadClass()->type_index_ == type_index_ &&
4996 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004997 }
4998
4999 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5000
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005001 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005002 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005003 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005004
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005005 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005006 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005007 }
5008
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005009 bool MustGenerateClinitCheck() const {
5010 return generate_clinit_check_;
5011 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005012
Calin Juravle0ba218d2015-05-19 18:46:01 +01005013 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005014 // The entrypoint the code generator is going to call does not do
5015 // clinit of the class.
5016 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01005017 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005018 }
5019
5020 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005021 return MustGenerateClinitCheck() ||
5022 (!is_referrers_class_ && !is_in_dex_cache_) ||
5023 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01005024 }
5025
5026 bool NeedsAccessCheck() const {
5027 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005028 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005029
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005030 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005031 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005032 }
5033
Calin Juravleacf735c2015-02-12 15:25:22 +00005034 ReferenceTypeInfo GetLoadedClassRTI() {
5035 return loaded_class_rti_;
5036 }
5037
5038 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5039 // Make sure we only set exact types (the loaded class should never be merged).
5040 DCHECK(rti.IsExact());
5041 loaded_class_rti_ = rti;
5042 }
5043
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005044 const DexFile& GetDexFile() { return dex_file_; }
5045
Vladimir Markodc151b22015-10-15 18:02:30 +01005046 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005047
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005048 static SideEffects SideEffectsForArchRuntimeCalls() {
5049 return SideEffects::CanTriggerGC();
5050 }
5051
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005052 bool IsInDexCache() const { return is_in_dex_cache_; }
5053
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005054 DECLARE_INSTRUCTION(LoadClass);
5055
5056 private:
5057 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005058 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005059 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005060 // Whether this instruction must generate the initialization check.
5061 // Used for code generation.
5062 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005063 const bool needs_access_check_;
5064 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005065
Calin Juravleacf735c2015-02-12 15:25:22 +00005066 ReferenceTypeInfo loaded_class_rti_;
5067
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005068 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5069};
5070
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005071class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005072 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005073 HLoadString(HCurrentMethod* current_method,
5074 uint32_t string_index,
5075 uint32_t dex_pc,
5076 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005077 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005078 string_index_(string_index),
5079 is_in_dex_cache_(is_in_dex_cache) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005080 SetRawInputAt(0, current_method);
5081 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005082
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005083 bool CanBeMoved() const OVERRIDE { return true; }
5084
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005085 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5086 return other->AsLoadString()->string_index_ == string_index_;
5087 }
5088
5089 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5090
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005091 uint32_t GetStringIndex() const { return string_index_; }
5092
5093 // TODO: Can we deopt or debug when we resolve a string?
5094 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01005095 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005096 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005097 bool IsInDexCache() const { return is_in_dex_cache_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005098
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005099 static SideEffects SideEffectsForArchRuntimeCalls() {
5100 return SideEffects::CanTriggerGC();
5101 }
5102
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005103 DECLARE_INSTRUCTION(LoadString);
5104
5105 private:
5106 const uint32_t string_index_;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005107 const bool is_in_dex_cache_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005108
5109 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5110};
5111
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005112/**
5113 * Performs an initialization check on its Class object input.
5114 */
5115class HClinitCheck : public HExpression<1> {
5116 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005117 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005118 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005119 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005120 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5121 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005122 SetRawInputAt(0, constant);
5123 }
5124
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005125 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005126 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005127 return true;
5128 }
5129
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005130 bool NeedsEnvironment() const OVERRIDE {
5131 // May call runtime to initialize the class.
5132 return true;
5133 }
5134
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005135 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005136
5137 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5138
5139 DECLARE_INSTRUCTION(ClinitCheck);
5140
5141 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005142 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5143};
5144
5145class HStaticFieldGet : public HExpression<1> {
5146 public:
5147 HStaticFieldGet(HInstruction* cls,
5148 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005149 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005150 bool is_volatile,
5151 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005152 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005153 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005154 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005155 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005156 : HExpression(field_type,
5157 SideEffects::FieldReadOfType(field_type, is_volatile),
5158 dex_pc),
5159 field_info_(field_offset,
5160 field_type,
5161 is_volatile,
5162 field_idx,
5163 declaring_class_def_index,
5164 dex_file,
5165 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005166 SetRawInputAt(0, cls);
5167 }
5168
Calin Juravle52c48962014-12-16 17:02:57 +00005169
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005170 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005171
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005172 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005173 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5174 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005175 }
5176
5177 size_t ComputeHashCode() const OVERRIDE {
5178 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5179 }
5180
Calin Juravle52c48962014-12-16 17:02:57 +00005181 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005182 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5183 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005184 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005185
5186 DECLARE_INSTRUCTION(StaticFieldGet);
5187
5188 private:
5189 const FieldInfo field_info_;
5190
5191 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5192};
5193
5194class HStaticFieldSet : public HTemplateInstruction<2> {
5195 public:
5196 HStaticFieldSet(HInstruction* cls,
5197 HInstruction* value,
5198 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005199 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005200 bool is_volatile,
5201 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005202 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005203 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005204 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005205 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005206 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5207 dex_pc),
5208 field_info_(field_offset,
5209 field_type,
5210 is_volatile,
5211 field_idx,
5212 declaring_class_def_index,
5213 dex_file,
5214 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005215 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005216 SetRawInputAt(0, cls);
5217 SetRawInputAt(1, value);
5218 }
5219
Calin Juravle52c48962014-12-16 17:02:57 +00005220 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005221 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5222 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005223 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005224
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005225 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005226 bool GetValueCanBeNull() const { return value_can_be_null_; }
5227 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005228
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005229 DECLARE_INSTRUCTION(StaticFieldSet);
5230
5231 private:
5232 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005233 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005234
5235 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5236};
5237
Calin Juravlee460d1d2015-09-29 04:52:17 +01005238class HUnresolvedInstanceFieldGet : public HExpression<1> {
5239 public:
5240 HUnresolvedInstanceFieldGet(HInstruction* obj,
5241 Primitive::Type field_type,
5242 uint32_t field_index,
5243 uint32_t dex_pc)
5244 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5245 field_index_(field_index) {
5246 SetRawInputAt(0, obj);
5247 }
5248
5249 bool NeedsEnvironment() const OVERRIDE { return true; }
5250 bool CanThrow() const OVERRIDE { return true; }
5251
5252 Primitive::Type GetFieldType() const { return GetType(); }
5253 uint32_t GetFieldIndex() const { return field_index_; }
5254
5255 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5256
5257 private:
5258 const uint32_t field_index_;
5259
5260 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5261};
5262
5263class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5264 public:
5265 HUnresolvedInstanceFieldSet(HInstruction* obj,
5266 HInstruction* value,
5267 Primitive::Type field_type,
5268 uint32_t field_index,
5269 uint32_t dex_pc)
5270 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5271 field_type_(field_type),
5272 field_index_(field_index) {
5273 DCHECK_EQ(field_type, value->GetType());
5274 SetRawInputAt(0, obj);
5275 SetRawInputAt(1, value);
5276 }
5277
5278 bool NeedsEnvironment() const OVERRIDE { return true; }
5279 bool CanThrow() const OVERRIDE { return true; }
5280
5281 Primitive::Type GetFieldType() const { return field_type_; }
5282 uint32_t GetFieldIndex() const { return field_index_; }
5283
5284 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5285
5286 private:
5287 const Primitive::Type field_type_;
5288 const uint32_t field_index_;
5289
5290 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5291};
5292
5293class HUnresolvedStaticFieldGet : public HExpression<0> {
5294 public:
5295 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5296 uint32_t field_index,
5297 uint32_t dex_pc)
5298 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5299 field_index_(field_index) {
5300 }
5301
5302 bool NeedsEnvironment() const OVERRIDE { return true; }
5303 bool CanThrow() const OVERRIDE { return true; }
5304
5305 Primitive::Type GetFieldType() const { return GetType(); }
5306 uint32_t GetFieldIndex() const { return field_index_; }
5307
5308 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5309
5310 private:
5311 const uint32_t field_index_;
5312
5313 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5314};
5315
5316class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5317 public:
5318 HUnresolvedStaticFieldSet(HInstruction* value,
5319 Primitive::Type field_type,
5320 uint32_t field_index,
5321 uint32_t dex_pc)
5322 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5323 field_type_(field_type),
5324 field_index_(field_index) {
5325 DCHECK_EQ(field_type, value->GetType());
5326 SetRawInputAt(0, value);
5327 }
5328
5329 bool NeedsEnvironment() const OVERRIDE { return true; }
5330 bool CanThrow() const OVERRIDE { return true; }
5331
5332 Primitive::Type GetFieldType() const { return field_type_; }
5333 uint32_t GetFieldIndex() const { return field_index_; }
5334
5335 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5336
5337 private:
5338 const Primitive::Type field_type_;
5339 const uint32_t field_index_;
5340
5341 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5342};
5343
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005344// Implement the move-exception DEX instruction.
5345class HLoadException : public HExpression<0> {
5346 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005347 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5348 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005349
David Brazdilbbd733e2015-08-18 17:48:17 +01005350 bool CanBeNull() const OVERRIDE { return false; }
5351
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005352 DECLARE_INSTRUCTION(LoadException);
5353
5354 private:
5355 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5356};
5357
David Brazdilcb1c0552015-08-04 16:22:25 +01005358// Implicit part of move-exception which clears thread-local exception storage.
5359// Must not be removed because the runtime expects the TLS to get cleared.
5360class HClearException : public HTemplateInstruction<0> {
5361 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005362 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5363 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005364
5365 DECLARE_INSTRUCTION(ClearException);
5366
5367 private:
5368 DISALLOW_COPY_AND_ASSIGN(HClearException);
5369};
5370
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005371class HThrow : public HTemplateInstruction<1> {
5372 public:
5373 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005374 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005375 SetRawInputAt(0, exception);
5376 }
5377
5378 bool IsControlFlow() const OVERRIDE { return true; }
5379
5380 bool NeedsEnvironment() const OVERRIDE { return true; }
5381
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005382 bool CanThrow() const OVERRIDE { return true; }
5383
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005384
5385 DECLARE_INSTRUCTION(Throw);
5386
5387 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005388 DISALLOW_COPY_AND_ASSIGN(HThrow);
5389};
5390
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005391/**
5392 * Implementation strategies for the code generator of a HInstanceOf
5393 * or `HCheckCast`.
5394 */
5395enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005396 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005397 kExactCheck, // Can do a single class compare.
5398 kClassHierarchyCheck, // Can just walk the super class chain.
5399 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5400 kInterfaceCheck, // No optimization yet when checking against an interface.
5401 kArrayObjectCheck, // Can just check if the array is not primitive.
5402 kArrayCheck // No optimization yet when checking against a generic array.
5403};
5404
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005405class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005406 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005407 HInstanceOf(HInstruction* object,
5408 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005409 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005410 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005411 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005412 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005413 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005414 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005415 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005416 SetRawInputAt(0, object);
5417 SetRawInputAt(1, constant);
5418 }
5419
5420 bool CanBeMoved() const OVERRIDE { return true; }
5421
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005422 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005423 return true;
5424 }
5425
5426 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005427 return false;
5428 }
5429
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005430 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5431
5432 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005433
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005434 // Used only in code generation.
5435 bool MustDoNullCheck() const { return must_do_null_check_; }
5436 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5437
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005438 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5439 return (check_kind == TypeCheckKind::kExactCheck)
5440 ? SideEffects::None()
5441 // Mips currently does runtime calls for any other checks.
5442 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005443 }
5444
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005445 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005446
5447 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005448 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005449 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005450
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005451 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5452};
5453
Calin Juravleb1498f62015-02-16 13:13:29 +00005454class HBoundType : public HExpression<1> {
5455 public:
David Brazdilf5552582015-12-27 13:36:12 +00005456 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005457 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
David Brazdilf5552582015-12-27 13:36:12 +00005458 upper_bound_(ReferenceTypeInfo::CreateInvalid()),
5459 upper_can_be_null_(true),
5460 can_be_null_(true) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005461 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005462 SetRawInputAt(0, input);
5463 }
5464
David Brazdilf5552582015-12-27 13:36:12 +00005465 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005466 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5467 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
David Brazdilf5552582015-12-27 13:36:12 +00005468 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005469
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005470 void SetCanBeNull(bool can_be_null) {
5471 DCHECK(upper_can_be_null_ || !can_be_null);
5472 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005473 }
5474
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005475 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5476
Calin Juravleb1498f62015-02-16 13:13:29 +00005477 DECLARE_INSTRUCTION(BoundType);
5478
5479 private:
5480 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005481 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5482 // It is used to bound the type in cases like:
5483 // if (x instanceof ClassX) {
5484 // // uper_bound_ will be ClassX
5485 // }
David Brazdilf5552582015-12-27 13:36:12 +00005486 ReferenceTypeInfo upper_bound_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005487 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5488 // is false then can_be_null_ cannot be true).
David Brazdilf5552582015-12-27 13:36:12 +00005489 bool upper_can_be_null_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005490 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005491
5492 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5493};
5494
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005495class HCheckCast : public HTemplateInstruction<2> {
5496 public:
5497 HCheckCast(HInstruction* object,
5498 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005499 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005500 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005501 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005502 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005503 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005504 SetRawInputAt(0, object);
5505 SetRawInputAt(1, constant);
5506 }
5507
5508 bool CanBeMoved() const OVERRIDE { return true; }
5509
5510 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5511 return true;
5512 }
5513
5514 bool NeedsEnvironment() const OVERRIDE {
5515 // Instruction may throw a CheckCastError.
5516 return true;
5517 }
5518
5519 bool CanThrow() const OVERRIDE { return true; }
5520
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005521 bool MustDoNullCheck() const { return must_do_null_check_; }
5522 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005523 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005524
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005525 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005526
5527 DECLARE_INSTRUCTION(CheckCast);
5528
5529 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005530 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005531 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005532
5533 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005534};
5535
Calin Juravle27df7582015-04-17 19:12:31 +01005536class HMemoryBarrier : public HTemplateInstruction<0> {
5537 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005538 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005539 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005540 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005541 barrier_kind_(barrier_kind) {}
5542
5543 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5544
5545 DECLARE_INSTRUCTION(MemoryBarrier);
5546
5547 private:
5548 const MemBarrierKind barrier_kind_;
5549
5550 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5551};
5552
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005553class HMonitorOperation : public HTemplateInstruction<1> {
5554 public:
5555 enum OperationKind {
5556 kEnter,
5557 kExit,
5558 };
5559
5560 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005561 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005562 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5563 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005564 SetRawInputAt(0, object);
5565 }
5566
5567 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005568 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5569
5570 bool CanThrow() const OVERRIDE {
5571 // Verifier guarantees that monitor-exit cannot throw.
5572 // This is important because it allows the HGraphBuilder to remove
5573 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5574 return IsEnter();
5575 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005576
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005577
5578 bool IsEnter() const { return kind_ == kEnter; }
5579
5580 DECLARE_INSTRUCTION(MonitorOperation);
5581
Calin Juravle52c48962014-12-16 17:02:57 +00005582 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005583 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005584
5585 private:
5586 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5587};
5588
Vladimir Markof9f64412015-09-02 14:05:49 +01005589class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005590 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005591 MoveOperands(Location source,
5592 Location destination,
5593 Primitive::Type type,
5594 HInstruction* instruction)
5595 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005596
5597 Location GetSource() const { return source_; }
5598 Location GetDestination() const { return destination_; }
5599
5600 void SetSource(Location value) { source_ = value; }
5601 void SetDestination(Location value) { destination_ = value; }
5602
5603 // The parallel move resolver marks moves as "in-progress" by clearing the
5604 // destination (but not the source).
5605 Location MarkPending() {
5606 DCHECK(!IsPending());
5607 Location dest = destination_;
5608 destination_ = Location::NoLocation();
5609 return dest;
5610 }
5611
5612 void ClearPending(Location dest) {
5613 DCHECK(IsPending());
5614 destination_ = dest;
5615 }
5616
5617 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00005618 DCHECK(source_.IsValid() || destination_.IsInvalid());
5619 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005620 }
5621
5622 // True if this blocks a move from the given location.
5623 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005624 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005625 }
5626
5627 // A move is redundant if it's been eliminated, if its source and
5628 // destination are the same, or if its destination is unneeded.
5629 bool IsRedundant() const {
5630 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5631 }
5632
5633 // We clear both operands to indicate move that's been eliminated.
5634 void Eliminate() {
5635 source_ = destination_ = Location::NoLocation();
5636 }
5637
5638 bool IsEliminated() const {
5639 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5640 return source_.IsInvalid();
5641 }
5642
Alexey Frunze4dda3372015-06-01 18:31:49 -07005643 Primitive::Type GetType() const { return type_; }
5644
Nicolas Geoffray90218252015-04-15 11:56:51 +01005645 bool Is64BitMove() const {
5646 return Primitive::Is64BitType(type_);
5647 }
5648
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005649 HInstruction* GetInstruction() const { return instruction_; }
5650
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005651 private:
5652 Location source_;
5653 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005654 // The type this move is for.
5655 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005656 // The instruction this move is assocatied with. Null when this move is
5657 // for moving an input in the expected locations of user (including a phi user).
5658 // This is only used in debug mode, to ensure we do not connect interval siblings
5659 // in the same parallel move.
5660 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005661};
5662
Roland Levillainc9285912015-12-18 10:38:42 +00005663std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
5664
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005665static constexpr size_t kDefaultNumberOfMoves = 4;
5666
5667class HParallelMove : public HTemplateInstruction<0> {
5668 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005669 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005670 : HTemplateInstruction(SideEffects::None(), dex_pc),
5671 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5672 moves_.reserve(kDefaultNumberOfMoves);
5673 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005674
Nicolas Geoffray90218252015-04-15 11:56:51 +01005675 void AddMove(Location source,
5676 Location destination,
5677 Primitive::Type type,
5678 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005679 DCHECK(source.IsValid());
5680 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005681 if (kIsDebugBuild) {
5682 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005683 for (const MoveOperands& move : moves_) {
5684 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005685 // Special case the situation where the move is for the spill slot
5686 // of the instruction.
5687 if ((GetPrevious() == instruction)
5688 || ((GetPrevious() == nullptr)
5689 && instruction->IsPhi()
5690 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005691 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005692 << "Doing parallel moves for the same instruction.";
5693 } else {
5694 DCHECK(false) << "Doing parallel moves for the same instruction.";
5695 }
5696 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005697 }
5698 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005699 for (const MoveOperands& move : moves_) {
5700 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005701 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005702 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005703 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005704 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005705 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005706 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005707 }
5708
Vladimir Marko225b6462015-09-28 12:17:40 +01005709 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005710 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005711 }
5712
Vladimir Marko225b6462015-09-28 12:17:40 +01005713 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005714
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005715 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005716
5717 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005718 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005719
5720 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5721};
5722
Mark Mendell0616ae02015-04-17 12:49:27 -04005723} // namespace art
5724
Vladimir Markob4536b72015-11-24 13:45:23 +00005725#ifdef ART_ENABLE_CODEGEN_arm
5726#include "nodes_arm.h"
5727#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005728#ifdef ART_ENABLE_CODEGEN_arm64
5729#include "nodes_arm64.h"
5730#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005731#ifdef ART_ENABLE_CODEGEN_x86
5732#include "nodes_x86.h"
5733#endif
5734
5735namespace art {
5736
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005737class HGraphVisitor : public ValueObject {
5738 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005739 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5740 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005741
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005742 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005743 virtual void VisitBasicBlock(HBasicBlock* block);
5744
Roland Levillain633021e2014-10-01 14:12:25 +01005745 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005746 void VisitInsertionOrder();
5747
Roland Levillain633021e2014-10-01 14:12:25 +01005748 // Visit the graph following dominator tree reverse post-order.
5749 void VisitReversePostOrder();
5750
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005751 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005752
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005753 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005754#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005755 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5756
5757 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5758
5759#undef DECLARE_VISIT_INSTRUCTION
5760
5761 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005762 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005763
5764 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5765};
5766
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005767class HGraphDelegateVisitor : public HGraphVisitor {
5768 public:
5769 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5770 virtual ~HGraphDelegateVisitor() {}
5771
5772 // Visit functions that delegate to to super class.
5773#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005774 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005775
5776 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5777
5778#undef DECLARE_VISIT_INSTRUCTION
5779
5780 private:
5781 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5782};
5783
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005784class HInsertionOrderIterator : public ValueObject {
5785 public:
5786 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5787
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005788 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005789 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005790 void Advance() { ++index_; }
5791
5792 private:
5793 const HGraph& graph_;
5794 size_t index_;
5795
5796 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5797};
5798
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005799class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005800 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005801 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5802 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005803 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005804 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005805
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005806 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5807 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005808 void Advance() { ++index_; }
5809
5810 private:
5811 const HGraph& graph_;
5812 size_t index_;
5813
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005814 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005815};
5816
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005817class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005818 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005819 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005820 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005821 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005822 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005823 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005824
5825 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005826 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005827 void Advance() { --index_; }
5828
5829 private:
5830 const HGraph& graph_;
5831 size_t index_;
5832
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005833 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005834};
5835
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005836class HLinearPostOrderIterator : public ValueObject {
5837 public:
5838 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005839 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005840
5841 bool Done() const { return index_ == 0; }
5842
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005843 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005844
5845 void Advance() {
5846 --index_;
5847 DCHECK_GE(index_, 0U);
5848 }
5849
5850 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005851 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005852 size_t index_;
5853
5854 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5855};
5856
5857class HLinearOrderIterator : public ValueObject {
5858 public:
5859 explicit HLinearOrderIterator(const HGraph& graph)
5860 : order_(graph.GetLinearOrder()), index_(0) {}
5861
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005862 bool Done() const { return index_ == order_.size(); }
5863 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005864 void Advance() { ++index_; }
5865
5866 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005867 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005868 size_t index_;
5869
5870 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5871};
5872
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005873// Iterator over the blocks that art part of the loop. Includes blocks part
5874// of an inner loop. The order in which the blocks are iterated is on their
5875// block id.
5876class HBlocksInLoopIterator : public ValueObject {
5877 public:
5878 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5879 : blocks_in_loop_(info.GetBlocks()),
5880 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5881 index_(0) {
5882 if (!blocks_in_loop_.IsBitSet(index_)) {
5883 Advance();
5884 }
5885 }
5886
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005887 bool Done() const { return index_ == blocks_.size(); }
5888 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005889 void Advance() {
5890 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005891 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005892 if (blocks_in_loop_.IsBitSet(index_)) {
5893 break;
5894 }
5895 }
5896 }
5897
5898 private:
5899 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005900 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005901 size_t index_;
5902
5903 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5904};
5905
Mingyao Yang3584bce2015-05-19 16:01:59 -07005906// Iterator over the blocks that art part of the loop. Includes blocks part
5907// of an inner loop. The order in which the blocks are iterated is reverse
5908// post order.
5909class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5910 public:
5911 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5912 : blocks_in_loop_(info.GetBlocks()),
5913 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5914 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005915 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005916 Advance();
5917 }
5918 }
5919
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005920 bool Done() const { return index_ == blocks_.size(); }
5921 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005922 void Advance() {
5923 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005924 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5925 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005926 break;
5927 }
5928 }
5929 }
5930
5931 private:
5932 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005933 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005934 size_t index_;
5935
5936 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5937};
5938
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005939inline int64_t Int64FromConstant(HConstant* constant) {
5940 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5941 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5942 : constant->AsLongConstant()->GetValue();
5943}
5944
Vladimir Marko58155012015-08-19 12:49:41 +00005945inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5946 // For the purposes of the compiler, the dex files must actually be the same object
5947 // if we want to safely treat them as the same. This is especially important for JIT
5948 // as custom class loaders can open the same underlying file (or memory) multiple
5949 // times and provide different class resolution but no two class loaders should ever
5950 // use the same DexFile object - doing so is an unsupported hack that can lead to
5951 // all sorts of weird failures.
5952 return &lhs == &rhs;
5953}
5954
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005955#define INSTRUCTION_TYPE_CHECK(type, super) \
5956 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
5957 inline const H##type* HInstruction::As##type() const { \
5958 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
5959 } \
5960 inline H##type* HInstruction::As##type() { \
5961 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
5962 }
5963
5964 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
5965#undef INSTRUCTION_TYPE_CHECK
5966
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005967} // namespace art
5968
5969#endif // ART_COMPILER_OPTIMIZING_NODES_H_