blob: b8083477cfeee1b6e1738aa0283914d0393bec4d [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010028#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000029#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "handle.h"
31#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "primitive.h"
David Brazdild26a4112015-11-10 11:07:31 +000038#include "utils/array_ref.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000039
40namespace art {
41
David Brazdil1abb4192015-02-17 18:33:36 +000042class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010046class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000048class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000051class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000052class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000054class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010056class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010057class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000059class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010060class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062
Mathieu Chartier736b5602015-09-02 14:54:11 -070063namespace mirror {
64class DexCache;
65} // namespace mirror
66
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067static const int kDefaultNumberOfBlocks = 8;
68static const int kDefaultNumberOfSuccessors = 2;
69static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010070static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010071static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000072static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Calin Juravle9aec02f2014-11-18 23:06:35 +000074static constexpr uint32_t kMaxIntShiftValue = 0x1f;
75static constexpr uint64_t kMaxLongShiftValue = 0x3f;
76
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010077static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070078static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010079
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010080static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
81
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060082static constexpr uint32_t kNoDexPc = -1;
83
Dave Allison20dfc792014-06-16 20:44:29 -070084enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070085 // All types.
86 kCondEQ, // ==
87 kCondNE, // !=
88 // Signed integers and floating-point numbers.
89 kCondLT, // <
90 kCondLE, // <=
91 kCondGT, // >
92 kCondGE, // >=
93 // Unsigned integers.
94 kCondB, // <
95 kCondBE, // <=
96 kCondA, // >
97 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -070098};
99
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000100enum GraphAnalysisResult {
101 kAnalysisFailThrowCatchLoop,
102 kAnalysisFailAmbiguousArrayOp,
103 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000104};
105
Vladimir Markof9f64412015-09-02 14:05:49 +0100106class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100107 public:
108 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
109
110 void AddInstruction(HInstruction* instruction);
111 void RemoveInstruction(HInstruction* instruction);
112
David Brazdilc3d743f2015-04-22 13:40:50 +0100113 // Insert `instruction` before/after an existing instruction `cursor`.
114 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
115 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
116
Roland Levillain6b469232014-09-25 10:10:38 +0100117 // Return true if this list contains `instruction`.
118 bool Contains(HInstruction* instruction) const;
119
Roland Levillainccc07a92014-09-16 14:48:16 +0100120 // Return true if `instruction1` is found before `instruction2` in
121 // this instruction list and false otherwise. Abort if none
122 // of these instructions is found.
123 bool FoundBefore(const HInstruction* instruction1,
124 const HInstruction* instruction2) const;
125
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000126 bool IsEmpty() const { return first_instruction_ == nullptr; }
127 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
128
129 // Update the block of all instructions to be `block`.
130 void SetBlockOfInstructions(HBasicBlock* block) const;
131
132 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
133 void Add(const HInstructionList& instruction_list);
134
David Brazdil2d7352b2015-04-20 14:52:42 +0100135 // Return the number of instructions in the list. This is an expensive operation.
136 size_t CountSize() const;
137
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100138 private:
139 HInstruction* first_instruction_;
140 HInstruction* last_instruction_;
141
142 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000143 friend class HGraph;
144 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100145 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100146 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100147
148 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
149};
150
David Brazdil4833f5a2015-12-16 10:37:39 +0000151class ReferenceTypeInfo : ValueObject {
152 public:
153 typedef Handle<mirror::Class> TypeHandle;
154
155 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
156 // The constructor will check that the type_handle is valid.
157 return ReferenceTypeInfo(type_handle, is_exact);
158 }
159
160 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
161
Vladimir Markof39745e2016-01-26 12:16:55 +0000162 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000163 return handle.GetReference() != nullptr;
164 }
165
166 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
167 return IsValidHandle(type_handle_);
168 }
169
170 bool IsExact() const { return is_exact_; }
171
172 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
173 DCHECK(IsValid());
174 return GetTypeHandle()->IsObjectClass();
175 }
176
177 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
178 DCHECK(IsValid());
179 return GetTypeHandle()->IsStringClass();
180 }
181
182 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
183 DCHECK(IsValid());
184 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
185 }
186
187 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
188 DCHECK(IsValid());
189 return GetTypeHandle()->IsInterface();
190 }
191
192 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
193 DCHECK(IsValid());
194 return GetTypeHandle()->IsArrayClass();
195 }
196
197 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
198 DCHECK(IsValid());
199 return GetTypeHandle()->IsPrimitiveArray();
200 }
201
202 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
203 DCHECK(IsValid());
204 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
205 }
206
207 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
208 DCHECK(IsValid());
209 if (!IsExact()) return false;
210 if (!IsArrayClass()) return false;
211 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
212 }
213
214 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
215 DCHECK(IsValid());
216 if (!IsExact()) return false;
217 if (!IsArrayClass()) return false;
218 if (!rti.IsArrayClass()) return false;
219 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
220 rti.GetTypeHandle()->GetComponentType());
221 }
222
223 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
224
225 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
226 DCHECK(IsValid());
227 DCHECK(rti.IsValid());
228 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
229 }
230
231 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
232 DCHECK(IsValid());
233 DCHECK(rti.IsValid());
234 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
235 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
236 }
237
238 // Returns true if the type information provide the same amount of details.
239 // Note that it does not mean that the instructions have the same actual type
240 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000241 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000242 if (!IsValid() && !rti.IsValid()) {
243 // Invalid types are equal.
244 return true;
245 }
246 if (!IsValid() || !rti.IsValid()) {
247 // One is valid, the other not.
248 return false;
249 }
250 return IsExact() == rti.IsExact()
251 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
252 }
253
254 private:
255 ReferenceTypeInfo();
256 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
257
258 // The class of the object.
259 TypeHandle type_handle_;
260 // Whether or not the type is exact or a superclass of the actual type.
261 // Whether or not we have any information about this type.
262 bool is_exact_;
263};
264
265std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
266
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000267// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100268class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000269 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100270 HGraph(ArenaAllocator* arena,
271 const DexFile& dex_file,
272 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100273 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700274 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100275 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100276 bool debuggable = false,
277 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000278 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100279 blocks_(arena->Adapter(kArenaAllocBlockList)),
280 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
281 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700282 entry_block_(nullptr),
283 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100284 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100285 number_of_vregs_(0),
286 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000287 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400288 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000289 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000290 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000291 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000292 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100293 dex_file_(dex_file),
294 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100295 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100296 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100297 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700298 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000299 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100300 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
301 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
302 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
303 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000304 cached_current_method_(nullptr),
305 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100306 blocks_.reserve(kDefaultNumberOfBlocks);
307 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000308
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000309 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100310 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
311
David Brazdil69ba7b72015-06-23 18:27:30 +0100312 bool IsInSsaForm() const { return in_ssa_form_; }
313
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000314 HBasicBlock* GetEntryBlock() const { return entry_block_; }
315 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100316 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000317
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000318 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
319 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000320
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000321 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100322
David Brazdil4833f5a2015-12-16 10:37:39 +0000323 // Try building the SSA form of this graph, with dominance computation and
324 // loop recognition. Returns a code specifying that it was successful or the
325 // reason for failure.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000326 GraphAnalysisResult TryBuildingSsa(StackHandleScopeCollection* handles);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000327
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100328 void ComputeDominanceInformation();
329 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000330 void ClearLoopInformation();
331 void FindBackEdges(ArenaBitVector* visited);
332 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100333 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100334 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000335
David Brazdil4833f5a2015-12-16 10:37:39 +0000336 // Analyze all natural loops in this graph. Returns a code specifying that it
337 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000338 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000339 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100340
David Brazdilffee3d32015-07-06 11:48:53 +0100341 // Iterate over blocks to compute try block membership. Needs reverse post
342 // order and loop information.
343 void ComputeTryBlockInformation();
344
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000345 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000346 // Returns the instruction used to replace the invoke expression or null if the
347 // invoke is for a void method.
348 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000349
Mingyao Yang3584bce2015-05-19 16:01:59 -0700350 // Need to add a couple of blocks to test if the loop body is entered and
351 // put deoptimization instructions, etc.
352 void TransformLoopHeaderForBCE(HBasicBlock* header);
353
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000354 // Removes `block` from the graph. Assumes `block` has been disconnected from
355 // other blocks and has no instructions or phis.
356 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000357
David Brazdilfc6a86a2015-06-26 10:33:45 +0000358 // Splits the edge between `block` and `successor` while preserving the
359 // indices in the predecessor/successor lists. If there are multiple edges
360 // between the blocks, the lowest indices are used.
361 // Returns the new block which is empty and has the same dex pc as `successor`.
362 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
363
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100364 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
365 void SimplifyLoop(HBasicBlock* header);
366
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000367 int32_t GetNextInstructionId() {
368 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000369 return current_instruction_id_++;
370 }
371
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000372 int32_t GetCurrentInstructionId() const {
373 return current_instruction_id_;
374 }
375
376 void SetCurrentInstructionId(int32_t id) {
377 current_instruction_id_ = id;
378 }
379
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100380 uint16_t GetMaximumNumberOfOutVRegs() const {
381 return maximum_number_of_out_vregs_;
382 }
383
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000384 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
385 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100386 }
387
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100388 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
389 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
390 }
391
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000392 void UpdateTemporariesVRegSlots(size_t slots) {
393 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100394 }
395
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000396 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100397 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000398 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100399 }
400
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100401 void SetNumberOfVRegs(uint16_t number_of_vregs) {
402 number_of_vregs_ = number_of_vregs;
403 }
404
405 uint16_t GetNumberOfVRegs() const {
406 return number_of_vregs_;
407 }
408
409 void SetNumberOfInVRegs(uint16_t value) {
410 number_of_in_vregs_ = value;
411 }
412
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100413 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100414 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100415 return number_of_vregs_ - number_of_in_vregs_;
416 }
417
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100418 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100419 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100420 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100421
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100422 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100423 return linear_order_;
424 }
425
Mark Mendell1152c922015-04-24 17:06:35 -0400426 bool HasBoundsChecks() const {
427 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800428 }
429
Mark Mendell1152c922015-04-24 17:06:35 -0400430 void SetHasBoundsChecks(bool value) {
431 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800432 }
433
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100434 bool ShouldGenerateConstructorBarrier() const {
435 return should_generate_constructor_barrier_;
436 }
437
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000438 bool IsDebuggable() const { return debuggable_; }
439
David Brazdil8d5b8b22015-03-24 10:51:52 +0000440 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000441 // already, it is created and inserted into the graph. This method is only for
442 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600443 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000444
445 // TODO: This is problematic for the consistency of reference type propagation
446 // because it can be created anytime after the pass and thus it will be left
447 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600448 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000449
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600450 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
451 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000452 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600453 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
454 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000455 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600456 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
457 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000458 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600459 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
460 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000461 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000462
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100463 HCurrentMethod* GetCurrentMethod();
464
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100465 const DexFile& GetDexFile() const {
466 return dex_file_;
467 }
468
469 uint32_t GetMethodIdx() const {
470 return method_idx_;
471 }
472
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100473 InvokeType GetInvokeType() const {
474 return invoke_type_;
475 }
476
Mark Mendellc4701932015-04-10 13:18:51 -0400477 InstructionSet GetInstructionSet() const {
478 return instruction_set_;
479 }
480
David Brazdil77a48ae2015-09-15 12:34:04 +0000481 bool HasTryCatch() const { return has_try_catch_; }
482 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100483
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000484 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
485 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
486
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100487 ArtMethod* GetArtMethod() const { return art_method_; }
488 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
489
Mark Mendellf6529172015-11-17 11:16:56 -0500490 // Returns an instruction with the opposite boolean value from 'cond'.
491 // The instruction has been inserted into the graph, either as a constant, or
492 // before cursor.
493 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
494
David Brazdil2d7352b2015-04-20 14:52:42 +0100495 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000496 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100497 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000498
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000499 template <class InstructionType, typename ValueType>
500 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600501 ArenaSafeMap<ValueType, InstructionType*>* cache,
502 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000503 // Try to find an existing constant of the given value.
504 InstructionType* constant = nullptr;
505 auto cached_constant = cache->find(value);
506 if (cached_constant != cache->end()) {
507 constant = cached_constant->second;
508 }
509
510 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100511 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000512 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600513 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000514 cache->Overwrite(value, constant);
515 InsertConstant(constant);
516 }
517 return constant;
518 }
519
David Brazdil8d5b8b22015-03-24 10:51:52 +0000520 void InsertConstant(HConstant* instruction);
521
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000522 // Cache a float constant into the graph. This method should only be
523 // called by the SsaBuilder when creating "equivalent" instructions.
524 void CacheFloatConstant(HFloatConstant* constant);
525
526 // See CacheFloatConstant comment.
527 void CacheDoubleConstant(HDoubleConstant* constant);
528
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000529 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000530
531 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100532 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000533
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100534 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100535 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000536
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100537 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100538 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100539
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000540 HBasicBlock* entry_block_;
541 HBasicBlock* exit_block_;
542
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100543 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100544 uint16_t maximum_number_of_out_vregs_;
545
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100546 // The number of virtual registers in this method. Contains the parameters.
547 uint16_t number_of_vregs_;
548
549 // The number of virtual registers used by parameters of this method.
550 uint16_t number_of_in_vregs_;
551
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000552 // Number of vreg size slots that the temporaries use (used in baseline compiler).
553 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100554
Mark Mendell1152c922015-04-24 17:06:35 -0400555 // Has bounds checks. We can totally skip BCE if it's false.
556 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800557
David Brazdil77a48ae2015-09-15 12:34:04 +0000558 // Flag whether there are any try/catch blocks in the graph. We will skip
559 // try/catch-related passes if false.
560 bool has_try_catch_;
561
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000562 // Flag whether there are any irreducible loops in the graph.
563 bool has_irreducible_loops_;
564
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000565 // Indicates whether the graph should be compiled in a way that
566 // ensures full debuggability. If false, we can apply more
567 // aggressive optimizations that may limit the level of debugging.
568 const bool debuggable_;
569
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000570 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000571 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000572
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100573 // The dex file from which the method is from.
574 const DexFile& dex_file_;
575
576 // The method index in the dex file.
577 const uint32_t method_idx_;
578
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100579 // If inlined, this encodes how the callee is being invoked.
580 const InvokeType invoke_type_;
581
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100582 // Whether the graph has been transformed to SSA form. Only used
583 // in debug mode to ensure we are not using properties only valid
584 // for non-SSA form (like the number of temporaries).
585 bool in_ssa_form_;
586
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100587 const bool should_generate_constructor_barrier_;
588
Mathieu Chartiere401d142015-04-22 13:56:20 -0700589 const InstructionSet instruction_set_;
590
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000591 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000592 HNullConstant* cached_null_constant_;
593 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000594 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000595 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000596 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000597
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100598 HCurrentMethod* cached_current_method_;
599
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100600 // The ArtMethod this graph is for. Note that for AOT, it may be null,
601 // for example for methods whose declaring class could not be resolved
602 // (such as when the superclass could not be found).
603 ArtMethod* art_method_;
604
David Brazdil4833f5a2015-12-16 10:37:39 +0000605 // Keep the RTI of inexact Object to avoid having to pass stack handle
606 // collection pointer to passes which may create NullConstant.
607 ReferenceTypeInfo inexact_object_rti_;
608
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000609 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100610 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000611 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000612 DISALLOW_COPY_AND_ASSIGN(HGraph);
613};
614
Vladimir Markof9f64412015-09-02 14:05:49 +0100615class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000616 public:
617 HLoopInformation(HBasicBlock* header, HGraph* graph)
618 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100619 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000620 irreducible_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100621 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100622 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100623 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
624 back_edges_.reserve(kDefaultNumberOfBackEdges);
625 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100626
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000627 bool IsIrreducible() const { return irreducible_; }
628
629 void Dump(std::ostream& os);
630
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100631 HBasicBlock* GetHeader() const {
632 return header_;
633 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000634
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000635 void SetHeader(HBasicBlock* block) {
636 header_ = block;
637 }
638
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100639 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
640 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
641 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
642
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000643 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100644 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000645 }
646
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100647 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100648 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100649 }
650
David Brazdil46e2a392015-03-16 17:31:52 +0000651 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100652 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100653 }
654
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000655 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100656 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000657 }
658
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100659 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100660
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100661 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100662 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100663 }
664
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100665 // Returns the lifetime position of the back edge that has the
666 // greatest lifetime position.
667 size_t GetLifetimeEnd() const;
668
669 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100670 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100671 }
672
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000673 // Finds blocks that are part of this loop.
674 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100675
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100676 // Returns whether this loop information contains `block`.
677 // Note that this loop information *must* be populated before entering this function.
678 bool Contains(const HBasicBlock& block) const;
679
680 // Returns whether this loop information is an inner loop of `other`.
681 // Note that `other` *must* be populated before entering this function.
682 bool IsIn(const HLoopInformation& other) const;
683
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800684 // Returns true if instruction is not defined within this loop.
685 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700686
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100687 const ArenaBitVector& GetBlocks() const { return blocks_; }
688
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000689 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000690 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000691
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000692 void ClearAllBlocks() {
693 blocks_.ClearAllBits();
694 }
695
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000696 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100697 // Internal recursive implementation of `Populate`.
698 void PopulateRecursive(HBasicBlock* block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000699 void PopulateIrreducibleRecursive(HBasicBlock* block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100700
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000701 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100702 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000703 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100704 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100705 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000706
707 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
708};
709
David Brazdilec16f792015-08-19 15:04:01 +0100710// Stores try/catch information for basic blocks.
711// Note that HGraph is constructed so that catch blocks cannot simultaneously
712// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100713class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100714 public:
715 // Try block information constructor.
716 explicit TryCatchInformation(const HTryBoundary& try_entry)
717 : try_entry_(&try_entry),
718 catch_dex_file_(nullptr),
719 catch_type_index_(DexFile::kDexNoIndex16) {
720 DCHECK(try_entry_ != nullptr);
721 }
722
723 // Catch block information constructor.
724 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
725 : try_entry_(nullptr),
726 catch_dex_file_(&dex_file),
727 catch_type_index_(catch_type_index) {}
728
729 bool IsTryBlock() const { return try_entry_ != nullptr; }
730
731 const HTryBoundary& GetTryEntry() const {
732 DCHECK(IsTryBlock());
733 return *try_entry_;
734 }
735
736 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
737
738 bool IsCatchAllTypeIndex() const {
739 DCHECK(IsCatchBlock());
740 return catch_type_index_ == DexFile::kDexNoIndex16;
741 }
742
743 uint16_t GetCatchTypeIndex() const {
744 DCHECK(IsCatchBlock());
745 return catch_type_index_;
746 }
747
748 const DexFile& GetCatchDexFile() const {
749 DCHECK(IsCatchBlock());
750 return *catch_dex_file_;
751 }
752
753 private:
754 // One of possibly several TryBoundary instructions entering the block's try.
755 // Only set for try blocks.
756 const HTryBoundary* try_entry_;
757
758 // Exception type information. Only set for catch blocks.
759 const DexFile* catch_dex_file_;
760 const uint16_t catch_type_index_;
761};
762
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100763static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100764static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100765
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000766// A block in a method. Contains the list of instructions represented
767// as a double linked list. Each block knows its predecessors and
768// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100769
Vladimir Markof9f64412015-09-02 14:05:49 +0100770class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000771 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600772 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000773 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000774 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
775 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000776 loop_information_(nullptr),
777 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000778 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100779 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100780 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100781 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000782 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000783 try_catch_information_(nullptr) {
784 predecessors_.reserve(kDefaultNumberOfPredecessors);
785 successors_.reserve(kDefaultNumberOfSuccessors);
786 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
787 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000788
Vladimir Marko60584552015-09-03 13:35:12 +0000789 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100790 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000791 }
792
Vladimir Marko60584552015-09-03 13:35:12 +0000793 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100794 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000795 }
796
David Brazdild26a4112015-11-10 11:07:31 +0000797 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
798 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
799
Vladimir Marko60584552015-09-03 13:35:12 +0000800 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
801 return ContainsElement(successors_, block, start_from);
802 }
803
804 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100805 return dominated_blocks_;
806 }
807
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100808 bool IsEntryBlock() const {
809 return graph_->GetEntryBlock() == this;
810 }
811
812 bool IsExitBlock() const {
813 return graph_->GetExitBlock() == this;
814 }
815
David Brazdil46e2a392015-03-16 17:31:52 +0000816 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000817 bool IsSingleTryBoundary() const;
818
819 // Returns true if this block emits nothing but a jump.
820 bool IsSingleJump() const {
821 HLoopInformation* loop_info = GetLoopInformation();
822 return (IsSingleGoto() || IsSingleTryBoundary())
823 // Back edges generate a suspend check.
824 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
825 }
David Brazdil46e2a392015-03-16 17:31:52 +0000826
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000827 void AddBackEdge(HBasicBlock* back_edge) {
828 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000829 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000830 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100831 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000832 loop_information_->AddBackEdge(back_edge);
833 }
834
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000835 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000836 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000837
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100838 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000839 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600840 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000841
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000842 HBasicBlock* GetDominator() const { return dominator_; }
843 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000844 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
845
846 void RemoveDominatedBlock(HBasicBlock* block) {
847 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100848 }
Vladimir Marko60584552015-09-03 13:35:12 +0000849
850 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
851 ReplaceElement(dominated_blocks_, existing, new_block);
852 }
853
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100854 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000855
856 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100857 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000858 }
859
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100860 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
861 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100862 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100863 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100864 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
865 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000866
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000867 HInstruction* GetFirstInstructionDisregardMoves() const;
868
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000869 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000870 successors_.push_back(block);
871 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000872 }
873
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100874 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
875 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100876 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000877 new_block->predecessors_.push_back(this);
878 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000879 }
880
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000881 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
882 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000883 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000884 new_block->successors_.push_back(this);
885 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000886 }
887
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100888 // Insert `this` between `predecessor` and `successor. This method
889 // preserves the indicies, and will update the first edge found between
890 // `predecessor` and `successor`.
891 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
892 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100893 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000894 successor->predecessors_[predecessor_index] = this;
895 predecessor->successors_[successor_index] = this;
896 successors_.push_back(successor);
897 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100898 }
899
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100900 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000901 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100902 }
903
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000904 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000905 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000906 }
907
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100908 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000909 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100910 }
911
912 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000913 predecessors_.push_back(block);
914 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100915 }
916
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100917 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000918 DCHECK_EQ(predecessors_.size(), 2u);
919 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100920 }
921
David Brazdil769c9e52015-04-27 13:54:09 +0100922 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000923 DCHECK_EQ(successors_.size(), 2u);
924 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100925 }
926
David Brazdilfc6a86a2015-06-26 10:33:45 +0000927 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000928 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100929 }
930
David Brazdilfc6a86a2015-06-26 10:33:45 +0000931 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000932 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100933 }
934
David Brazdilfc6a86a2015-06-26 10:33:45 +0000935 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000936 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100937 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000938 }
939
940 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000941 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100942 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000943 }
944
945 // Returns whether the first occurrence of `predecessor` in the list of
946 // predecessors is at index `idx`.
947 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100948 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000949 return GetPredecessorIndexOf(predecessor) == idx;
950 }
951
David Brazdild7558da2015-09-22 13:04:14 +0100952 // Create a new block between this block and its predecessors. The new block
953 // is added to the graph, all predecessor edges are relinked to it and an edge
954 // is created to `this`. Returns the new empty block. Reverse post order or
955 // loop and try/catch information are not updated.
956 HBasicBlock* CreateImmediateDominator();
957
David Brazdilfc6a86a2015-06-26 10:33:45 +0000958 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100959 // created, latter block. Note that this method will add the block to the
960 // graph, create a Goto at the end of the former block and will create an edge
961 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100962 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000963 HBasicBlock* SplitBefore(HInstruction* cursor);
964
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000965 // Split the block into two blocks just after `cursor`. Returns the newly
966 // created block. Note that this method just updates raw block information,
967 // like predecessors, successors, dominators, and instruction list. It does not
968 // update the graph, reverse post order, loop information, nor make sure the
969 // blocks are consistent (for example ending with a control flow instruction).
970 HBasicBlock* SplitAfter(HInstruction* cursor);
971
David Brazdil9bc43612015-11-05 21:25:24 +0000972 // Split catch block into two blocks after the original move-exception bytecode
973 // instruction, or at the beginning if not present. Returns the newly created,
974 // latter block, or nullptr if such block could not be created (must be dead
975 // in that case). Note that this method just updates raw block information,
976 // like predecessors, successors, dominators, and instruction list. It does not
977 // update the graph, reverse post order, loop information, nor make sure the
978 // blocks are consistent (for example ending with a control flow instruction).
979 HBasicBlock* SplitCatchBlockAfterMoveException();
980
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000981 // Merge `other` at the end of `this`. Successors and dominated blocks of
982 // `other` are changed to be successors and dominated blocks of `this`. Note
983 // that this method does not update the graph, reverse post order, loop
984 // information, nor make sure the blocks are consistent (for example ending
985 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100986 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000987
988 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
989 // of `this` are moved to `other`.
990 // Note that this method does not update the graph, reverse post order, loop
991 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000992 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000993 void ReplaceWith(HBasicBlock* other);
994
David Brazdil2d7352b2015-04-20 14:52:42 +0100995 // Merge `other` at the end of `this`. This method updates loops, reverse post
996 // order, links to predecessors, successors, dominators and deletes the block
997 // from the graph. The two blocks must be successive, i.e. `this` the only
998 // predecessor of `other` and vice versa.
999 void MergeWith(HBasicBlock* other);
1000
1001 // Disconnects `this` from all its predecessors, successors and dominator,
1002 // removes it from all loops it is included in and eventually from the graph.
1003 // The block must not dominate any other block. Predecessors and successors
1004 // are safely updated.
1005 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001006
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001007 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001008 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001009 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001010 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001011 // Replace instruction `initial` with `replacement` within this block.
1012 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1013 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001014 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001015 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001016 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001017 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1018 // instruction list. With 'ensure_safety' set to true, it verifies that the
1019 // instruction is not in use and removes it from the use lists of its inputs.
1020 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1021 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001022 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001023
1024 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001025 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001026 }
1027
Roland Levillain6b879dd2014-09-22 17:13:44 +01001028 bool IsLoopPreHeaderFirstPredecessor() const {
1029 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001030 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001031 }
1032
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001033 bool IsFirstPredecessorBackEdge() const {
1034 DCHECK(IsLoopHeader());
1035 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1036 }
1037
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001038 HLoopInformation* GetLoopInformation() const {
1039 return loop_information_;
1040 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001041
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001042 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001043 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001044 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001045 void SetInLoop(HLoopInformation* info) {
1046 if (IsLoopHeader()) {
1047 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001048 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001049 loop_information_ = info;
1050 } else if (loop_information_->Contains(*info->GetHeader())) {
1051 // Block is currently part of an outer loop. Make it part of this inner loop.
1052 // Note that a non loop header having a loop information means this loop information
1053 // has already been populated
1054 loop_information_ = info;
1055 } else {
1056 // Block is part of an inner loop. Do not update the loop information.
1057 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1058 // at this point, because this method is being called while populating `info`.
1059 }
1060 }
1061
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001062 // Raw update of the loop information.
1063 void SetLoopInformation(HLoopInformation* info) {
1064 loop_information_ = info;
1065 }
1066
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001067 bool IsInLoop() const { return loop_information_ != nullptr; }
1068
David Brazdilec16f792015-08-19 15:04:01 +01001069 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1070
1071 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1072 try_catch_information_ = try_catch_information;
1073 }
1074
1075 bool IsTryBlock() const {
1076 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1077 }
1078
1079 bool IsCatchBlock() const {
1080 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1081 }
David Brazdilffee3d32015-07-06 11:48:53 +01001082
1083 // Returns the try entry that this block's successors should have. They will
1084 // be in the same try, unless the block ends in a try boundary. In that case,
1085 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001086 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001087
David Brazdild7558da2015-09-22 13:04:14 +01001088 bool HasThrowingInstructions() const;
1089
David Brazdila4b8c212015-05-07 09:59:30 +01001090 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001091 bool Dominates(HBasicBlock* block) const;
1092
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001093 size_t GetLifetimeStart() const { return lifetime_start_; }
1094 size_t GetLifetimeEnd() const { return lifetime_end_; }
1095
1096 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1097 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1098
David Brazdil8d5b8b22015-03-24 10:51:52 +00001099 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001100 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001101 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001102 bool HasSinglePhi() const;
1103
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001104 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001105 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001106 ArenaVector<HBasicBlock*> predecessors_;
1107 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001108 HInstructionList instructions_;
1109 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001110 HLoopInformation* loop_information_;
1111 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001112 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001113 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001114 // The dex program counter of the first instruction of this block.
1115 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001116 size_t lifetime_start_;
1117 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001118 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001119
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001120 friend class HGraph;
1121 friend class HInstruction;
1122
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001123 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1124};
1125
David Brazdilb2bd1c52015-03-25 11:17:37 +00001126// Iterates over the LoopInformation of all loops which contain 'block'
1127// from the innermost to the outermost.
1128class HLoopInformationOutwardIterator : public ValueObject {
1129 public:
1130 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1131 : current_(block.GetLoopInformation()) {}
1132
1133 bool Done() const { return current_ == nullptr; }
1134
1135 void Advance() {
1136 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001137 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001138 }
1139
1140 HLoopInformation* Current() const {
1141 DCHECK(!Done());
1142 return current_;
1143 }
1144
1145 private:
1146 HLoopInformation* current_;
1147
1148 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1149};
1150
Alexandre Ramesef20f712015-06-09 10:29:30 +01001151#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001152 M(Above, Condition) \
1153 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001154 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001155 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001156 M(ArrayGet, Instruction) \
1157 M(ArrayLength, Instruction) \
1158 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001159 M(Below, Condition) \
1160 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001161 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001162 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001163 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001164 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001165 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001166 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001167 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001168 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001169 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001170 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001171 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001172 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001173 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001174 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001175 M(Exit, Instruction) \
1176 M(FloatConstant, Constant) \
1177 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001178 M(GreaterThan, Condition) \
1179 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001180 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001181 M(InstanceFieldGet, Instruction) \
1182 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001183 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001184 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001185 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001186 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001187 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001188 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001189 M(LessThan, Condition) \
1190 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001191 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001192 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001193 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001194 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001195 M(Local, Instruction) \
1196 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001197 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001198 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001199 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001200 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001201 M(Neg, UnaryOperation) \
1202 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001203 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001204 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001205 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001206 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001207 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001208 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001209 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001210 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001211 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001212 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001213 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001214 M(Return, Instruction) \
1215 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001216 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001217 M(Shl, BinaryOperation) \
1218 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001219 M(StaticFieldGet, Instruction) \
1220 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001221 M(UnresolvedInstanceFieldGet, Instruction) \
1222 M(UnresolvedInstanceFieldSet, Instruction) \
1223 M(UnresolvedStaticFieldGet, Instruction) \
1224 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001225 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001226 M(StoreLocal, Instruction) \
1227 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001228 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001229 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001230 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001231 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001232 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001233 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001234 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001235
Vladimir Markob4536b72015-11-24 13:45:23 +00001236#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001237#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001238#else
1239#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1240 M(ArmDexCacheArraysBase, Instruction)
1241#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001242
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001243#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001244#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001245#else
1246#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001247 M(Arm64DataProcWithShifterOp, Instruction) \
Nicolas Geoffray6b5afdd2016-01-22 09:31:52 +00001248 M(Arm64IntermediateAddress, Instruction) \
1249 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001250#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001251
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001252#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1253
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001254#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1255
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001256#ifndef ART_ENABLE_CODEGEN_x86
1257#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1258#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001259#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1260 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001261 M(X86LoadFromConstantTable, Instruction) \
1262 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001263#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001264
1265#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1266
1267#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1268 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1269 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1270 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001271 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001272 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001273 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1274 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1275
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001276#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1277 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001278 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001279 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001280 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001281 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001282
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001283#define FOR_EACH_INSTRUCTION(M) \
1284 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1285 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1286
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001287#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001288FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1289#undef FORWARD_DECLARATION
1290
Roland Levillainccc07a92014-09-16 14:48:16 +01001291#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001292 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001293 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001294 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001295 return other->Is##type(); \
1296 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001297 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001298
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001299#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1300 bool Is##type() const { return As##type() != nullptr; } \
1301 const H##type* As##type() const { return this; } \
1302 H##type* As##type() { return this; }
1303
David Brazdiled596192015-01-23 10:39:45 +00001304template <typename T> class HUseList;
1305
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001306template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001307class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001308 public:
David Brazdiled596192015-01-23 10:39:45 +00001309 HUseListNode* GetPrevious() const { return prev_; }
1310 HUseListNode* GetNext() const { return next_; }
1311 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001312 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001313 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001314
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001315 private:
David Brazdiled596192015-01-23 10:39:45 +00001316 HUseListNode(T user, size_t index)
1317 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1318
1319 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001320 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001321 HUseListNode<T>* prev_;
1322 HUseListNode<T>* next_;
1323
1324 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001325
1326 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1327};
1328
David Brazdiled596192015-01-23 10:39:45 +00001329template <typename T>
1330class HUseList : public ValueObject {
1331 public:
1332 HUseList() : first_(nullptr) {}
1333
1334 void Clear() {
1335 first_ = nullptr;
1336 }
1337
1338 // Adds a new entry at the beginning of the use list and returns
1339 // the newly created node.
1340 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001341 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001342 if (IsEmpty()) {
1343 first_ = new_node;
1344 } else {
1345 first_->prev_ = new_node;
1346 new_node->next_ = first_;
1347 first_ = new_node;
1348 }
1349 return new_node;
1350 }
1351
1352 HUseListNode<T>* GetFirst() const {
1353 return first_;
1354 }
1355
1356 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001357 DCHECK(node != nullptr);
1358 DCHECK(Contains(node));
1359
David Brazdiled596192015-01-23 10:39:45 +00001360 if (node->prev_ != nullptr) {
1361 node->prev_->next_ = node->next_;
1362 }
1363 if (node->next_ != nullptr) {
1364 node->next_->prev_ = node->prev_;
1365 }
1366 if (node == first_) {
1367 first_ = node->next_;
1368 }
1369 }
1370
David Brazdil1abb4192015-02-17 18:33:36 +00001371 bool Contains(const HUseListNode<T>* node) const {
1372 if (node == nullptr) {
1373 return false;
1374 }
1375 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1376 if (current == node) {
1377 return true;
1378 }
1379 }
1380 return false;
1381 }
1382
David Brazdiled596192015-01-23 10:39:45 +00001383 bool IsEmpty() const {
1384 return first_ == nullptr;
1385 }
1386
1387 bool HasOnlyOneUse() const {
1388 return first_ != nullptr && first_->next_ == nullptr;
1389 }
1390
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001391 size_t SizeSlow() const {
1392 size_t count = 0;
1393 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1394 ++count;
1395 }
1396 return count;
1397 }
1398
David Brazdiled596192015-01-23 10:39:45 +00001399 private:
1400 HUseListNode<T>* first_;
1401};
1402
1403template<typename T>
1404class HUseIterator : public ValueObject {
1405 public:
1406 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1407
1408 bool Done() const { return current_ == nullptr; }
1409
1410 void Advance() {
1411 DCHECK(!Done());
1412 current_ = current_->GetNext();
1413 }
1414
1415 HUseListNode<T>* Current() const {
1416 DCHECK(!Done());
1417 return current_;
1418 }
1419
1420 private:
1421 HUseListNode<T>* current_;
1422
1423 friend class HValue;
1424};
1425
David Brazdil1abb4192015-02-17 18:33:36 +00001426// This class is used by HEnvironment and HInstruction classes to record the
1427// instructions they use and pointers to the corresponding HUseListNodes kept
1428// by the used instructions.
1429template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001430class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001431 public:
1432 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1433 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1434
1435 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1436 : instruction_(old_record.instruction_), use_node_(use_node) {
1437 DCHECK(instruction_ != nullptr);
1438 DCHECK(use_node_ != nullptr);
1439 DCHECK(old_record.use_node_ == nullptr);
1440 }
1441
1442 HInstruction* GetInstruction() const { return instruction_; }
1443 HUseListNode<T>* GetUseNode() const { return use_node_; }
1444
1445 private:
1446 // Instruction used by the user.
1447 HInstruction* instruction_;
1448
1449 // Corresponding entry in the use list kept by 'instruction_'.
1450 HUseListNode<T>* use_node_;
1451};
1452
Aart Bik854a02b2015-07-14 16:07:00 -07001453/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001454 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001455 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001456 * For write/read dependences on fields/arrays, the dependence analysis uses
1457 * type disambiguation (e.g. a float field write cannot modify the value of an
1458 * integer field read) and the access type (e.g. a reference array write cannot
1459 * modify the value of a reference field read [although it may modify the
1460 * reference fetch prior to reading the field, which is represented by its own
1461 * write/read dependence]). The analysis makes conservative points-to
1462 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1463 * the same, and any reference read depends on any reference read without
1464 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001465 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001466 * The internal representation uses 38-bit and is described in the table below.
1467 * The first line indicates the side effect, and for field/array accesses the
1468 * second line indicates the type of the access (in the order of the
1469 * Primitive::Type enum).
1470 * The two numbered lines below indicate the bit position in the bitfield (read
1471 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001472 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001473 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1474 * +-------------+---------+---------+--------------+---------+---------+
1475 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1476 * | 3 |333333322|222222221| 1 |111111110|000000000|
1477 * | 7 |654321098|765432109| 8 |765432109|876543210|
1478 *
1479 * Note that, to ease the implementation, 'changes' bits are least significant
1480 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001481 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001482class SideEffects : public ValueObject {
1483 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001484 SideEffects() : flags_(0) {}
1485
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001486 static SideEffects None() {
1487 return SideEffects(0);
1488 }
1489
1490 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001491 return SideEffects(kAllChangeBits | kAllDependOnBits);
1492 }
1493
1494 static SideEffects AllChanges() {
1495 return SideEffects(kAllChangeBits);
1496 }
1497
1498 static SideEffects AllDependencies() {
1499 return SideEffects(kAllDependOnBits);
1500 }
1501
1502 static SideEffects AllExceptGCDependency() {
1503 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1504 }
1505
1506 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001507 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001508 }
1509
Aart Bik34c3ba92015-07-20 14:08:59 -07001510 static SideEffects AllWrites() {
1511 return SideEffects(kAllWrites);
1512 }
1513
1514 static SideEffects AllReads() {
1515 return SideEffects(kAllReads);
1516 }
1517
1518 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1519 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001520 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001521 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001522 }
1523
Aart Bik854a02b2015-07-14 16:07:00 -07001524 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1525 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001526 }
1527
Aart Bik34c3ba92015-07-20 14:08:59 -07001528 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1529 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001530 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001531 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001532 }
1533
1534 static SideEffects ArrayReadOfType(Primitive::Type type) {
1535 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1536 }
1537
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001538 static SideEffects CanTriggerGC() {
1539 return SideEffects(1ULL << kCanTriggerGCBit);
1540 }
1541
1542 static SideEffects DependsOnGC() {
1543 return SideEffects(1ULL << kDependsOnGCBit);
1544 }
1545
Aart Bik854a02b2015-07-14 16:07:00 -07001546 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001547 SideEffects Union(SideEffects other) const {
1548 return SideEffects(flags_ | other.flags_);
1549 }
1550
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001551 SideEffects Exclusion(SideEffects other) const {
1552 return SideEffects(flags_ & ~other.flags_);
1553 }
1554
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001555 void Add(SideEffects other) {
1556 flags_ |= other.flags_;
1557 }
1558
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001559 bool Includes(SideEffects other) const {
1560 return (other.flags_ & flags_) == other.flags_;
1561 }
1562
1563 bool HasSideEffects() const {
1564 return (flags_ & kAllChangeBits);
1565 }
1566
1567 bool HasDependencies() const {
1568 return (flags_ & kAllDependOnBits);
1569 }
1570
1571 // Returns true if there are no side effects or dependencies.
1572 bool DoesNothing() const {
1573 return flags_ == 0;
1574 }
1575
Aart Bik854a02b2015-07-14 16:07:00 -07001576 // Returns true if something is written.
1577 bool DoesAnyWrite() const {
1578 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001579 }
1580
Aart Bik854a02b2015-07-14 16:07:00 -07001581 // Returns true if something is read.
1582 bool DoesAnyRead() const {
1583 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001584 }
1585
Aart Bik854a02b2015-07-14 16:07:00 -07001586 // Returns true if potentially everything is written and read
1587 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001588 bool DoesAllReadWrite() const {
1589 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1590 }
1591
Aart Bik854a02b2015-07-14 16:07:00 -07001592 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001593 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001594 }
1595
Roland Levillain0d5a2812015-11-13 10:07:31 +00001596 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001597 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001598 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1599 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001600 }
1601
1602 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001603 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001604 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001605 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001606 for (int s = kLastBit; s >= 0; s--) {
1607 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1608 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1609 // This is a bit for the GC side effect.
1610 if (current_bit_is_set) {
1611 flags += "GC";
1612 }
Aart Bik854a02b2015-07-14 16:07:00 -07001613 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001614 } else {
1615 // This is a bit for the array/field analysis.
1616 // The underscore character stands for the 'can trigger GC' bit.
1617 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1618 if (current_bit_is_set) {
1619 flags += kDebug[s];
1620 }
1621 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1622 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1623 flags += "|";
1624 }
1625 }
Aart Bik854a02b2015-07-14 16:07:00 -07001626 }
1627 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001628 }
1629
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001630 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001631
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001632 private:
1633 static constexpr int kFieldArrayAnalysisBits = 9;
1634
1635 static constexpr int kFieldWriteOffset = 0;
1636 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1637 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1638 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1639
1640 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1641
1642 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1643 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1644 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1645 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1646
1647 static constexpr int kLastBit = kDependsOnGCBit;
1648 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1649
1650 // Aliases.
1651
1652 static_assert(kChangeBits == kDependOnBits,
1653 "the 'change' bits should match the 'depend on' bits.");
1654
1655 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1656 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1657 static constexpr uint64_t kAllWrites =
1658 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1659 static constexpr uint64_t kAllReads =
1660 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001661
Aart Bik854a02b2015-07-14 16:07:00 -07001662 // Work around the fact that HIR aliases I/F and J/D.
1663 // TODO: remove this interceptor once HIR types are clean
1664 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1665 switch (type) {
1666 case Primitive::kPrimInt:
1667 case Primitive::kPrimFloat:
1668 return TypeFlag(Primitive::kPrimInt, offset) |
1669 TypeFlag(Primitive::kPrimFloat, offset);
1670 case Primitive::kPrimLong:
1671 case Primitive::kPrimDouble:
1672 return TypeFlag(Primitive::kPrimLong, offset) |
1673 TypeFlag(Primitive::kPrimDouble, offset);
1674 default:
1675 return TypeFlag(type, offset);
1676 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001677 }
1678
Aart Bik854a02b2015-07-14 16:07:00 -07001679 // Translates type to bit flag.
1680 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1681 CHECK_NE(type, Primitive::kPrimVoid);
1682 const uint64_t one = 1;
1683 const int shift = type; // 0-based consecutive enum
1684 DCHECK_LE(kFieldWriteOffset, shift);
1685 DCHECK_LT(shift, kArrayWriteOffset);
1686 return one << (type + offset);
1687 }
1688
1689 // Private constructor on direct flags value.
1690 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1691
1692 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001693};
1694
David Brazdiled596192015-01-23 10:39:45 +00001695// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001696class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001697 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001698 HEnvironment(ArenaAllocator* arena,
1699 size_t number_of_vregs,
1700 const DexFile& dex_file,
1701 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001702 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001703 InvokeType invoke_type,
1704 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001705 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1706 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001707 parent_(nullptr),
1708 dex_file_(dex_file),
1709 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001710 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001711 invoke_type_(invoke_type),
1712 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001713 }
1714
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001715 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001716 : HEnvironment(arena,
1717 to_copy.Size(),
1718 to_copy.GetDexFile(),
1719 to_copy.GetMethodIdx(),
1720 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001721 to_copy.GetInvokeType(),
1722 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001723
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001724 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001725 if (parent_ != nullptr) {
1726 parent_->SetAndCopyParentChain(allocator, parent);
1727 } else {
1728 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1729 parent_->CopyFrom(parent);
1730 if (parent->GetParent() != nullptr) {
1731 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1732 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001733 }
David Brazdiled596192015-01-23 10:39:45 +00001734 }
1735
Vladimir Marko71bf8092015-09-15 15:33:14 +01001736 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001737 void CopyFrom(HEnvironment* environment);
1738
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001739 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1740 // input to the loop phi instead. This is for inserting instructions that
1741 // require an environment (like HDeoptimization) in the loop pre-header.
1742 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001743
1744 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001745 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001746 }
1747
1748 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001749 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001750 }
1751
David Brazdil1abb4192015-02-17 18:33:36 +00001752 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001753
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001754 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001755
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001756 HEnvironment* GetParent() const { return parent_; }
1757
1758 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001759 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001760 }
1761
1762 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001763 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001764 }
1765
1766 uint32_t GetDexPc() const {
1767 return dex_pc_;
1768 }
1769
1770 uint32_t GetMethodIdx() const {
1771 return method_idx_;
1772 }
1773
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001774 InvokeType GetInvokeType() const {
1775 return invoke_type_;
1776 }
1777
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001778 const DexFile& GetDexFile() const {
1779 return dex_file_;
1780 }
1781
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001782 HInstruction* GetHolder() const {
1783 return holder_;
1784 }
1785
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001786
1787 bool IsFromInlinedInvoke() const {
1788 return GetParent() != nullptr;
1789 }
1790
David Brazdiled596192015-01-23 10:39:45 +00001791 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001792 // Record instructions' use entries of this environment for constant-time removal.
1793 // It should only be called by HInstruction when a new environment use is added.
1794 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1795 DCHECK(env_use->GetUser() == this);
1796 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001797 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001798 }
David Brazdiled596192015-01-23 10:39:45 +00001799
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001800 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1801 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001802 HEnvironment* parent_;
1803 const DexFile& dex_file_;
1804 const uint32_t method_idx_;
1805 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001806 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001807
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001808 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001809 HInstruction* const holder_;
1810
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001811 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001812
1813 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1814};
1815
Vladimir Markof9f64412015-09-02 14:05:49 +01001816class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001817 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001818 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001819 : previous_(nullptr),
1820 next_(nullptr),
1821 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001822 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001823 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001824 ssa_index_(-1),
David Brazdilb3e773e2016-01-26 11:28:37 +00001825 emitted_at_use_site_(false),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001826 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001827 locations_(nullptr),
1828 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001829 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001830 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001831 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001832
Dave Allison20dfc792014-06-16 20:44:29 -07001833 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001834
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001835#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001836 enum InstructionKind {
1837 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1838 };
1839#undef DECLARE_KIND
1840
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001841 HInstruction* GetNext() const { return next_; }
1842 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001843
Calin Juravle77520bc2015-01-12 18:45:46 +00001844 HInstruction* GetNextDisregardingMoves() const;
1845 HInstruction* GetPreviousDisregardingMoves() const;
1846
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001847 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001848 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001849 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001850 bool IsInBlock() const { return block_ != nullptr; }
1851 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001852 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1853 bool IsIrreducibleLoopHeaderPhi() const {
1854 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1855 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001856
Roland Levillain6b879dd2014-09-22 17:13:44 +01001857 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001858 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001859
1860 virtual void Accept(HGraphVisitor* visitor) = 0;
1861 virtual const char* DebugName() const = 0;
1862
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001863 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001864 void SetRawInputAt(size_t index, HInstruction* input) {
1865 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1866 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001867
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001868 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001869
1870 uint32_t GetDexPc() const { return dex_pc_; }
1871
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001872 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001873
Roland Levillaine161a2a2014-10-03 12:45:18 +01001874 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001875 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001876
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001877 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001878 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001879
Calin Juravle10e244f2015-01-26 18:54:32 +00001880 // Does not apply for all instructions, but having this at top level greatly
1881 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001882 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001883 virtual bool CanBeNull() const {
1884 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1885 return true;
1886 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001887
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001888 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001889 return false;
1890 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001891
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001892 virtual bool IsActualObject() const {
1893 return GetType() == Primitive::kPrimNot;
1894 }
1895
Calin Juravle2e768302015-07-28 14:41:11 +00001896 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001897
Calin Juravle61d544b2015-02-23 16:46:57 +00001898 ReferenceTypeInfo GetReferenceTypeInfo() const {
1899 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1900 return reference_type_info_;
1901 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001902
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001903 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001904 DCHECK(user != nullptr);
1905 HUseListNode<HInstruction*>* use =
1906 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1907 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001908 }
1909
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001910 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001911 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001912 HUseListNode<HEnvironment*>* env_use =
1913 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1914 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001915 }
1916
David Brazdil1abb4192015-02-17 18:33:36 +00001917 void RemoveAsUserOfInput(size_t input) {
1918 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1919 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1920 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001921
David Brazdil1abb4192015-02-17 18:33:36 +00001922 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1923 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001924
David Brazdiled596192015-01-23 10:39:45 +00001925 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1926 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001927 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001928 bool HasOnlyOneNonEnvironmentUse() const {
1929 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1930 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001931
Roland Levillain6c82d402014-10-13 16:10:27 +01001932 // Does this instruction strictly dominate `other_instruction`?
1933 // Returns false if this instruction and `other_instruction` are the same.
1934 // Aborts if this instruction and `other_instruction` are both phis.
1935 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001936
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001937 int GetId() const { return id_; }
1938 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001939
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001940 int GetSsaIndex() const { return ssa_index_; }
1941 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1942 bool HasSsaIndex() const { return ssa_index_ != -1; }
1943
1944 bool HasEnvironment() const { return environment_ != nullptr; }
1945 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001946 // Set the `environment_` field. Raw because this method does not
1947 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001948 void SetRawEnvironment(HEnvironment* environment) {
1949 DCHECK(environment_ == nullptr);
1950 DCHECK_EQ(environment->GetHolder(), this);
1951 environment_ = environment;
1952 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001953
1954 // Set the environment of this instruction, copying it from `environment`. While
1955 // copying, the uses lists are being updated.
1956 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001957 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001958 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001959 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001960 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001961 if (environment->GetParent() != nullptr) {
1962 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1963 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001964 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001965
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001966 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1967 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001968 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001969 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001970 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001971 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001972 if (environment->GetParent() != nullptr) {
1973 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1974 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001975 }
1976
Nicolas Geoffray39468442014-09-02 15:17:15 +01001977 // Returns the number of entries in the environment. Typically, that is the
1978 // number of dex registers in a method. It could be more in case of inlining.
1979 size_t EnvironmentSize() const;
1980
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001981 LocationSummary* GetLocations() const { return locations_; }
1982 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001983
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001984 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001985 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001986
Alexandre Rames188d4312015-04-09 18:30:21 +01001987 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1988 // uses of this instruction by `other` are *not* updated.
1989 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1990 ReplaceWith(other);
1991 other->ReplaceInput(this, use_index);
1992 }
1993
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001994 // Move `this` instruction before `cursor`.
1995 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001996
Vladimir Markofb337ea2015-11-25 15:25:10 +00001997 // Move `this` before its first user and out of any loops. If there is no
1998 // out-of-loop user that dominates all other users, move the instruction
1999 // to the end of the out-of-loop common dominator of the user's blocks.
2000 //
2001 // This can be used only on non-throwing instructions with no side effects that
2002 // have at least one use but no environment uses.
2003 void MoveBeforeFirstUserAndOutOfLoops();
2004
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002005#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002006 bool Is##type() const; \
2007 const H##type* As##type() const; \
2008 H##type* As##type();
2009
2010 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2011#undef INSTRUCTION_TYPE_CHECK
2012
2013#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002014 bool Is##type() const { return (As##type() != nullptr); } \
2015 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002016 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002017 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002018#undef INSTRUCTION_TYPE_CHECK
2019
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002020 // Returns whether the instruction can be moved within the graph.
2021 virtual bool CanBeMoved() const { return false; }
2022
2023 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002024 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002025 return false;
2026 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002027
2028 // Returns whether any data encoded in the two instructions is equal.
2029 // This method does not look at the inputs. Both instructions must be
2030 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002031 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002032 return false;
2033 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002034
2035 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002036 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002037 // 2) Their inputs are identical.
2038 bool Equals(HInstruction* other) const;
2039
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002040 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2041 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2042 // the virtual function because the __attribute__((__pure__)) doesn't really
2043 // apply the strong requirement for virtual functions, preventing optimizations.
2044 InstructionKind GetKind() const PURE;
2045 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002046
2047 virtual size_t ComputeHashCode() const {
2048 size_t result = GetKind();
2049 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2050 result = (result * 31) + InputAt(i)->GetId();
2051 }
2052 return result;
2053 }
2054
2055 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002056 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002057
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002058 size_t GetLifetimePosition() const { return lifetime_position_; }
2059 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2060 LiveInterval* GetLiveInterval() const { return live_interval_; }
2061 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2062 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2063
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002064 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2065
2066 // Returns whether the code generation of the instruction will require to have access
2067 // to the current method. Such instructions are:
2068 // (1): Instructions that require an environment, as calling the runtime requires
2069 // to walk the stack and have the current method stored at a specific stack address.
2070 // (2): Object literals like classes and strings, that are loaded from the dex cache
2071 // fields of the current method.
2072 bool NeedsCurrentMethod() const {
2073 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2074 }
2075
Vladimir Markodc151b22015-10-15 18:02:30 +01002076 // Returns whether the code generation of the instruction will require to have access
2077 // to the dex cache of the current method's declaring class via the current method.
2078 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002079
Mark Mendellc4701932015-04-10 13:18:51 -04002080 // Does this instruction have any use in an environment before
2081 // control flow hits 'other'?
2082 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2083
2084 // Remove all references to environment uses of this instruction.
2085 // The caller must ensure that this is safe to do.
2086 void RemoveEnvironmentUsers();
2087
David Brazdilb3e773e2016-01-26 11:28:37 +00002088 bool IsEmittedAtUseSite() const { return emitted_at_use_site_; }
2089 void MarkEmittedAtUseSite() { emitted_at_use_site_ = true; }
2090
David Brazdil1abb4192015-02-17 18:33:36 +00002091 protected:
2092 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2093 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
Aart Bik5d75afe2015-12-14 11:57:01 -08002094 void SetSideEffects(SideEffects other) { side_effects_ = other; }
David Brazdil1abb4192015-02-17 18:33:36 +00002095
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002096 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002097 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2098
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002099 HInstruction* previous_;
2100 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002101 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002102 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002103
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002104 // An instruction gets an id when it is added to the graph.
2105 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002106 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002107 int id_;
2108
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002109 // When doing liveness analysis, instructions that have uses get an SSA index.
2110 int ssa_index_;
2111
David Brazdilb3e773e2016-01-26 11:28:37 +00002112 // If set, the machine code for this instruction is assumed to be generated by
2113 // its users. Used by liveness analysis to compute use positions accordingly.
2114 bool emitted_at_use_site_;
2115
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002116 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002117 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002118
2119 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002120 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002121
Nicolas Geoffray39468442014-09-02 15:17:15 +01002122 // The environment associated with this instruction. Not null if the instruction
2123 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002124 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002125
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002126 // Set by the code generator.
2127 LocationSummary* locations_;
2128
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002129 // Set by the liveness analysis.
2130 LiveInterval* live_interval_;
2131
2132 // Set by the liveness analysis, this is the position in a linear
2133 // order of blocks where this instruction's live interval start.
2134 size_t lifetime_position_;
2135
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002136 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002137
Calin Juravleacf735c2015-02-12 15:25:22 +00002138 // TODO: for primitive types this should be marked as invalid.
2139 ReferenceTypeInfo reference_type_info_;
2140
David Brazdil1abb4192015-02-17 18:33:36 +00002141 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002142 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002143 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002144 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002145 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002146
2147 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2148};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002149std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002150
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002151class HInputIterator : public ValueObject {
2152 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002153 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002154
2155 bool Done() const { return index_ == instruction_->InputCount(); }
2156 HInstruction* Current() const { return instruction_->InputAt(index_); }
2157 void Advance() { index_++; }
2158
2159 private:
2160 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002161 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002162
2163 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2164};
2165
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002166class HInstructionIterator : public ValueObject {
2167 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002168 explicit HInstructionIterator(const HInstructionList& instructions)
2169 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002170 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002171 }
2172
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002173 bool Done() const { return instruction_ == nullptr; }
2174 HInstruction* Current() const { return instruction_; }
2175 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002176 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002177 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002178 }
2179
2180 private:
2181 HInstruction* instruction_;
2182 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002183
2184 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002185};
2186
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002187class HBackwardInstructionIterator : public ValueObject {
2188 public:
2189 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2190 : instruction_(instructions.last_instruction_) {
2191 next_ = Done() ? nullptr : instruction_->GetPrevious();
2192 }
2193
2194 bool Done() const { return instruction_ == nullptr; }
2195 HInstruction* Current() const { return instruction_; }
2196 void Advance() {
2197 instruction_ = next_;
2198 next_ = Done() ? nullptr : instruction_->GetPrevious();
2199 }
2200
2201 private:
2202 HInstruction* instruction_;
2203 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002204
2205 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002206};
2207
Vladimir Markof9f64412015-09-02 14:05:49 +01002208template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002209class HTemplateInstruction: public HInstruction {
2210 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002211 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002212 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002213 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002214
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002215 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002216
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002217 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002218 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2219 DCHECK_LT(i, N);
2220 return inputs_[i];
2221 }
David Brazdil1abb4192015-02-17 18:33:36 +00002222
2223 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002224 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002225 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002226 }
2227
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002228 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002229 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002230
2231 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002232};
2233
Vladimir Markof9f64412015-09-02 14:05:49 +01002234// HTemplateInstruction specialization for N=0.
2235template<>
2236class HTemplateInstruction<0>: public HInstruction {
2237 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002238 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002239 : HInstruction(side_effects, dex_pc) {}
2240
Vladimir Markof9f64412015-09-02 14:05:49 +01002241 virtual ~HTemplateInstruction() {}
2242
2243 size_t InputCount() const OVERRIDE { return 0; }
2244
2245 protected:
2246 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2247 LOG(FATAL) << "Unreachable";
2248 UNREACHABLE();
2249 }
2250
2251 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2252 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2253 LOG(FATAL) << "Unreachable";
2254 UNREACHABLE();
2255 }
2256
2257 private:
2258 friend class SsaBuilder;
2259};
2260
Dave Allison20dfc792014-06-16 20:44:29 -07002261template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002262class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002263 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002264 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002265 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002266 virtual ~HExpression() {}
2267
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002268 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002269
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002270 protected:
2271 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002272};
2273
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002274// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2275// instruction that branches to the exit block.
2276class HReturnVoid : public HTemplateInstruction<0> {
2277 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002278 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2279 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002280
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002281 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002282
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002283 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002284
2285 private:
2286 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2287};
2288
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002289// Represents dex's RETURN opcodes. A HReturn is a control flow
2290// instruction that branches to the exit block.
2291class HReturn : public HTemplateInstruction<1> {
2292 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002293 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2294 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002295 SetRawInputAt(0, value);
2296 }
2297
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002298 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002299
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002300 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002301
2302 private:
2303 DISALLOW_COPY_AND_ASSIGN(HReturn);
2304};
2305
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002306// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002307// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002308// exit block.
2309class HExit : public HTemplateInstruction<0> {
2310 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002311 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002312
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002313 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002314
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002315 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002316
2317 private:
2318 DISALLOW_COPY_AND_ASSIGN(HExit);
2319};
2320
2321// Jumps from one block to another.
2322class HGoto : public HTemplateInstruction<0> {
2323 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002324 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002325
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002326 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002327
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002328 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002329 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002330 }
2331
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002332 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002333
2334 private:
2335 DISALLOW_COPY_AND_ASSIGN(HGoto);
2336};
2337
Roland Levillain9867bc72015-08-05 10:21:34 +01002338class HConstant : public HExpression<0> {
2339 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002340 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2341 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002342
2343 bool CanBeMoved() const OVERRIDE { return true; }
2344
2345 virtual bool IsMinusOne() const { return false; }
2346 virtual bool IsZero() const { return false; }
2347 virtual bool IsOne() const { return false; }
2348
David Brazdil77a48ae2015-09-15 12:34:04 +00002349 virtual uint64_t GetValueAsUint64() const = 0;
2350
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002351 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002352
2353 private:
2354 DISALLOW_COPY_AND_ASSIGN(HConstant);
2355};
2356
2357class HNullConstant : public HConstant {
2358 public:
2359 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2360 return true;
2361 }
2362
David Brazdil77a48ae2015-09-15 12:34:04 +00002363 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2364
Roland Levillain9867bc72015-08-05 10:21:34 +01002365 size_t ComputeHashCode() const OVERRIDE { return 0; }
2366
2367 DECLARE_INSTRUCTION(NullConstant);
2368
2369 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002370 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002371
2372 friend class HGraph;
2373 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2374};
2375
2376// Constants of the type int. Those can be from Dex instructions, or
2377// synthesized (for example with the if-eqz instruction).
2378class HIntConstant : public HConstant {
2379 public:
2380 int32_t GetValue() const { return value_; }
2381
David Brazdil9f389d42015-10-01 14:32:56 +01002382 uint64_t GetValueAsUint64() const OVERRIDE {
2383 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2384 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002385
Roland Levillain9867bc72015-08-05 10:21:34 +01002386 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2387 DCHECK(other->IsIntConstant());
2388 return other->AsIntConstant()->value_ == value_;
2389 }
2390
2391 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2392
2393 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2394 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2395 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2396
2397 DECLARE_INSTRUCTION(IntConstant);
2398
2399 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002400 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2401 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2402 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2403 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002404
2405 const int32_t value_;
2406
2407 friend class HGraph;
2408 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2409 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2410 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2411};
2412
2413class HLongConstant : public HConstant {
2414 public:
2415 int64_t GetValue() const { return value_; }
2416
David Brazdil77a48ae2015-09-15 12:34:04 +00002417 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2418
Roland Levillain9867bc72015-08-05 10:21:34 +01002419 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2420 DCHECK(other->IsLongConstant());
2421 return other->AsLongConstant()->value_ == value_;
2422 }
2423
2424 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2425
2426 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2427 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2428 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2429
2430 DECLARE_INSTRUCTION(LongConstant);
2431
2432 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002433 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2434 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002435
2436 const int64_t value_;
2437
2438 friend class HGraph;
2439 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2440};
Dave Allison20dfc792014-06-16 20:44:29 -07002441
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002442// Conditional branch. A block ending with an HIf instruction must have
2443// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002444class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002445 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002446 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2447 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002448 SetRawInputAt(0, input);
2449 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002450
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002451 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002452
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002453 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002454 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002455 }
2456
2457 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002458 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002459 }
2460
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002461 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002462
2463 private:
2464 DISALLOW_COPY_AND_ASSIGN(HIf);
2465};
2466
David Brazdilfc6a86a2015-06-26 10:33:45 +00002467
2468// Abstract instruction which marks the beginning and/or end of a try block and
2469// links it to the respective exception handlers. Behaves the same as a Goto in
2470// non-exceptional control flow.
2471// Normal-flow successor is stored at index zero, exception handlers under
2472// higher indices in no particular order.
2473class HTryBoundary : public HTemplateInstruction<0> {
2474 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002475 enum BoundaryKind {
2476 kEntry,
2477 kExit,
2478 };
2479
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002480 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2481 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002482
2483 bool IsControlFlow() const OVERRIDE { return true; }
2484
2485 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002486 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002487
David Brazdild26a4112015-11-10 11:07:31 +00002488 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2489 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2490 }
2491
David Brazdilfc6a86a2015-06-26 10:33:45 +00002492 // Returns whether `handler` is among its exception handlers (non-zero index
2493 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002494 bool HasExceptionHandler(const HBasicBlock& handler) const {
2495 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002496 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002497 }
2498
2499 // If not present already, adds `handler` to its block's list of exception
2500 // handlers.
2501 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002502 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002503 GetBlock()->AddSuccessor(handler);
2504 }
2505 }
2506
David Brazdil56e1acc2015-06-30 15:41:36 +01002507 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002508
David Brazdilffee3d32015-07-06 11:48:53 +01002509 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2510
David Brazdilfc6a86a2015-06-26 10:33:45 +00002511 DECLARE_INSTRUCTION(TryBoundary);
2512
2513 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002514 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002515
2516 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2517};
2518
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002519// Deoptimize to interpreter, upon checking a condition.
2520class HDeoptimize : public HTemplateInstruction<1> {
2521 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002522 // We set CanTriggerGC to prevent any intermediate address to be live
2523 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002524 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002525 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002526 SetRawInputAt(0, cond);
2527 }
2528
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002529 bool CanBeMoved() const OVERRIDE { return true; }
2530 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2531 return true;
2532 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002533 bool NeedsEnvironment() const OVERRIDE { return true; }
2534 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002535
2536 DECLARE_INSTRUCTION(Deoptimize);
2537
2538 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002539 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2540};
2541
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002542// Represents the ArtMethod that was passed as a first argument to
2543// the method. It is used by instructions that depend on it, like
2544// instructions that work with the dex cache.
2545class HCurrentMethod : public HExpression<0> {
2546 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002547 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2548 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002549
2550 DECLARE_INSTRUCTION(CurrentMethod);
2551
2552 private:
2553 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2554};
2555
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002556// Fetches an ArtMethod from the virtual table or the interface method table
2557// of a class.
2558class HClassTableGet : public HExpression<1> {
2559 public:
2560 enum TableKind {
2561 kVTable,
2562 kIMTable,
2563 };
2564 HClassTableGet(HInstruction* cls,
2565 Primitive::Type type,
2566 TableKind kind,
2567 size_t index,
2568 uint32_t dex_pc)
2569 : HExpression(type, SideEffects::None(), dex_pc),
2570 index_(index),
2571 table_kind_(kind) {
2572 SetRawInputAt(0, cls);
2573 }
2574
2575 bool CanBeMoved() const OVERRIDE { return true; }
2576 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2577 return other->AsClassTableGet()->GetIndex() == index_ &&
2578 other->AsClassTableGet()->GetTableKind() == table_kind_;
2579 }
2580
2581 TableKind GetTableKind() const { return table_kind_; }
2582 size_t GetIndex() const { return index_; }
2583
2584 DECLARE_INSTRUCTION(ClassTableGet);
2585
2586 private:
2587 // The index of the ArtMethod in the table.
2588 const size_t index_;
2589 const TableKind table_kind_;
2590
2591 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2592};
2593
Mark Mendellfe57faa2015-09-18 09:26:15 -04002594// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2595// have one successor for each entry in the switch table, and the final successor
2596// will be the block containing the next Dex opcode.
2597class HPackedSwitch : public HTemplateInstruction<1> {
2598 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002599 HPackedSwitch(int32_t start_value,
2600 uint32_t num_entries,
2601 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002602 uint32_t dex_pc = kNoDexPc)
2603 : HTemplateInstruction(SideEffects::None(), dex_pc),
2604 start_value_(start_value),
2605 num_entries_(num_entries) {
2606 SetRawInputAt(0, input);
2607 }
2608
2609 bool IsControlFlow() const OVERRIDE { return true; }
2610
2611 int32_t GetStartValue() const { return start_value_; }
2612
Vladimir Marko211c2112015-09-24 16:52:33 +01002613 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002614
2615 HBasicBlock* GetDefaultBlock() const {
2616 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002617 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002618 }
2619 DECLARE_INSTRUCTION(PackedSwitch);
2620
2621 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002622 const int32_t start_value_;
2623 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002624
2625 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2626};
2627
Roland Levillain88cb1752014-10-20 16:36:47 +01002628class HUnaryOperation : public HExpression<1> {
2629 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002630 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2631 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002632 SetRawInputAt(0, input);
2633 }
2634
2635 HInstruction* GetInput() const { return InputAt(0); }
2636 Primitive::Type GetResultType() const { return GetType(); }
2637
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002638 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002639 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002640 return true;
2641 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002642
Roland Levillain9240d6a2014-10-20 16:47:04 +01002643 // Try to statically evaluate `operation` and return a HConstant
2644 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002645 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002646 HConstant* TryStaticEvaluation() const;
2647
2648 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002649 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2650 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002651
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002652 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002653
2654 private:
2655 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2656};
2657
Dave Allison20dfc792014-06-16 20:44:29 -07002658class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002659 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002660 HBinaryOperation(Primitive::Type result_type,
2661 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002662 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002663 SideEffects side_effects = SideEffects::None(),
2664 uint32_t dex_pc = kNoDexPc)
2665 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002666 SetRawInputAt(0, left);
2667 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002668 }
2669
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002670 HInstruction* GetLeft() const { return InputAt(0); }
2671 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002672 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002673
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002674 virtual bool IsCommutative() const { return false; }
2675
2676 // Put constant on the right.
2677 // Returns whether order is changed.
2678 bool OrderInputsWithConstantOnTheRight() {
2679 HInstruction* left = InputAt(0);
2680 HInstruction* right = InputAt(1);
2681 if (left->IsConstant() && !right->IsConstant()) {
2682 ReplaceInput(right, 0);
2683 ReplaceInput(left, 1);
2684 return true;
2685 }
2686 return false;
2687 }
2688
2689 // Order inputs by instruction id, but favor constant on the right side.
2690 // This helps GVN for commutative ops.
2691 void OrderInputs() {
2692 DCHECK(IsCommutative());
2693 HInstruction* left = InputAt(0);
2694 HInstruction* right = InputAt(1);
2695 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2696 return;
2697 }
2698 if (OrderInputsWithConstantOnTheRight()) {
2699 return;
2700 }
2701 // Order according to instruction id.
2702 if (left->GetId() > right->GetId()) {
2703 ReplaceInput(right, 0);
2704 ReplaceInput(left, 1);
2705 }
2706 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002707
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002708 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002709 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002710 return true;
2711 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002712
Roland Levillain9240d6a2014-10-20 16:47:04 +01002713 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002714 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002715 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002716 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002717
2718 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002719 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2720 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2721 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2722 HLongConstant* y ATTRIBUTE_UNUSED) const {
2723 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2724 return nullptr;
2725 }
2726 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2727 HIntConstant* y ATTRIBUTE_UNUSED) const {
2728 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2729 return nullptr;
2730 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002731 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2732 HNullConstant* y ATTRIBUTE_UNUSED) const {
2733 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2734 return nullptr;
2735 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002736
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002737 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002738 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002739 HConstant* GetConstantRight() const;
2740
2741 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002742 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002743 HInstruction* GetLeastConstantLeft() const;
2744
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002745 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002746
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002747 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002748 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2749};
2750
Mark Mendellc4701932015-04-10 13:18:51 -04002751// The comparison bias applies for floating point operations and indicates how NaN
2752// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002753enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002754 kNoBias, // bias is not applicable (i.e. for long operation)
2755 kGtBias, // return 1 for NaN comparisons
2756 kLtBias, // return -1 for NaN comparisons
2757};
2758
Dave Allison20dfc792014-06-16 20:44:29 -07002759class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002760 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002761 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2762 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002763 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002764
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002765 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002766 // `instruction`, and disregard moves in between.
2767 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002768
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002769 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002770
2771 virtual IfCondition GetCondition() const = 0;
2772
Mark Mendellc4701932015-04-10 13:18:51 -04002773 virtual IfCondition GetOppositeCondition() const = 0;
2774
Roland Levillain4fa13f62015-07-06 18:11:54 +01002775 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002776
2777 void SetBias(ComparisonBias bias) { bias_ = bias; }
2778
2779 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2780 return bias_ == other->AsCondition()->bias_;
2781 }
2782
Roland Levillain4fa13f62015-07-06 18:11:54 +01002783 bool IsFPConditionTrueIfNaN() const {
2784 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2785 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002786 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002787 }
2788
2789 bool IsFPConditionFalseIfNaN() const {
2790 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2791 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002792 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002793 }
2794
Dave Allison20dfc792014-06-16 20:44:29 -07002795 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002796 // Needed if we merge a HCompare into a HCondition.
2797 ComparisonBias bias_;
2798
Dave Allison20dfc792014-06-16 20:44:29 -07002799 DISALLOW_COPY_AND_ASSIGN(HCondition);
2800};
2801
2802// Instruction to check if two inputs are equal to each other.
2803class HEqual : public HCondition {
2804 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002805 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2806 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002807
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002808 bool IsCommutative() const OVERRIDE { return true; }
2809
Roland Levillain9867bc72015-08-05 10:21:34 +01002810 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002811 return GetBlock()->GetGraph()->GetIntConstant(
2812 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002813 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002814 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002815 return GetBlock()->GetGraph()->GetIntConstant(
2816 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002817 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002818 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2819 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002820 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002821 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002822
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002823 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002824
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002825 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002826 return kCondEQ;
2827 }
2828
Mark Mendellc4701932015-04-10 13:18:51 -04002829 IfCondition GetOppositeCondition() const OVERRIDE {
2830 return kCondNE;
2831 }
2832
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002833 private:
Aart Bike9f37602015-10-09 11:15:55 -07002834 template <typename T> bool Compute(T x, T y) const { return x == y; }
2835
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002836 DISALLOW_COPY_AND_ASSIGN(HEqual);
2837};
2838
Dave Allison20dfc792014-06-16 20:44:29 -07002839class HNotEqual : public HCondition {
2840 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002841 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2842 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002843
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002844 bool IsCommutative() const OVERRIDE { return true; }
2845
Roland Levillain9867bc72015-08-05 10:21:34 +01002846 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002847 return GetBlock()->GetGraph()->GetIntConstant(
2848 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002849 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002850 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002851 return GetBlock()->GetGraph()->GetIntConstant(
2852 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002853 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002854 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2855 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002856 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002857 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002858
Dave Allison20dfc792014-06-16 20:44:29 -07002859 DECLARE_INSTRUCTION(NotEqual);
2860
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002861 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002862 return kCondNE;
2863 }
2864
Mark Mendellc4701932015-04-10 13:18:51 -04002865 IfCondition GetOppositeCondition() const OVERRIDE {
2866 return kCondEQ;
2867 }
2868
Dave Allison20dfc792014-06-16 20:44:29 -07002869 private:
Aart Bike9f37602015-10-09 11:15:55 -07002870 template <typename T> bool Compute(T x, T y) const { return x != y; }
2871
Dave Allison20dfc792014-06-16 20:44:29 -07002872 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2873};
2874
2875class HLessThan : public HCondition {
2876 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002877 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2878 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002879
Roland Levillain9867bc72015-08-05 10:21:34 +01002880 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002881 return GetBlock()->GetGraph()->GetIntConstant(
2882 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002883 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002884 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002885 return GetBlock()->GetGraph()->GetIntConstant(
2886 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002887 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002888
Dave Allison20dfc792014-06-16 20:44:29 -07002889 DECLARE_INSTRUCTION(LessThan);
2890
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002891 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002892 return kCondLT;
2893 }
2894
Mark Mendellc4701932015-04-10 13:18:51 -04002895 IfCondition GetOppositeCondition() const OVERRIDE {
2896 return kCondGE;
2897 }
2898
Dave Allison20dfc792014-06-16 20:44:29 -07002899 private:
Aart Bike9f37602015-10-09 11:15:55 -07002900 template <typename T> bool Compute(T x, T y) const { return x < y; }
2901
Dave Allison20dfc792014-06-16 20:44:29 -07002902 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2903};
2904
2905class HLessThanOrEqual : public HCondition {
2906 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002907 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2908 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002909
Roland Levillain9867bc72015-08-05 10:21:34 +01002910 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002911 return GetBlock()->GetGraph()->GetIntConstant(
2912 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002913 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002914 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002915 return GetBlock()->GetGraph()->GetIntConstant(
2916 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002917 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002918
Dave Allison20dfc792014-06-16 20:44:29 -07002919 DECLARE_INSTRUCTION(LessThanOrEqual);
2920
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002921 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002922 return kCondLE;
2923 }
2924
Mark Mendellc4701932015-04-10 13:18:51 -04002925 IfCondition GetOppositeCondition() const OVERRIDE {
2926 return kCondGT;
2927 }
2928
Dave Allison20dfc792014-06-16 20:44:29 -07002929 private:
Aart Bike9f37602015-10-09 11:15:55 -07002930 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2931
Dave Allison20dfc792014-06-16 20:44:29 -07002932 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2933};
2934
2935class HGreaterThan : public HCondition {
2936 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002937 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2938 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002939
Roland Levillain9867bc72015-08-05 10:21:34 +01002940 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002941 return GetBlock()->GetGraph()->GetIntConstant(
2942 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002943 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002944 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002945 return GetBlock()->GetGraph()->GetIntConstant(
2946 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002947 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002948
Dave Allison20dfc792014-06-16 20:44:29 -07002949 DECLARE_INSTRUCTION(GreaterThan);
2950
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002951 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002952 return kCondGT;
2953 }
2954
Mark Mendellc4701932015-04-10 13:18:51 -04002955 IfCondition GetOppositeCondition() const OVERRIDE {
2956 return kCondLE;
2957 }
2958
Dave Allison20dfc792014-06-16 20:44:29 -07002959 private:
Aart Bike9f37602015-10-09 11:15:55 -07002960 template <typename T> bool Compute(T x, T y) const { return x > y; }
2961
Dave Allison20dfc792014-06-16 20:44:29 -07002962 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2963};
2964
2965class HGreaterThanOrEqual : public HCondition {
2966 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002967 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2968 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002969
Roland Levillain9867bc72015-08-05 10:21:34 +01002970 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002971 return GetBlock()->GetGraph()->GetIntConstant(
2972 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002973 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002974 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002975 return GetBlock()->GetGraph()->GetIntConstant(
2976 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002977 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002978
Dave Allison20dfc792014-06-16 20:44:29 -07002979 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2980
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002981 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002982 return kCondGE;
2983 }
2984
Mark Mendellc4701932015-04-10 13:18:51 -04002985 IfCondition GetOppositeCondition() const OVERRIDE {
2986 return kCondLT;
2987 }
2988
Dave Allison20dfc792014-06-16 20:44:29 -07002989 private:
Aart Bike9f37602015-10-09 11:15:55 -07002990 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2991
Dave Allison20dfc792014-06-16 20:44:29 -07002992 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2993};
2994
Aart Bike9f37602015-10-09 11:15:55 -07002995class HBelow : public HCondition {
2996 public:
2997 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2998 : HCondition(first, second, dex_pc) {}
2999
3000 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3001 return GetBlock()->GetGraph()->GetIntConstant(
3002 Compute(static_cast<uint32_t>(x->GetValue()),
3003 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3004 }
3005 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3006 return GetBlock()->GetGraph()->GetIntConstant(
3007 Compute(static_cast<uint64_t>(x->GetValue()),
3008 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3009 }
3010
3011 DECLARE_INSTRUCTION(Below);
3012
3013 IfCondition GetCondition() const OVERRIDE {
3014 return kCondB;
3015 }
3016
3017 IfCondition GetOppositeCondition() const OVERRIDE {
3018 return kCondAE;
3019 }
3020
3021 private:
3022 template <typename T> bool Compute(T x, T y) const { return x < y; }
3023
3024 DISALLOW_COPY_AND_ASSIGN(HBelow);
3025};
3026
3027class HBelowOrEqual : public HCondition {
3028 public:
3029 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3030 : HCondition(first, second, dex_pc) {}
3031
3032 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3033 return GetBlock()->GetGraph()->GetIntConstant(
3034 Compute(static_cast<uint32_t>(x->GetValue()),
3035 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3036 }
3037 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3038 return GetBlock()->GetGraph()->GetIntConstant(
3039 Compute(static_cast<uint64_t>(x->GetValue()),
3040 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3041 }
3042
3043 DECLARE_INSTRUCTION(BelowOrEqual);
3044
3045 IfCondition GetCondition() const OVERRIDE {
3046 return kCondBE;
3047 }
3048
3049 IfCondition GetOppositeCondition() const OVERRIDE {
3050 return kCondA;
3051 }
3052
3053 private:
3054 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3055
3056 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3057};
3058
3059class HAbove : public HCondition {
3060 public:
3061 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3062 : HCondition(first, second, dex_pc) {}
3063
3064 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3065 return GetBlock()->GetGraph()->GetIntConstant(
3066 Compute(static_cast<uint32_t>(x->GetValue()),
3067 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3068 }
3069 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3070 return GetBlock()->GetGraph()->GetIntConstant(
3071 Compute(static_cast<uint64_t>(x->GetValue()),
3072 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3073 }
3074
3075 DECLARE_INSTRUCTION(Above);
3076
3077 IfCondition GetCondition() const OVERRIDE {
3078 return kCondA;
3079 }
3080
3081 IfCondition GetOppositeCondition() const OVERRIDE {
3082 return kCondBE;
3083 }
3084
3085 private:
3086 template <typename T> bool Compute(T x, T y) const { return x > y; }
3087
3088 DISALLOW_COPY_AND_ASSIGN(HAbove);
3089};
3090
3091class HAboveOrEqual : public HCondition {
3092 public:
3093 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3094 : HCondition(first, second, dex_pc) {}
3095
3096 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3097 return GetBlock()->GetGraph()->GetIntConstant(
3098 Compute(static_cast<uint32_t>(x->GetValue()),
3099 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3100 }
3101 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3102 return GetBlock()->GetGraph()->GetIntConstant(
3103 Compute(static_cast<uint64_t>(x->GetValue()),
3104 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3105 }
3106
3107 DECLARE_INSTRUCTION(AboveOrEqual);
3108
3109 IfCondition GetCondition() const OVERRIDE {
3110 return kCondAE;
3111 }
3112
3113 IfCondition GetOppositeCondition() const OVERRIDE {
3114 return kCondB;
3115 }
3116
3117 private:
3118 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3119
3120 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3121};
Dave Allison20dfc792014-06-16 20:44:29 -07003122
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003123// Instruction to check how two inputs compare to each other.
3124// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3125class HCompare : public HBinaryOperation {
3126 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003127 HCompare(Primitive::Type type,
3128 HInstruction* first,
3129 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003130 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003131 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003132 : HBinaryOperation(Primitive::kPrimInt,
3133 first,
3134 second,
3135 SideEffectsForArchRuntimeCalls(type),
3136 dex_pc),
3137 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003138 DCHECK_EQ(type, first->GetType());
3139 DCHECK_EQ(type, second->GetType());
3140 }
3141
Roland Levillain9867bc72015-08-05 10:21:34 +01003142 template <typename T>
3143 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003144
Roland Levillain9867bc72015-08-05 10:21:34 +01003145 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003146 return GetBlock()->GetGraph()->GetIntConstant(
3147 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003148 }
3149 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003150 return GetBlock()->GetGraph()->GetIntConstant(
3151 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003152 }
3153
Calin Juravleddb7df22014-11-25 20:56:51 +00003154 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3155 return bias_ == other->AsCompare()->bias_;
3156 }
3157
Mark Mendellc4701932015-04-10 13:18:51 -04003158 ComparisonBias GetBias() const { return bias_; }
3159
Roland Levillain4fa13f62015-07-06 18:11:54 +01003160 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003161
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003162
3163 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3164 // MIPS64 uses a runtime call for FP comparisons.
3165 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3166 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003167
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003168 DECLARE_INSTRUCTION(Compare);
3169
3170 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003171 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003172
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003173 DISALLOW_COPY_AND_ASSIGN(HCompare);
3174};
3175
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003176// A local in the graph. Corresponds to a Dex register.
3177class HLocal : public HTemplateInstruction<0> {
3178 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003179 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003180 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003181
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003182 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003183
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003184 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003185
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003186 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003187 // The Dex register number.
3188 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003189
3190 DISALLOW_COPY_AND_ASSIGN(HLocal);
3191};
3192
3193// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003194class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003195 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003196 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3197 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003198 SetRawInputAt(0, local);
3199 }
3200
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003201 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3202
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003203 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003204
3205 private:
3206 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3207};
3208
3209// Store a value in a given local. This instruction has two inputs: the value
3210// and the local.
3211class HStoreLocal : public HTemplateInstruction<2> {
3212 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003213 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3214 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003215 SetRawInputAt(0, local);
3216 SetRawInputAt(1, value);
3217 }
3218
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003219 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3220
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003221 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003222
3223 private:
3224 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3225};
3226
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003227class HFloatConstant : public HConstant {
3228 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003229 float GetValue() const { return value_; }
3230
David Brazdil77a48ae2015-09-15 12:34:04 +00003231 uint64_t GetValueAsUint64() const OVERRIDE {
3232 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3233 }
3234
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003235 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003236 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003237 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003238 }
3239
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003240 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003241
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003242 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003243 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003244 }
3245 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003246 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003247 }
3248 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003249 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3250 }
3251 bool IsNaN() const {
3252 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003253 }
3254
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003255 DECLARE_INSTRUCTION(FloatConstant);
3256
3257 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003258 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3259 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3260 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3261 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003262
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003263 const float value_;
3264
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003265 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003266 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003267 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003268 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3269};
3270
3271class HDoubleConstant : public HConstant {
3272 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003273 double GetValue() const { return value_; }
3274
David Brazdil77a48ae2015-09-15 12:34:04 +00003275 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3276
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003277 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003278 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003279 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003280 }
3281
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003282 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003283
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003284 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003285 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003286 }
3287 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003288 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003289 }
3290 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003291 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3292 }
3293 bool IsNaN() const {
3294 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003295 }
3296
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003297 DECLARE_INSTRUCTION(DoubleConstant);
3298
3299 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003300 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3301 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3302 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3303 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003304
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003305 const double value_;
3306
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003307 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003308 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003309 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003310 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3311};
3312
David Brazdil6de19382016-01-08 17:37:10 +00003313class HNewInstance : public HExpression<2> {
3314 public:
3315 HNewInstance(HInstruction* cls,
3316 HCurrentMethod* current_method,
3317 uint32_t dex_pc,
3318 uint16_t type_index,
3319 const DexFile& dex_file,
3320 bool can_throw,
3321 bool finalizable,
3322 QuickEntrypointEnum entrypoint)
3323 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3324 type_index_(type_index),
3325 dex_file_(dex_file),
3326 can_throw_(can_throw),
3327 finalizable_(finalizable),
3328 entrypoint_(entrypoint) {
3329 SetRawInputAt(0, cls);
3330 SetRawInputAt(1, current_method);
3331 }
3332
3333 uint16_t GetTypeIndex() const { return type_index_; }
3334 const DexFile& GetDexFile() const { return dex_file_; }
3335
3336 // Calls runtime so needs an environment.
3337 bool NeedsEnvironment() const OVERRIDE { return true; }
3338
3339 // It may throw when called on type that's not instantiable/accessible.
3340 // It can throw OOME.
3341 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3342 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3343
3344 bool IsFinalizable() const { return finalizable_; }
3345
3346 bool CanBeNull() const OVERRIDE { return false; }
3347
3348 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3349
3350 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3351 entrypoint_ = entrypoint;
3352 }
3353
3354 bool IsStringAlloc() const;
3355
3356 DECLARE_INSTRUCTION(NewInstance);
3357
3358 private:
3359 const uint16_t type_index_;
3360 const DexFile& dex_file_;
3361 const bool can_throw_;
3362 const bool finalizable_;
3363 QuickEntrypointEnum entrypoint_;
3364
3365 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3366};
3367
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003368enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003369#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3370 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003371#include "intrinsics_list.h"
3372 kNone,
3373 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3374#undef INTRINSICS_LIST
3375#undef OPTIMIZING_INTRINSICS
3376};
3377std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3378
Agi Csaki05f20562015-08-19 14:58:14 -07003379enum IntrinsicNeedsEnvironmentOrCache {
3380 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3381 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003382};
3383
Aart Bik5d75afe2015-12-14 11:57:01 -08003384enum IntrinsicSideEffects {
3385 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3386 kReadSideEffects, // Intrinsic may read heap memory.
3387 kWriteSideEffects, // Intrinsic may write heap memory.
3388 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3389};
3390
3391enum IntrinsicExceptions {
3392 kNoThrow, // Intrinsic does not throw any exceptions.
3393 kCanThrow // Intrinsic may throw exceptions.
3394};
3395
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003396class HInvoke : public HInstruction {
3397 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003398 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003399
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003400 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003401
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003402 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003403 SetRawInputAt(index, argument);
3404 }
3405
Roland Levillain3e3d7332015-04-28 11:00:54 +01003406 // Return the number of arguments. This number can be lower than
3407 // the number of inputs returned by InputCount(), as some invoke
3408 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3409 // inputs at the end of their list of inputs.
3410 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3411
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003412 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003413
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003414 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003415 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003416
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003417 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3418
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003419 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003420 return intrinsic_;
3421 }
3422
Aart Bik5d75afe2015-12-14 11:57:01 -08003423 void SetIntrinsic(Intrinsics intrinsic,
3424 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3425 IntrinsicSideEffects side_effects,
3426 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003427
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003428 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003429 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003430 }
3431
Aart Bik5d75afe2015-12-14 11:57:01 -08003432 bool CanThrow() const OVERRIDE { return can_throw_; }
3433
3434 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3435
3436 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3437 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3438 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003439
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003440 uint32_t* GetIntrinsicOptimizations() {
3441 return &intrinsic_optimizations_;
3442 }
3443
3444 const uint32_t* GetIntrinsicOptimizations() const {
3445 return &intrinsic_optimizations_;
3446 }
3447
3448 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3449
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003450 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003451
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003452 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003453 HInvoke(ArenaAllocator* arena,
3454 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003455 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003456 Primitive::Type return_type,
3457 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003458 uint32_t dex_method_index,
3459 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003460 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003461 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003462 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003463 inputs_(number_of_arguments + number_of_other_inputs,
3464 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003465 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003466 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003467 original_invoke_type_(original_invoke_type),
Aart Bik5d75afe2015-12-14 11:57:01 -08003468 can_throw_(true),
agicsaki57b81ec2015-08-11 17:39:37 -07003469 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003470 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003471 }
3472
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003473 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003474 return inputs_[index];
3475 }
3476
David Brazdil1abb4192015-02-17 18:33:36 +00003477 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003478 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003479 }
3480
Aart Bik5d75afe2015-12-14 11:57:01 -08003481 void SetCanThrow(bool can_throw) { can_throw_ = can_throw; }
3482
Roland Levillain3e3d7332015-04-28 11:00:54 +01003483 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003484 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003485 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003486 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003487 const InvokeType original_invoke_type_;
Aart Bik5d75afe2015-12-14 11:57:01 -08003488 bool can_throw_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003489 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003490
3491 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3492 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003493
3494 private:
3495 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3496};
3497
Calin Juravle175dc732015-08-25 15:42:32 +01003498class HInvokeUnresolved : public HInvoke {
3499 public:
3500 HInvokeUnresolved(ArenaAllocator* arena,
3501 uint32_t number_of_arguments,
3502 Primitive::Type return_type,
3503 uint32_t dex_pc,
3504 uint32_t dex_method_index,
3505 InvokeType invoke_type)
3506 : HInvoke(arena,
3507 number_of_arguments,
3508 0u /* number_of_other_inputs */,
3509 return_type,
3510 dex_pc,
3511 dex_method_index,
3512 invoke_type) {
3513 }
3514
3515 DECLARE_INSTRUCTION(InvokeUnresolved);
3516
3517 private:
3518 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3519};
3520
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003521class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003522 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003523 // Requirements of this method call regarding the class
3524 // initialization (clinit) check of its declaring class.
3525 enum class ClinitCheckRequirement {
3526 kNone, // Class already initialized.
3527 kExplicit, // Static call having explicit clinit check as last input.
3528 kImplicit, // Static call implicitly requiring a clinit check.
3529 };
3530
Vladimir Marko58155012015-08-19 12:49:41 +00003531 // Determines how to load the target ArtMethod*.
3532 enum class MethodLoadKind {
3533 // Use a String init ArtMethod* loaded from Thread entrypoints.
3534 kStringInit,
3535
3536 // Use the method's own ArtMethod* loaded by the register allocator.
3537 kRecursive,
3538
3539 // Use ArtMethod* at a known address, embed the direct address in the code.
3540 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3541 kDirectAddress,
3542
3543 // Use ArtMethod* at an address that will be known at link time, embed the direct
3544 // address in the code. If the image is relocatable, emit .patch_oat entry.
3545 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3546 // the image relocatable or not.
3547 kDirectAddressWithFixup,
3548
3549 // Load from resoved methods array in the dex cache using a PC-relative load.
3550 // Used when we need to use the dex cache, for example for invoke-static that
3551 // may cause class initialization (the entry may point to a resolution method),
3552 // and we know that we can access the dex cache arrays using a PC-relative load.
3553 kDexCachePcRelative,
3554
3555 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3556 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3557 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3558 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3559 kDexCacheViaMethod,
3560 };
3561
3562 // Determines the location of the code pointer.
3563 enum class CodePtrLocation {
3564 // Recursive call, use local PC-relative call instruction.
3565 kCallSelf,
3566
3567 // Use PC-relative call instruction patched at link time.
3568 // Used for calls within an oat file, boot->boot or app->app.
3569 kCallPCRelative,
3570
3571 // Call to a known target address, embed the direct address in code.
3572 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3573 kCallDirect,
3574
3575 // Call to a target address that will be known at link time, embed the direct
3576 // address in code. If the image is relocatable, emit .patch_oat entry.
3577 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3578 // the image relocatable or not.
3579 kCallDirectWithFixup,
3580
3581 // Use code pointer from the ArtMethod*.
3582 // Used when we don't know the target code. This is also the last-resort-kind used when
3583 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3584 kCallArtMethod,
3585 };
3586
3587 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003588 MethodLoadKind method_load_kind;
3589 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003590 // The method load data holds
3591 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3592 // Note that there are multiple string init methods, each having its own offset.
3593 // - the method address for kDirectAddress
3594 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003595 uint64_t method_load_data;
3596 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003597 };
3598
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003599 HInvokeStaticOrDirect(ArenaAllocator* arena,
3600 uint32_t number_of_arguments,
3601 Primitive::Type return_type,
3602 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003603 uint32_t method_index,
3604 MethodReference target_method,
3605 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003606 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003607 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003608 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003609 : HInvoke(arena,
3610 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003611 // There is potentially one extra argument for the HCurrentMethod node, and
3612 // potentially one other if the clinit check is explicit, and potentially
3613 // one other if the method is a string factory.
3614 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3615 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3616 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003617 return_type,
3618 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003619 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003620 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003621 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003622 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003623 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003624 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003625
Vladimir Markodc151b22015-10-15 18:02:30 +01003626 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003627 bool had_current_method_input = HasCurrentMethodInput();
3628 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3629
3630 // Using the current method is the default and once we find a better
3631 // method load kind, we should not go back to using the current method.
3632 DCHECK(had_current_method_input || !needs_current_method_input);
3633
3634 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003635 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3636 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003637 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003638 dispatch_info_ = dispatch_info;
3639 }
3640
Vladimir Markoc53c0792015-11-19 15:48:33 +00003641 void AddSpecialInput(HInstruction* input) {
3642 // We allow only one special input.
3643 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3644 DCHECK(InputCount() == GetSpecialInputIndex() ||
3645 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3646 InsertInputAt(GetSpecialInputIndex(), input);
3647 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003648
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003649 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003650 // We access the method via the dex cache so we can't do an implicit null check.
3651 // TODO: for intrinsics we can generate implicit null checks.
3652 return false;
3653 }
3654
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003655 bool CanBeNull() const OVERRIDE {
3656 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3657 }
3658
Vladimir Markoc53c0792015-11-19 15:48:33 +00003659 // Get the index of the special input, if any.
3660 //
David Brazdil6de19382016-01-08 17:37:10 +00003661 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3662 // method pointer; otherwise there may be one platform-specific special input,
3663 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003664 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3665
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003666 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3667 void SetOptimizedInvokeType(InvokeType invoke_type) {
3668 optimized_invoke_type_ = invoke_type;
3669 }
3670
Vladimir Marko58155012015-08-19 12:49:41 +00003671 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3672 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3673 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003674 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003675 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003676 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003677 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003678 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3679 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003680 bool HasCurrentMethodInput() const {
3681 // This function can be called only after the invoke has been fully initialized by the builder.
3682 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003683 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003684 return true;
3685 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003686 DCHECK(InputCount() == GetSpecialInputIndex() ||
3687 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003688 return false;
3689 }
3690 }
Vladimir Marko58155012015-08-19 12:49:41 +00003691 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3692 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003693 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003694
3695 int32_t GetStringInitOffset() const {
3696 DCHECK(IsStringInit());
3697 return dispatch_info_.method_load_data;
3698 }
3699
3700 uint64_t GetMethodAddress() const {
3701 DCHECK(HasMethodAddress());
3702 return dispatch_info_.method_load_data;
3703 }
3704
3705 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003706 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003707 return dispatch_info_.method_load_data;
3708 }
3709
3710 uint64_t GetDirectCodePtr() const {
3711 DCHECK(HasDirectCodePtr());
3712 return dispatch_info_.direct_code_ptr;
3713 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003714
Calin Juravle68ad6492015-08-18 17:08:12 +01003715 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3716
Roland Levillain4c0eb422015-04-24 16:43:49 +01003717 // Is this instruction a call to a static method?
3718 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003719 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003720 }
3721
Vladimir Markofbb184a2015-11-13 14:47:00 +00003722 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3723 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3724 // instruction; only relevant for static calls with explicit clinit check.
3725 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003726 DCHECK(IsStaticWithExplicitClinitCheck());
3727 size_t last_input_index = InputCount() - 1;
3728 HInstruction* last_input = InputAt(last_input_index);
3729 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003730 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003731 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003732 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003733 clinit_check_requirement_ = new_requirement;
3734 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003735 }
3736
David Brazdilbc9ab162016-01-20 14:50:19 +00003737 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00003738 DCHECK(IsStringInit());
3739 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00003740 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00003741 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003742 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00003743 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003744 }
3745
Roland Levillain4c0eb422015-04-24 16:43:49 +01003746 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003747 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003748 bool IsStaticWithExplicitClinitCheck() const {
3749 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3750 }
3751
3752 // Is this a call to a static method whose declaring class has an
3753 // implicit intialization check requirement?
3754 bool IsStaticWithImplicitClinitCheck() const {
3755 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3756 }
3757
Vladimir Markob554b5a2015-11-06 12:57:55 +00003758 // Does this method load kind need the current method as an input?
3759 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3760 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3761 }
3762
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003763 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003764
Roland Levillain4c0eb422015-04-24 16:43:49 +01003765 protected:
3766 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3767 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3768 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3769 HInstruction* input = input_record.GetInstruction();
3770 // `input` is the last input of a static invoke marked as having
3771 // an explicit clinit check. It must either be:
3772 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3773 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3774 DCHECK(input != nullptr);
3775 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3776 }
3777 return input_record;
3778 }
3779
Vladimir Markoc53c0792015-11-19 15:48:33 +00003780 void InsertInputAt(size_t index, HInstruction* input);
3781 void RemoveInputAt(size_t index);
3782
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003783 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003784 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003785 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003786 // The target method may refer to different dex file or method index than the original
3787 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3788 // in derived class to increase visibility.
3789 MethodReference target_method_;
3790 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003791
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003792 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003793};
Vladimir Markof64242a2015-12-01 14:58:23 +00003794std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003795std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003796
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003797class HInvokeVirtual : public HInvoke {
3798 public:
3799 HInvokeVirtual(ArenaAllocator* arena,
3800 uint32_t number_of_arguments,
3801 Primitive::Type return_type,
3802 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003803 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003804 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003805 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003806 vtable_index_(vtable_index) {}
3807
Calin Juravle641547a2015-04-21 22:08:51 +01003808 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003809 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003810 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003811 }
3812
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003813 uint32_t GetVTableIndex() const { return vtable_index_; }
3814
3815 DECLARE_INSTRUCTION(InvokeVirtual);
3816
3817 private:
3818 const uint32_t vtable_index_;
3819
3820 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3821};
3822
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003823class HInvokeInterface : public HInvoke {
3824 public:
3825 HInvokeInterface(ArenaAllocator* arena,
3826 uint32_t number_of_arguments,
3827 Primitive::Type return_type,
3828 uint32_t dex_pc,
3829 uint32_t dex_method_index,
3830 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003831 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003832 imt_index_(imt_index) {}
3833
Calin Juravle641547a2015-04-21 22:08:51 +01003834 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003835 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003836 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003837 }
3838
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003839 uint32_t GetImtIndex() const { return imt_index_; }
3840 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3841
3842 DECLARE_INSTRUCTION(InvokeInterface);
3843
3844 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003845 const uint32_t imt_index_;
3846
3847 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3848};
3849
Roland Levillain88cb1752014-10-20 16:36:47 +01003850class HNeg : public HUnaryOperation {
3851 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003852 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3853 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003854
Roland Levillain9867bc72015-08-05 10:21:34 +01003855 template <typename T> T Compute(T x) const { return -x; }
3856
3857 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003858 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003859 }
3860 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003861 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003862 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003863
Roland Levillain88cb1752014-10-20 16:36:47 +01003864 DECLARE_INSTRUCTION(Neg);
3865
3866 private:
3867 DISALLOW_COPY_AND_ASSIGN(HNeg);
3868};
3869
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003870class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003871 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003872 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003873 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003874 uint32_t dex_pc,
3875 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003876 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003877 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003878 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003879 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003880 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003881 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003882 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003883 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003884 }
3885
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003886 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003887 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003888
3889 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003890 bool NeedsEnvironment() const OVERRIDE { return true; }
3891
Mingyao Yang0c365e62015-03-31 15:09:29 -07003892 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3893 bool CanThrow() const OVERRIDE { return true; }
3894
Calin Juravle10e244f2015-01-26 18:54:32 +00003895 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003896
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003897 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3898
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003899 DECLARE_INSTRUCTION(NewArray);
3900
3901 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003902 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003903 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003904 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003905
3906 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3907};
3908
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003909class HAdd : public HBinaryOperation {
3910 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003911 HAdd(Primitive::Type result_type,
3912 HInstruction* left,
3913 HInstruction* right,
3914 uint32_t dex_pc = kNoDexPc)
3915 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003916
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003917 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003918
Roland Levillain9867bc72015-08-05 10:21:34 +01003919 template <typename T> T Compute(T x, T y) const { return x + y; }
3920
3921 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003922 return GetBlock()->GetGraph()->GetIntConstant(
3923 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003924 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003925 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003926 return GetBlock()->GetGraph()->GetLongConstant(
3927 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003928 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003929
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003930 DECLARE_INSTRUCTION(Add);
3931
3932 private:
3933 DISALLOW_COPY_AND_ASSIGN(HAdd);
3934};
3935
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003936class HSub : public HBinaryOperation {
3937 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003938 HSub(Primitive::Type result_type,
3939 HInstruction* left,
3940 HInstruction* right,
3941 uint32_t dex_pc = kNoDexPc)
3942 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003943
Roland Levillain9867bc72015-08-05 10:21:34 +01003944 template <typename T> T Compute(T x, T y) const { return x - y; }
3945
3946 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003947 return GetBlock()->GetGraph()->GetIntConstant(
3948 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003949 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003950 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003951 return GetBlock()->GetGraph()->GetLongConstant(
3952 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003953 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003954
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003955 DECLARE_INSTRUCTION(Sub);
3956
3957 private:
3958 DISALLOW_COPY_AND_ASSIGN(HSub);
3959};
3960
Calin Juravle34bacdf2014-10-07 20:23:36 +01003961class HMul : public HBinaryOperation {
3962 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003963 HMul(Primitive::Type result_type,
3964 HInstruction* left,
3965 HInstruction* right,
3966 uint32_t dex_pc = kNoDexPc)
3967 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003968
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003969 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003970
Roland Levillain9867bc72015-08-05 10:21:34 +01003971 template <typename T> T Compute(T x, T y) const { return x * y; }
3972
3973 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003974 return GetBlock()->GetGraph()->GetIntConstant(
3975 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003976 }
3977 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003978 return GetBlock()->GetGraph()->GetLongConstant(
3979 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003980 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003981
3982 DECLARE_INSTRUCTION(Mul);
3983
3984 private:
3985 DISALLOW_COPY_AND_ASSIGN(HMul);
3986};
3987
Calin Juravle7c4954d2014-10-28 16:57:40 +00003988class HDiv : public HBinaryOperation {
3989 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003990 HDiv(Primitive::Type result_type,
3991 HInstruction* left,
3992 HInstruction* right,
3993 uint32_t dex_pc)
3994 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003995
Roland Levillain9867bc72015-08-05 10:21:34 +01003996 template <typename T>
3997 T Compute(T x, T y) const {
3998 // Our graph structure ensures we never have 0 for `y` during
3999 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004000 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004001 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004002 return (y == -1) ? -x : x / y;
4003 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004004
Roland Levillain9867bc72015-08-05 10:21:34 +01004005 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004006 return GetBlock()->GetGraph()->GetIntConstant(
4007 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004008 }
4009 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004010 return GetBlock()->GetGraph()->GetLongConstant(
4011 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004012 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004013
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004014 static SideEffects SideEffectsForArchRuntimeCalls() {
4015 // The generated code can use a runtime call.
4016 return SideEffects::CanTriggerGC();
4017 }
4018
Calin Juravle7c4954d2014-10-28 16:57:40 +00004019 DECLARE_INSTRUCTION(Div);
4020
4021 private:
4022 DISALLOW_COPY_AND_ASSIGN(HDiv);
4023};
4024
Calin Juravlebacfec32014-11-14 15:54:36 +00004025class HRem : public HBinaryOperation {
4026 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004027 HRem(Primitive::Type result_type,
4028 HInstruction* left,
4029 HInstruction* right,
4030 uint32_t dex_pc)
4031 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004032
Roland Levillain9867bc72015-08-05 10:21:34 +01004033 template <typename T>
4034 T Compute(T x, T y) const {
4035 // Our graph structure ensures we never have 0 for `y` during
4036 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004037 DCHECK_NE(y, 0);
4038 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4039 return (y == -1) ? 0 : x % y;
4040 }
4041
Roland Levillain9867bc72015-08-05 10:21:34 +01004042 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004043 return GetBlock()->GetGraph()->GetIntConstant(
4044 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004045 }
4046 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004047 return GetBlock()->GetGraph()->GetLongConstant(
4048 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004049 }
4050
Calin Juravlebacfec32014-11-14 15:54:36 +00004051
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004052 static SideEffects SideEffectsForArchRuntimeCalls() {
4053 return SideEffects::CanTriggerGC();
4054 }
4055
Calin Juravlebacfec32014-11-14 15:54:36 +00004056 DECLARE_INSTRUCTION(Rem);
4057
4058 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004059 DISALLOW_COPY_AND_ASSIGN(HRem);
4060};
4061
Calin Juravled0d48522014-11-04 16:40:20 +00004062class HDivZeroCheck : public HExpression<1> {
4063 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004064 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4065 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004066 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004067 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004068 SetRawInputAt(0, value);
4069 }
4070
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004071 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4072
Calin Juravled0d48522014-11-04 16:40:20 +00004073 bool CanBeMoved() const OVERRIDE { return true; }
4074
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004075 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004076 return true;
4077 }
4078
4079 bool NeedsEnvironment() const OVERRIDE { return true; }
4080 bool CanThrow() const OVERRIDE { return true; }
4081
Calin Juravled0d48522014-11-04 16:40:20 +00004082 DECLARE_INSTRUCTION(DivZeroCheck);
4083
4084 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004085 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4086};
4087
Calin Juravle9aec02f2014-11-18 23:06:35 +00004088class HShl : public HBinaryOperation {
4089 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004090 HShl(Primitive::Type result_type,
4091 HInstruction* left,
4092 HInstruction* right,
4093 uint32_t dex_pc = kNoDexPc)
4094 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004095
Roland Levillain9867bc72015-08-05 10:21:34 +01004096 template <typename T, typename U, typename V>
4097 T Compute(T x, U y, V max_shift_value) const {
4098 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4099 "V is not the unsigned integer type corresponding to T");
4100 return x << (y & max_shift_value);
4101 }
4102
4103 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4104 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004105 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004106 }
4107 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4108 // case is handled as `x << static_cast<int>(y)`.
4109 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4110 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004111 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004112 }
4113 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4114 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004115 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004116 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004117
4118 DECLARE_INSTRUCTION(Shl);
4119
4120 private:
4121 DISALLOW_COPY_AND_ASSIGN(HShl);
4122};
4123
4124class HShr : public HBinaryOperation {
4125 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004126 HShr(Primitive::Type result_type,
4127 HInstruction* left,
4128 HInstruction* right,
4129 uint32_t dex_pc = kNoDexPc)
4130 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004131
Roland Levillain9867bc72015-08-05 10:21:34 +01004132 template <typename T, typename U, typename V>
4133 T Compute(T x, U y, V max_shift_value) const {
4134 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4135 "V is not the unsigned integer type corresponding to T");
4136 return x >> (y & max_shift_value);
4137 }
4138
4139 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4140 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004141 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004142 }
4143 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4144 // case is handled as `x >> static_cast<int>(y)`.
4145 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4146 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004147 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004148 }
4149 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4150 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004151 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004152 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004153
4154 DECLARE_INSTRUCTION(Shr);
4155
4156 private:
4157 DISALLOW_COPY_AND_ASSIGN(HShr);
4158};
4159
4160class HUShr : public HBinaryOperation {
4161 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004162 HUShr(Primitive::Type result_type,
4163 HInstruction* left,
4164 HInstruction* right,
4165 uint32_t dex_pc = kNoDexPc)
4166 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004167
Roland Levillain9867bc72015-08-05 10:21:34 +01004168 template <typename T, typename U, typename V>
4169 T Compute(T x, U y, V max_shift_value) const {
4170 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4171 "V is not the unsigned integer type corresponding to T");
4172 V ux = static_cast<V>(x);
4173 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004174 }
4175
Roland Levillain9867bc72015-08-05 10:21:34 +01004176 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4177 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004178 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004179 }
4180 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4181 // case is handled as `x >>> static_cast<int>(y)`.
4182 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4183 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004184 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004185 }
4186 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4187 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004188 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004189 }
4190
4191 DECLARE_INSTRUCTION(UShr);
4192
4193 private:
4194 DISALLOW_COPY_AND_ASSIGN(HUShr);
4195};
4196
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004197class HAnd : public HBinaryOperation {
4198 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004199 HAnd(Primitive::Type result_type,
4200 HInstruction* left,
4201 HInstruction* right,
4202 uint32_t dex_pc = kNoDexPc)
4203 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004204
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004205 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004206
Roland Levillain9867bc72015-08-05 10:21:34 +01004207 template <typename T, typename U>
4208 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4209
4210 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004211 return GetBlock()->GetGraph()->GetIntConstant(
4212 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004213 }
4214 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004215 return GetBlock()->GetGraph()->GetLongConstant(
4216 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004217 }
4218 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004219 return GetBlock()->GetGraph()->GetLongConstant(
4220 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004221 }
4222 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004223 return GetBlock()->GetGraph()->GetLongConstant(
4224 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004225 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004226
4227 DECLARE_INSTRUCTION(And);
4228
4229 private:
4230 DISALLOW_COPY_AND_ASSIGN(HAnd);
4231};
4232
4233class HOr : public HBinaryOperation {
4234 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004235 HOr(Primitive::Type result_type,
4236 HInstruction* left,
4237 HInstruction* right,
4238 uint32_t dex_pc = kNoDexPc)
4239 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004240
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004241 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004242
Roland Levillain9867bc72015-08-05 10:21:34 +01004243 template <typename T, typename U>
4244 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4245
4246 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004247 return GetBlock()->GetGraph()->GetIntConstant(
4248 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004249 }
4250 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004251 return GetBlock()->GetGraph()->GetLongConstant(
4252 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004253 }
4254 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004255 return GetBlock()->GetGraph()->GetLongConstant(
4256 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004257 }
4258 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004259 return GetBlock()->GetGraph()->GetLongConstant(
4260 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004261 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004262
4263 DECLARE_INSTRUCTION(Or);
4264
4265 private:
4266 DISALLOW_COPY_AND_ASSIGN(HOr);
4267};
4268
4269class HXor : public HBinaryOperation {
4270 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004271 HXor(Primitive::Type result_type,
4272 HInstruction* left,
4273 HInstruction* right,
4274 uint32_t dex_pc = kNoDexPc)
4275 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004276
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004277 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004278
Roland Levillain9867bc72015-08-05 10:21:34 +01004279 template <typename T, typename U>
4280 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4281
4282 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004283 return GetBlock()->GetGraph()->GetIntConstant(
4284 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004285 }
4286 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004287 return GetBlock()->GetGraph()->GetLongConstant(
4288 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004289 }
4290 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004291 return GetBlock()->GetGraph()->GetLongConstant(
4292 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004293 }
4294 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004295 return GetBlock()->GetGraph()->GetLongConstant(
4296 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004297 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004298
4299 DECLARE_INSTRUCTION(Xor);
4300
4301 private:
4302 DISALLOW_COPY_AND_ASSIGN(HXor);
4303};
4304
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004305class HRor : public HBinaryOperation {
4306 public:
4307 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4308 : HBinaryOperation(result_type, value, distance) {}
4309
4310 template <typename T, typename U, typename V>
4311 T Compute(T x, U y, V max_shift_value) const {
4312 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4313 "V is not the unsigned integer type corresponding to T");
4314 V ux = static_cast<V>(x);
4315 if ((y & max_shift_value) == 0) {
4316 return static_cast<T>(ux);
4317 } else {
4318 const V reg_bits = sizeof(T) * 8;
4319 return static_cast<T>(ux >> (y & max_shift_value)) |
4320 (x << (reg_bits - (y & max_shift_value)));
4321 }
4322 }
4323
4324 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4325 return GetBlock()->GetGraph()->GetIntConstant(
4326 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4327 }
4328 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4329 return GetBlock()->GetGraph()->GetLongConstant(
4330 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4331 }
4332 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4333 return GetBlock()->GetGraph()->GetLongConstant(
4334 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4335 }
4336
4337 DECLARE_INSTRUCTION(Ror);
4338
4339 private:
4340 DISALLOW_COPY_AND_ASSIGN(HRor);
4341};
4342
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004343// The value of a parameter in this method. Its location depends on
4344// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004345class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004346 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004347 HParameterValue(const DexFile& dex_file,
4348 uint16_t type_index,
4349 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004350 Primitive::Type parameter_type,
4351 bool is_this = false)
4352 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004353 dex_file_(dex_file),
4354 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004355 index_(index),
4356 is_this_(is_this),
4357 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004358
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004359 const DexFile& GetDexFile() const { return dex_file_; }
4360 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004361 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004362 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004363
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004364 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4365 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004366
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004367 DECLARE_INSTRUCTION(ParameterValue);
4368
4369 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004370 const DexFile& dex_file_;
4371 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004372 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004373 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004374 const uint8_t index_;
4375
Calin Juravle10e244f2015-01-26 18:54:32 +00004376 // Whether or not the parameter value corresponds to 'this' argument.
4377 const bool is_this_;
4378
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004379 bool can_be_null_;
4380
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004381 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4382};
4383
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004384class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004385 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004386 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4387 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004388
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004389 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004390 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004391 return true;
4392 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004393
Roland Levillain9867bc72015-08-05 10:21:34 +01004394 template <typename T> T Compute(T x) const { return ~x; }
4395
4396 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004397 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004398 }
4399 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004400 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004401 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004402
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004403 DECLARE_INSTRUCTION(Not);
4404
4405 private:
4406 DISALLOW_COPY_AND_ASSIGN(HNot);
4407};
4408
David Brazdil66d126e2015-04-03 16:02:44 +01004409class HBooleanNot : public HUnaryOperation {
4410 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004411 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4412 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004413
4414 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004415 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004416 return true;
4417 }
4418
Roland Levillain9867bc72015-08-05 10:21:34 +01004419 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004420 DCHECK(IsUint<1>(x));
4421 return !x;
4422 }
4423
Roland Levillain9867bc72015-08-05 10:21:34 +01004424 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004425 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004426 }
4427 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4428 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004429 UNREACHABLE();
4430 }
4431
4432 DECLARE_INSTRUCTION(BooleanNot);
4433
4434 private:
4435 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4436};
4437
Roland Levillaindff1f282014-11-05 14:15:05 +00004438class HTypeConversion : public HExpression<1> {
4439 public:
4440 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004441 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004442 : HExpression(result_type,
4443 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4444 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004445 SetRawInputAt(0, input);
4446 DCHECK_NE(input->GetType(), result_type);
4447 }
4448
4449 HInstruction* GetInput() const { return InputAt(0); }
4450 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4451 Primitive::Type GetResultType() const { return GetType(); }
4452
4453 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004454 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004455
Mark Mendelle82549b2015-05-06 10:55:34 -04004456 // Try to statically evaluate the conversion and return a HConstant
4457 // containing the result. If the input cannot be converted, return nullptr.
4458 HConstant* TryStaticEvaluation() const;
4459
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004460 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4461 Primitive::Type result_type) {
4462 // Some architectures may not require the 'GC' side effects, but at this point
4463 // in the compilation process we do not know what architecture we will
4464 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004465 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4466 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004467 return SideEffects::CanTriggerGC();
4468 }
4469 return SideEffects::None();
4470 }
4471
Roland Levillaindff1f282014-11-05 14:15:05 +00004472 DECLARE_INSTRUCTION(TypeConversion);
4473
4474 private:
4475 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4476};
4477
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004478static constexpr uint32_t kNoRegNumber = -1;
4479
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004480class HPhi : public HInstruction {
4481 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004482 HPhi(ArenaAllocator* arena,
4483 uint32_t reg_number,
4484 size_t number_of_inputs,
4485 Primitive::Type type,
4486 uint32_t dex_pc = kNoDexPc)
4487 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004488 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004489 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004490 type_(ToPhiType(type)),
4491 // Phis are constructed live and marked dead if conflicting or unused.
4492 // Individual steps of SsaBuilder should assume that if a phi has been
4493 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4494 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004495 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004496 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004497 }
4498
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004499 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4500 static Primitive::Type ToPhiType(Primitive::Type type) {
4501 switch (type) {
4502 case Primitive::kPrimBoolean:
4503 case Primitive::kPrimByte:
4504 case Primitive::kPrimShort:
4505 case Primitive::kPrimChar:
4506 return Primitive::kPrimInt;
4507 default:
4508 return type;
4509 }
4510 }
4511
David Brazdilffee3d32015-07-06 11:48:53 +01004512 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4513
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004514 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004515
4516 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004517 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004518
Calin Juravle10e244f2015-01-26 18:54:32 +00004519 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil4833f5a2015-12-16 10:37:39 +00004520 void SetType(Primitive::Type new_type) {
4521 // Make sure that only valid type changes occur. The following are allowed:
4522 // (1) int -> float/ref (primitive type propagation),
4523 // (2) long -> double (primitive type propagation).
4524 DCHECK(type_ == new_type ||
4525 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4526 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4527 (type_ == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4528 type_ = new_type;
4529 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004530
Calin Juravle10e244f2015-01-26 18:54:32 +00004531 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4532 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4533
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004534 uint32_t GetRegNumber() const { return reg_number_; }
4535
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004536 void SetDead() { is_live_ = false; }
4537 void SetLive() { is_live_ = true; }
4538 bool IsDead() const { return !is_live_; }
4539 bool IsLive() const { return is_live_; }
4540
David Brazdil77a48ae2015-09-15 12:34:04 +00004541 bool IsVRegEquivalentOf(HInstruction* other) const {
4542 return other != nullptr
4543 && other->IsPhi()
4544 && other->AsPhi()->GetBlock() == GetBlock()
4545 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4546 }
4547
Calin Juravlea4f88312015-04-16 12:57:19 +01004548 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4549 // An equivalent phi is a phi having the same dex register and type.
4550 // It assumes that phis with the same dex register are adjacent.
4551 HPhi* GetNextEquivalentPhiWithSameType() {
4552 HInstruction* next = GetNext();
4553 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4554 if (next->GetType() == GetType()) {
4555 return next->AsPhi();
4556 }
4557 next = next->GetNext();
4558 }
4559 return nullptr;
4560 }
4561
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004562 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004563
David Brazdil1abb4192015-02-17 18:33:36 +00004564 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004565 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004566 return inputs_[index];
4567 }
David Brazdil1abb4192015-02-17 18:33:36 +00004568
4569 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004570 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004571 }
4572
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004573 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004574 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004575 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004576 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004577 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004578 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004579
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004580 DISALLOW_COPY_AND_ASSIGN(HPhi);
4581};
4582
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004583class HNullCheck : public HExpression<1> {
4584 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004585 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4586 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004587 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004588 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004589 SetRawInputAt(0, value);
4590 }
4591
Calin Juravle10e244f2015-01-26 18:54:32 +00004592 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004593 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004594 return true;
4595 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004596
Calin Juravle10e244f2015-01-26 18:54:32 +00004597 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004598
Calin Juravle10e244f2015-01-26 18:54:32 +00004599 bool CanThrow() const OVERRIDE { return true; }
4600
4601 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004602
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004603
4604 DECLARE_INSTRUCTION(NullCheck);
4605
4606 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004607 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4608};
4609
4610class FieldInfo : public ValueObject {
4611 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004612 FieldInfo(MemberOffset field_offset,
4613 Primitive::Type field_type,
4614 bool is_volatile,
4615 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004616 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004617 const DexFile& dex_file,
4618 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004619 : field_offset_(field_offset),
4620 field_type_(field_type),
4621 is_volatile_(is_volatile),
4622 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004623 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004624 dex_file_(dex_file),
4625 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004626
4627 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004628 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004629 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004630 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004631 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004632 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004633 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004634
4635 private:
4636 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004637 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004638 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004639 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004640 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004641 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004642 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004643};
4644
4645class HInstanceFieldGet : public HExpression<1> {
4646 public:
4647 HInstanceFieldGet(HInstruction* value,
4648 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004649 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004650 bool is_volatile,
4651 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004652 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004653 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004654 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004655 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004656 : HExpression(field_type,
4657 SideEffects::FieldReadOfType(field_type, is_volatile),
4658 dex_pc),
4659 field_info_(field_offset,
4660 field_type,
4661 is_volatile,
4662 field_idx,
4663 declaring_class_def_index,
4664 dex_file,
4665 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004666 SetRawInputAt(0, value);
4667 }
4668
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004669 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004670
4671 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4672 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4673 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004674 }
4675
Calin Juravle641547a2015-04-21 22:08:51 +01004676 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4677 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004678 }
4679
4680 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004681 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4682 }
4683
Calin Juravle52c48962014-12-16 17:02:57 +00004684 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004685 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004686 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004687 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004688
4689 DECLARE_INSTRUCTION(InstanceFieldGet);
4690
4691 private:
4692 const FieldInfo field_info_;
4693
4694 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4695};
4696
4697class HInstanceFieldSet : public HTemplateInstruction<2> {
4698 public:
4699 HInstanceFieldSet(HInstruction* object,
4700 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004701 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004702 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004703 bool is_volatile,
4704 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004705 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004706 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004707 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004708 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004709 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4710 dex_pc),
4711 field_info_(field_offset,
4712 field_type,
4713 is_volatile,
4714 field_idx,
4715 declaring_class_def_index,
4716 dex_file,
4717 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004718 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004719 SetRawInputAt(0, object);
4720 SetRawInputAt(1, value);
4721 }
4722
Calin Juravle641547a2015-04-21 22:08:51 +01004723 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4724 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004725 }
4726
Calin Juravle52c48962014-12-16 17:02:57 +00004727 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004728 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004729 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004730 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004731 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004732 bool GetValueCanBeNull() const { return value_can_be_null_; }
4733 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004734
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004735 DECLARE_INSTRUCTION(InstanceFieldSet);
4736
4737 private:
4738 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004739 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004740
4741 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4742};
4743
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004744class HArrayGet : public HExpression<2> {
4745 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004746 HArrayGet(HInstruction* array,
4747 HInstruction* index,
4748 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004749 uint32_t dex_pc,
4750 SideEffects additional_side_effects = SideEffects::None())
4751 : HExpression(type,
4752 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004753 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004754 SetRawInputAt(0, array);
4755 SetRawInputAt(1, index);
4756 }
4757
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004758 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004759 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004760 return true;
4761 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004762 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004763 // TODO: We can be smarter here.
4764 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4765 // which generates the implicit null check. There are cases when these can be removed
4766 // to produce better code. If we ever add optimizations to do so we should allow an
4767 // implicit check here (as long as the address falls in the first page).
4768 return false;
4769 }
4770
David Brazdil4833f5a2015-12-16 10:37:39 +00004771 bool IsEquivalentOf(HArrayGet* other) const {
4772 bool result = (GetDexPc() == other->GetDexPc());
4773 if (kIsDebugBuild && result) {
4774 DCHECK_EQ(GetBlock(), other->GetBlock());
4775 DCHECK_EQ(GetArray(), other->GetArray());
4776 DCHECK_EQ(GetIndex(), other->GetIndex());
4777 if (Primitive::IsIntOrLongType(GetType())) {
4778 DCHECK(Primitive::IsFloatingPointType(other->GetType()));
4779 } else {
4780 DCHECK(Primitive::IsFloatingPointType(GetType()));
4781 DCHECK(Primitive::IsIntOrLongType(other->GetType()));
4782 }
4783 }
4784 return result;
4785 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004786
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004787 HInstruction* GetArray() const { return InputAt(0); }
4788 HInstruction* GetIndex() const { return InputAt(1); }
4789
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004790 DECLARE_INSTRUCTION(ArrayGet);
4791
4792 private:
4793 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4794};
4795
4796class HArraySet : public HTemplateInstruction<3> {
4797 public:
4798 HArraySet(HInstruction* array,
4799 HInstruction* index,
4800 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004801 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004802 uint32_t dex_pc,
4803 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004804 : HTemplateInstruction(
4805 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004806 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4807 additional_side_effects),
4808 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004809 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004810 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004811 value_can_be_null_(true),
4812 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004813 SetRawInputAt(0, array);
4814 SetRawInputAt(1, index);
4815 SetRawInputAt(2, value);
4816 }
4817
Calin Juravle77520bc2015-01-12 18:45:46 +00004818 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00004819 // We call a runtime method to throw ArrayStoreException.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004820 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004821 }
4822
Mingyao Yang81014cb2015-06-02 03:16:27 -07004823 // Can throw ArrayStoreException.
4824 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4825
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004826 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004827 // TODO: Same as for ArrayGet.
4828 return false;
4829 }
4830
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004831 void ClearNeedsTypeCheck() {
4832 needs_type_check_ = false;
4833 }
4834
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004835 void ClearValueCanBeNull() {
4836 value_can_be_null_ = false;
4837 }
4838
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004839 void SetStaticTypeOfArrayIsObjectArray() {
4840 static_type_of_array_is_object_array_ = true;
4841 }
4842
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004843 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004844 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004845 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004846
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004847 HInstruction* GetArray() const { return InputAt(0); }
4848 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004849 HInstruction* GetValue() const { return InputAt(2); }
4850
4851 Primitive::Type GetComponentType() const {
4852 // The Dex format does not type floating point index operations. Since the
4853 // `expected_component_type_` is set during building and can therefore not
4854 // be correct, we also check what is the value type. If it is a floating
4855 // point type, we must use that type.
4856 Primitive::Type value_type = GetValue()->GetType();
4857 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4858 ? value_type
4859 : expected_component_type_;
4860 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004861
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004862 Primitive::Type GetRawExpectedComponentType() const {
4863 return expected_component_type_;
4864 }
4865
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004866 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4867 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4868 }
4869
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004870 DECLARE_INSTRUCTION(ArraySet);
4871
4872 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004873 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004874 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004875 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004876 // Cached information for the reference_type_info_ so that codegen
4877 // does not need to inspect the static type.
4878 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004879
4880 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4881};
4882
4883class HArrayLength : public HExpression<1> {
4884 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004885 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004886 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004887 // Note that arrays do not change length, so the instruction does not
4888 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004889 SetRawInputAt(0, array);
4890 }
4891
Calin Juravle77520bc2015-01-12 18:45:46 +00004892 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004893 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004894 return true;
4895 }
Calin Juravle641547a2015-04-21 22:08:51 +01004896 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4897 return obj == InputAt(0);
4898 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004899
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004900 DECLARE_INSTRUCTION(ArrayLength);
4901
4902 private:
4903 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4904};
4905
4906class HBoundsCheck : public HExpression<2> {
4907 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004908 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
4909 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004910 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004911 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004912 DCHECK(index->GetType() == Primitive::kPrimInt);
4913 SetRawInputAt(0, index);
4914 SetRawInputAt(1, length);
4915 }
4916
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004917 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004918 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004919 return true;
4920 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004921
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004922 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004923
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004924 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004925
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004926 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004927
4928 DECLARE_INSTRUCTION(BoundsCheck);
4929
4930 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004931 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4932};
4933
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004934/**
4935 * Some DEX instructions are folded into multiple HInstructions that need
4936 * to stay live until the last HInstruction. This class
4937 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004938 * HInstruction stays live. `index` represents the stack location index of the
4939 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004940 */
4941class HTemporary : public HTemplateInstruction<0> {
4942 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004943 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4944 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004945
4946 size_t GetIndex() const { return index_; }
4947
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004948 Primitive::Type GetType() const OVERRIDE {
4949 // The previous instruction is the one that will be stored in the temporary location.
4950 DCHECK(GetPrevious() != nullptr);
4951 return GetPrevious()->GetType();
4952 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004953
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004954 DECLARE_INSTRUCTION(Temporary);
4955
4956 private:
4957 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004958 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4959};
4960
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004961class HSuspendCheck : public HTemplateInstruction<0> {
4962 public:
4963 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004964 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004965
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004966 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004967 return true;
4968 }
4969
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004970 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4971 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004972
4973 DECLARE_INSTRUCTION(SuspendCheck);
4974
4975 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004976 // Only used for code generation, in order to share the same slow path between back edges
4977 // of a same loop.
4978 SlowPathCode* slow_path_;
4979
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004980 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4981};
4982
David Srbecky0cf44932015-12-09 14:09:59 +00004983// Pseudo-instruction which provides the native debugger with mapping information.
4984// It ensures that we can generate line number and local variables at this point.
4985class HNativeDebugInfo : public HTemplateInstruction<0> {
4986 public:
4987 explicit HNativeDebugInfo(uint32_t dex_pc)
4988 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
4989
4990 bool NeedsEnvironment() const OVERRIDE {
4991 return true;
4992 }
4993
4994 DECLARE_INSTRUCTION(NativeDebugInfo);
4995
4996 private:
4997 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
4998};
4999
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005000/**
5001 * Instruction to load a Class object.
5002 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005003class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005004 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005005 HLoadClass(HCurrentMethod* current_method,
5006 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005007 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005008 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005009 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005010 bool needs_access_check,
5011 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005012 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005013 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005014 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005015 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00005016 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01005017 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005018 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00005019 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005020 // Referrers class should not need access check. We never inline unverified
5021 // methods so we can't possibly end up in this situation.
5022 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005023 SetRawInputAt(0, current_method);
5024 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005025
5026 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005027
5028 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005029 // Note that we don't need to test for generate_clinit_check_.
5030 // Whether or not we need to generate the clinit check is processed in
5031 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005032 return other->AsLoadClass()->type_index_ == type_index_ &&
5033 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005034 }
5035
5036 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5037
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005038 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005039 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005040 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005041
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005042 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005043 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005044 }
5045
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005046 bool MustGenerateClinitCheck() const {
5047 return generate_clinit_check_;
5048 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005049
Calin Juravle0ba218d2015-05-19 18:46:01 +01005050 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005051 // The entrypoint the code generator is going to call does not do
5052 // clinit of the class.
5053 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01005054 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005055 }
5056
5057 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005058 return MustGenerateClinitCheck() ||
5059 (!is_referrers_class_ && !is_in_dex_cache_) ||
5060 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01005061 }
5062
5063 bool NeedsAccessCheck() const {
5064 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005065 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005066
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005067 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005068 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005069 }
5070
Calin Juravleacf735c2015-02-12 15:25:22 +00005071 ReferenceTypeInfo GetLoadedClassRTI() {
5072 return loaded_class_rti_;
5073 }
5074
5075 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5076 // Make sure we only set exact types (the loaded class should never be merged).
5077 DCHECK(rti.IsExact());
5078 loaded_class_rti_ = rti;
5079 }
5080
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005081 const DexFile& GetDexFile() { return dex_file_; }
5082
Vladimir Markodc151b22015-10-15 18:02:30 +01005083 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005084
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005085 static SideEffects SideEffectsForArchRuntimeCalls() {
5086 return SideEffects::CanTriggerGC();
5087 }
5088
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005089 bool IsInDexCache() const { return is_in_dex_cache_; }
5090
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005091 DECLARE_INSTRUCTION(LoadClass);
5092
5093 private:
5094 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005095 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005096 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005097 // Whether this instruction must generate the initialization check.
5098 // Used for code generation.
5099 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005100 const bool needs_access_check_;
5101 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005102
Calin Juravleacf735c2015-02-12 15:25:22 +00005103 ReferenceTypeInfo loaded_class_rti_;
5104
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005105 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5106};
5107
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005108class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005109 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005110 HLoadString(HCurrentMethod* current_method,
5111 uint32_t string_index,
5112 uint32_t dex_pc,
5113 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005114 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005115 string_index_(string_index),
5116 is_in_dex_cache_(is_in_dex_cache) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005117 SetRawInputAt(0, current_method);
5118 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005119
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005120 bool CanBeMoved() const OVERRIDE { return true; }
5121
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005122 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5123 return other->AsLoadString()->string_index_ == string_index_;
5124 }
5125
5126 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5127
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005128 uint32_t GetStringIndex() const { return string_index_; }
5129
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005130 // Will call the runtime if the string is not already in the dex cache.
5131 bool NeedsEnvironment() const OVERRIDE { return !IsInDexCache(); }
5132
Vladimir Markodc151b22015-10-15 18:02:30 +01005133 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005134 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005135 bool IsInDexCache() const { return is_in_dex_cache_; }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005136 bool CanThrow() const OVERRIDE { return !IsInDexCache(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005137
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005138 static SideEffects SideEffectsForArchRuntimeCalls() {
5139 return SideEffects::CanTriggerGC();
5140 }
5141
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005142 DECLARE_INSTRUCTION(LoadString);
5143
5144 private:
5145 const uint32_t string_index_;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005146 const bool is_in_dex_cache_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005147
5148 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5149};
5150
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005151/**
5152 * Performs an initialization check on its Class object input.
5153 */
5154class HClinitCheck : public HExpression<1> {
5155 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005156 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005157 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005158 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005159 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5160 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005161 SetRawInputAt(0, constant);
5162 }
5163
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005164 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005165 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005166 return true;
5167 }
5168
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005169 bool NeedsEnvironment() const OVERRIDE {
5170 // May call runtime to initialize the class.
5171 return true;
5172 }
5173
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005174 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005175
5176 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5177
5178 DECLARE_INSTRUCTION(ClinitCheck);
5179
5180 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005181 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5182};
5183
5184class HStaticFieldGet : public HExpression<1> {
5185 public:
5186 HStaticFieldGet(HInstruction* cls,
5187 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005188 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005189 bool is_volatile,
5190 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005191 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005192 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005193 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005194 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005195 : HExpression(field_type,
5196 SideEffects::FieldReadOfType(field_type, is_volatile),
5197 dex_pc),
5198 field_info_(field_offset,
5199 field_type,
5200 is_volatile,
5201 field_idx,
5202 declaring_class_def_index,
5203 dex_file,
5204 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005205 SetRawInputAt(0, cls);
5206 }
5207
Calin Juravle52c48962014-12-16 17:02:57 +00005208
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005209 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005210
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005211 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005212 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5213 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005214 }
5215
5216 size_t ComputeHashCode() const OVERRIDE {
5217 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5218 }
5219
Calin Juravle52c48962014-12-16 17:02:57 +00005220 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005221 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5222 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005223 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005224
5225 DECLARE_INSTRUCTION(StaticFieldGet);
5226
5227 private:
5228 const FieldInfo field_info_;
5229
5230 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5231};
5232
5233class HStaticFieldSet : public HTemplateInstruction<2> {
5234 public:
5235 HStaticFieldSet(HInstruction* cls,
5236 HInstruction* value,
5237 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005238 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005239 bool is_volatile,
5240 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005241 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005242 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005243 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005244 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005245 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5246 dex_pc),
5247 field_info_(field_offset,
5248 field_type,
5249 is_volatile,
5250 field_idx,
5251 declaring_class_def_index,
5252 dex_file,
5253 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005254 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005255 SetRawInputAt(0, cls);
5256 SetRawInputAt(1, value);
5257 }
5258
Calin Juravle52c48962014-12-16 17:02:57 +00005259 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005260 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5261 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005262 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005263
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005264 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005265 bool GetValueCanBeNull() const { return value_can_be_null_; }
5266 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005267
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005268 DECLARE_INSTRUCTION(StaticFieldSet);
5269
5270 private:
5271 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005272 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005273
5274 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5275};
5276
Calin Juravlee460d1d2015-09-29 04:52:17 +01005277class HUnresolvedInstanceFieldGet : public HExpression<1> {
5278 public:
5279 HUnresolvedInstanceFieldGet(HInstruction* obj,
5280 Primitive::Type field_type,
5281 uint32_t field_index,
5282 uint32_t dex_pc)
5283 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5284 field_index_(field_index) {
5285 SetRawInputAt(0, obj);
5286 }
5287
5288 bool NeedsEnvironment() const OVERRIDE { return true; }
5289 bool CanThrow() const OVERRIDE { return true; }
5290
5291 Primitive::Type GetFieldType() const { return GetType(); }
5292 uint32_t GetFieldIndex() const { return field_index_; }
5293
5294 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5295
5296 private:
5297 const uint32_t field_index_;
5298
5299 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5300};
5301
5302class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5303 public:
5304 HUnresolvedInstanceFieldSet(HInstruction* obj,
5305 HInstruction* value,
5306 Primitive::Type field_type,
5307 uint32_t field_index,
5308 uint32_t dex_pc)
5309 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5310 field_type_(field_type),
5311 field_index_(field_index) {
5312 DCHECK_EQ(field_type, value->GetType());
5313 SetRawInputAt(0, obj);
5314 SetRawInputAt(1, value);
5315 }
5316
5317 bool NeedsEnvironment() const OVERRIDE { return true; }
5318 bool CanThrow() const OVERRIDE { return true; }
5319
5320 Primitive::Type GetFieldType() const { return field_type_; }
5321 uint32_t GetFieldIndex() const { return field_index_; }
5322
5323 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5324
5325 private:
5326 const Primitive::Type field_type_;
5327 const uint32_t field_index_;
5328
5329 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5330};
5331
5332class HUnresolvedStaticFieldGet : public HExpression<0> {
5333 public:
5334 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5335 uint32_t field_index,
5336 uint32_t dex_pc)
5337 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5338 field_index_(field_index) {
5339 }
5340
5341 bool NeedsEnvironment() const OVERRIDE { return true; }
5342 bool CanThrow() const OVERRIDE { return true; }
5343
5344 Primitive::Type GetFieldType() const { return GetType(); }
5345 uint32_t GetFieldIndex() const { return field_index_; }
5346
5347 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5348
5349 private:
5350 const uint32_t field_index_;
5351
5352 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5353};
5354
5355class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5356 public:
5357 HUnresolvedStaticFieldSet(HInstruction* value,
5358 Primitive::Type field_type,
5359 uint32_t field_index,
5360 uint32_t dex_pc)
5361 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5362 field_type_(field_type),
5363 field_index_(field_index) {
5364 DCHECK_EQ(field_type, value->GetType());
5365 SetRawInputAt(0, value);
5366 }
5367
5368 bool NeedsEnvironment() const OVERRIDE { return true; }
5369 bool CanThrow() const OVERRIDE { return true; }
5370
5371 Primitive::Type GetFieldType() const { return field_type_; }
5372 uint32_t GetFieldIndex() const { return field_index_; }
5373
5374 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5375
5376 private:
5377 const Primitive::Type field_type_;
5378 const uint32_t field_index_;
5379
5380 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5381};
5382
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005383// Implement the move-exception DEX instruction.
5384class HLoadException : public HExpression<0> {
5385 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005386 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5387 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005388
David Brazdilbbd733e2015-08-18 17:48:17 +01005389 bool CanBeNull() const OVERRIDE { return false; }
5390
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005391 DECLARE_INSTRUCTION(LoadException);
5392
5393 private:
5394 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5395};
5396
David Brazdilcb1c0552015-08-04 16:22:25 +01005397// Implicit part of move-exception which clears thread-local exception storage.
5398// Must not be removed because the runtime expects the TLS to get cleared.
5399class HClearException : public HTemplateInstruction<0> {
5400 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005401 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5402 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005403
5404 DECLARE_INSTRUCTION(ClearException);
5405
5406 private:
5407 DISALLOW_COPY_AND_ASSIGN(HClearException);
5408};
5409
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005410class HThrow : public HTemplateInstruction<1> {
5411 public:
5412 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005413 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005414 SetRawInputAt(0, exception);
5415 }
5416
5417 bool IsControlFlow() const OVERRIDE { return true; }
5418
5419 bool NeedsEnvironment() const OVERRIDE { return true; }
5420
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005421 bool CanThrow() const OVERRIDE { return true; }
5422
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005423
5424 DECLARE_INSTRUCTION(Throw);
5425
5426 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005427 DISALLOW_COPY_AND_ASSIGN(HThrow);
5428};
5429
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005430/**
5431 * Implementation strategies for the code generator of a HInstanceOf
5432 * or `HCheckCast`.
5433 */
5434enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005435 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005436 kExactCheck, // Can do a single class compare.
5437 kClassHierarchyCheck, // Can just walk the super class chain.
5438 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5439 kInterfaceCheck, // No optimization yet when checking against an interface.
5440 kArrayObjectCheck, // Can just check if the array is not primitive.
5441 kArrayCheck // No optimization yet when checking against a generic array.
5442};
5443
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005444class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005445 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005446 HInstanceOf(HInstruction* object,
5447 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005448 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005449 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005450 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005451 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005452 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005453 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005454 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005455 SetRawInputAt(0, object);
5456 SetRawInputAt(1, constant);
5457 }
5458
5459 bool CanBeMoved() const OVERRIDE { return true; }
5460
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005461 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005462 return true;
5463 }
5464
5465 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005466 return CanCallRuntime(check_kind_);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005467 }
5468
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005469 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5470
5471 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005472
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005473 // Used only in code generation.
5474 bool MustDoNullCheck() const { return must_do_null_check_; }
5475 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5476
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005477 static bool CanCallRuntime(TypeCheckKind check_kind) {
5478 // Mips currently does runtime calls for any other checks.
5479 return check_kind != TypeCheckKind::kExactCheck;
5480 }
5481
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005482 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005483 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005484 }
5485
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005486 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005487
5488 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005489 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005490 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005491
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005492 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5493};
5494
Calin Juravleb1498f62015-02-16 13:13:29 +00005495class HBoundType : public HExpression<1> {
5496 public:
David Brazdilf5552582015-12-27 13:36:12 +00005497 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005498 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
David Brazdilf5552582015-12-27 13:36:12 +00005499 upper_bound_(ReferenceTypeInfo::CreateInvalid()),
5500 upper_can_be_null_(true),
5501 can_be_null_(true) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005502 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005503 SetRawInputAt(0, input);
5504 }
5505
David Brazdilf5552582015-12-27 13:36:12 +00005506 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005507 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5508 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
David Brazdilf5552582015-12-27 13:36:12 +00005509 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005510
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005511 void SetCanBeNull(bool can_be_null) {
5512 DCHECK(upper_can_be_null_ || !can_be_null);
5513 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005514 }
5515
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005516 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5517
Calin Juravleb1498f62015-02-16 13:13:29 +00005518 DECLARE_INSTRUCTION(BoundType);
5519
5520 private:
5521 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005522 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5523 // It is used to bound the type in cases like:
5524 // if (x instanceof ClassX) {
5525 // // uper_bound_ will be ClassX
5526 // }
David Brazdilf5552582015-12-27 13:36:12 +00005527 ReferenceTypeInfo upper_bound_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005528 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5529 // is false then can_be_null_ cannot be true).
David Brazdilf5552582015-12-27 13:36:12 +00005530 bool upper_can_be_null_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005531 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005532
5533 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5534};
5535
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005536class HCheckCast : public HTemplateInstruction<2> {
5537 public:
5538 HCheckCast(HInstruction* object,
5539 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005540 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005541 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005542 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005543 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005544 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005545 SetRawInputAt(0, object);
5546 SetRawInputAt(1, constant);
5547 }
5548
5549 bool CanBeMoved() const OVERRIDE { return true; }
5550
5551 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5552 return true;
5553 }
5554
5555 bool NeedsEnvironment() const OVERRIDE {
5556 // Instruction may throw a CheckCastError.
5557 return true;
5558 }
5559
5560 bool CanThrow() const OVERRIDE { return true; }
5561
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005562 bool MustDoNullCheck() const { return must_do_null_check_; }
5563 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005564 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005565
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005566 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005567
5568 DECLARE_INSTRUCTION(CheckCast);
5569
5570 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005571 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005572 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005573
5574 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005575};
5576
Calin Juravle27df7582015-04-17 19:12:31 +01005577class HMemoryBarrier : public HTemplateInstruction<0> {
5578 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005579 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005580 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005581 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005582 barrier_kind_(barrier_kind) {}
5583
5584 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5585
5586 DECLARE_INSTRUCTION(MemoryBarrier);
5587
5588 private:
5589 const MemBarrierKind barrier_kind_;
5590
5591 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5592};
5593
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005594class HMonitorOperation : public HTemplateInstruction<1> {
5595 public:
5596 enum OperationKind {
5597 kEnter,
5598 kExit,
5599 };
5600
5601 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005602 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005603 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5604 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005605 SetRawInputAt(0, object);
5606 }
5607
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005608 // Instruction may go into runtime, so we need an environment.
5609 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00005610
5611 bool CanThrow() const OVERRIDE {
5612 // Verifier guarantees that monitor-exit cannot throw.
5613 // This is important because it allows the HGraphBuilder to remove
5614 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5615 return IsEnter();
5616 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005617
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005618
5619 bool IsEnter() const { return kind_ == kEnter; }
5620
5621 DECLARE_INSTRUCTION(MonitorOperation);
5622
Calin Juravle52c48962014-12-16 17:02:57 +00005623 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005624 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005625
5626 private:
5627 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5628};
5629
David Brazdil74eb1b22015-12-14 11:44:01 +00005630class HSelect : public HExpression<3> {
5631 public:
5632 HSelect(HInstruction* condition,
5633 HInstruction* true_value,
5634 HInstruction* false_value,
5635 uint32_t dex_pc)
5636 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
5637 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
5638
5639 // First input must be `true_value` or `false_value` to allow codegens to
5640 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
5641 // that architectures which implement HSelect as a conditional move also
5642 // will not need to invert the condition.
5643 SetRawInputAt(0, false_value);
5644 SetRawInputAt(1, true_value);
5645 SetRawInputAt(2, condition);
5646 }
5647
5648 HInstruction* GetFalseValue() const { return InputAt(0); }
5649 HInstruction* GetTrueValue() const { return InputAt(1); }
5650 HInstruction* GetCondition() const { return InputAt(2); }
5651
5652 bool CanBeMoved() const OVERRIDE { return true; }
5653 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
5654
5655 bool CanBeNull() const OVERRIDE {
5656 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
5657 }
5658
5659 DECLARE_INSTRUCTION(Select);
5660
5661 private:
5662 DISALLOW_COPY_AND_ASSIGN(HSelect);
5663};
5664
Vladimir Markof9f64412015-09-02 14:05:49 +01005665class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005666 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005667 MoveOperands(Location source,
5668 Location destination,
5669 Primitive::Type type,
5670 HInstruction* instruction)
5671 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005672
5673 Location GetSource() const { return source_; }
5674 Location GetDestination() const { return destination_; }
5675
5676 void SetSource(Location value) { source_ = value; }
5677 void SetDestination(Location value) { destination_ = value; }
5678
5679 // The parallel move resolver marks moves as "in-progress" by clearing the
5680 // destination (but not the source).
5681 Location MarkPending() {
5682 DCHECK(!IsPending());
5683 Location dest = destination_;
5684 destination_ = Location::NoLocation();
5685 return dest;
5686 }
5687
5688 void ClearPending(Location dest) {
5689 DCHECK(IsPending());
5690 destination_ = dest;
5691 }
5692
5693 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00005694 DCHECK(source_.IsValid() || destination_.IsInvalid());
5695 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005696 }
5697
5698 // True if this blocks a move from the given location.
5699 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005700 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005701 }
5702
5703 // A move is redundant if it's been eliminated, if its source and
5704 // destination are the same, or if its destination is unneeded.
5705 bool IsRedundant() const {
5706 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5707 }
5708
5709 // We clear both operands to indicate move that's been eliminated.
5710 void Eliminate() {
5711 source_ = destination_ = Location::NoLocation();
5712 }
5713
5714 bool IsEliminated() const {
5715 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5716 return source_.IsInvalid();
5717 }
5718
Alexey Frunze4dda3372015-06-01 18:31:49 -07005719 Primitive::Type GetType() const { return type_; }
5720
Nicolas Geoffray90218252015-04-15 11:56:51 +01005721 bool Is64BitMove() const {
5722 return Primitive::Is64BitType(type_);
5723 }
5724
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005725 HInstruction* GetInstruction() const { return instruction_; }
5726
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005727 private:
5728 Location source_;
5729 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005730 // The type this move is for.
5731 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005732 // The instruction this move is assocatied with. Null when this move is
5733 // for moving an input in the expected locations of user (including a phi user).
5734 // This is only used in debug mode, to ensure we do not connect interval siblings
5735 // in the same parallel move.
5736 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005737};
5738
Roland Levillainc9285912015-12-18 10:38:42 +00005739std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
5740
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005741static constexpr size_t kDefaultNumberOfMoves = 4;
5742
5743class HParallelMove : public HTemplateInstruction<0> {
5744 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005745 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005746 : HTemplateInstruction(SideEffects::None(), dex_pc),
5747 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5748 moves_.reserve(kDefaultNumberOfMoves);
5749 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005750
Nicolas Geoffray90218252015-04-15 11:56:51 +01005751 void AddMove(Location source,
5752 Location destination,
5753 Primitive::Type type,
5754 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005755 DCHECK(source.IsValid());
5756 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005757 if (kIsDebugBuild) {
5758 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005759 for (const MoveOperands& move : moves_) {
5760 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005761 // Special case the situation where the move is for the spill slot
5762 // of the instruction.
5763 if ((GetPrevious() == instruction)
5764 || ((GetPrevious() == nullptr)
5765 && instruction->IsPhi()
5766 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005767 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005768 << "Doing parallel moves for the same instruction.";
5769 } else {
5770 DCHECK(false) << "Doing parallel moves for the same instruction.";
5771 }
5772 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005773 }
5774 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005775 for (const MoveOperands& move : moves_) {
5776 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005777 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005778 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005779 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005780 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005781 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005782 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005783 }
5784
Vladimir Marko225b6462015-09-28 12:17:40 +01005785 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005786 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005787 }
5788
Vladimir Marko225b6462015-09-28 12:17:40 +01005789 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005790
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005791 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005792
5793 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005794 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005795
5796 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5797};
5798
Mark Mendell0616ae02015-04-17 12:49:27 -04005799} // namespace art
5800
Vladimir Markob4536b72015-11-24 13:45:23 +00005801#ifdef ART_ENABLE_CODEGEN_arm
5802#include "nodes_arm.h"
5803#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005804#ifdef ART_ENABLE_CODEGEN_arm64
5805#include "nodes_arm64.h"
5806#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005807#ifdef ART_ENABLE_CODEGEN_x86
5808#include "nodes_x86.h"
5809#endif
5810
5811namespace art {
5812
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005813class HGraphVisitor : public ValueObject {
5814 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005815 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5816 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005817
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005818 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005819 virtual void VisitBasicBlock(HBasicBlock* block);
5820
Roland Levillain633021e2014-10-01 14:12:25 +01005821 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005822 void VisitInsertionOrder();
5823
Roland Levillain633021e2014-10-01 14:12:25 +01005824 // Visit the graph following dominator tree reverse post-order.
5825 void VisitReversePostOrder();
5826
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005827 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005828
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005829 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005830#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005831 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5832
5833 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5834
5835#undef DECLARE_VISIT_INSTRUCTION
5836
5837 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005838 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005839
5840 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5841};
5842
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005843class HGraphDelegateVisitor : public HGraphVisitor {
5844 public:
5845 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5846 virtual ~HGraphDelegateVisitor() {}
5847
5848 // Visit functions that delegate to to super class.
5849#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005850 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005851
5852 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5853
5854#undef DECLARE_VISIT_INSTRUCTION
5855
5856 private:
5857 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5858};
5859
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005860class HInsertionOrderIterator : public ValueObject {
5861 public:
5862 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5863
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005864 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005865 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005866 void Advance() { ++index_; }
5867
5868 private:
5869 const HGraph& graph_;
5870 size_t index_;
5871
5872 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5873};
5874
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005875class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005876 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005877 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5878 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005879 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005880 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005881
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005882 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5883 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005884 void Advance() { ++index_; }
5885
5886 private:
5887 const HGraph& graph_;
5888 size_t index_;
5889
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005890 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005891};
5892
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005893class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005894 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005895 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005896 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005897 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005898 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005899 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005900
5901 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005902 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005903 void Advance() { --index_; }
5904
5905 private:
5906 const HGraph& graph_;
5907 size_t index_;
5908
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005909 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005910};
5911
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005912class HLinearPostOrderIterator : public ValueObject {
5913 public:
5914 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005915 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005916
5917 bool Done() const { return index_ == 0; }
5918
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005919 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005920
5921 void Advance() {
5922 --index_;
5923 DCHECK_GE(index_, 0U);
5924 }
5925
5926 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005927 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005928 size_t index_;
5929
5930 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5931};
5932
5933class HLinearOrderIterator : public ValueObject {
5934 public:
5935 explicit HLinearOrderIterator(const HGraph& graph)
5936 : order_(graph.GetLinearOrder()), index_(0) {}
5937
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005938 bool Done() const { return index_ == order_.size(); }
5939 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005940 void Advance() { ++index_; }
5941
5942 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005943 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005944 size_t index_;
5945
5946 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5947};
5948
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005949// Iterator over the blocks that art part of the loop. Includes blocks part
5950// of an inner loop. The order in which the blocks are iterated is on their
5951// block id.
5952class HBlocksInLoopIterator : public ValueObject {
5953 public:
5954 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5955 : blocks_in_loop_(info.GetBlocks()),
5956 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5957 index_(0) {
5958 if (!blocks_in_loop_.IsBitSet(index_)) {
5959 Advance();
5960 }
5961 }
5962
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005963 bool Done() const { return index_ == blocks_.size(); }
5964 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005965 void Advance() {
5966 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005967 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005968 if (blocks_in_loop_.IsBitSet(index_)) {
5969 break;
5970 }
5971 }
5972 }
5973
5974 private:
5975 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005976 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005977 size_t index_;
5978
5979 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5980};
5981
Mingyao Yang3584bce2015-05-19 16:01:59 -07005982// Iterator over the blocks that art part of the loop. Includes blocks part
5983// of an inner loop. The order in which the blocks are iterated is reverse
5984// post order.
5985class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5986 public:
5987 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5988 : blocks_in_loop_(info.GetBlocks()),
5989 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5990 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005991 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005992 Advance();
5993 }
5994 }
5995
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005996 bool Done() const { return index_ == blocks_.size(); }
5997 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005998 void Advance() {
5999 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006000 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6001 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006002 break;
6003 }
6004 }
6005 }
6006
6007 private:
6008 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006009 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006010 size_t index_;
6011
6012 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6013};
6014
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006015inline int64_t Int64FromConstant(HConstant* constant) {
6016 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
6017 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
6018 : constant->AsLongConstant()->GetValue();
6019}
6020
Vladimir Marko58155012015-08-19 12:49:41 +00006021inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6022 // For the purposes of the compiler, the dex files must actually be the same object
6023 // if we want to safely treat them as the same. This is especially important for JIT
6024 // as custom class loaders can open the same underlying file (or memory) multiple
6025 // times and provide different class resolution but no two class loaders should ever
6026 // use the same DexFile object - doing so is an unsupported hack that can lead to
6027 // all sorts of weird failures.
6028 return &lhs == &rhs;
6029}
6030
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006031#define INSTRUCTION_TYPE_CHECK(type, super) \
6032 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6033 inline const H##type* HInstruction::As##type() const { \
6034 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6035 } \
6036 inline H##type* HInstruction::As##type() { \
6037 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6038 }
6039
6040 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6041#undef INSTRUCTION_TYPE_CHECK
6042
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006043} // namespace art
6044
6045#endif // ART_COMPILER_OPTIMIZING_NODES_H_