blob: 57fa55812960b4e0c3aa71ec039188508c06986d [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
162 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
163 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 Geoffraybe9a92a2014-02-25 14:22:56 +0000692 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100693 // Internal recursive implementation of `Populate`.
694 void PopulateRecursive(HBasicBlock* block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000695 void PopulateIrreducibleRecursive(HBasicBlock* block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100696
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000697 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100698 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000699 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100700 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100701 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000702
703 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
704};
705
David Brazdilec16f792015-08-19 15:04:01 +0100706// Stores try/catch information for basic blocks.
707// Note that HGraph is constructed so that catch blocks cannot simultaneously
708// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100709class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100710 public:
711 // Try block information constructor.
712 explicit TryCatchInformation(const HTryBoundary& try_entry)
713 : try_entry_(&try_entry),
714 catch_dex_file_(nullptr),
715 catch_type_index_(DexFile::kDexNoIndex16) {
716 DCHECK(try_entry_ != nullptr);
717 }
718
719 // Catch block information constructor.
720 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
721 : try_entry_(nullptr),
722 catch_dex_file_(&dex_file),
723 catch_type_index_(catch_type_index) {}
724
725 bool IsTryBlock() const { return try_entry_ != nullptr; }
726
727 const HTryBoundary& GetTryEntry() const {
728 DCHECK(IsTryBlock());
729 return *try_entry_;
730 }
731
732 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
733
734 bool IsCatchAllTypeIndex() const {
735 DCHECK(IsCatchBlock());
736 return catch_type_index_ == DexFile::kDexNoIndex16;
737 }
738
739 uint16_t GetCatchTypeIndex() const {
740 DCHECK(IsCatchBlock());
741 return catch_type_index_;
742 }
743
744 const DexFile& GetCatchDexFile() const {
745 DCHECK(IsCatchBlock());
746 return *catch_dex_file_;
747 }
748
749 private:
750 // One of possibly several TryBoundary instructions entering the block's try.
751 // Only set for try blocks.
752 const HTryBoundary* try_entry_;
753
754 // Exception type information. Only set for catch blocks.
755 const DexFile* catch_dex_file_;
756 const uint16_t catch_type_index_;
757};
758
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100759static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100760static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100761
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000762// A block in a method. Contains the list of instructions represented
763// as a double linked list. Each block knows its predecessors and
764// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100765
Vladimir Markof9f64412015-09-02 14:05:49 +0100766class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000767 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600768 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000769 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000770 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
771 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000772 loop_information_(nullptr),
773 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000774 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100775 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100776 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100777 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000778 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000779 try_catch_information_(nullptr) {
780 predecessors_.reserve(kDefaultNumberOfPredecessors);
781 successors_.reserve(kDefaultNumberOfSuccessors);
782 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
783 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000784
Vladimir Marko60584552015-09-03 13:35:12 +0000785 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100786 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000787 }
788
Vladimir Marko60584552015-09-03 13:35:12 +0000789 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100790 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000791 }
792
David Brazdild26a4112015-11-10 11:07:31 +0000793 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
794 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
795
Vladimir Marko60584552015-09-03 13:35:12 +0000796 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
797 return ContainsElement(successors_, block, start_from);
798 }
799
800 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100801 return dominated_blocks_;
802 }
803
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100804 bool IsEntryBlock() const {
805 return graph_->GetEntryBlock() == this;
806 }
807
808 bool IsExitBlock() const {
809 return graph_->GetExitBlock() == this;
810 }
811
David Brazdil46e2a392015-03-16 17:31:52 +0000812 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000813 bool IsSingleTryBoundary() const;
814
815 // Returns true if this block emits nothing but a jump.
816 bool IsSingleJump() const {
817 HLoopInformation* loop_info = GetLoopInformation();
818 return (IsSingleGoto() || IsSingleTryBoundary())
819 // Back edges generate a suspend check.
820 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
821 }
David Brazdil46e2a392015-03-16 17:31:52 +0000822
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000823 void AddBackEdge(HBasicBlock* back_edge) {
824 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000825 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000826 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100827 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000828 loop_information_->AddBackEdge(back_edge);
829 }
830
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000831 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000832 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000833
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100834 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000835 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600836 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000837
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000838 HBasicBlock* GetDominator() const { return dominator_; }
839 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000840 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
841
842 void RemoveDominatedBlock(HBasicBlock* block) {
843 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100844 }
Vladimir Marko60584552015-09-03 13:35:12 +0000845
846 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
847 ReplaceElement(dominated_blocks_, existing, new_block);
848 }
849
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100850 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000851
852 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100853 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000854 }
855
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100856 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
857 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100858 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100859 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100860 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
861 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000862
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000863 HInstruction* GetFirstInstructionDisregardMoves() const;
864
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000865 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000866 successors_.push_back(block);
867 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000868 }
869
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100870 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
871 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100872 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000873 new_block->predecessors_.push_back(this);
874 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000875 }
876
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000877 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
878 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000879 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000880 new_block->successors_.push_back(this);
881 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000882 }
883
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100884 // Insert `this` between `predecessor` and `successor. This method
885 // preserves the indicies, and will update the first edge found between
886 // `predecessor` and `successor`.
887 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
888 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100889 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000890 successor->predecessors_[predecessor_index] = this;
891 predecessor->successors_[successor_index] = this;
892 successors_.push_back(successor);
893 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100894 }
895
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100896 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000897 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100898 }
899
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000900 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000901 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000902 }
903
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100904 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000905 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100906 }
907
908 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000909 predecessors_.push_back(block);
910 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100911 }
912
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100913 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000914 DCHECK_EQ(predecessors_.size(), 2u);
915 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100916 }
917
David Brazdil769c9e52015-04-27 13:54:09 +0100918 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000919 DCHECK_EQ(successors_.size(), 2u);
920 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100921 }
922
David Brazdilfc6a86a2015-06-26 10:33:45 +0000923 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000924 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100925 }
926
David Brazdilfc6a86a2015-06-26 10:33:45 +0000927 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000928 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100929 }
930
David Brazdilfc6a86a2015-06-26 10:33:45 +0000931 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000932 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100933 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000934 }
935
936 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000937 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100938 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000939 }
940
941 // Returns whether the first occurrence of `predecessor` in the list of
942 // predecessors is at index `idx`.
943 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100944 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000945 return GetPredecessorIndexOf(predecessor) == idx;
946 }
947
David Brazdild7558da2015-09-22 13:04:14 +0100948 // Create a new block between this block and its predecessors. The new block
949 // is added to the graph, all predecessor edges are relinked to it and an edge
950 // is created to `this`. Returns the new empty block. Reverse post order or
951 // loop and try/catch information are not updated.
952 HBasicBlock* CreateImmediateDominator();
953
David Brazdilfc6a86a2015-06-26 10:33:45 +0000954 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100955 // created, latter block. Note that this method will add the block to the
956 // graph, create a Goto at the end of the former block and will create an edge
957 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100958 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000959 HBasicBlock* SplitBefore(HInstruction* cursor);
960
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000961 // Split the block into two blocks just after `cursor`. Returns the newly
962 // created block. Note that this method just updates raw block information,
963 // like predecessors, successors, dominators, and instruction list. It does not
964 // update the graph, reverse post order, loop information, nor make sure the
965 // blocks are consistent (for example ending with a control flow instruction).
966 HBasicBlock* SplitAfter(HInstruction* cursor);
967
David Brazdil9bc43612015-11-05 21:25:24 +0000968 // Split catch block into two blocks after the original move-exception bytecode
969 // instruction, or at the beginning if not present. Returns the newly created,
970 // latter block, or nullptr if such block could not be created (must be dead
971 // in that case). Note that this method just updates raw block information,
972 // like predecessors, successors, dominators, and instruction list. It does not
973 // update the graph, reverse post order, loop information, nor make sure the
974 // blocks are consistent (for example ending with a control flow instruction).
975 HBasicBlock* SplitCatchBlockAfterMoveException();
976
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000977 // Merge `other` at the end of `this`. Successors and dominated blocks of
978 // `other` are changed to be successors and dominated blocks of `this`. Note
979 // that this method does not update the graph, reverse post order, loop
980 // information, nor make sure the blocks are consistent (for example ending
981 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100982 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000983
984 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
985 // of `this` are moved to `other`.
986 // Note that this method does not update the graph, reverse post order, loop
987 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000988 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000989 void ReplaceWith(HBasicBlock* other);
990
David Brazdil2d7352b2015-04-20 14:52:42 +0100991 // Merge `other` at the end of `this`. This method updates loops, reverse post
992 // order, links to predecessors, successors, dominators and deletes the block
993 // from the graph. The two blocks must be successive, i.e. `this` the only
994 // predecessor of `other` and vice versa.
995 void MergeWith(HBasicBlock* other);
996
997 // Disconnects `this` from all its predecessors, successors and dominator,
998 // removes it from all loops it is included in and eventually from the graph.
999 // The block must not dominate any other block. Predecessors and successors
1000 // are safely updated.
1001 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001002
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001003 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001004 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001005 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001006 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001007 // Replace instruction `initial` with `replacement` within this block.
1008 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1009 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001010 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001011 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001012 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1013 // instruction list. With 'ensure_safety' set to true, it verifies that the
1014 // instruction is not in use and removes it from the use lists of its inputs.
1015 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1016 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001017 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001018
1019 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001020 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001021 }
1022
Roland Levillain6b879dd2014-09-22 17:13:44 +01001023 bool IsLoopPreHeaderFirstPredecessor() const {
1024 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001025 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001026 }
1027
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001028 bool IsFirstPredecessorBackEdge() const {
1029 DCHECK(IsLoopHeader());
1030 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1031 }
1032
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001033 HLoopInformation* GetLoopInformation() const {
1034 return loop_information_;
1035 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001036
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001037 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001038 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001039 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001040 void SetInLoop(HLoopInformation* info) {
1041 if (IsLoopHeader()) {
1042 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001043 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001044 loop_information_ = info;
1045 } else if (loop_information_->Contains(*info->GetHeader())) {
1046 // Block is currently part of an outer loop. Make it part of this inner loop.
1047 // Note that a non loop header having a loop information means this loop information
1048 // has already been populated
1049 loop_information_ = info;
1050 } else {
1051 // Block is part of an inner loop. Do not update the loop information.
1052 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1053 // at this point, because this method is being called while populating `info`.
1054 }
1055 }
1056
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001057 // Raw update of the loop information.
1058 void SetLoopInformation(HLoopInformation* info) {
1059 loop_information_ = info;
1060 }
1061
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001062 bool IsInLoop() const { return loop_information_ != nullptr; }
1063
David Brazdilec16f792015-08-19 15:04:01 +01001064 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1065
1066 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1067 try_catch_information_ = try_catch_information;
1068 }
1069
1070 bool IsTryBlock() const {
1071 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1072 }
1073
1074 bool IsCatchBlock() const {
1075 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1076 }
David Brazdilffee3d32015-07-06 11:48:53 +01001077
1078 // Returns the try entry that this block's successors should have. They will
1079 // be in the same try, unless the block ends in a try boundary. In that case,
1080 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001081 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001082
David Brazdild7558da2015-09-22 13:04:14 +01001083 bool HasThrowingInstructions() const;
1084
David Brazdila4b8c212015-05-07 09:59:30 +01001085 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001086 bool Dominates(HBasicBlock* block) const;
1087
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001088 size_t GetLifetimeStart() const { return lifetime_start_; }
1089 size_t GetLifetimeEnd() const { return lifetime_end_; }
1090
1091 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1092 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1093
David Brazdil8d5b8b22015-03-24 10:51:52 +00001094 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001095 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001096 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001097 bool HasSinglePhi() const;
1098
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001099 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001100 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001101 ArenaVector<HBasicBlock*> predecessors_;
1102 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001103 HInstructionList instructions_;
1104 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001105 HLoopInformation* loop_information_;
1106 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001107 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001108 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001109 // The dex program counter of the first instruction of this block.
1110 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001111 size_t lifetime_start_;
1112 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001113 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001114
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001115 friend class HGraph;
1116 friend class HInstruction;
1117
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001118 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1119};
1120
David Brazdilb2bd1c52015-03-25 11:17:37 +00001121// Iterates over the LoopInformation of all loops which contain 'block'
1122// from the innermost to the outermost.
1123class HLoopInformationOutwardIterator : public ValueObject {
1124 public:
1125 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1126 : current_(block.GetLoopInformation()) {}
1127
1128 bool Done() const { return current_ == nullptr; }
1129
1130 void Advance() {
1131 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001132 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001133 }
1134
1135 HLoopInformation* Current() const {
1136 DCHECK(!Done());
1137 return current_;
1138 }
1139
1140 private:
1141 HLoopInformation* current_;
1142
1143 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1144};
1145
Alexandre Ramesef20f712015-06-09 10:29:30 +01001146#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001147 M(Above, Condition) \
1148 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001149 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001150 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001151 M(ArrayGet, Instruction) \
1152 M(ArrayLength, Instruction) \
1153 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001154 M(Below, Condition) \
1155 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001156 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001157 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001158 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001159 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001160 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001161 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001162 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001163 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001164 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001165 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001166 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001167 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001168 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001169 M(Exit, Instruction) \
1170 M(FloatConstant, Constant) \
1171 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001172 M(GreaterThan, Condition) \
1173 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001174 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001175 M(InstanceFieldGet, Instruction) \
1176 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001177 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001178 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001179 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001180 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001181 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001182 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001183 M(LessThan, Condition) \
1184 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001185 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001186 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001187 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001188 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001189 M(Local, Instruction) \
1190 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001191 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001192 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001193 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001194 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001195 M(Neg, UnaryOperation) \
1196 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001197 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001198 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001199 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001200 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001201 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001202 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001203 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001204 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001205 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001206 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001207 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001208 M(Return, Instruction) \
1209 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001210 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001211 M(Shl, BinaryOperation) \
1212 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001213 M(StaticFieldGet, Instruction) \
1214 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001215 M(UnresolvedInstanceFieldGet, Instruction) \
1216 M(UnresolvedInstanceFieldSet, Instruction) \
1217 M(UnresolvedStaticFieldGet, Instruction) \
1218 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001219 M(StoreLocal, Instruction) \
1220 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001221 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001222 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001223 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001224 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001225 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001226 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001227 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001228
Ilmir Usmanovdebeb982015-12-11 11:39:44 +03001229/*
1230 * Instructions, shared across several (not all) architectures.
1231 */
1232#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1233#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1234#else
1235#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
1236 M(MultiplyAccumulate, Instruction)
1237#endif
1238
Vladimir Markob4536b72015-11-24 13:45:23 +00001239#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001240#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001241#else
1242#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1243 M(ArmDexCacheArraysBase, Instruction)
1244#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001245
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001246#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001247#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001248#else
1249#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001250 M(Arm64DataProcWithShifterOp, Instruction) \
Ilmir Usmanovdebeb982015-12-11 11:39:44 +03001251 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001252#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001253
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001254#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1255
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001256#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1257
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001258#ifndef ART_ENABLE_CODEGEN_x86
1259#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1260#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001261#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1262 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001263 M(X86LoadFromConstantTable, Instruction) \
1264 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001265#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001266
1267#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1268
1269#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1270 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Ilmir Usmanovdebeb982015-12-11 11:39:44 +03001271 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001272 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1273 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001274 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001275 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001276 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1277 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1278
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001279#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1280 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001281 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001282 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001283 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001284 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001285
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001286#define FOR_EACH_INSTRUCTION(M) \
1287 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1288 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1289
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001290#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001291FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1292#undef FORWARD_DECLARATION
1293
Roland Levillainccc07a92014-09-16 14:48:16 +01001294#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001295 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001296 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001297 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001298 return other->Is##type(); \
1299 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001300 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001301
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001302#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1303 bool Is##type() const { return As##type() != nullptr; } \
1304 const H##type* As##type() const { return this; } \
1305 H##type* As##type() { return this; }
1306
David Brazdiled596192015-01-23 10:39:45 +00001307template <typename T> class HUseList;
1308
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001309template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001310class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001311 public:
David Brazdiled596192015-01-23 10:39:45 +00001312 HUseListNode* GetPrevious() const { return prev_; }
1313 HUseListNode* GetNext() const { return next_; }
1314 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001315 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001316 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001317
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001318 private:
David Brazdiled596192015-01-23 10:39:45 +00001319 HUseListNode(T user, size_t index)
1320 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1321
1322 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001323 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001324 HUseListNode<T>* prev_;
1325 HUseListNode<T>* next_;
1326
1327 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001328
1329 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1330};
1331
David Brazdiled596192015-01-23 10:39:45 +00001332template <typename T>
1333class HUseList : public ValueObject {
1334 public:
1335 HUseList() : first_(nullptr) {}
1336
1337 void Clear() {
1338 first_ = nullptr;
1339 }
1340
1341 // Adds a new entry at the beginning of the use list and returns
1342 // the newly created node.
1343 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001344 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001345 if (IsEmpty()) {
1346 first_ = new_node;
1347 } else {
1348 first_->prev_ = new_node;
1349 new_node->next_ = first_;
1350 first_ = new_node;
1351 }
1352 return new_node;
1353 }
1354
1355 HUseListNode<T>* GetFirst() const {
1356 return first_;
1357 }
1358
1359 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001360 DCHECK(node != nullptr);
1361 DCHECK(Contains(node));
1362
David Brazdiled596192015-01-23 10:39:45 +00001363 if (node->prev_ != nullptr) {
1364 node->prev_->next_ = node->next_;
1365 }
1366 if (node->next_ != nullptr) {
1367 node->next_->prev_ = node->prev_;
1368 }
1369 if (node == first_) {
1370 first_ = node->next_;
1371 }
1372 }
1373
David Brazdil1abb4192015-02-17 18:33:36 +00001374 bool Contains(const HUseListNode<T>* node) const {
1375 if (node == nullptr) {
1376 return false;
1377 }
1378 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1379 if (current == node) {
1380 return true;
1381 }
1382 }
1383 return false;
1384 }
1385
David Brazdiled596192015-01-23 10:39:45 +00001386 bool IsEmpty() const {
1387 return first_ == nullptr;
1388 }
1389
1390 bool HasOnlyOneUse() const {
1391 return first_ != nullptr && first_->next_ == nullptr;
1392 }
1393
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001394 size_t SizeSlow() const {
1395 size_t count = 0;
1396 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1397 ++count;
1398 }
1399 return count;
1400 }
1401
David Brazdiled596192015-01-23 10:39:45 +00001402 private:
1403 HUseListNode<T>* first_;
1404};
1405
1406template<typename T>
1407class HUseIterator : public ValueObject {
1408 public:
1409 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1410
1411 bool Done() const { return current_ == nullptr; }
1412
1413 void Advance() {
1414 DCHECK(!Done());
1415 current_ = current_->GetNext();
1416 }
1417
1418 HUseListNode<T>* Current() const {
1419 DCHECK(!Done());
1420 return current_;
1421 }
1422
1423 private:
1424 HUseListNode<T>* current_;
1425
1426 friend class HValue;
1427};
1428
David Brazdil1abb4192015-02-17 18:33:36 +00001429// This class is used by HEnvironment and HInstruction classes to record the
1430// instructions they use and pointers to the corresponding HUseListNodes kept
1431// by the used instructions.
1432template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001433class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001434 public:
1435 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1436 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1437
1438 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1439 : instruction_(old_record.instruction_), use_node_(use_node) {
1440 DCHECK(instruction_ != nullptr);
1441 DCHECK(use_node_ != nullptr);
1442 DCHECK(old_record.use_node_ == nullptr);
1443 }
1444
1445 HInstruction* GetInstruction() const { return instruction_; }
1446 HUseListNode<T>* GetUseNode() const { return use_node_; }
1447
1448 private:
1449 // Instruction used by the user.
1450 HInstruction* instruction_;
1451
1452 // Corresponding entry in the use list kept by 'instruction_'.
1453 HUseListNode<T>* use_node_;
1454};
1455
Aart Bik854a02b2015-07-14 16:07:00 -07001456/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001457 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001458 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001459 * For write/read dependences on fields/arrays, the dependence analysis uses
1460 * type disambiguation (e.g. a float field write cannot modify the value of an
1461 * integer field read) and the access type (e.g. a reference array write cannot
1462 * modify the value of a reference field read [although it may modify the
1463 * reference fetch prior to reading the field, which is represented by its own
1464 * write/read dependence]). The analysis makes conservative points-to
1465 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1466 * the same, and any reference read depends on any reference read without
1467 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001468 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001469 * The internal representation uses 38-bit and is described in the table below.
1470 * The first line indicates the side effect, and for field/array accesses the
1471 * second line indicates the type of the access (in the order of the
1472 * Primitive::Type enum).
1473 * The two numbered lines below indicate the bit position in the bitfield (read
1474 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001475 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001476 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1477 * +-------------+---------+---------+--------------+---------+---------+
1478 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1479 * | 3 |333333322|222222221| 1 |111111110|000000000|
1480 * | 7 |654321098|765432109| 8 |765432109|876543210|
1481 *
1482 * Note that, to ease the implementation, 'changes' bits are least significant
1483 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001484 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001485class SideEffects : public ValueObject {
1486 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001487 SideEffects() : flags_(0) {}
1488
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001489 static SideEffects None() {
1490 return SideEffects(0);
1491 }
1492
1493 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001494 return SideEffects(kAllChangeBits | kAllDependOnBits);
1495 }
1496
1497 static SideEffects AllChanges() {
1498 return SideEffects(kAllChangeBits);
1499 }
1500
1501 static SideEffects AllDependencies() {
1502 return SideEffects(kAllDependOnBits);
1503 }
1504
1505 static SideEffects AllExceptGCDependency() {
1506 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1507 }
1508
1509 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001510 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001511 }
1512
Aart Bik34c3ba92015-07-20 14:08:59 -07001513 static SideEffects AllWrites() {
1514 return SideEffects(kAllWrites);
1515 }
1516
1517 static SideEffects AllReads() {
1518 return SideEffects(kAllReads);
1519 }
1520
1521 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1522 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001523 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001524 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001525 }
1526
Aart Bik854a02b2015-07-14 16:07:00 -07001527 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1528 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001529 }
1530
Aart Bik34c3ba92015-07-20 14:08:59 -07001531 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1532 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001533 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001534 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001535 }
1536
1537 static SideEffects ArrayReadOfType(Primitive::Type type) {
1538 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1539 }
1540
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001541 static SideEffects CanTriggerGC() {
1542 return SideEffects(1ULL << kCanTriggerGCBit);
1543 }
1544
1545 static SideEffects DependsOnGC() {
1546 return SideEffects(1ULL << kDependsOnGCBit);
1547 }
1548
Aart Bik854a02b2015-07-14 16:07:00 -07001549 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001550 SideEffects Union(SideEffects other) const {
1551 return SideEffects(flags_ | other.flags_);
1552 }
1553
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001554 SideEffects Exclusion(SideEffects other) const {
1555 return SideEffects(flags_ & ~other.flags_);
1556 }
1557
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001558 void Add(SideEffects other) {
1559 flags_ |= other.flags_;
1560 }
1561
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001562 bool Includes(SideEffects other) const {
1563 return (other.flags_ & flags_) == other.flags_;
1564 }
1565
1566 bool HasSideEffects() const {
1567 return (flags_ & kAllChangeBits);
1568 }
1569
1570 bool HasDependencies() const {
1571 return (flags_ & kAllDependOnBits);
1572 }
1573
1574 // Returns true if there are no side effects or dependencies.
1575 bool DoesNothing() const {
1576 return flags_ == 0;
1577 }
1578
Aart Bik854a02b2015-07-14 16:07:00 -07001579 // Returns true if something is written.
1580 bool DoesAnyWrite() const {
1581 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001582 }
1583
Aart Bik854a02b2015-07-14 16:07:00 -07001584 // Returns true if something is read.
1585 bool DoesAnyRead() const {
1586 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001587 }
1588
Aart Bik854a02b2015-07-14 16:07:00 -07001589 // Returns true if potentially everything is written and read
1590 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001591 bool DoesAllReadWrite() const {
1592 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1593 }
1594
Aart Bik854a02b2015-07-14 16:07:00 -07001595 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001596 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001597 }
1598
Roland Levillain0d5a2812015-11-13 10:07:31 +00001599 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001600 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001601 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1602 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001603 }
1604
1605 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001606 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001607 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001608 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001609 for (int s = kLastBit; s >= 0; s--) {
1610 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1611 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1612 // This is a bit for the GC side effect.
1613 if (current_bit_is_set) {
1614 flags += "GC";
1615 }
Aart Bik854a02b2015-07-14 16:07:00 -07001616 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001617 } else {
1618 // This is a bit for the array/field analysis.
1619 // The underscore character stands for the 'can trigger GC' bit.
1620 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1621 if (current_bit_is_set) {
1622 flags += kDebug[s];
1623 }
1624 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1625 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1626 flags += "|";
1627 }
1628 }
Aart Bik854a02b2015-07-14 16:07:00 -07001629 }
1630 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001631 }
1632
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001633 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001634
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001635 private:
1636 static constexpr int kFieldArrayAnalysisBits = 9;
1637
1638 static constexpr int kFieldWriteOffset = 0;
1639 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1640 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1641 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1642
1643 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1644
1645 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1646 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1647 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1648 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1649
1650 static constexpr int kLastBit = kDependsOnGCBit;
1651 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1652
1653 // Aliases.
1654
1655 static_assert(kChangeBits == kDependOnBits,
1656 "the 'change' bits should match the 'depend on' bits.");
1657
1658 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1659 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1660 static constexpr uint64_t kAllWrites =
1661 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1662 static constexpr uint64_t kAllReads =
1663 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001664
Aart Bik854a02b2015-07-14 16:07:00 -07001665 // Work around the fact that HIR aliases I/F and J/D.
1666 // TODO: remove this interceptor once HIR types are clean
1667 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1668 switch (type) {
1669 case Primitive::kPrimInt:
1670 case Primitive::kPrimFloat:
1671 return TypeFlag(Primitive::kPrimInt, offset) |
1672 TypeFlag(Primitive::kPrimFloat, offset);
1673 case Primitive::kPrimLong:
1674 case Primitive::kPrimDouble:
1675 return TypeFlag(Primitive::kPrimLong, offset) |
1676 TypeFlag(Primitive::kPrimDouble, offset);
1677 default:
1678 return TypeFlag(type, offset);
1679 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001680 }
1681
Aart Bik854a02b2015-07-14 16:07:00 -07001682 // Translates type to bit flag.
1683 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1684 CHECK_NE(type, Primitive::kPrimVoid);
1685 const uint64_t one = 1;
1686 const int shift = type; // 0-based consecutive enum
1687 DCHECK_LE(kFieldWriteOffset, shift);
1688 DCHECK_LT(shift, kArrayWriteOffset);
1689 return one << (type + offset);
1690 }
1691
1692 // Private constructor on direct flags value.
1693 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1694
1695 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001696};
1697
David Brazdiled596192015-01-23 10:39:45 +00001698// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001699class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001700 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001701 HEnvironment(ArenaAllocator* arena,
1702 size_t number_of_vregs,
1703 const DexFile& dex_file,
1704 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001705 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001706 InvokeType invoke_type,
1707 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001708 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1709 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001710 parent_(nullptr),
1711 dex_file_(dex_file),
1712 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001713 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001714 invoke_type_(invoke_type),
1715 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001716 }
1717
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001718 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001719 : HEnvironment(arena,
1720 to_copy.Size(),
1721 to_copy.GetDexFile(),
1722 to_copy.GetMethodIdx(),
1723 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001724 to_copy.GetInvokeType(),
1725 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001726
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001727 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001728 if (parent_ != nullptr) {
1729 parent_->SetAndCopyParentChain(allocator, parent);
1730 } else {
1731 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1732 parent_->CopyFrom(parent);
1733 if (parent->GetParent() != nullptr) {
1734 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1735 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001736 }
David Brazdiled596192015-01-23 10:39:45 +00001737 }
1738
Vladimir Marko71bf8092015-09-15 15:33:14 +01001739 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001740 void CopyFrom(HEnvironment* environment);
1741
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001742 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1743 // input to the loop phi instead. This is for inserting instructions that
1744 // require an environment (like HDeoptimization) in the loop pre-header.
1745 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001746
1747 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001748 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001749 }
1750
1751 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001752 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001753 }
1754
David Brazdil1abb4192015-02-17 18:33:36 +00001755 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001756
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001757 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001758
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001759 HEnvironment* GetParent() const { return parent_; }
1760
1761 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001762 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001763 }
1764
1765 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001766 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001767 }
1768
1769 uint32_t GetDexPc() const {
1770 return dex_pc_;
1771 }
1772
1773 uint32_t GetMethodIdx() const {
1774 return method_idx_;
1775 }
1776
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001777 InvokeType GetInvokeType() const {
1778 return invoke_type_;
1779 }
1780
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001781 const DexFile& GetDexFile() const {
1782 return dex_file_;
1783 }
1784
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001785 HInstruction* GetHolder() const {
1786 return holder_;
1787 }
1788
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001789
1790 bool IsFromInlinedInvoke() const {
1791 return GetParent() != nullptr;
1792 }
1793
David Brazdiled596192015-01-23 10:39:45 +00001794 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001795 // Record instructions' use entries of this environment for constant-time removal.
1796 // It should only be called by HInstruction when a new environment use is added.
1797 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1798 DCHECK(env_use->GetUser() == this);
1799 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001800 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001801 }
David Brazdiled596192015-01-23 10:39:45 +00001802
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001803 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1804 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001805 HEnvironment* parent_;
1806 const DexFile& dex_file_;
1807 const uint32_t method_idx_;
1808 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001809 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001810
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001811 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001812 HInstruction* const holder_;
1813
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001814 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001815
1816 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1817};
1818
Vladimir Markof9f64412015-09-02 14:05:49 +01001819class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001820 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001821 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001822 : previous_(nullptr),
1823 next_(nullptr),
1824 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001825 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001826 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001827 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001828 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001829 locations_(nullptr),
1830 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001831 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001832 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001833 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001834
Dave Allison20dfc792014-06-16 20:44:29 -07001835 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001836
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001837#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001838 enum InstructionKind {
1839 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1840 };
1841#undef DECLARE_KIND
1842
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001843 HInstruction* GetNext() const { return next_; }
1844 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001845
Calin Juravle77520bc2015-01-12 18:45:46 +00001846 HInstruction* GetNextDisregardingMoves() const;
1847 HInstruction* GetPreviousDisregardingMoves() const;
1848
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001849 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001850 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001851 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001852 bool IsInBlock() const { return block_ != nullptr; }
1853 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001854 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1855 bool IsIrreducibleLoopHeaderPhi() const {
1856 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1857 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001858
Roland Levillain6b879dd2014-09-22 17:13:44 +01001859 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001860 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001861
1862 virtual void Accept(HGraphVisitor* visitor) = 0;
1863 virtual const char* DebugName() const = 0;
1864
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001865 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001866 void SetRawInputAt(size_t index, HInstruction* input) {
1867 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1868 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001869
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001870 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001871
1872 uint32_t GetDexPc() const { return dex_pc_; }
1873
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001874 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001875
Roland Levillaine161a2a2014-10-03 12:45:18 +01001876 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001877 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001878
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001879 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001880 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001881
Calin Juravle10e244f2015-01-26 18:54:32 +00001882 // Does not apply for all instructions, but having this at top level greatly
1883 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001884 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001885 virtual bool CanBeNull() const {
1886 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1887 return true;
1888 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001889
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001890 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001891 return false;
1892 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001893
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001894 virtual bool IsActualObject() const {
1895 return GetType() == Primitive::kPrimNot;
1896 }
1897
Calin Juravle2e768302015-07-28 14:41:11 +00001898 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001899
Calin Juravle61d544b2015-02-23 16:46:57 +00001900 ReferenceTypeInfo GetReferenceTypeInfo() const {
1901 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1902 return reference_type_info_;
1903 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001904
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001905 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001906 DCHECK(user != nullptr);
1907 HUseListNode<HInstruction*>* use =
1908 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1909 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001910 }
1911
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001912 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001913 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001914 HUseListNode<HEnvironment*>* env_use =
1915 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1916 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001917 }
1918
David Brazdil1abb4192015-02-17 18:33:36 +00001919 void RemoveAsUserOfInput(size_t input) {
1920 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1921 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1922 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001923
David Brazdil1abb4192015-02-17 18:33:36 +00001924 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1925 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001926
David Brazdiled596192015-01-23 10:39:45 +00001927 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1928 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001929 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001930 bool HasOnlyOneNonEnvironmentUse() const {
1931 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1932 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001933
Roland Levillain6c82d402014-10-13 16:10:27 +01001934 // Does this instruction strictly dominate `other_instruction`?
1935 // Returns false if this instruction and `other_instruction` are the same.
1936 // Aborts if this instruction and `other_instruction` are both phis.
1937 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001938
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001939 int GetId() const { return id_; }
1940 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001941
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001942 int GetSsaIndex() const { return ssa_index_; }
1943 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1944 bool HasSsaIndex() const { return ssa_index_ != -1; }
1945
1946 bool HasEnvironment() const { return environment_ != nullptr; }
1947 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001948 // Set the `environment_` field. Raw because this method does not
1949 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001950 void SetRawEnvironment(HEnvironment* environment) {
1951 DCHECK(environment_ == nullptr);
1952 DCHECK_EQ(environment->GetHolder(), this);
1953 environment_ = environment;
1954 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001955
1956 // Set the environment of this instruction, copying it from `environment`. While
1957 // copying, the uses lists are being updated.
1958 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001959 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001960 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001961 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001962 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001963 if (environment->GetParent() != nullptr) {
1964 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1965 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001966 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001967
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001968 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1969 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001970 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001971 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001972 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001973 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001974 if (environment->GetParent() != nullptr) {
1975 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1976 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001977 }
1978
Nicolas Geoffray39468442014-09-02 15:17:15 +01001979 // Returns the number of entries in the environment. Typically, that is the
1980 // number of dex registers in a method. It could be more in case of inlining.
1981 size_t EnvironmentSize() const;
1982
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001983 LocationSummary* GetLocations() const { return locations_; }
1984 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001985
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001986 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001987 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001988
Alexandre Rames188d4312015-04-09 18:30:21 +01001989 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1990 // uses of this instruction by `other` are *not* updated.
1991 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1992 ReplaceWith(other);
1993 other->ReplaceInput(this, use_index);
1994 }
1995
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001996 // Move `this` instruction before `cursor`.
1997 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001998
Vladimir Markofb337ea2015-11-25 15:25:10 +00001999 // Move `this` before its first user and out of any loops. If there is no
2000 // out-of-loop user that dominates all other users, move the instruction
2001 // to the end of the out-of-loop common dominator of the user's blocks.
2002 //
2003 // This can be used only on non-throwing instructions with no side effects that
2004 // have at least one use but no environment uses.
2005 void MoveBeforeFirstUserAndOutOfLoops();
2006
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002007#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002008 bool Is##type() const; \
2009 const H##type* As##type() const; \
2010 H##type* As##type();
2011
2012 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2013#undef INSTRUCTION_TYPE_CHECK
2014
2015#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002016 bool Is##type() const { return (As##type() != nullptr); } \
2017 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002018 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002019 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002020#undef INSTRUCTION_TYPE_CHECK
2021
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002022 // Returns whether the instruction can be moved within the graph.
2023 virtual bool CanBeMoved() const { return false; }
2024
2025 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002026 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002027 return false;
2028 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002029
2030 // Returns whether any data encoded in the two instructions is equal.
2031 // This method does not look at the inputs. Both instructions must be
2032 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002033 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002034 return false;
2035 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002036
2037 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002038 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002039 // 2) Their inputs are identical.
2040 bool Equals(HInstruction* other) const;
2041
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002042 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2043 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2044 // the virtual function because the __attribute__((__pure__)) doesn't really
2045 // apply the strong requirement for virtual functions, preventing optimizations.
2046 InstructionKind GetKind() const PURE;
2047 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002048
2049 virtual size_t ComputeHashCode() const {
2050 size_t result = GetKind();
2051 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2052 result = (result * 31) + InputAt(i)->GetId();
2053 }
2054 return result;
2055 }
2056
2057 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002058 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002059
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002060 size_t GetLifetimePosition() const { return lifetime_position_; }
2061 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2062 LiveInterval* GetLiveInterval() const { return live_interval_; }
2063 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2064 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2065
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002066 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2067
2068 // Returns whether the code generation of the instruction will require to have access
2069 // to the current method. Such instructions are:
2070 // (1): Instructions that require an environment, as calling the runtime requires
2071 // to walk the stack and have the current method stored at a specific stack address.
2072 // (2): Object literals like classes and strings, that are loaded from the dex cache
2073 // fields of the current method.
2074 bool NeedsCurrentMethod() const {
2075 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2076 }
2077
Vladimir Markodc151b22015-10-15 18:02:30 +01002078 // Returns whether the code generation of the instruction will require to have access
2079 // to the dex cache of the current method's declaring class via the current method.
2080 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002081
Mark Mendellc4701932015-04-10 13:18:51 -04002082 // Does this instruction have any use in an environment before
2083 // control flow hits 'other'?
2084 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2085
2086 // Remove all references to environment uses of this instruction.
2087 // The caller must ensure that this is safe to do.
2088 void RemoveEnvironmentUsers();
2089
David Brazdil1abb4192015-02-17 18:33:36 +00002090 protected:
2091 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2092 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
Aart Bik5d75afe2015-12-14 11:57:01 -08002093 void SetSideEffects(SideEffects other) { side_effects_ = other; }
David Brazdil1abb4192015-02-17 18:33:36 +00002094
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002095 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002096 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2097
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002098 HInstruction* previous_;
2099 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002100 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002101 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002102
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002103 // An instruction gets an id when it is added to the graph.
2104 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002105 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002106 int id_;
2107
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002108 // When doing liveness analysis, instructions that have uses get an SSA index.
2109 int ssa_index_;
2110
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002111 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002112 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002113
2114 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002115 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002116
Nicolas Geoffray39468442014-09-02 15:17:15 +01002117 // The environment associated with this instruction. Not null if the instruction
2118 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002119 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002120
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002121 // Set by the code generator.
2122 LocationSummary* locations_;
2123
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002124 // Set by the liveness analysis.
2125 LiveInterval* live_interval_;
2126
2127 // Set by the liveness analysis, this is the position in a linear
2128 // order of blocks where this instruction's live interval start.
2129 size_t lifetime_position_;
2130
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002131 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002132
Calin Juravleacf735c2015-02-12 15:25:22 +00002133 // TODO: for primitive types this should be marked as invalid.
2134 ReferenceTypeInfo reference_type_info_;
2135
David Brazdil1abb4192015-02-17 18:33:36 +00002136 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002137 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002138 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002139 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002140 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002141
2142 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2143};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002144std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002145
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002146class HInputIterator : public ValueObject {
2147 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002148 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002149
2150 bool Done() const { return index_ == instruction_->InputCount(); }
2151 HInstruction* Current() const { return instruction_->InputAt(index_); }
2152 void Advance() { index_++; }
2153
2154 private:
2155 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002156 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002157
2158 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2159};
2160
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002161class HInstructionIterator : public ValueObject {
2162 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002163 explicit HInstructionIterator(const HInstructionList& instructions)
2164 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002165 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002166 }
2167
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002168 bool Done() const { return instruction_ == nullptr; }
2169 HInstruction* Current() const { return instruction_; }
2170 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002171 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002172 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002173 }
2174
2175 private:
2176 HInstruction* instruction_;
2177 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002178
2179 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002180};
2181
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002182class HBackwardInstructionIterator : public ValueObject {
2183 public:
2184 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2185 : instruction_(instructions.last_instruction_) {
2186 next_ = Done() ? nullptr : instruction_->GetPrevious();
2187 }
2188
2189 bool Done() const { return instruction_ == nullptr; }
2190 HInstruction* Current() const { return instruction_; }
2191 void Advance() {
2192 instruction_ = next_;
2193 next_ = Done() ? nullptr : instruction_->GetPrevious();
2194 }
2195
2196 private:
2197 HInstruction* instruction_;
2198 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002199
2200 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002201};
2202
Vladimir Markof9f64412015-09-02 14:05:49 +01002203template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002204class HTemplateInstruction: public HInstruction {
2205 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002206 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002207 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002208 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002209
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002210 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002211
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002212 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002213 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2214 DCHECK_LT(i, N);
2215 return inputs_[i];
2216 }
David Brazdil1abb4192015-02-17 18:33:36 +00002217
2218 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002219 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002220 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002221 }
2222
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002223 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002224 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002225
2226 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002227};
2228
Vladimir Markof9f64412015-09-02 14:05:49 +01002229// HTemplateInstruction specialization for N=0.
2230template<>
2231class HTemplateInstruction<0>: public HInstruction {
2232 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002233 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002234 : HInstruction(side_effects, dex_pc) {}
2235
Vladimir Markof9f64412015-09-02 14:05:49 +01002236 virtual ~HTemplateInstruction() {}
2237
2238 size_t InputCount() const OVERRIDE { return 0; }
2239
2240 protected:
2241 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2242 LOG(FATAL) << "Unreachable";
2243 UNREACHABLE();
2244 }
2245
2246 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2247 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2248 LOG(FATAL) << "Unreachable";
2249 UNREACHABLE();
2250 }
2251
2252 private:
2253 friend class SsaBuilder;
2254};
2255
Dave Allison20dfc792014-06-16 20:44:29 -07002256template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002257class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002258 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002259 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002260 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002261 virtual ~HExpression() {}
2262
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002263 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002264
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002265 protected:
2266 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002267};
2268
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002269// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2270// instruction that branches to the exit block.
2271class HReturnVoid : public HTemplateInstruction<0> {
2272 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002273 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2274 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002275
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002276 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002277
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002278 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002279
2280 private:
2281 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2282};
2283
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002284// Represents dex's RETURN opcodes. A HReturn is a control flow
2285// instruction that branches to the exit block.
2286class HReturn : public HTemplateInstruction<1> {
2287 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002288 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2289 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002290 SetRawInputAt(0, value);
2291 }
2292
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002293 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002294
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002295 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002296
2297 private:
2298 DISALLOW_COPY_AND_ASSIGN(HReturn);
2299};
2300
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002301// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002302// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002303// exit block.
2304class HExit : public HTemplateInstruction<0> {
2305 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002306 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002307
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002308 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002309
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002310 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002311
2312 private:
2313 DISALLOW_COPY_AND_ASSIGN(HExit);
2314};
2315
2316// Jumps from one block to another.
2317class HGoto : public HTemplateInstruction<0> {
2318 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002319 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002320
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002321 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002322
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002323 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002324 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002325 }
2326
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002327 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002328
2329 private:
2330 DISALLOW_COPY_AND_ASSIGN(HGoto);
2331};
2332
Roland Levillain9867bc72015-08-05 10:21:34 +01002333class HConstant : public HExpression<0> {
2334 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002335 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2336 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002337
2338 bool CanBeMoved() const OVERRIDE { return true; }
2339
2340 virtual bool IsMinusOne() const { return false; }
2341 virtual bool IsZero() const { return false; }
2342 virtual bool IsOne() const { return false; }
2343
David Brazdil77a48ae2015-09-15 12:34:04 +00002344 virtual uint64_t GetValueAsUint64() const = 0;
2345
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002346 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002347
2348 private:
2349 DISALLOW_COPY_AND_ASSIGN(HConstant);
2350};
2351
2352class HNullConstant : public HConstant {
2353 public:
2354 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2355 return true;
2356 }
2357
David Brazdil77a48ae2015-09-15 12:34:04 +00002358 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2359
Roland Levillain9867bc72015-08-05 10:21:34 +01002360 size_t ComputeHashCode() const OVERRIDE { return 0; }
2361
2362 DECLARE_INSTRUCTION(NullConstant);
2363
2364 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002365 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002366
2367 friend class HGraph;
2368 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2369};
2370
2371// Constants of the type int. Those can be from Dex instructions, or
2372// synthesized (for example with the if-eqz instruction).
2373class HIntConstant : public HConstant {
2374 public:
2375 int32_t GetValue() const { return value_; }
2376
David Brazdil9f389d42015-10-01 14:32:56 +01002377 uint64_t GetValueAsUint64() const OVERRIDE {
2378 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2379 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002380
Roland Levillain9867bc72015-08-05 10:21:34 +01002381 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2382 DCHECK(other->IsIntConstant());
2383 return other->AsIntConstant()->value_ == value_;
2384 }
2385
2386 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2387
2388 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2389 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2390 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2391
2392 DECLARE_INSTRUCTION(IntConstant);
2393
2394 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002395 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2396 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2397 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2398 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002399
2400 const int32_t value_;
2401
2402 friend class HGraph;
2403 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2404 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2405 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2406};
2407
2408class HLongConstant : public HConstant {
2409 public:
2410 int64_t GetValue() const { return value_; }
2411
David Brazdil77a48ae2015-09-15 12:34:04 +00002412 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2413
Roland Levillain9867bc72015-08-05 10:21:34 +01002414 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2415 DCHECK(other->IsLongConstant());
2416 return other->AsLongConstant()->value_ == value_;
2417 }
2418
2419 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2420
2421 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2422 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2423 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2424
2425 DECLARE_INSTRUCTION(LongConstant);
2426
2427 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002428 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2429 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002430
2431 const int64_t value_;
2432
2433 friend class HGraph;
2434 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2435};
Dave Allison20dfc792014-06-16 20:44:29 -07002436
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002437// Conditional branch. A block ending with an HIf instruction must have
2438// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002439class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002440 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002441 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2442 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002443 SetRawInputAt(0, input);
2444 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002445
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002446 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002447
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002448 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002449 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002450 }
2451
2452 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002453 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002454 }
2455
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002456 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002457
2458 private:
2459 DISALLOW_COPY_AND_ASSIGN(HIf);
2460};
2461
David Brazdilfc6a86a2015-06-26 10:33:45 +00002462
2463// Abstract instruction which marks the beginning and/or end of a try block and
2464// links it to the respective exception handlers. Behaves the same as a Goto in
2465// non-exceptional control flow.
2466// Normal-flow successor is stored at index zero, exception handlers under
2467// higher indices in no particular order.
2468class HTryBoundary : public HTemplateInstruction<0> {
2469 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002470 enum BoundaryKind {
2471 kEntry,
2472 kExit,
2473 };
2474
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002475 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2476 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002477
2478 bool IsControlFlow() const OVERRIDE { return true; }
2479
2480 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002481 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002482
David Brazdild26a4112015-11-10 11:07:31 +00002483 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2484 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2485 }
2486
David Brazdilfc6a86a2015-06-26 10:33:45 +00002487 // Returns whether `handler` is among its exception handlers (non-zero index
2488 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002489 bool HasExceptionHandler(const HBasicBlock& handler) const {
2490 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002491 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002492 }
2493
2494 // If not present already, adds `handler` to its block's list of exception
2495 // handlers.
2496 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002497 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002498 GetBlock()->AddSuccessor(handler);
2499 }
2500 }
2501
David Brazdil56e1acc2015-06-30 15:41:36 +01002502 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002503
David Brazdilffee3d32015-07-06 11:48:53 +01002504 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2505
David Brazdilfc6a86a2015-06-26 10:33:45 +00002506 DECLARE_INSTRUCTION(TryBoundary);
2507
2508 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002509 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002510
2511 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2512};
2513
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002514// Deoptimize to interpreter, upon checking a condition.
2515class HDeoptimize : public HTemplateInstruction<1> {
2516 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002517 // We set CanTriggerGC to prevent any intermediate address to be live
2518 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002519 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002520 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002521 SetRawInputAt(0, cond);
2522 }
2523
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002524 bool CanBeMoved() const OVERRIDE { return true; }
2525 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2526 return true;
2527 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002528 bool NeedsEnvironment() const OVERRIDE { return true; }
2529 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002530
2531 DECLARE_INSTRUCTION(Deoptimize);
2532
2533 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002534 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2535};
2536
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002537// Represents the ArtMethod that was passed as a first argument to
2538// the method. It is used by instructions that depend on it, like
2539// instructions that work with the dex cache.
2540class HCurrentMethod : public HExpression<0> {
2541 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002542 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2543 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002544
2545 DECLARE_INSTRUCTION(CurrentMethod);
2546
2547 private:
2548 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2549};
2550
Mark Mendellfe57faa2015-09-18 09:26:15 -04002551// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2552// have one successor for each entry in the switch table, and the final successor
2553// will be the block containing the next Dex opcode.
2554class HPackedSwitch : public HTemplateInstruction<1> {
2555 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002556 HPackedSwitch(int32_t start_value,
2557 uint32_t num_entries,
2558 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002559 uint32_t dex_pc = kNoDexPc)
2560 : HTemplateInstruction(SideEffects::None(), dex_pc),
2561 start_value_(start_value),
2562 num_entries_(num_entries) {
2563 SetRawInputAt(0, input);
2564 }
2565
2566 bool IsControlFlow() const OVERRIDE { return true; }
2567
2568 int32_t GetStartValue() const { return start_value_; }
2569
Vladimir Marko211c2112015-09-24 16:52:33 +01002570 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002571
2572 HBasicBlock* GetDefaultBlock() const {
2573 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002574 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002575 }
2576 DECLARE_INSTRUCTION(PackedSwitch);
2577
2578 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002579 const int32_t start_value_;
2580 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002581
2582 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2583};
2584
Roland Levillain88cb1752014-10-20 16:36:47 +01002585class HUnaryOperation : public HExpression<1> {
2586 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002587 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2588 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002589 SetRawInputAt(0, input);
2590 }
2591
2592 HInstruction* GetInput() const { return InputAt(0); }
2593 Primitive::Type GetResultType() const { return GetType(); }
2594
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002595 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002596 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002597 return true;
2598 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002599
Roland Levillain9240d6a2014-10-20 16:47:04 +01002600 // Try to statically evaluate `operation` and return a HConstant
2601 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002602 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002603 HConstant* TryStaticEvaluation() const;
2604
2605 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002606 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2607 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002608
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002609 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002610
2611 private:
2612 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2613};
2614
Dave Allison20dfc792014-06-16 20:44:29 -07002615class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002616 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002617 HBinaryOperation(Primitive::Type result_type,
2618 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002619 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002620 SideEffects side_effects = SideEffects::None(),
2621 uint32_t dex_pc = kNoDexPc)
2622 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002623 SetRawInputAt(0, left);
2624 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002625 }
2626
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002627 HInstruction* GetLeft() const { return InputAt(0); }
2628 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002629 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002630
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002631 virtual bool IsCommutative() const { return false; }
2632
2633 // Put constant on the right.
2634 // Returns whether order is changed.
2635 bool OrderInputsWithConstantOnTheRight() {
2636 HInstruction* left = InputAt(0);
2637 HInstruction* right = InputAt(1);
2638 if (left->IsConstant() && !right->IsConstant()) {
2639 ReplaceInput(right, 0);
2640 ReplaceInput(left, 1);
2641 return true;
2642 }
2643 return false;
2644 }
2645
2646 // Order inputs by instruction id, but favor constant on the right side.
2647 // This helps GVN for commutative ops.
2648 void OrderInputs() {
2649 DCHECK(IsCommutative());
2650 HInstruction* left = InputAt(0);
2651 HInstruction* right = InputAt(1);
2652 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2653 return;
2654 }
2655 if (OrderInputsWithConstantOnTheRight()) {
2656 return;
2657 }
2658 // Order according to instruction id.
2659 if (left->GetId() > right->GetId()) {
2660 ReplaceInput(right, 0);
2661 ReplaceInput(left, 1);
2662 }
2663 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002664
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002665 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002666 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002667 return true;
2668 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002669
Roland Levillain9240d6a2014-10-20 16:47:04 +01002670 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002671 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002672 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002673 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002674
2675 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002676 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2677 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2678 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2679 HLongConstant* y ATTRIBUTE_UNUSED) const {
2680 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2681 return nullptr;
2682 }
2683 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2684 HIntConstant* y ATTRIBUTE_UNUSED) const {
2685 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2686 return nullptr;
2687 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002688 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2689 HNullConstant* y ATTRIBUTE_UNUSED) const {
2690 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2691 return nullptr;
2692 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002693
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002694 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002695 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002696 HConstant* GetConstantRight() const;
2697
2698 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002699 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002700 HInstruction* GetLeastConstantLeft() const;
2701
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002702 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002703
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002704 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002705 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2706};
2707
Mark Mendellc4701932015-04-10 13:18:51 -04002708// The comparison bias applies for floating point operations and indicates how NaN
2709// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002710enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002711 kNoBias, // bias is not applicable (i.e. for long operation)
2712 kGtBias, // return 1 for NaN comparisons
2713 kLtBias, // return -1 for NaN comparisons
2714};
2715
Dave Allison20dfc792014-06-16 20:44:29 -07002716class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002717 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002718 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2719 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002720 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002721 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002722
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002723 bool NeedsMaterialization() const { return needs_materialization_; }
2724 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002725
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002726 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002727 // `instruction`, and disregard moves in between.
2728 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002729
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002730 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002731
2732 virtual IfCondition GetCondition() const = 0;
2733
Mark Mendellc4701932015-04-10 13:18:51 -04002734 virtual IfCondition GetOppositeCondition() const = 0;
2735
Roland Levillain4fa13f62015-07-06 18:11:54 +01002736 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002737
2738 void SetBias(ComparisonBias bias) { bias_ = bias; }
2739
2740 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2741 return bias_ == other->AsCondition()->bias_;
2742 }
2743
Roland Levillain4fa13f62015-07-06 18:11:54 +01002744 bool IsFPConditionTrueIfNaN() const {
2745 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2746 IfCondition if_cond = GetCondition();
2747 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2748 }
2749
2750 bool IsFPConditionFalseIfNaN() const {
2751 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2752 IfCondition if_cond = GetCondition();
2753 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2754 }
2755
Dave Allison20dfc792014-06-16 20:44:29 -07002756 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002757 // For register allocation purposes, returns whether this instruction needs to be
2758 // materialized (that is, not just be in the processor flags).
2759 bool needs_materialization_;
2760
Mark Mendellc4701932015-04-10 13:18:51 -04002761 // Needed if we merge a HCompare into a HCondition.
2762 ComparisonBias bias_;
2763
Dave Allison20dfc792014-06-16 20:44:29 -07002764 DISALLOW_COPY_AND_ASSIGN(HCondition);
2765};
2766
2767// Instruction to check if two inputs are equal to each other.
2768class HEqual : public HCondition {
2769 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002770 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2771 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002772
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002773 bool IsCommutative() const OVERRIDE { return true; }
2774
Roland Levillain9867bc72015-08-05 10:21:34 +01002775 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002776 return GetBlock()->GetGraph()->GetIntConstant(
2777 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002778 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002779 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002780 return GetBlock()->GetGraph()->GetIntConstant(
2781 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002782 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002783 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2784 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002785 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002786 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002787
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002788 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002789
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002790 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002791 return kCondEQ;
2792 }
2793
Mark Mendellc4701932015-04-10 13:18:51 -04002794 IfCondition GetOppositeCondition() const OVERRIDE {
2795 return kCondNE;
2796 }
2797
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002798 private:
Aart Bike9f37602015-10-09 11:15:55 -07002799 template <typename T> bool Compute(T x, T y) const { return x == y; }
2800
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002801 DISALLOW_COPY_AND_ASSIGN(HEqual);
2802};
2803
Dave Allison20dfc792014-06-16 20:44:29 -07002804class HNotEqual : public HCondition {
2805 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002806 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2807 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002808
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002809 bool IsCommutative() const OVERRIDE { return true; }
2810
Roland Levillain9867bc72015-08-05 10:21:34 +01002811 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002812 return GetBlock()->GetGraph()->GetIntConstant(
2813 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002814 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002815 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002816 return GetBlock()->GetGraph()->GetIntConstant(
2817 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002818 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002819 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2820 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002821 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002822 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002823
Dave Allison20dfc792014-06-16 20:44:29 -07002824 DECLARE_INSTRUCTION(NotEqual);
2825
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002826 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002827 return kCondNE;
2828 }
2829
Mark Mendellc4701932015-04-10 13:18:51 -04002830 IfCondition GetOppositeCondition() const OVERRIDE {
2831 return kCondEQ;
2832 }
2833
Dave Allison20dfc792014-06-16 20:44:29 -07002834 private:
Aart Bike9f37602015-10-09 11:15:55 -07002835 template <typename T> bool Compute(T x, T y) const { return x != y; }
2836
Dave Allison20dfc792014-06-16 20:44:29 -07002837 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2838};
2839
2840class HLessThan : public HCondition {
2841 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002842 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2843 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002844
Roland Levillain9867bc72015-08-05 10:21:34 +01002845 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002846 return GetBlock()->GetGraph()->GetIntConstant(
2847 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002848 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002849 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002850 return GetBlock()->GetGraph()->GetIntConstant(
2851 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002852 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002853
Dave Allison20dfc792014-06-16 20:44:29 -07002854 DECLARE_INSTRUCTION(LessThan);
2855
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002856 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002857 return kCondLT;
2858 }
2859
Mark Mendellc4701932015-04-10 13:18:51 -04002860 IfCondition GetOppositeCondition() const OVERRIDE {
2861 return kCondGE;
2862 }
2863
Dave Allison20dfc792014-06-16 20:44:29 -07002864 private:
Aart Bike9f37602015-10-09 11:15:55 -07002865 template <typename T> bool Compute(T x, T y) const { return x < y; }
2866
Dave Allison20dfc792014-06-16 20:44:29 -07002867 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2868};
2869
2870class HLessThanOrEqual : public HCondition {
2871 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002872 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2873 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002874
Roland Levillain9867bc72015-08-05 10:21:34 +01002875 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002876 return GetBlock()->GetGraph()->GetIntConstant(
2877 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002878 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002879 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002880 return GetBlock()->GetGraph()->GetIntConstant(
2881 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002882 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002883
Dave Allison20dfc792014-06-16 20:44:29 -07002884 DECLARE_INSTRUCTION(LessThanOrEqual);
2885
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002886 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002887 return kCondLE;
2888 }
2889
Mark Mendellc4701932015-04-10 13:18:51 -04002890 IfCondition GetOppositeCondition() const OVERRIDE {
2891 return kCondGT;
2892 }
2893
Dave Allison20dfc792014-06-16 20:44:29 -07002894 private:
Aart Bike9f37602015-10-09 11:15:55 -07002895 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2896
Dave Allison20dfc792014-06-16 20:44:29 -07002897 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2898};
2899
2900class HGreaterThan : public HCondition {
2901 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002902 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2903 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002904
Roland Levillain9867bc72015-08-05 10:21:34 +01002905 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002906 return GetBlock()->GetGraph()->GetIntConstant(
2907 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002908 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002909 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002910 return GetBlock()->GetGraph()->GetIntConstant(
2911 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002912 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002913
Dave Allison20dfc792014-06-16 20:44:29 -07002914 DECLARE_INSTRUCTION(GreaterThan);
2915
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002916 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002917 return kCondGT;
2918 }
2919
Mark Mendellc4701932015-04-10 13:18:51 -04002920 IfCondition GetOppositeCondition() const OVERRIDE {
2921 return kCondLE;
2922 }
2923
Dave Allison20dfc792014-06-16 20:44:29 -07002924 private:
Aart Bike9f37602015-10-09 11:15:55 -07002925 template <typename T> bool Compute(T x, T y) const { return x > y; }
2926
Dave Allison20dfc792014-06-16 20:44:29 -07002927 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2928};
2929
2930class HGreaterThanOrEqual : public HCondition {
2931 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002932 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2933 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002934
Roland Levillain9867bc72015-08-05 10:21:34 +01002935 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002936 return GetBlock()->GetGraph()->GetIntConstant(
2937 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002938 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002939 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002940 return GetBlock()->GetGraph()->GetIntConstant(
2941 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002942 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002943
Dave Allison20dfc792014-06-16 20:44:29 -07002944 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2945
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002946 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002947 return kCondGE;
2948 }
2949
Mark Mendellc4701932015-04-10 13:18:51 -04002950 IfCondition GetOppositeCondition() const OVERRIDE {
2951 return kCondLT;
2952 }
2953
Dave Allison20dfc792014-06-16 20:44:29 -07002954 private:
Aart Bike9f37602015-10-09 11:15:55 -07002955 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2956
Dave Allison20dfc792014-06-16 20:44:29 -07002957 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2958};
2959
Aart Bike9f37602015-10-09 11:15:55 -07002960class HBelow : public HCondition {
2961 public:
2962 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2963 : HCondition(first, second, dex_pc) {}
2964
2965 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2966 return GetBlock()->GetGraph()->GetIntConstant(
2967 Compute(static_cast<uint32_t>(x->GetValue()),
2968 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2969 }
2970 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2971 return GetBlock()->GetGraph()->GetIntConstant(
2972 Compute(static_cast<uint64_t>(x->GetValue()),
2973 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2974 }
2975
2976 DECLARE_INSTRUCTION(Below);
2977
2978 IfCondition GetCondition() const OVERRIDE {
2979 return kCondB;
2980 }
2981
2982 IfCondition GetOppositeCondition() const OVERRIDE {
2983 return kCondAE;
2984 }
2985
2986 private:
2987 template <typename T> bool Compute(T x, T y) const { return x < y; }
2988
2989 DISALLOW_COPY_AND_ASSIGN(HBelow);
2990};
2991
2992class HBelowOrEqual : public HCondition {
2993 public:
2994 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2995 : HCondition(first, second, dex_pc) {}
2996
2997 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2998 return GetBlock()->GetGraph()->GetIntConstant(
2999 Compute(static_cast<uint32_t>(x->GetValue()),
3000 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3001 }
3002 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3003 return GetBlock()->GetGraph()->GetIntConstant(
3004 Compute(static_cast<uint64_t>(x->GetValue()),
3005 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3006 }
3007
3008 DECLARE_INSTRUCTION(BelowOrEqual);
3009
3010 IfCondition GetCondition() const OVERRIDE {
3011 return kCondBE;
3012 }
3013
3014 IfCondition GetOppositeCondition() const OVERRIDE {
3015 return kCondA;
3016 }
3017
3018 private:
3019 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3020
3021 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3022};
3023
3024class HAbove : public HCondition {
3025 public:
3026 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3027 : HCondition(first, second, dex_pc) {}
3028
3029 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3030 return GetBlock()->GetGraph()->GetIntConstant(
3031 Compute(static_cast<uint32_t>(x->GetValue()),
3032 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3033 }
3034 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3035 return GetBlock()->GetGraph()->GetIntConstant(
3036 Compute(static_cast<uint64_t>(x->GetValue()),
3037 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3038 }
3039
3040 DECLARE_INSTRUCTION(Above);
3041
3042 IfCondition GetCondition() const OVERRIDE {
3043 return kCondA;
3044 }
3045
3046 IfCondition GetOppositeCondition() const OVERRIDE {
3047 return kCondBE;
3048 }
3049
3050 private:
3051 template <typename T> bool Compute(T x, T y) const { return x > y; }
3052
3053 DISALLOW_COPY_AND_ASSIGN(HAbove);
3054};
3055
3056class HAboveOrEqual : public HCondition {
3057 public:
3058 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3059 : HCondition(first, second, dex_pc) {}
3060
3061 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3062 return GetBlock()->GetGraph()->GetIntConstant(
3063 Compute(static_cast<uint32_t>(x->GetValue()),
3064 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3065 }
3066 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3067 return GetBlock()->GetGraph()->GetIntConstant(
3068 Compute(static_cast<uint64_t>(x->GetValue()),
3069 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3070 }
3071
3072 DECLARE_INSTRUCTION(AboveOrEqual);
3073
3074 IfCondition GetCondition() const OVERRIDE {
3075 return kCondAE;
3076 }
3077
3078 IfCondition GetOppositeCondition() const OVERRIDE {
3079 return kCondB;
3080 }
3081
3082 private:
3083 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3084
3085 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3086};
Dave Allison20dfc792014-06-16 20:44:29 -07003087
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003088// Instruction to check how two inputs compare to each other.
3089// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3090class HCompare : public HBinaryOperation {
3091 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003092 HCompare(Primitive::Type type,
3093 HInstruction* first,
3094 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003095 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003096 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003097 : HBinaryOperation(Primitive::kPrimInt,
3098 first,
3099 second,
3100 SideEffectsForArchRuntimeCalls(type),
3101 dex_pc),
3102 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003103 DCHECK_EQ(type, first->GetType());
3104 DCHECK_EQ(type, second->GetType());
3105 }
3106
Roland Levillain9867bc72015-08-05 10:21:34 +01003107 template <typename T>
3108 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003109
Roland Levillain9867bc72015-08-05 10:21:34 +01003110 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003111 return GetBlock()->GetGraph()->GetIntConstant(
3112 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003113 }
3114 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003115 return GetBlock()->GetGraph()->GetIntConstant(
3116 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003117 }
3118
Calin Juravleddb7df22014-11-25 20:56:51 +00003119 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3120 return bias_ == other->AsCompare()->bias_;
3121 }
3122
Mark Mendellc4701932015-04-10 13:18:51 -04003123 ComparisonBias GetBias() const { return bias_; }
3124
Roland Levillain4fa13f62015-07-06 18:11:54 +01003125 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003126
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003127
3128 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3129 // MIPS64 uses a runtime call for FP comparisons.
3130 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3131 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003132
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003133 DECLARE_INSTRUCTION(Compare);
3134
3135 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003136 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003137
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003138 DISALLOW_COPY_AND_ASSIGN(HCompare);
3139};
3140
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003141// A local in the graph. Corresponds to a Dex register.
3142class HLocal : public HTemplateInstruction<0> {
3143 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003144 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003145 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003146
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003147 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003148
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003149 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003150
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003151 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003152 // The Dex register number.
3153 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003154
3155 DISALLOW_COPY_AND_ASSIGN(HLocal);
3156};
3157
3158// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003159class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003160 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003161 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3162 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003163 SetRawInputAt(0, local);
3164 }
3165
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003166 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3167
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003168 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003169
3170 private:
3171 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3172};
3173
3174// Store a value in a given local. This instruction has two inputs: the value
3175// and the local.
3176class HStoreLocal : public HTemplateInstruction<2> {
3177 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003178 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3179 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003180 SetRawInputAt(0, local);
3181 SetRawInputAt(1, value);
3182 }
3183
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003184 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3185
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003186 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003187
3188 private:
3189 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3190};
3191
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003192class HFloatConstant : public HConstant {
3193 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003194 float GetValue() const { return value_; }
3195
David Brazdil77a48ae2015-09-15 12:34:04 +00003196 uint64_t GetValueAsUint64() const OVERRIDE {
3197 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3198 }
3199
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003200 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003201 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003202 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003203 }
3204
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003205 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003206
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003207 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003208 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003209 }
3210 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003211 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003212 }
3213 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003214 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3215 }
3216 bool IsNaN() const {
3217 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003218 }
3219
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003220 DECLARE_INSTRUCTION(FloatConstant);
3221
3222 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003223 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3224 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3225 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3226 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003227
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003228 const float value_;
3229
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003230 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003231 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003232 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003233 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3234};
3235
3236class HDoubleConstant : public HConstant {
3237 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003238 double GetValue() const { return value_; }
3239
David Brazdil77a48ae2015-09-15 12:34:04 +00003240 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3241
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003242 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003243 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003244 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003245 }
3246
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003247 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003248
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003249 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003250 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003251 }
3252 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003253 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003254 }
3255 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003256 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3257 }
3258 bool IsNaN() const {
3259 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003260 }
3261
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003262 DECLARE_INSTRUCTION(DoubleConstant);
3263
3264 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003265 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3266 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3267 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3268 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003269
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003270 const double value_;
3271
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003272 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003273 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003274 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003275 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3276};
3277
David Brazdil6de19382016-01-08 17:37:10 +00003278class HNewInstance : public HExpression<2> {
3279 public:
3280 HNewInstance(HInstruction* cls,
3281 HCurrentMethod* current_method,
3282 uint32_t dex_pc,
3283 uint16_t type_index,
3284 const DexFile& dex_file,
3285 bool can_throw,
3286 bool finalizable,
3287 QuickEntrypointEnum entrypoint)
3288 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3289 type_index_(type_index),
3290 dex_file_(dex_file),
3291 can_throw_(can_throw),
3292 finalizable_(finalizable),
3293 entrypoint_(entrypoint) {
3294 SetRawInputAt(0, cls);
3295 SetRawInputAt(1, current_method);
3296 }
3297
3298 uint16_t GetTypeIndex() const { return type_index_; }
3299 const DexFile& GetDexFile() const { return dex_file_; }
3300
3301 // Calls runtime so needs an environment.
3302 bool NeedsEnvironment() const OVERRIDE { return true; }
3303
3304 // It may throw when called on type that's not instantiable/accessible.
3305 // It can throw OOME.
3306 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3307 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3308
3309 bool IsFinalizable() const { return finalizable_; }
3310
3311 bool CanBeNull() const OVERRIDE { return false; }
3312
3313 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3314
3315 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3316 entrypoint_ = entrypoint;
3317 }
3318
3319 bool IsStringAlloc() const;
3320
3321 DECLARE_INSTRUCTION(NewInstance);
3322
3323 private:
3324 const uint16_t type_index_;
3325 const DexFile& dex_file_;
3326 const bool can_throw_;
3327 const bool finalizable_;
3328 QuickEntrypointEnum entrypoint_;
3329
3330 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3331};
3332
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003333enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003334#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3335 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003336#include "intrinsics_list.h"
3337 kNone,
3338 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3339#undef INTRINSICS_LIST
3340#undef OPTIMIZING_INTRINSICS
3341};
3342std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3343
Agi Csaki05f20562015-08-19 14:58:14 -07003344enum IntrinsicNeedsEnvironmentOrCache {
3345 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3346 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003347};
3348
Aart Bik5d75afe2015-12-14 11:57:01 -08003349enum IntrinsicSideEffects {
3350 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3351 kReadSideEffects, // Intrinsic may read heap memory.
3352 kWriteSideEffects, // Intrinsic may write heap memory.
3353 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3354};
3355
3356enum IntrinsicExceptions {
3357 kNoThrow, // Intrinsic does not throw any exceptions.
3358 kCanThrow // Intrinsic may throw exceptions.
3359};
3360
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003361class HInvoke : public HInstruction {
3362 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003363 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003364
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003365 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003366
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003367 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003368 SetRawInputAt(index, argument);
3369 }
3370
Roland Levillain3e3d7332015-04-28 11:00:54 +01003371 // Return the number of arguments. This number can be lower than
3372 // the number of inputs returned by InputCount(), as some invoke
3373 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3374 // inputs at the end of their list of inputs.
3375 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3376
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003377 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003378
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003379 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003380 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003381
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003382 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3383
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003384 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003385 return intrinsic_;
3386 }
3387
Aart Bik5d75afe2015-12-14 11:57:01 -08003388 void SetIntrinsic(Intrinsics intrinsic,
3389 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3390 IntrinsicSideEffects side_effects,
3391 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003392
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003393 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003394 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003395 }
3396
Aart Bik5d75afe2015-12-14 11:57:01 -08003397 bool CanThrow() const OVERRIDE { return can_throw_; }
3398
3399 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3400
3401 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3402 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3403 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003404
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003405 uint32_t* GetIntrinsicOptimizations() {
3406 return &intrinsic_optimizations_;
3407 }
3408
3409 const uint32_t* GetIntrinsicOptimizations() const {
3410 return &intrinsic_optimizations_;
3411 }
3412
3413 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3414
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003415 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003416
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003417 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003418 HInvoke(ArenaAllocator* arena,
3419 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003420 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003421 Primitive::Type return_type,
3422 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003423 uint32_t dex_method_index,
3424 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003425 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003426 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003427 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003428 inputs_(number_of_arguments + number_of_other_inputs,
3429 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003430 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003431 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003432 original_invoke_type_(original_invoke_type),
Aart Bik5d75afe2015-12-14 11:57:01 -08003433 can_throw_(true),
agicsaki57b81ec2015-08-11 17:39:37 -07003434 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003435 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003436 }
3437
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003438 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003439 return inputs_[index];
3440 }
3441
David Brazdil1abb4192015-02-17 18:33:36 +00003442 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003443 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003444 }
3445
Aart Bik5d75afe2015-12-14 11:57:01 -08003446 void SetCanThrow(bool can_throw) { can_throw_ = can_throw; }
3447
Roland Levillain3e3d7332015-04-28 11:00:54 +01003448 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003449 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003450 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003451 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003452 const InvokeType original_invoke_type_;
Aart Bik5d75afe2015-12-14 11:57:01 -08003453 bool can_throw_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003454 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003455
3456 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3457 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003458
3459 private:
3460 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3461};
3462
Calin Juravle175dc732015-08-25 15:42:32 +01003463class HInvokeUnresolved : public HInvoke {
3464 public:
3465 HInvokeUnresolved(ArenaAllocator* arena,
3466 uint32_t number_of_arguments,
3467 Primitive::Type return_type,
3468 uint32_t dex_pc,
3469 uint32_t dex_method_index,
3470 InvokeType invoke_type)
3471 : HInvoke(arena,
3472 number_of_arguments,
3473 0u /* number_of_other_inputs */,
3474 return_type,
3475 dex_pc,
3476 dex_method_index,
3477 invoke_type) {
3478 }
3479
3480 DECLARE_INSTRUCTION(InvokeUnresolved);
3481
3482 private:
3483 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3484};
3485
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003486class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003487 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003488 // Requirements of this method call regarding the class
3489 // initialization (clinit) check of its declaring class.
3490 enum class ClinitCheckRequirement {
3491 kNone, // Class already initialized.
3492 kExplicit, // Static call having explicit clinit check as last input.
3493 kImplicit, // Static call implicitly requiring a clinit check.
3494 };
3495
Vladimir Marko58155012015-08-19 12:49:41 +00003496 // Determines how to load the target ArtMethod*.
3497 enum class MethodLoadKind {
3498 // Use a String init ArtMethod* loaded from Thread entrypoints.
3499 kStringInit,
3500
3501 // Use the method's own ArtMethod* loaded by the register allocator.
3502 kRecursive,
3503
3504 // Use ArtMethod* at a known address, embed the direct address in the code.
3505 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3506 kDirectAddress,
3507
3508 // Use ArtMethod* at an address that will be known at link time, embed the direct
3509 // address in the code. If the image is relocatable, emit .patch_oat entry.
3510 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3511 // the image relocatable or not.
3512 kDirectAddressWithFixup,
3513
3514 // Load from resoved methods array in the dex cache using a PC-relative load.
3515 // Used when we need to use the dex cache, for example for invoke-static that
3516 // may cause class initialization (the entry may point to a resolution method),
3517 // and we know that we can access the dex cache arrays using a PC-relative load.
3518 kDexCachePcRelative,
3519
3520 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3521 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3522 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3523 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3524 kDexCacheViaMethod,
3525 };
3526
3527 // Determines the location of the code pointer.
3528 enum class CodePtrLocation {
3529 // Recursive call, use local PC-relative call instruction.
3530 kCallSelf,
3531
3532 // Use PC-relative call instruction patched at link time.
3533 // Used for calls within an oat file, boot->boot or app->app.
3534 kCallPCRelative,
3535
3536 // Call to a known target address, embed the direct address in code.
3537 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3538 kCallDirect,
3539
3540 // Call to a target address that will be known at link time, embed the direct
3541 // address in code. If the image is relocatable, emit .patch_oat entry.
3542 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3543 // the image relocatable or not.
3544 kCallDirectWithFixup,
3545
3546 // Use code pointer from the ArtMethod*.
3547 // Used when we don't know the target code. This is also the last-resort-kind used when
3548 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3549 kCallArtMethod,
3550 };
3551
3552 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003553 MethodLoadKind method_load_kind;
3554 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003555 // The method load data holds
3556 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3557 // Note that there are multiple string init methods, each having its own offset.
3558 // - the method address for kDirectAddress
3559 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003560 uint64_t method_load_data;
3561 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003562 };
3563
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003564 HInvokeStaticOrDirect(ArenaAllocator* arena,
3565 uint32_t number_of_arguments,
3566 Primitive::Type return_type,
3567 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003568 uint32_t method_index,
3569 MethodReference target_method,
3570 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003571 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003572 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003573 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003574 : HInvoke(arena,
3575 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003576 // There is potentially one extra argument for the HCurrentMethod node, and
3577 // potentially one other if the clinit check is explicit, and potentially
3578 // one other if the method is a string factory.
3579 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3580 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3581 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003582 return_type,
3583 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003584 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003585 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003586 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003587 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003588 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003589 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003590
Vladimir Markodc151b22015-10-15 18:02:30 +01003591 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003592 bool had_current_method_input = HasCurrentMethodInput();
3593 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3594
3595 // Using the current method is the default and once we find a better
3596 // method load kind, we should not go back to using the current method.
3597 DCHECK(had_current_method_input || !needs_current_method_input);
3598
3599 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003600 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3601 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003602 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003603 dispatch_info_ = dispatch_info;
3604 }
3605
Vladimir Markoc53c0792015-11-19 15:48:33 +00003606 void AddSpecialInput(HInstruction* input) {
3607 // We allow only one special input.
3608 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3609 DCHECK(InputCount() == GetSpecialInputIndex() ||
3610 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3611 InsertInputAt(GetSpecialInputIndex(), input);
3612 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003613
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003614 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003615 // We access the method via the dex cache so we can't do an implicit null check.
3616 // TODO: for intrinsics we can generate implicit null checks.
3617 return false;
3618 }
3619
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003620 bool CanBeNull() const OVERRIDE {
3621 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3622 }
3623
Vladimir Markoc53c0792015-11-19 15:48:33 +00003624 // Get the index of the special input, if any.
3625 //
David Brazdil6de19382016-01-08 17:37:10 +00003626 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3627 // method pointer; otherwise there may be one platform-specific special input,
3628 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003629 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3630
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003631 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3632 void SetOptimizedInvokeType(InvokeType invoke_type) {
3633 optimized_invoke_type_ = invoke_type;
3634 }
3635
Vladimir Marko58155012015-08-19 12:49:41 +00003636 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3637 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3638 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003639 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003640 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003641 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003642 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003643 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3644 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003645 bool HasCurrentMethodInput() const {
3646 // This function can be called only after the invoke has been fully initialized by the builder.
3647 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003648 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003649 return true;
3650 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003651 DCHECK(InputCount() == GetSpecialInputIndex() ||
3652 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003653 return false;
3654 }
3655 }
Vladimir Marko58155012015-08-19 12:49:41 +00003656 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3657 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003658 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003659
3660 int32_t GetStringInitOffset() const {
3661 DCHECK(IsStringInit());
3662 return dispatch_info_.method_load_data;
3663 }
3664
3665 uint64_t GetMethodAddress() const {
3666 DCHECK(HasMethodAddress());
3667 return dispatch_info_.method_load_data;
3668 }
3669
3670 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003671 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003672 return dispatch_info_.method_load_data;
3673 }
3674
3675 uint64_t GetDirectCodePtr() const {
3676 DCHECK(HasDirectCodePtr());
3677 return dispatch_info_.direct_code_ptr;
3678 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003679
Calin Juravle68ad6492015-08-18 17:08:12 +01003680 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3681
Roland Levillain4c0eb422015-04-24 16:43:49 +01003682 // Is this instruction a call to a static method?
3683 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003684 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003685 }
3686
Vladimir Markofbb184a2015-11-13 14:47:00 +00003687 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3688 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3689 // instruction; only relevant for static calls with explicit clinit check.
3690 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003691 DCHECK(IsStaticWithExplicitClinitCheck());
3692 size_t last_input_index = InputCount() - 1;
3693 HInstruction* last_input = InputAt(last_input_index);
3694 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003695 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003696 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003697 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003698 clinit_check_requirement_ = new_requirement;
3699 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003700 }
3701
David Brazdilbc9ab162016-01-20 14:50:19 +00003702 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00003703 DCHECK(IsStringInit());
3704 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00003705 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00003706 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003707 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00003708 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003709 }
3710
Roland Levillain4c0eb422015-04-24 16:43:49 +01003711 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003712 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003713 bool IsStaticWithExplicitClinitCheck() const {
3714 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3715 }
3716
3717 // Is this a call to a static method whose declaring class has an
3718 // implicit intialization check requirement?
3719 bool IsStaticWithImplicitClinitCheck() const {
3720 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3721 }
3722
Vladimir Markob554b5a2015-11-06 12:57:55 +00003723 // Does this method load kind need the current method as an input?
3724 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3725 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3726 }
3727
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003728 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003729
Roland Levillain4c0eb422015-04-24 16:43:49 +01003730 protected:
3731 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3732 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3733 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3734 HInstruction* input = input_record.GetInstruction();
3735 // `input` is the last input of a static invoke marked as having
3736 // an explicit clinit check. It must either be:
3737 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3738 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3739 DCHECK(input != nullptr);
3740 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3741 }
3742 return input_record;
3743 }
3744
Vladimir Markoc53c0792015-11-19 15:48:33 +00003745 void InsertInputAt(size_t index, HInstruction* input);
3746 void RemoveInputAt(size_t index);
3747
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003748 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003749 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003750 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003751 // The target method may refer to different dex file or method index than the original
3752 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3753 // in derived class to increase visibility.
3754 MethodReference target_method_;
3755 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003756
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003757 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003758};
Vladimir Markof64242a2015-12-01 14:58:23 +00003759std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003760std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003761
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003762class HInvokeVirtual : public HInvoke {
3763 public:
3764 HInvokeVirtual(ArenaAllocator* arena,
3765 uint32_t number_of_arguments,
3766 Primitive::Type return_type,
3767 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003768 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003769 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003770 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003771 vtable_index_(vtable_index) {}
3772
Calin Juravle641547a2015-04-21 22:08:51 +01003773 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003774 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003775 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003776 }
3777
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003778 uint32_t GetVTableIndex() const { return vtable_index_; }
3779
3780 DECLARE_INSTRUCTION(InvokeVirtual);
3781
3782 private:
3783 const uint32_t vtable_index_;
3784
3785 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3786};
3787
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003788class HInvokeInterface : public HInvoke {
3789 public:
3790 HInvokeInterface(ArenaAllocator* arena,
3791 uint32_t number_of_arguments,
3792 Primitive::Type return_type,
3793 uint32_t dex_pc,
3794 uint32_t dex_method_index,
3795 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003796 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003797 imt_index_(imt_index) {}
3798
Calin Juravle641547a2015-04-21 22:08:51 +01003799 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003800 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003801 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003802 }
3803
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003804 uint32_t GetImtIndex() const { return imt_index_; }
3805 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3806
3807 DECLARE_INSTRUCTION(InvokeInterface);
3808
3809 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003810 const uint32_t imt_index_;
3811
3812 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3813};
3814
Roland Levillain88cb1752014-10-20 16:36:47 +01003815class HNeg : public HUnaryOperation {
3816 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003817 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3818 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003819
Roland Levillain9867bc72015-08-05 10:21:34 +01003820 template <typename T> T Compute(T x) const { return -x; }
3821
3822 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003823 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003824 }
3825 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003826 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003827 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003828
Roland Levillain88cb1752014-10-20 16:36:47 +01003829 DECLARE_INSTRUCTION(Neg);
3830
3831 private:
3832 DISALLOW_COPY_AND_ASSIGN(HNeg);
3833};
3834
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003835class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003836 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003837 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003838 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003839 uint32_t dex_pc,
3840 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003841 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003842 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003843 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003844 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003845 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003846 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003847 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003848 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003849 }
3850
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003851 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003852 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003853
3854 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003855 bool NeedsEnvironment() const OVERRIDE { return true; }
3856
Mingyao Yang0c365e62015-03-31 15:09:29 -07003857 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3858 bool CanThrow() const OVERRIDE { return true; }
3859
Calin Juravle10e244f2015-01-26 18:54:32 +00003860 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003861
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003862 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3863
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003864 DECLARE_INSTRUCTION(NewArray);
3865
3866 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003867 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003868 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003869 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003870
3871 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3872};
3873
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003874class HAdd : public HBinaryOperation {
3875 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003876 HAdd(Primitive::Type result_type,
3877 HInstruction* left,
3878 HInstruction* right,
3879 uint32_t dex_pc = kNoDexPc)
3880 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003881
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003882 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003883
Roland Levillain9867bc72015-08-05 10:21:34 +01003884 template <typename T> T Compute(T x, T y) const { return x + y; }
3885
3886 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003887 return GetBlock()->GetGraph()->GetIntConstant(
3888 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003889 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003890 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003891 return GetBlock()->GetGraph()->GetLongConstant(
3892 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003893 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003894
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003895 DECLARE_INSTRUCTION(Add);
3896
3897 private:
3898 DISALLOW_COPY_AND_ASSIGN(HAdd);
3899};
3900
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003901class HSub : public HBinaryOperation {
3902 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003903 HSub(Primitive::Type result_type,
3904 HInstruction* left,
3905 HInstruction* right,
3906 uint32_t dex_pc = kNoDexPc)
3907 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003908
Roland Levillain9867bc72015-08-05 10:21:34 +01003909 template <typename T> T Compute(T x, T y) const { return x - y; }
3910
3911 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003912 return GetBlock()->GetGraph()->GetIntConstant(
3913 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003914 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003915 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003916 return GetBlock()->GetGraph()->GetLongConstant(
3917 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003918 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003919
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003920 DECLARE_INSTRUCTION(Sub);
3921
3922 private:
3923 DISALLOW_COPY_AND_ASSIGN(HSub);
3924};
3925
Calin Juravle34bacdf2014-10-07 20:23:36 +01003926class HMul : public HBinaryOperation {
3927 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003928 HMul(Primitive::Type result_type,
3929 HInstruction* left,
3930 HInstruction* right,
3931 uint32_t dex_pc = kNoDexPc)
3932 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003933
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003934 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003935
Roland Levillain9867bc72015-08-05 10:21:34 +01003936 template <typename T> T Compute(T x, T y) const { return x * y; }
3937
3938 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003939 return GetBlock()->GetGraph()->GetIntConstant(
3940 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003941 }
3942 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003943 return GetBlock()->GetGraph()->GetLongConstant(
3944 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003945 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003946
3947 DECLARE_INSTRUCTION(Mul);
3948
3949 private:
3950 DISALLOW_COPY_AND_ASSIGN(HMul);
3951};
3952
Calin Juravle7c4954d2014-10-28 16:57:40 +00003953class HDiv : public HBinaryOperation {
3954 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003955 HDiv(Primitive::Type result_type,
3956 HInstruction* left,
3957 HInstruction* right,
3958 uint32_t dex_pc)
3959 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003960
Roland Levillain9867bc72015-08-05 10:21:34 +01003961 template <typename T>
3962 T Compute(T x, T y) const {
3963 // Our graph structure ensures we never have 0 for `y` during
3964 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003965 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003966 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003967 return (y == -1) ? -x : x / y;
3968 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003969
Roland Levillain9867bc72015-08-05 10:21:34 +01003970 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003971 return GetBlock()->GetGraph()->GetIntConstant(
3972 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003973 }
3974 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003975 return GetBlock()->GetGraph()->GetLongConstant(
3976 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003977 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003978
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003979 static SideEffects SideEffectsForArchRuntimeCalls() {
3980 // The generated code can use a runtime call.
3981 return SideEffects::CanTriggerGC();
3982 }
3983
Calin Juravle7c4954d2014-10-28 16:57:40 +00003984 DECLARE_INSTRUCTION(Div);
3985
3986 private:
3987 DISALLOW_COPY_AND_ASSIGN(HDiv);
3988};
3989
Calin Juravlebacfec32014-11-14 15:54:36 +00003990class HRem : public HBinaryOperation {
3991 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003992 HRem(Primitive::Type result_type,
3993 HInstruction* left,
3994 HInstruction* right,
3995 uint32_t dex_pc)
3996 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003997
Roland Levillain9867bc72015-08-05 10:21:34 +01003998 template <typename T>
3999 T Compute(T x, T y) const {
4000 // Our graph structure ensures we never have 0 for `y` during
4001 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004002 DCHECK_NE(y, 0);
4003 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4004 return (y == -1) ? 0 : x % y;
4005 }
4006
Roland Levillain9867bc72015-08-05 10:21:34 +01004007 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004008 return GetBlock()->GetGraph()->GetIntConstant(
4009 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004010 }
4011 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004012 return GetBlock()->GetGraph()->GetLongConstant(
4013 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004014 }
4015
Calin Juravlebacfec32014-11-14 15:54:36 +00004016
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004017 static SideEffects SideEffectsForArchRuntimeCalls() {
4018 return SideEffects::CanTriggerGC();
4019 }
4020
Calin Juravlebacfec32014-11-14 15:54:36 +00004021 DECLARE_INSTRUCTION(Rem);
4022
4023 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004024 DISALLOW_COPY_AND_ASSIGN(HRem);
4025};
4026
Calin Juravled0d48522014-11-04 16:40:20 +00004027class HDivZeroCheck : public HExpression<1> {
4028 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004029 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4030 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004031 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004032 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004033 SetRawInputAt(0, value);
4034 }
4035
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004036 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4037
Calin Juravled0d48522014-11-04 16:40:20 +00004038 bool CanBeMoved() const OVERRIDE { return true; }
4039
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004040 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004041 return true;
4042 }
4043
4044 bool NeedsEnvironment() const OVERRIDE { return true; }
4045 bool CanThrow() const OVERRIDE { return true; }
4046
Calin Juravled0d48522014-11-04 16:40:20 +00004047 DECLARE_INSTRUCTION(DivZeroCheck);
4048
4049 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004050 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4051};
4052
Calin Juravle9aec02f2014-11-18 23:06:35 +00004053class HShl : public HBinaryOperation {
4054 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004055 HShl(Primitive::Type result_type,
4056 HInstruction* left,
4057 HInstruction* right,
4058 uint32_t dex_pc = kNoDexPc)
4059 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004060
Roland Levillain9867bc72015-08-05 10:21:34 +01004061 template <typename T, typename U, typename V>
4062 T Compute(T x, U y, V max_shift_value) const {
4063 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4064 "V is not the unsigned integer type corresponding to T");
4065 return x << (y & max_shift_value);
4066 }
4067
4068 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4069 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004070 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004071 }
4072 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4073 // case is handled as `x << static_cast<int>(y)`.
4074 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4075 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004076 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004077 }
4078 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4079 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004080 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004081 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004082
4083 DECLARE_INSTRUCTION(Shl);
4084
4085 private:
4086 DISALLOW_COPY_AND_ASSIGN(HShl);
4087};
4088
4089class HShr : public HBinaryOperation {
4090 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004091 HShr(Primitive::Type result_type,
4092 HInstruction* left,
4093 HInstruction* right,
4094 uint32_t dex_pc = kNoDexPc)
4095 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004096
Roland Levillain9867bc72015-08-05 10:21:34 +01004097 template <typename T, typename U, typename V>
4098 T Compute(T x, U y, V max_shift_value) const {
4099 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4100 "V is not the unsigned integer type corresponding to T");
4101 return x >> (y & max_shift_value);
4102 }
4103
4104 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4105 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004106 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004107 }
4108 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4109 // case is handled as `x >> static_cast<int>(y)`.
4110 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4111 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004112 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004113 }
4114 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4115 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004116 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004117 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004118
4119 DECLARE_INSTRUCTION(Shr);
4120
4121 private:
4122 DISALLOW_COPY_AND_ASSIGN(HShr);
4123};
4124
4125class HUShr : public HBinaryOperation {
4126 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004127 HUShr(Primitive::Type result_type,
4128 HInstruction* left,
4129 HInstruction* right,
4130 uint32_t dex_pc = kNoDexPc)
4131 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004132
Roland Levillain9867bc72015-08-05 10:21:34 +01004133 template <typename T, typename U, typename V>
4134 T Compute(T x, U y, V max_shift_value) const {
4135 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4136 "V is not the unsigned integer type corresponding to T");
4137 V ux = static_cast<V>(x);
4138 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004139 }
4140
Roland Levillain9867bc72015-08-05 10:21:34 +01004141 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4142 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004143 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004144 }
4145 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4146 // case is handled as `x >>> static_cast<int>(y)`.
4147 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4148 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004149 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004150 }
4151 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4152 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004153 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004154 }
4155
4156 DECLARE_INSTRUCTION(UShr);
4157
4158 private:
4159 DISALLOW_COPY_AND_ASSIGN(HUShr);
4160};
4161
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004162class HAnd : public HBinaryOperation {
4163 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004164 HAnd(Primitive::Type result_type,
4165 HInstruction* left,
4166 HInstruction* right,
4167 uint32_t dex_pc = kNoDexPc)
4168 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004169
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004170 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004171
Roland Levillain9867bc72015-08-05 10:21:34 +01004172 template <typename T, typename U>
4173 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4174
4175 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004176 return GetBlock()->GetGraph()->GetIntConstant(
4177 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004178 }
4179 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004180 return GetBlock()->GetGraph()->GetLongConstant(
4181 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004182 }
4183 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004184 return GetBlock()->GetGraph()->GetLongConstant(
4185 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004186 }
4187 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004188 return GetBlock()->GetGraph()->GetLongConstant(
4189 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004190 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004191
4192 DECLARE_INSTRUCTION(And);
4193
4194 private:
4195 DISALLOW_COPY_AND_ASSIGN(HAnd);
4196};
4197
4198class HOr : public HBinaryOperation {
4199 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004200 HOr(Primitive::Type result_type,
4201 HInstruction* left,
4202 HInstruction* right,
4203 uint32_t dex_pc = kNoDexPc)
4204 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004205
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004206 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004207
Roland Levillain9867bc72015-08-05 10:21:34 +01004208 template <typename T, typename U>
4209 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4210
4211 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004212 return GetBlock()->GetGraph()->GetIntConstant(
4213 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004214 }
4215 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004216 return GetBlock()->GetGraph()->GetLongConstant(
4217 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004218 }
4219 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004220 return GetBlock()->GetGraph()->GetLongConstant(
4221 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004222 }
4223 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004224 return GetBlock()->GetGraph()->GetLongConstant(
4225 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004226 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004227
4228 DECLARE_INSTRUCTION(Or);
4229
4230 private:
4231 DISALLOW_COPY_AND_ASSIGN(HOr);
4232};
4233
4234class HXor : public HBinaryOperation {
4235 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004236 HXor(Primitive::Type result_type,
4237 HInstruction* left,
4238 HInstruction* right,
4239 uint32_t dex_pc = kNoDexPc)
4240 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004241
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004242 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004243
Roland Levillain9867bc72015-08-05 10:21:34 +01004244 template <typename T, typename U>
4245 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4246
4247 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004248 return GetBlock()->GetGraph()->GetIntConstant(
4249 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004250 }
4251 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004252 return GetBlock()->GetGraph()->GetLongConstant(
4253 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004254 }
4255 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004256 return GetBlock()->GetGraph()->GetLongConstant(
4257 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004258 }
4259 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004260 return GetBlock()->GetGraph()->GetLongConstant(
4261 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004262 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004263
4264 DECLARE_INSTRUCTION(Xor);
4265
4266 private:
4267 DISALLOW_COPY_AND_ASSIGN(HXor);
4268};
4269
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004270class HRor : public HBinaryOperation {
4271 public:
4272 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4273 : HBinaryOperation(result_type, value, distance) {}
4274
4275 template <typename T, typename U, typename V>
4276 T Compute(T x, U y, V max_shift_value) const {
4277 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4278 "V is not the unsigned integer type corresponding to T");
4279 V ux = static_cast<V>(x);
4280 if ((y & max_shift_value) == 0) {
4281 return static_cast<T>(ux);
4282 } else {
4283 const V reg_bits = sizeof(T) * 8;
4284 return static_cast<T>(ux >> (y & max_shift_value)) |
4285 (x << (reg_bits - (y & max_shift_value)));
4286 }
4287 }
4288
4289 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4290 return GetBlock()->GetGraph()->GetIntConstant(
4291 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4292 }
4293 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4294 return GetBlock()->GetGraph()->GetLongConstant(
4295 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4296 }
4297 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4298 return GetBlock()->GetGraph()->GetLongConstant(
4299 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4300 }
4301
4302 DECLARE_INSTRUCTION(Ror);
4303
4304 private:
4305 DISALLOW_COPY_AND_ASSIGN(HRor);
4306};
4307
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004308// The value of a parameter in this method. Its location depends on
4309// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004310class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004311 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004312 HParameterValue(const DexFile& dex_file,
4313 uint16_t type_index,
4314 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004315 Primitive::Type parameter_type,
4316 bool is_this = false)
4317 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004318 dex_file_(dex_file),
4319 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004320 index_(index),
4321 is_this_(is_this),
4322 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004323
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004324 const DexFile& GetDexFile() const { return dex_file_; }
4325 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004326 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004327 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004328
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004329 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4330 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004331
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004332 DECLARE_INSTRUCTION(ParameterValue);
4333
4334 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004335 const DexFile& dex_file_;
4336 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004337 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004338 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004339 const uint8_t index_;
4340
Calin Juravle10e244f2015-01-26 18:54:32 +00004341 // Whether or not the parameter value corresponds to 'this' argument.
4342 const bool is_this_;
4343
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004344 bool can_be_null_;
4345
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004346 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4347};
4348
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004349class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004350 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004351 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4352 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004353
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004354 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004355 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004356 return true;
4357 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004358
Roland Levillain9867bc72015-08-05 10:21:34 +01004359 template <typename T> T Compute(T x) const { return ~x; }
4360
4361 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004362 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004363 }
4364 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004365 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004366 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004367
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004368 DECLARE_INSTRUCTION(Not);
4369
4370 private:
4371 DISALLOW_COPY_AND_ASSIGN(HNot);
4372};
4373
David Brazdil66d126e2015-04-03 16:02:44 +01004374class HBooleanNot : public HUnaryOperation {
4375 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004376 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4377 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004378
4379 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004380 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004381 return true;
4382 }
4383
Roland Levillain9867bc72015-08-05 10:21:34 +01004384 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004385 DCHECK(IsUint<1>(x));
4386 return !x;
4387 }
4388
Roland Levillain9867bc72015-08-05 10:21:34 +01004389 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004390 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004391 }
4392 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4393 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004394 UNREACHABLE();
4395 }
4396
4397 DECLARE_INSTRUCTION(BooleanNot);
4398
4399 private:
4400 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4401};
4402
Roland Levillaindff1f282014-11-05 14:15:05 +00004403class HTypeConversion : public HExpression<1> {
4404 public:
4405 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004406 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004407 : HExpression(result_type,
4408 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4409 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004410 SetRawInputAt(0, input);
4411 DCHECK_NE(input->GetType(), result_type);
4412 }
4413
4414 HInstruction* GetInput() const { return InputAt(0); }
4415 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4416 Primitive::Type GetResultType() const { return GetType(); }
4417
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004418 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004419 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004420
Roland Levillaindff1f282014-11-05 14:15:05 +00004421 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004422 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004423
Mark Mendelle82549b2015-05-06 10:55:34 -04004424 // Try to statically evaluate the conversion and return a HConstant
4425 // containing the result. If the input cannot be converted, return nullptr.
4426 HConstant* TryStaticEvaluation() const;
4427
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004428 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4429 Primitive::Type result_type) {
4430 // Some architectures may not require the 'GC' side effects, but at this point
4431 // in the compilation process we do not know what architecture we will
4432 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004433 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4434 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004435 return SideEffects::CanTriggerGC();
4436 }
4437 return SideEffects::None();
4438 }
4439
Roland Levillaindff1f282014-11-05 14:15:05 +00004440 DECLARE_INSTRUCTION(TypeConversion);
4441
4442 private:
4443 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4444};
4445
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004446static constexpr uint32_t kNoRegNumber = -1;
4447
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004448class HPhi : public HInstruction {
4449 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004450 HPhi(ArenaAllocator* arena,
4451 uint32_t reg_number,
4452 size_t number_of_inputs,
4453 Primitive::Type type,
4454 uint32_t dex_pc = kNoDexPc)
4455 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004456 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004457 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004458 type_(ToPhiType(type)),
4459 // Phis are constructed live and marked dead if conflicting or unused.
4460 // Individual steps of SsaBuilder should assume that if a phi has been
4461 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4462 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004463 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004464 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004465 }
4466
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004467 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4468 static Primitive::Type ToPhiType(Primitive::Type type) {
4469 switch (type) {
4470 case Primitive::kPrimBoolean:
4471 case Primitive::kPrimByte:
4472 case Primitive::kPrimShort:
4473 case Primitive::kPrimChar:
4474 return Primitive::kPrimInt;
4475 default:
4476 return type;
4477 }
4478 }
4479
David Brazdilffee3d32015-07-06 11:48:53 +01004480 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4481
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004482 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004483
4484 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004485 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004486
Calin Juravle10e244f2015-01-26 18:54:32 +00004487 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil4833f5a2015-12-16 10:37:39 +00004488 void SetType(Primitive::Type new_type) {
4489 // Make sure that only valid type changes occur. The following are allowed:
4490 // (1) int -> float/ref (primitive type propagation),
4491 // (2) long -> double (primitive type propagation).
4492 DCHECK(type_ == new_type ||
4493 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4494 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4495 (type_ == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4496 type_ = new_type;
4497 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004498
Calin Juravle10e244f2015-01-26 18:54:32 +00004499 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4500 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4501
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004502 uint32_t GetRegNumber() const { return reg_number_; }
4503
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004504 void SetDead() { is_live_ = false; }
4505 void SetLive() { is_live_ = true; }
4506 bool IsDead() const { return !is_live_; }
4507 bool IsLive() const { return is_live_; }
4508
David Brazdil77a48ae2015-09-15 12:34:04 +00004509 bool IsVRegEquivalentOf(HInstruction* other) const {
4510 return other != nullptr
4511 && other->IsPhi()
4512 && other->AsPhi()->GetBlock() == GetBlock()
4513 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4514 }
4515
Calin Juravlea4f88312015-04-16 12:57:19 +01004516 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4517 // An equivalent phi is a phi having the same dex register and type.
4518 // It assumes that phis with the same dex register are adjacent.
4519 HPhi* GetNextEquivalentPhiWithSameType() {
4520 HInstruction* next = GetNext();
4521 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4522 if (next->GetType() == GetType()) {
4523 return next->AsPhi();
4524 }
4525 next = next->GetNext();
4526 }
4527 return nullptr;
4528 }
4529
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004530 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004531
David Brazdil1abb4192015-02-17 18:33:36 +00004532 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004533 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004534 return inputs_[index];
4535 }
David Brazdil1abb4192015-02-17 18:33:36 +00004536
4537 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004538 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004539 }
4540
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004541 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004542 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004543 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004544 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004545 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004546 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004547
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004548 DISALLOW_COPY_AND_ASSIGN(HPhi);
4549};
4550
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004551class HNullCheck : public HExpression<1> {
4552 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004553 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4554 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004555 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004556 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004557 SetRawInputAt(0, value);
4558 }
4559
Calin Juravle10e244f2015-01-26 18:54:32 +00004560 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004561 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004562 return true;
4563 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004564
Calin Juravle10e244f2015-01-26 18:54:32 +00004565 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004566
Calin Juravle10e244f2015-01-26 18:54:32 +00004567 bool CanThrow() const OVERRIDE { return true; }
4568
4569 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004570
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004571
4572 DECLARE_INSTRUCTION(NullCheck);
4573
4574 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004575 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4576};
4577
4578class FieldInfo : public ValueObject {
4579 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004580 FieldInfo(MemberOffset field_offset,
4581 Primitive::Type field_type,
4582 bool is_volatile,
4583 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004584 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004585 const DexFile& dex_file,
4586 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004587 : field_offset_(field_offset),
4588 field_type_(field_type),
4589 is_volatile_(is_volatile),
4590 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004591 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004592 dex_file_(dex_file),
4593 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004594
4595 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004596 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004597 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004598 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004599 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004600 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004601 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004602
4603 private:
4604 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004605 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004606 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004607 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004608 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004609 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004610 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004611};
4612
4613class HInstanceFieldGet : public HExpression<1> {
4614 public:
4615 HInstanceFieldGet(HInstruction* value,
4616 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004617 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004618 bool is_volatile,
4619 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004620 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004621 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004622 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004623 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004624 : HExpression(field_type,
4625 SideEffects::FieldReadOfType(field_type, is_volatile),
4626 dex_pc),
4627 field_info_(field_offset,
4628 field_type,
4629 is_volatile,
4630 field_idx,
4631 declaring_class_def_index,
4632 dex_file,
4633 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004634 SetRawInputAt(0, value);
4635 }
4636
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004637 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004638
4639 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4640 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4641 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004642 }
4643
Calin Juravle641547a2015-04-21 22:08:51 +01004644 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4645 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004646 }
4647
4648 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004649 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4650 }
4651
Calin Juravle52c48962014-12-16 17:02:57 +00004652 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004653 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004654 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004655 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004656
4657 DECLARE_INSTRUCTION(InstanceFieldGet);
4658
4659 private:
4660 const FieldInfo field_info_;
4661
4662 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4663};
4664
4665class HInstanceFieldSet : public HTemplateInstruction<2> {
4666 public:
4667 HInstanceFieldSet(HInstruction* object,
4668 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004669 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004670 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004671 bool is_volatile,
4672 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004673 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004674 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004675 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004676 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004677 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4678 dex_pc),
4679 field_info_(field_offset,
4680 field_type,
4681 is_volatile,
4682 field_idx,
4683 declaring_class_def_index,
4684 dex_file,
4685 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004686 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004687 SetRawInputAt(0, object);
4688 SetRawInputAt(1, value);
4689 }
4690
Calin Juravle641547a2015-04-21 22:08:51 +01004691 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4692 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004693 }
4694
Calin Juravle52c48962014-12-16 17:02:57 +00004695 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004696 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004697 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004698 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004699 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004700 bool GetValueCanBeNull() const { return value_can_be_null_; }
4701 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004702
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004703 DECLARE_INSTRUCTION(InstanceFieldSet);
4704
4705 private:
4706 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004707 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004708
4709 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4710};
4711
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004712class HArrayGet : public HExpression<2> {
4713 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004714 HArrayGet(HInstruction* array,
4715 HInstruction* index,
4716 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004717 uint32_t dex_pc,
4718 SideEffects additional_side_effects = SideEffects::None())
4719 : HExpression(type,
4720 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004721 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004722 SetRawInputAt(0, array);
4723 SetRawInputAt(1, index);
4724 }
4725
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004726 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004727 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004728 return true;
4729 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004730 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004731 // TODO: We can be smarter here.
4732 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4733 // which generates the implicit null check. There are cases when these can be removed
4734 // to produce better code. If we ever add optimizations to do so we should allow an
4735 // implicit check here (as long as the address falls in the first page).
4736 return false;
4737 }
4738
David Brazdil4833f5a2015-12-16 10:37:39 +00004739 bool IsEquivalentOf(HArrayGet* other) const {
4740 bool result = (GetDexPc() == other->GetDexPc());
4741 if (kIsDebugBuild && result) {
4742 DCHECK_EQ(GetBlock(), other->GetBlock());
4743 DCHECK_EQ(GetArray(), other->GetArray());
4744 DCHECK_EQ(GetIndex(), other->GetIndex());
4745 if (Primitive::IsIntOrLongType(GetType())) {
4746 DCHECK(Primitive::IsFloatingPointType(other->GetType()));
4747 } else {
4748 DCHECK(Primitive::IsFloatingPointType(GetType()));
4749 DCHECK(Primitive::IsIntOrLongType(other->GetType()));
4750 }
4751 }
4752 return result;
4753 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004754
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004755 HInstruction* GetArray() const { return InputAt(0); }
4756 HInstruction* GetIndex() const { return InputAt(1); }
4757
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004758 DECLARE_INSTRUCTION(ArrayGet);
4759
4760 private:
4761 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4762};
4763
4764class HArraySet : public HTemplateInstruction<3> {
4765 public:
4766 HArraySet(HInstruction* array,
4767 HInstruction* index,
4768 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004769 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004770 uint32_t dex_pc,
4771 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004772 : HTemplateInstruction(
4773 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004774 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4775 additional_side_effects),
4776 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004777 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004778 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004779 value_can_be_null_(true),
4780 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004781 SetRawInputAt(0, array);
4782 SetRawInputAt(1, index);
4783 SetRawInputAt(2, value);
4784 }
4785
Calin Juravle77520bc2015-01-12 18:45:46 +00004786 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004787 // We currently always call a runtime method to catch array store
4788 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004789 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004790 }
4791
Mingyao Yang81014cb2015-06-02 03:16:27 -07004792 // Can throw ArrayStoreException.
4793 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4794
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004795 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004796 // TODO: Same as for ArrayGet.
4797 return false;
4798 }
4799
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004800 void ClearNeedsTypeCheck() {
4801 needs_type_check_ = false;
4802 }
4803
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004804 void ClearValueCanBeNull() {
4805 value_can_be_null_ = false;
4806 }
4807
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004808 void SetStaticTypeOfArrayIsObjectArray() {
4809 static_type_of_array_is_object_array_ = true;
4810 }
4811
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004812 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004813 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004814 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004815
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004816 HInstruction* GetArray() const { return InputAt(0); }
4817 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004818 HInstruction* GetValue() const { return InputAt(2); }
4819
4820 Primitive::Type GetComponentType() const {
4821 // The Dex format does not type floating point index operations. Since the
4822 // `expected_component_type_` is set during building and can therefore not
4823 // be correct, we also check what is the value type. If it is a floating
4824 // point type, we must use that type.
4825 Primitive::Type value_type = GetValue()->GetType();
4826 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4827 ? value_type
4828 : expected_component_type_;
4829 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004830
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004831 Primitive::Type GetRawExpectedComponentType() const {
4832 return expected_component_type_;
4833 }
4834
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004835 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4836 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4837 }
4838
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004839 DECLARE_INSTRUCTION(ArraySet);
4840
4841 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004842 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004843 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004844 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004845 // Cached information for the reference_type_info_ so that codegen
4846 // does not need to inspect the static type.
4847 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004848
4849 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4850};
4851
4852class HArrayLength : public HExpression<1> {
4853 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004854 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004855 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004856 // Note that arrays do not change length, so the instruction does not
4857 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004858 SetRawInputAt(0, array);
4859 }
4860
Calin Juravle77520bc2015-01-12 18:45:46 +00004861 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004862 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004863 return true;
4864 }
Calin Juravle641547a2015-04-21 22:08:51 +01004865 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4866 return obj == InputAt(0);
4867 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004868
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004869 DECLARE_INSTRUCTION(ArrayLength);
4870
4871 private:
4872 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4873};
4874
4875class HBoundsCheck : public HExpression<2> {
4876 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004877 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
4878 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004879 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004880 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004881 DCHECK(index->GetType() == Primitive::kPrimInt);
4882 SetRawInputAt(0, index);
4883 SetRawInputAt(1, length);
4884 }
4885
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004886 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004887 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004888 return true;
4889 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004890
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004891 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004892
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004893 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004894
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004895 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004896
4897 DECLARE_INSTRUCTION(BoundsCheck);
4898
4899 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004900 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4901};
4902
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004903/**
4904 * Some DEX instructions are folded into multiple HInstructions that need
4905 * to stay live until the last HInstruction. This class
4906 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004907 * HInstruction stays live. `index` represents the stack location index of the
4908 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004909 */
4910class HTemporary : public HTemplateInstruction<0> {
4911 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004912 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4913 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004914
4915 size_t GetIndex() const { return index_; }
4916
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004917 Primitive::Type GetType() const OVERRIDE {
4918 // The previous instruction is the one that will be stored in the temporary location.
4919 DCHECK(GetPrevious() != nullptr);
4920 return GetPrevious()->GetType();
4921 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004922
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004923 DECLARE_INSTRUCTION(Temporary);
4924
4925 private:
4926 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004927 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4928};
4929
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004930class HSuspendCheck : public HTemplateInstruction<0> {
4931 public:
4932 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004933 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004934
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004935 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004936 return true;
4937 }
4938
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004939 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4940 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004941
4942 DECLARE_INSTRUCTION(SuspendCheck);
4943
4944 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004945 // Only used for code generation, in order to share the same slow path between back edges
4946 // of a same loop.
4947 SlowPathCode* slow_path_;
4948
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004949 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4950};
4951
David Srbecky0cf44932015-12-09 14:09:59 +00004952// Pseudo-instruction which provides the native debugger with mapping information.
4953// It ensures that we can generate line number and local variables at this point.
4954class HNativeDebugInfo : public HTemplateInstruction<0> {
4955 public:
4956 explicit HNativeDebugInfo(uint32_t dex_pc)
4957 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
4958
4959 bool NeedsEnvironment() const OVERRIDE {
4960 return true;
4961 }
4962
4963 DECLARE_INSTRUCTION(NativeDebugInfo);
4964
4965 private:
4966 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
4967};
4968
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004969/**
4970 * Instruction to load a Class object.
4971 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004972class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004973 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004974 HLoadClass(HCurrentMethod* current_method,
4975 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004976 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004977 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004978 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004979 bool needs_access_check,
4980 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004981 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004982 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004983 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004984 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004985 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004986 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004987 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00004988 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004989 // Referrers class should not need access check. We never inline unverified
4990 // methods so we can't possibly end up in this situation.
4991 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004992 SetRawInputAt(0, current_method);
4993 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004994
4995 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004996
4997 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004998 // Note that we don't need to test for generate_clinit_check_.
4999 // Whether or not we need to generate the clinit check is processed in
5000 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005001 return other->AsLoadClass()->type_index_ == type_index_ &&
5002 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005003 }
5004
5005 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5006
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005007 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005008 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005009 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005010
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005011 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005012 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005013 }
5014
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005015 bool MustGenerateClinitCheck() const {
5016 return generate_clinit_check_;
5017 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005018
Calin Juravle0ba218d2015-05-19 18:46:01 +01005019 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005020 // The entrypoint the code generator is going to call does not do
5021 // clinit of the class.
5022 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01005023 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005024 }
5025
5026 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005027 return MustGenerateClinitCheck() ||
5028 (!is_referrers_class_ && !is_in_dex_cache_) ||
5029 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01005030 }
5031
5032 bool NeedsAccessCheck() const {
5033 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005034 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005035
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005036 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005037 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005038 }
5039
Calin Juravleacf735c2015-02-12 15:25:22 +00005040 ReferenceTypeInfo GetLoadedClassRTI() {
5041 return loaded_class_rti_;
5042 }
5043
5044 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5045 // Make sure we only set exact types (the loaded class should never be merged).
5046 DCHECK(rti.IsExact());
5047 loaded_class_rti_ = rti;
5048 }
5049
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005050 const DexFile& GetDexFile() { return dex_file_; }
5051
Vladimir Markodc151b22015-10-15 18:02:30 +01005052 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005053
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005054 static SideEffects SideEffectsForArchRuntimeCalls() {
5055 return SideEffects::CanTriggerGC();
5056 }
5057
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005058 bool IsInDexCache() const { return is_in_dex_cache_; }
5059
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005060 DECLARE_INSTRUCTION(LoadClass);
5061
5062 private:
5063 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005064 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005065 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005066 // Whether this instruction must generate the initialization check.
5067 // Used for code generation.
5068 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005069 const bool needs_access_check_;
5070 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005071
Calin Juravleacf735c2015-02-12 15:25:22 +00005072 ReferenceTypeInfo loaded_class_rti_;
5073
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005074 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5075};
5076
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005077class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005078 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005079 HLoadString(HCurrentMethod* current_method,
5080 uint32_t string_index,
5081 uint32_t dex_pc,
5082 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005083 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005084 string_index_(string_index),
5085 is_in_dex_cache_(is_in_dex_cache) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005086 SetRawInputAt(0, current_method);
5087 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005088
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005089 bool CanBeMoved() const OVERRIDE { return true; }
5090
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005091 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5092 return other->AsLoadString()->string_index_ == string_index_;
5093 }
5094
5095 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5096
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005097 uint32_t GetStringIndex() const { return string_index_; }
5098
5099 // TODO: Can we deopt or debug when we resolve a string?
5100 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01005101 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005102 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005103 bool IsInDexCache() const { return is_in_dex_cache_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005104
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005105 static SideEffects SideEffectsForArchRuntimeCalls() {
5106 return SideEffects::CanTriggerGC();
5107 }
5108
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005109 DECLARE_INSTRUCTION(LoadString);
5110
5111 private:
5112 const uint32_t string_index_;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005113 const bool is_in_dex_cache_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005114
5115 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5116};
5117
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005118/**
5119 * Performs an initialization check on its Class object input.
5120 */
5121class HClinitCheck : public HExpression<1> {
5122 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005123 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005124 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005125 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005126 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5127 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005128 SetRawInputAt(0, constant);
5129 }
5130
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005131 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005132 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005133 return true;
5134 }
5135
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005136 bool NeedsEnvironment() const OVERRIDE {
5137 // May call runtime to initialize the class.
5138 return true;
5139 }
5140
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005141 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005142
5143 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5144
5145 DECLARE_INSTRUCTION(ClinitCheck);
5146
5147 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005148 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5149};
5150
5151class HStaticFieldGet : public HExpression<1> {
5152 public:
5153 HStaticFieldGet(HInstruction* cls,
5154 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005155 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005156 bool is_volatile,
5157 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005158 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005159 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005160 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005161 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005162 : HExpression(field_type,
5163 SideEffects::FieldReadOfType(field_type, is_volatile),
5164 dex_pc),
5165 field_info_(field_offset,
5166 field_type,
5167 is_volatile,
5168 field_idx,
5169 declaring_class_def_index,
5170 dex_file,
5171 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005172 SetRawInputAt(0, cls);
5173 }
5174
Calin Juravle52c48962014-12-16 17:02:57 +00005175
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005176 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005177
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005178 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005179 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5180 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005181 }
5182
5183 size_t ComputeHashCode() const OVERRIDE {
5184 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5185 }
5186
Calin Juravle52c48962014-12-16 17:02:57 +00005187 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005188 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5189 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005190 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005191
5192 DECLARE_INSTRUCTION(StaticFieldGet);
5193
5194 private:
5195 const FieldInfo field_info_;
5196
5197 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5198};
5199
5200class HStaticFieldSet : public HTemplateInstruction<2> {
5201 public:
5202 HStaticFieldSet(HInstruction* cls,
5203 HInstruction* value,
5204 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005205 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005206 bool is_volatile,
5207 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005208 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005209 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005210 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005211 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005212 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5213 dex_pc),
5214 field_info_(field_offset,
5215 field_type,
5216 is_volatile,
5217 field_idx,
5218 declaring_class_def_index,
5219 dex_file,
5220 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005221 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005222 SetRawInputAt(0, cls);
5223 SetRawInputAt(1, value);
5224 }
5225
Calin Juravle52c48962014-12-16 17:02:57 +00005226 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005227 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5228 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005229 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005230
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005231 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005232 bool GetValueCanBeNull() const { return value_can_be_null_; }
5233 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005234
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005235 DECLARE_INSTRUCTION(StaticFieldSet);
5236
5237 private:
5238 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005239 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005240
5241 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5242};
5243
Calin Juravlee460d1d2015-09-29 04:52:17 +01005244class HUnresolvedInstanceFieldGet : public HExpression<1> {
5245 public:
5246 HUnresolvedInstanceFieldGet(HInstruction* obj,
5247 Primitive::Type field_type,
5248 uint32_t field_index,
5249 uint32_t dex_pc)
5250 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5251 field_index_(field_index) {
5252 SetRawInputAt(0, obj);
5253 }
5254
5255 bool NeedsEnvironment() const OVERRIDE { return true; }
5256 bool CanThrow() const OVERRIDE { return true; }
5257
5258 Primitive::Type GetFieldType() const { return GetType(); }
5259 uint32_t GetFieldIndex() const { return field_index_; }
5260
5261 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5262
5263 private:
5264 const uint32_t field_index_;
5265
5266 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5267};
5268
5269class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5270 public:
5271 HUnresolvedInstanceFieldSet(HInstruction* obj,
5272 HInstruction* value,
5273 Primitive::Type field_type,
5274 uint32_t field_index,
5275 uint32_t dex_pc)
5276 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5277 field_type_(field_type),
5278 field_index_(field_index) {
5279 DCHECK_EQ(field_type, value->GetType());
5280 SetRawInputAt(0, obj);
5281 SetRawInputAt(1, value);
5282 }
5283
5284 bool NeedsEnvironment() const OVERRIDE { return true; }
5285 bool CanThrow() const OVERRIDE { return true; }
5286
5287 Primitive::Type GetFieldType() const { return field_type_; }
5288 uint32_t GetFieldIndex() const { return field_index_; }
5289
5290 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5291
5292 private:
5293 const Primitive::Type field_type_;
5294 const uint32_t field_index_;
5295
5296 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5297};
5298
5299class HUnresolvedStaticFieldGet : public HExpression<0> {
5300 public:
5301 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5302 uint32_t field_index,
5303 uint32_t dex_pc)
5304 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5305 field_index_(field_index) {
5306 }
5307
5308 bool NeedsEnvironment() const OVERRIDE { return true; }
5309 bool CanThrow() const OVERRIDE { return true; }
5310
5311 Primitive::Type GetFieldType() const { return GetType(); }
5312 uint32_t GetFieldIndex() const { return field_index_; }
5313
5314 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5315
5316 private:
5317 const uint32_t field_index_;
5318
5319 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5320};
5321
5322class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5323 public:
5324 HUnresolvedStaticFieldSet(HInstruction* value,
5325 Primitive::Type field_type,
5326 uint32_t field_index,
5327 uint32_t dex_pc)
5328 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5329 field_type_(field_type),
5330 field_index_(field_index) {
5331 DCHECK_EQ(field_type, value->GetType());
5332 SetRawInputAt(0, value);
5333 }
5334
5335 bool NeedsEnvironment() const OVERRIDE { return true; }
5336 bool CanThrow() const OVERRIDE { return true; }
5337
5338 Primitive::Type GetFieldType() const { return field_type_; }
5339 uint32_t GetFieldIndex() const { return field_index_; }
5340
5341 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5342
5343 private:
5344 const Primitive::Type field_type_;
5345 const uint32_t field_index_;
5346
5347 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5348};
5349
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005350// Implement the move-exception DEX instruction.
5351class HLoadException : public HExpression<0> {
5352 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005353 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5354 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005355
David Brazdilbbd733e2015-08-18 17:48:17 +01005356 bool CanBeNull() const OVERRIDE { return false; }
5357
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005358 DECLARE_INSTRUCTION(LoadException);
5359
5360 private:
5361 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5362};
5363
David Brazdilcb1c0552015-08-04 16:22:25 +01005364// Implicit part of move-exception which clears thread-local exception storage.
5365// Must not be removed because the runtime expects the TLS to get cleared.
5366class HClearException : public HTemplateInstruction<0> {
5367 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005368 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5369 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005370
5371 DECLARE_INSTRUCTION(ClearException);
5372
5373 private:
5374 DISALLOW_COPY_AND_ASSIGN(HClearException);
5375};
5376
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005377class HThrow : public HTemplateInstruction<1> {
5378 public:
5379 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005380 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005381 SetRawInputAt(0, exception);
5382 }
5383
5384 bool IsControlFlow() const OVERRIDE { return true; }
5385
5386 bool NeedsEnvironment() const OVERRIDE { return true; }
5387
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005388 bool CanThrow() const OVERRIDE { return true; }
5389
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005390
5391 DECLARE_INSTRUCTION(Throw);
5392
5393 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005394 DISALLOW_COPY_AND_ASSIGN(HThrow);
5395};
5396
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005397/**
5398 * Implementation strategies for the code generator of a HInstanceOf
5399 * or `HCheckCast`.
5400 */
5401enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005402 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005403 kExactCheck, // Can do a single class compare.
5404 kClassHierarchyCheck, // Can just walk the super class chain.
5405 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5406 kInterfaceCheck, // No optimization yet when checking against an interface.
5407 kArrayObjectCheck, // Can just check if the array is not primitive.
5408 kArrayCheck // No optimization yet when checking against a generic array.
5409};
5410
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005411class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005412 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005413 HInstanceOf(HInstruction* object,
5414 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005415 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005416 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005417 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005418 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005419 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005420 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005421 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005422 SetRawInputAt(0, object);
5423 SetRawInputAt(1, constant);
5424 }
5425
5426 bool CanBeMoved() const OVERRIDE { return true; }
5427
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005428 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005429 return true;
5430 }
5431
5432 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005433 return false;
5434 }
5435
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005436 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5437
5438 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005439
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005440 // Used only in code generation.
5441 bool MustDoNullCheck() const { return must_do_null_check_; }
5442 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5443
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005444 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5445 return (check_kind == TypeCheckKind::kExactCheck)
5446 ? SideEffects::None()
5447 // Mips currently does runtime calls for any other checks.
5448 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005449 }
5450
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005451 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005452
5453 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005454 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005455 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005456
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005457 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5458};
5459
Calin Juravleb1498f62015-02-16 13:13:29 +00005460class HBoundType : public HExpression<1> {
5461 public:
David Brazdilf5552582015-12-27 13:36:12 +00005462 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005463 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
David Brazdilf5552582015-12-27 13:36:12 +00005464 upper_bound_(ReferenceTypeInfo::CreateInvalid()),
5465 upper_can_be_null_(true),
5466 can_be_null_(true) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005467 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005468 SetRawInputAt(0, input);
5469 }
5470
David Brazdilf5552582015-12-27 13:36:12 +00005471 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005472 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5473 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
David Brazdilf5552582015-12-27 13:36:12 +00005474 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005475
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005476 void SetCanBeNull(bool can_be_null) {
5477 DCHECK(upper_can_be_null_ || !can_be_null);
5478 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005479 }
5480
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005481 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5482
Calin Juravleb1498f62015-02-16 13:13:29 +00005483 DECLARE_INSTRUCTION(BoundType);
5484
5485 private:
5486 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005487 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5488 // It is used to bound the type in cases like:
5489 // if (x instanceof ClassX) {
5490 // // uper_bound_ will be ClassX
5491 // }
David Brazdilf5552582015-12-27 13:36:12 +00005492 ReferenceTypeInfo upper_bound_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005493 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5494 // is false then can_be_null_ cannot be true).
David Brazdilf5552582015-12-27 13:36:12 +00005495 bool upper_can_be_null_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005496 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005497
5498 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5499};
5500
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005501class HCheckCast : public HTemplateInstruction<2> {
5502 public:
5503 HCheckCast(HInstruction* object,
5504 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005505 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005506 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005507 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005508 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005509 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005510 SetRawInputAt(0, object);
5511 SetRawInputAt(1, constant);
5512 }
5513
5514 bool CanBeMoved() const OVERRIDE { return true; }
5515
5516 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5517 return true;
5518 }
5519
5520 bool NeedsEnvironment() const OVERRIDE {
5521 // Instruction may throw a CheckCastError.
5522 return true;
5523 }
5524
5525 bool CanThrow() const OVERRIDE { return true; }
5526
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005527 bool MustDoNullCheck() const { return must_do_null_check_; }
5528 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005529 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005530
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005531 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005532
5533 DECLARE_INSTRUCTION(CheckCast);
5534
5535 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005536 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005537 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005538
5539 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005540};
5541
Calin Juravle27df7582015-04-17 19:12:31 +01005542class HMemoryBarrier : public HTemplateInstruction<0> {
5543 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005544 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005545 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005546 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005547 barrier_kind_(barrier_kind) {}
5548
5549 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5550
5551 DECLARE_INSTRUCTION(MemoryBarrier);
5552
5553 private:
5554 const MemBarrierKind barrier_kind_;
5555
5556 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5557};
5558
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005559class HMonitorOperation : public HTemplateInstruction<1> {
5560 public:
5561 enum OperationKind {
5562 kEnter,
5563 kExit,
5564 };
5565
5566 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005567 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005568 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5569 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005570 SetRawInputAt(0, object);
5571 }
5572
5573 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005574 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5575
5576 bool CanThrow() const OVERRIDE {
5577 // Verifier guarantees that monitor-exit cannot throw.
5578 // This is important because it allows the HGraphBuilder to remove
5579 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5580 return IsEnter();
5581 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005582
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005583
5584 bool IsEnter() const { return kind_ == kEnter; }
5585
5586 DECLARE_INSTRUCTION(MonitorOperation);
5587
Calin Juravle52c48962014-12-16 17:02:57 +00005588 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005589 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005590
5591 private:
5592 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5593};
5594
Vladimir Markof9f64412015-09-02 14:05:49 +01005595class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005596 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005597 MoveOperands(Location source,
5598 Location destination,
5599 Primitive::Type type,
5600 HInstruction* instruction)
5601 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005602
5603 Location GetSource() const { return source_; }
5604 Location GetDestination() const { return destination_; }
5605
5606 void SetSource(Location value) { source_ = value; }
5607 void SetDestination(Location value) { destination_ = value; }
5608
5609 // The parallel move resolver marks moves as "in-progress" by clearing the
5610 // destination (but not the source).
5611 Location MarkPending() {
5612 DCHECK(!IsPending());
5613 Location dest = destination_;
5614 destination_ = Location::NoLocation();
5615 return dest;
5616 }
5617
5618 void ClearPending(Location dest) {
5619 DCHECK(IsPending());
5620 destination_ = dest;
5621 }
5622
5623 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00005624 DCHECK(source_.IsValid() || destination_.IsInvalid());
5625 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005626 }
5627
5628 // True if this blocks a move from the given location.
5629 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005630 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005631 }
5632
5633 // A move is redundant if it's been eliminated, if its source and
5634 // destination are the same, or if its destination is unneeded.
5635 bool IsRedundant() const {
5636 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5637 }
5638
5639 // We clear both operands to indicate move that's been eliminated.
5640 void Eliminate() {
5641 source_ = destination_ = Location::NoLocation();
5642 }
5643
5644 bool IsEliminated() const {
5645 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5646 return source_.IsInvalid();
5647 }
5648
Alexey Frunze4dda3372015-06-01 18:31:49 -07005649 Primitive::Type GetType() const { return type_; }
5650
Nicolas Geoffray90218252015-04-15 11:56:51 +01005651 bool Is64BitMove() const {
5652 return Primitive::Is64BitType(type_);
5653 }
5654
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005655 HInstruction* GetInstruction() const { return instruction_; }
5656
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005657 private:
5658 Location source_;
5659 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005660 // The type this move is for.
5661 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005662 // The instruction this move is assocatied with. Null when this move is
5663 // for moving an input in the expected locations of user (including a phi user).
5664 // This is only used in debug mode, to ensure we do not connect interval siblings
5665 // in the same parallel move.
5666 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005667};
5668
Roland Levillainc9285912015-12-18 10:38:42 +00005669std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
5670
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005671static constexpr size_t kDefaultNumberOfMoves = 4;
5672
5673class HParallelMove : public HTemplateInstruction<0> {
5674 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005675 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005676 : HTemplateInstruction(SideEffects::None(), dex_pc),
5677 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5678 moves_.reserve(kDefaultNumberOfMoves);
5679 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005680
Nicolas Geoffray90218252015-04-15 11:56:51 +01005681 void AddMove(Location source,
5682 Location destination,
5683 Primitive::Type type,
5684 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005685 DCHECK(source.IsValid());
5686 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005687 if (kIsDebugBuild) {
5688 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005689 for (const MoveOperands& move : moves_) {
5690 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005691 // Special case the situation where the move is for the spill slot
5692 // of the instruction.
5693 if ((GetPrevious() == instruction)
5694 || ((GetPrevious() == nullptr)
5695 && instruction->IsPhi()
5696 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005697 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005698 << "Doing parallel moves for the same instruction.";
5699 } else {
5700 DCHECK(false) << "Doing parallel moves for the same instruction.";
5701 }
5702 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005703 }
5704 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005705 for (const MoveOperands& move : moves_) {
5706 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005707 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005708 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005709 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005710 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005711 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005712 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005713 }
5714
Vladimir Marko225b6462015-09-28 12:17:40 +01005715 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005716 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005717 }
5718
Vladimir Marko225b6462015-09-28 12:17:40 +01005719 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005720
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005721 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005722
5723 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005724 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005725
5726 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5727};
5728
Mark Mendell0616ae02015-04-17 12:49:27 -04005729} // namespace art
5730
Ilmir Usmanovdebeb982015-12-11 11:39:44 +03005731#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
5732#include "nodes_shared.h"
5733#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00005734#ifdef ART_ENABLE_CODEGEN_arm
5735#include "nodes_arm.h"
5736#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005737#ifdef ART_ENABLE_CODEGEN_arm64
5738#include "nodes_arm64.h"
5739#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005740#ifdef ART_ENABLE_CODEGEN_x86
5741#include "nodes_x86.h"
5742#endif
5743
5744namespace art {
5745
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005746class HGraphVisitor : public ValueObject {
5747 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005748 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5749 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005750
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005751 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005752 virtual void VisitBasicBlock(HBasicBlock* block);
5753
Roland Levillain633021e2014-10-01 14:12:25 +01005754 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005755 void VisitInsertionOrder();
5756
Roland Levillain633021e2014-10-01 14:12:25 +01005757 // Visit the graph following dominator tree reverse post-order.
5758 void VisitReversePostOrder();
5759
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005760 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005761
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005762 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005763#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005764 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5765
5766 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5767
5768#undef DECLARE_VISIT_INSTRUCTION
5769
5770 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005771 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005772
5773 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5774};
5775
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005776class HGraphDelegateVisitor : public HGraphVisitor {
5777 public:
5778 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5779 virtual ~HGraphDelegateVisitor() {}
5780
5781 // Visit functions that delegate to to super class.
5782#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005783 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005784
5785 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5786
5787#undef DECLARE_VISIT_INSTRUCTION
5788
5789 private:
5790 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5791};
5792
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005793class HInsertionOrderIterator : public ValueObject {
5794 public:
5795 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5796
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005797 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005798 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005799 void Advance() { ++index_; }
5800
5801 private:
5802 const HGraph& graph_;
5803 size_t index_;
5804
5805 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5806};
5807
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005808class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005809 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005810 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5811 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005812 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005813 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005814
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005815 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5816 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005817 void Advance() { ++index_; }
5818
5819 private:
5820 const HGraph& graph_;
5821 size_t index_;
5822
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005823 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005824};
5825
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005826class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005827 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005828 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005829 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005830 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005831 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005832 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005833
5834 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005835 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005836 void Advance() { --index_; }
5837
5838 private:
5839 const HGraph& graph_;
5840 size_t index_;
5841
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005842 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005843};
5844
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005845class HLinearPostOrderIterator : public ValueObject {
5846 public:
5847 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005848 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005849
5850 bool Done() const { return index_ == 0; }
5851
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005852 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005853
5854 void Advance() {
5855 --index_;
5856 DCHECK_GE(index_, 0U);
5857 }
5858
5859 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005860 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005861 size_t index_;
5862
5863 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5864};
5865
5866class HLinearOrderIterator : public ValueObject {
5867 public:
5868 explicit HLinearOrderIterator(const HGraph& graph)
5869 : order_(graph.GetLinearOrder()), index_(0) {}
5870
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005871 bool Done() const { return index_ == order_.size(); }
5872 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005873 void Advance() { ++index_; }
5874
5875 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005876 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005877 size_t index_;
5878
5879 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5880};
5881
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005882// Iterator over the blocks that art part of the loop. Includes blocks part
5883// of an inner loop. The order in which the blocks are iterated is on their
5884// block id.
5885class HBlocksInLoopIterator : public ValueObject {
5886 public:
5887 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5888 : blocks_in_loop_(info.GetBlocks()),
5889 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5890 index_(0) {
5891 if (!blocks_in_loop_.IsBitSet(index_)) {
5892 Advance();
5893 }
5894 }
5895
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005896 bool Done() const { return index_ == blocks_.size(); }
5897 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005898 void Advance() {
5899 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005900 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005901 if (blocks_in_loop_.IsBitSet(index_)) {
5902 break;
5903 }
5904 }
5905 }
5906
5907 private:
5908 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005909 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005910 size_t index_;
5911
5912 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5913};
5914
Mingyao Yang3584bce2015-05-19 16:01:59 -07005915// Iterator over the blocks that art part of the loop. Includes blocks part
5916// of an inner loop. The order in which the blocks are iterated is reverse
5917// post order.
5918class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5919 public:
5920 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5921 : blocks_in_loop_(info.GetBlocks()),
5922 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5923 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005924 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005925 Advance();
5926 }
5927 }
5928
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005929 bool Done() const { return index_ == blocks_.size(); }
5930 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005931 void Advance() {
5932 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005933 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5934 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005935 break;
5936 }
5937 }
5938 }
5939
5940 private:
5941 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005942 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005943 size_t index_;
5944
5945 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5946};
5947
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005948inline int64_t Int64FromConstant(HConstant* constant) {
5949 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5950 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5951 : constant->AsLongConstant()->GetValue();
5952}
5953
Vladimir Marko58155012015-08-19 12:49:41 +00005954inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5955 // For the purposes of the compiler, the dex files must actually be the same object
5956 // if we want to safely treat them as the same. This is especially important for JIT
5957 // as custom class loaders can open the same underlying file (or memory) multiple
5958 // times and provide different class resolution but no two class loaders should ever
5959 // use the same DexFile object - doing so is an unsupported hack that can lead to
5960 // all sorts of weird failures.
5961 return &lhs == &rhs;
5962}
5963
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005964#define INSTRUCTION_TYPE_CHECK(type, super) \
5965 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
5966 inline const H##type* HInstruction::As##type() const { \
5967 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
5968 } \
5969 inline H##type* HInstruction::As##type() { \
5970 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
5971 }
5972
5973 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
5974#undef INSTRUCTION_TYPE_CHECK
5975
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005976} // namespace art
5977
5978#endif // ART_COMPILER_OPTIMIZING_NODES_H_