blob: 019be5d4946920f20b0f3baa19fa0b48eccf206a [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010028#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000029#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "handle.h"
31#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "primitive.h"
David Brazdild26a4112015-11-10 11:07:31 +000038#include "utils/array_ref.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000039
40namespace art {
41
David Brazdil1abb4192015-02-17 18:33:36 +000042class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010046class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000048class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000051class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000052class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000054class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010056class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010057class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000059class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010060class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062
Mathieu Chartier736b5602015-09-02 14:54:11 -070063namespace mirror {
64class DexCache;
65} // namespace mirror
66
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067static const int kDefaultNumberOfBlocks = 8;
68static const int kDefaultNumberOfSuccessors = 2;
69static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010070static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010071static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000072static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Calin Juravle9aec02f2014-11-18 23:06:35 +000074static constexpr uint32_t kMaxIntShiftValue = 0x1f;
75static constexpr uint64_t kMaxLongShiftValue = 0x3f;
76
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010077static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070078static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010079
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010080static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
81
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060082static constexpr uint32_t kNoDexPc = -1;
83
Dave Allison20dfc792014-06-16 20:44:29 -070084enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070085 // All types.
86 kCondEQ, // ==
87 kCondNE, // !=
88 // Signed integers and floating-point numbers.
89 kCondLT, // <
90 kCondLE, // <=
91 kCondGT, // >
92 kCondGE, // >=
93 // Unsigned integers.
94 kCondB, // <
95 kCondBE, // <=
96 kCondA, // >
97 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -070098};
99
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000100enum GraphAnalysisResult {
101 kAnalysisFailThrowCatchLoop,
102 kAnalysisFailAmbiguousArrayOp,
103 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000104};
105
Vladimir Markof9f64412015-09-02 14:05:49 +0100106class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100107 public:
108 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
109
110 void AddInstruction(HInstruction* instruction);
111 void RemoveInstruction(HInstruction* instruction);
112
David Brazdilc3d743f2015-04-22 13:40:50 +0100113 // Insert `instruction` before/after an existing instruction `cursor`.
114 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
115 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
116
Roland Levillain6b469232014-09-25 10:10:38 +0100117 // Return true if this list contains `instruction`.
118 bool Contains(HInstruction* instruction) const;
119
Roland Levillainccc07a92014-09-16 14:48:16 +0100120 // Return true if `instruction1` is found before `instruction2` in
121 // this instruction list and false otherwise. Abort if none
122 // of these instructions is found.
123 bool FoundBefore(const HInstruction* instruction1,
124 const HInstruction* instruction2) const;
125
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000126 bool IsEmpty() const { return first_instruction_ == nullptr; }
127 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
128
129 // Update the block of all instructions to be `block`.
130 void SetBlockOfInstructions(HBasicBlock* block) const;
131
132 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
133 void Add(const HInstructionList& instruction_list);
134
David Brazdil2d7352b2015-04-20 14:52:42 +0100135 // Return the number of instructions in the list. This is an expensive operation.
136 size_t CountSize() const;
137
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100138 private:
139 HInstruction* first_instruction_;
140 HInstruction* last_instruction_;
141
142 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000143 friend class HGraph;
144 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100145 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100146 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100147
148 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
149};
150
David Brazdil4833f5a2015-12-16 10:37:39 +0000151class ReferenceTypeInfo : ValueObject {
152 public:
153 typedef Handle<mirror::Class> TypeHandle;
154
155 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
156 // The constructor will check that the type_handle is valid.
157 return ReferenceTypeInfo(type_handle, is_exact);
158 }
159
160 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
161
162 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
163 return handle.GetReference() != nullptr;
164 }
165
166 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
167 return IsValidHandle(type_handle_);
168 }
169
170 bool IsExact() const { return is_exact_; }
171
172 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
173 DCHECK(IsValid());
174 return GetTypeHandle()->IsObjectClass();
175 }
176
177 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
178 DCHECK(IsValid());
179 return GetTypeHandle()->IsStringClass();
180 }
181
182 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
183 DCHECK(IsValid());
184 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
185 }
186
187 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
188 DCHECK(IsValid());
189 return GetTypeHandle()->IsInterface();
190 }
191
192 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
193 DCHECK(IsValid());
194 return GetTypeHandle()->IsArrayClass();
195 }
196
197 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
198 DCHECK(IsValid());
199 return GetTypeHandle()->IsPrimitiveArray();
200 }
201
202 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
203 DCHECK(IsValid());
204 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
205 }
206
207 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
208 DCHECK(IsValid());
209 if (!IsExact()) return false;
210 if (!IsArrayClass()) return false;
211 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
212 }
213
214 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
215 DCHECK(IsValid());
216 if (!IsExact()) return false;
217 if (!IsArrayClass()) return false;
218 if (!rti.IsArrayClass()) return false;
219 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
220 rti.GetTypeHandle()->GetComponentType());
221 }
222
223 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
224
225 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
226 DCHECK(IsValid());
227 DCHECK(rti.IsValid());
228 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
229 }
230
231 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
232 DCHECK(IsValid());
233 DCHECK(rti.IsValid());
234 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
235 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
236 }
237
238 // Returns true if the type information provide the same amount of details.
239 // Note that it does not mean that the instructions have the same actual type
240 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000241 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000242 if (!IsValid() && !rti.IsValid()) {
243 // Invalid types are equal.
244 return true;
245 }
246 if (!IsValid() || !rti.IsValid()) {
247 // One is valid, the other not.
248 return false;
249 }
250 return IsExact() == rti.IsExact()
251 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
252 }
253
254 private:
255 ReferenceTypeInfo();
256 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
257
258 // The class of the object.
259 TypeHandle type_handle_;
260 // Whether or not the type is exact or a superclass of the actual type.
261 // Whether or not we have any information about this type.
262 bool is_exact_;
263};
264
265std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
266
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000267// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100268class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000269 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100270 HGraph(ArenaAllocator* arena,
271 const DexFile& dex_file,
272 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100273 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700274 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100275 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100276 bool debuggable = false,
277 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000278 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100279 blocks_(arena->Adapter(kArenaAllocBlockList)),
280 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
281 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700282 entry_block_(nullptr),
283 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100284 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100285 number_of_vregs_(0),
286 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000287 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400288 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000289 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000290 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000291 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000292 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100293 dex_file_(dex_file),
294 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100295 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100296 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100297 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700298 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000299 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100300 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
301 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
302 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
303 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000304 cached_current_method_(nullptr),
305 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100306 blocks_.reserve(kDefaultNumberOfBlocks);
307 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000308
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000309 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100310 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
311
David Brazdil69ba7b72015-06-23 18:27:30 +0100312 bool IsInSsaForm() const { return in_ssa_form_; }
313
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000314 HBasicBlock* GetEntryBlock() const { return entry_block_; }
315 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100316 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000317
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000318 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
319 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000320
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000321 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100322
David Brazdil4833f5a2015-12-16 10:37:39 +0000323 // Try building the SSA form of this graph, with dominance computation and
324 // loop recognition. Returns a code specifying that it was successful or the
325 // reason for failure.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000326 GraphAnalysisResult TryBuildingSsa(StackHandleScopeCollection* handles);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000327
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100328 void ComputeDominanceInformation();
329 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000330 void ClearLoopInformation();
331 void FindBackEdges(ArenaBitVector* visited);
332 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100333 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100334 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000335
David Brazdil4833f5a2015-12-16 10:37:39 +0000336 // Analyze all natural loops in this graph. Returns a code specifying that it
337 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000338 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000339 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100340
David Brazdilffee3d32015-07-06 11:48:53 +0100341 // Iterate over blocks to compute try block membership. Needs reverse post
342 // order and loop information.
343 void ComputeTryBlockInformation();
344
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000345 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000346 // Returns the instruction used to replace the invoke expression or null if the
347 // invoke is for a void method.
348 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000349
Mingyao Yang3584bce2015-05-19 16:01:59 -0700350 // Need to add a couple of blocks to test if the loop body is entered and
351 // put deoptimization instructions, etc.
352 void TransformLoopHeaderForBCE(HBasicBlock* header);
353
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000354 // Removes `block` from the graph. Assumes `block` has been disconnected from
355 // other blocks and has no instructions or phis.
356 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000357
David Brazdilfc6a86a2015-06-26 10:33:45 +0000358 // Splits the edge between `block` and `successor` while preserving the
359 // indices in the predecessor/successor lists. If there are multiple edges
360 // between the blocks, the lowest indices are used.
361 // Returns the new block which is empty and has the same dex pc as `successor`.
362 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
363
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100364 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
365 void SimplifyLoop(HBasicBlock* header);
366
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000367 int32_t GetNextInstructionId() {
368 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000369 return current_instruction_id_++;
370 }
371
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000372 int32_t GetCurrentInstructionId() const {
373 return current_instruction_id_;
374 }
375
376 void SetCurrentInstructionId(int32_t id) {
377 current_instruction_id_ = id;
378 }
379
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100380 uint16_t GetMaximumNumberOfOutVRegs() const {
381 return maximum_number_of_out_vregs_;
382 }
383
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000384 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
385 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100386 }
387
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100388 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
389 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
390 }
391
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000392 void UpdateTemporariesVRegSlots(size_t slots) {
393 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100394 }
395
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000396 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100397 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000398 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100399 }
400
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100401 void SetNumberOfVRegs(uint16_t number_of_vregs) {
402 number_of_vregs_ = number_of_vregs;
403 }
404
405 uint16_t GetNumberOfVRegs() const {
406 return number_of_vregs_;
407 }
408
409 void SetNumberOfInVRegs(uint16_t value) {
410 number_of_in_vregs_ = value;
411 }
412
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100413 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100414 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100415 return number_of_vregs_ - number_of_in_vregs_;
416 }
417
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100418 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100419 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100420 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100421
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100422 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100423 return linear_order_;
424 }
425
Mark Mendell1152c922015-04-24 17:06:35 -0400426 bool HasBoundsChecks() const {
427 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800428 }
429
Mark Mendell1152c922015-04-24 17:06:35 -0400430 void SetHasBoundsChecks(bool value) {
431 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800432 }
433
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100434 bool ShouldGenerateConstructorBarrier() const {
435 return should_generate_constructor_barrier_;
436 }
437
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000438 bool IsDebuggable() const { return debuggable_; }
439
David Brazdil8d5b8b22015-03-24 10:51:52 +0000440 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000441 // already, it is created and inserted into the graph. This method is only for
442 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600443 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000444
445 // TODO: This is problematic for the consistency of reference type propagation
446 // because it can be created anytime after the pass and thus it will be left
447 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600448 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000449
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600450 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
451 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000452 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600453 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
454 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000455 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600456 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
457 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000458 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600459 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
460 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000461 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000462
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100463 HCurrentMethod* GetCurrentMethod();
464
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100465 const DexFile& GetDexFile() const {
466 return dex_file_;
467 }
468
469 uint32_t GetMethodIdx() const {
470 return method_idx_;
471 }
472
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100473 InvokeType GetInvokeType() const {
474 return invoke_type_;
475 }
476
Mark Mendellc4701932015-04-10 13:18:51 -0400477 InstructionSet GetInstructionSet() const {
478 return instruction_set_;
479 }
480
David Brazdil77a48ae2015-09-15 12:34:04 +0000481 bool HasTryCatch() const { return has_try_catch_; }
482 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100483
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000484 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
485 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
486
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100487 ArtMethod* GetArtMethod() const { return art_method_; }
488 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
489
Mark Mendellf6529172015-11-17 11:16:56 -0500490 // Returns an instruction with the opposite boolean value from 'cond'.
491 // The instruction has been inserted into the graph, either as a constant, or
492 // before cursor.
493 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
494
David Brazdil2d7352b2015-04-20 14:52:42 +0100495 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000496 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100497 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000498
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000499 template <class InstructionType, typename ValueType>
500 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600501 ArenaSafeMap<ValueType, InstructionType*>* cache,
502 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000503 // Try to find an existing constant of the given value.
504 InstructionType* constant = nullptr;
505 auto cached_constant = cache->find(value);
506 if (cached_constant != cache->end()) {
507 constant = cached_constant->second;
508 }
509
510 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100511 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000512 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600513 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000514 cache->Overwrite(value, constant);
515 InsertConstant(constant);
516 }
517 return constant;
518 }
519
David Brazdil8d5b8b22015-03-24 10:51:52 +0000520 void InsertConstant(HConstant* instruction);
521
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000522 // Cache a float constant into the graph. This method should only be
523 // called by the SsaBuilder when creating "equivalent" instructions.
524 void CacheFloatConstant(HFloatConstant* constant);
525
526 // See CacheFloatConstant comment.
527 void CacheDoubleConstant(HDoubleConstant* constant);
528
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000529 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000530
531 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100532 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000533
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100534 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100535 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000536
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100537 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100538 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100539
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000540 HBasicBlock* entry_block_;
541 HBasicBlock* exit_block_;
542
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100543 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100544 uint16_t maximum_number_of_out_vregs_;
545
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100546 // The number of virtual registers in this method. Contains the parameters.
547 uint16_t number_of_vregs_;
548
549 // The number of virtual registers used by parameters of this method.
550 uint16_t number_of_in_vregs_;
551
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000552 // Number of vreg size slots that the temporaries use (used in baseline compiler).
553 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100554
Mark Mendell1152c922015-04-24 17:06:35 -0400555 // Has bounds checks. We can totally skip BCE if it's false.
556 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800557
David Brazdil77a48ae2015-09-15 12:34:04 +0000558 // Flag whether there are any try/catch blocks in the graph. We will skip
559 // try/catch-related passes if false.
560 bool has_try_catch_;
561
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000562 // Flag whether there are any irreducible loops in the graph.
563 bool has_irreducible_loops_;
564
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000565 // Indicates whether the graph should be compiled in a way that
566 // ensures full debuggability. If false, we can apply more
567 // aggressive optimizations that may limit the level of debugging.
568 const bool debuggable_;
569
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000570 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000571 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000572
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100573 // The dex file from which the method is from.
574 const DexFile& dex_file_;
575
576 // The method index in the dex file.
577 const uint32_t method_idx_;
578
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100579 // If inlined, this encodes how the callee is being invoked.
580 const InvokeType invoke_type_;
581
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100582 // Whether the graph has been transformed to SSA form. Only used
583 // in debug mode to ensure we are not using properties only valid
584 // for non-SSA form (like the number of temporaries).
585 bool in_ssa_form_;
586
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100587 const bool should_generate_constructor_barrier_;
588
Mathieu Chartiere401d142015-04-22 13:56:20 -0700589 const InstructionSet instruction_set_;
590
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000591 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000592 HNullConstant* cached_null_constant_;
593 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000594 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000595 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000596 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000597
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100598 HCurrentMethod* cached_current_method_;
599
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100600 // The ArtMethod this graph is for. Note that for AOT, it may be null,
601 // for example for methods whose declaring class could not be resolved
602 // (such as when the superclass could not be found).
603 ArtMethod* art_method_;
604
David Brazdil4833f5a2015-12-16 10:37:39 +0000605 // Keep the RTI of inexact Object to avoid having to pass stack handle
606 // collection pointer to passes which may create NullConstant.
607 ReferenceTypeInfo inexact_object_rti_;
608
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000609 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100610 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000611 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000612 DISALLOW_COPY_AND_ASSIGN(HGraph);
613};
614
Vladimir Markof9f64412015-09-02 14:05:49 +0100615class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000616 public:
617 HLoopInformation(HBasicBlock* header, HGraph* graph)
618 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100619 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000620 irreducible_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100621 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100622 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100623 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
624 back_edges_.reserve(kDefaultNumberOfBackEdges);
625 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100626
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000627 bool IsIrreducible() const { return irreducible_; }
628
629 void Dump(std::ostream& os);
630
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100631 HBasicBlock* GetHeader() const {
632 return header_;
633 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000634
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000635 void SetHeader(HBasicBlock* block) {
636 header_ = block;
637 }
638
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100639 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
640 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
641 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
642
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000643 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100644 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000645 }
646
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100647 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100648 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100649 }
650
David Brazdil46e2a392015-03-16 17:31:52 +0000651 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100652 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100653 }
654
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000655 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100656 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000657 }
658
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100659 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100660
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100661 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100662 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100663 }
664
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100665 // Returns the lifetime position of the back edge that has the
666 // greatest lifetime position.
667 size_t GetLifetimeEnd() const;
668
669 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100670 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100671 }
672
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000673 // Finds blocks that are part of this loop.
674 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100675
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100676 // Returns whether this loop information contains `block`.
677 // Note that this loop information *must* be populated before entering this function.
678 bool Contains(const HBasicBlock& block) const;
679
680 // Returns whether this loop information is an inner loop of `other`.
681 // Note that `other` *must* be populated before entering this function.
682 bool IsIn(const HLoopInformation& other) const;
683
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800684 // Returns true if instruction is not defined within this loop.
685 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700686
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100687 const ArenaBitVector& GetBlocks() const { return blocks_; }
688
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000689 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000690 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000691
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000692 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100693 // Internal recursive implementation of `Populate`.
694 void PopulateRecursive(HBasicBlock* block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000695 void PopulateIrreducibleRecursive(HBasicBlock* block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100696
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000697 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100698 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000699 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100700 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100701 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000702
703 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
704};
705
David Brazdilec16f792015-08-19 15:04:01 +0100706// Stores try/catch information for basic blocks.
707// Note that HGraph is constructed so that catch blocks cannot simultaneously
708// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100709class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100710 public:
711 // Try block information constructor.
712 explicit TryCatchInformation(const HTryBoundary& try_entry)
713 : try_entry_(&try_entry),
714 catch_dex_file_(nullptr),
715 catch_type_index_(DexFile::kDexNoIndex16) {
716 DCHECK(try_entry_ != nullptr);
717 }
718
719 // Catch block information constructor.
720 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
721 : try_entry_(nullptr),
722 catch_dex_file_(&dex_file),
723 catch_type_index_(catch_type_index) {}
724
725 bool IsTryBlock() const { return try_entry_ != nullptr; }
726
727 const HTryBoundary& GetTryEntry() const {
728 DCHECK(IsTryBlock());
729 return *try_entry_;
730 }
731
732 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
733
734 bool IsCatchAllTypeIndex() const {
735 DCHECK(IsCatchBlock());
736 return catch_type_index_ == DexFile::kDexNoIndex16;
737 }
738
739 uint16_t GetCatchTypeIndex() const {
740 DCHECK(IsCatchBlock());
741 return catch_type_index_;
742 }
743
744 const DexFile& GetCatchDexFile() const {
745 DCHECK(IsCatchBlock());
746 return *catch_dex_file_;
747 }
748
749 private:
750 // One of possibly several TryBoundary instructions entering the block's try.
751 // Only set for try blocks.
752 const HTryBoundary* try_entry_;
753
754 // Exception type information. Only set for catch blocks.
755 const DexFile* catch_dex_file_;
756 const uint16_t catch_type_index_;
757};
758
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100759static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100760static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100761
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000762// A block in a method. Contains the list of instructions represented
763// as a double linked list. Each block knows its predecessors and
764// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100765
Vladimir Markof9f64412015-09-02 14:05:49 +0100766class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000767 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600768 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000769 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000770 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
771 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000772 loop_information_(nullptr),
773 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000774 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100775 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100776 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100777 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000778 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000779 try_catch_information_(nullptr) {
780 predecessors_.reserve(kDefaultNumberOfPredecessors);
781 successors_.reserve(kDefaultNumberOfSuccessors);
782 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
783 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000784
Vladimir Marko60584552015-09-03 13:35:12 +0000785 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100786 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000787 }
788
Vladimir Marko60584552015-09-03 13:35:12 +0000789 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100790 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000791 }
792
David Brazdild26a4112015-11-10 11:07:31 +0000793 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
794 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
795
Vladimir Marko60584552015-09-03 13:35:12 +0000796 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
797 return ContainsElement(successors_, block, start_from);
798 }
799
800 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100801 return dominated_blocks_;
802 }
803
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100804 bool IsEntryBlock() const {
805 return graph_->GetEntryBlock() == this;
806 }
807
808 bool IsExitBlock() const {
809 return graph_->GetExitBlock() == this;
810 }
811
David Brazdil46e2a392015-03-16 17:31:52 +0000812 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000813 bool IsSingleTryBoundary() const;
814
815 // Returns true if this block emits nothing but a jump.
816 bool IsSingleJump() const {
817 HLoopInformation* loop_info = GetLoopInformation();
818 return (IsSingleGoto() || IsSingleTryBoundary())
819 // Back edges generate a suspend check.
820 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
821 }
David Brazdil46e2a392015-03-16 17:31:52 +0000822
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000823 void AddBackEdge(HBasicBlock* back_edge) {
824 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000825 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000826 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100827 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000828 loop_information_->AddBackEdge(back_edge);
829 }
830
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000831 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000832 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000833
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100834 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000835 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600836 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000837
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000838 HBasicBlock* GetDominator() const { return dominator_; }
839 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000840 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
841
842 void RemoveDominatedBlock(HBasicBlock* block) {
843 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100844 }
Vladimir Marko60584552015-09-03 13:35:12 +0000845
846 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
847 ReplaceElement(dominated_blocks_, existing, new_block);
848 }
849
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100850 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000851
852 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100853 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000854 }
855
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100856 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
857 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100858 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100859 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100860 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
861 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000862
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000863 HInstruction* GetFirstInstructionDisregardMoves() const;
864
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000865 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000866 successors_.push_back(block);
867 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000868 }
869
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100870 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
871 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100872 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000873 new_block->predecessors_.push_back(this);
874 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000875 }
876
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000877 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
878 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000879 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000880 new_block->successors_.push_back(this);
881 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000882 }
883
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100884 // Insert `this` between `predecessor` and `successor. This method
885 // preserves the indicies, and will update the first edge found between
886 // `predecessor` and `successor`.
887 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
888 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100889 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000890 successor->predecessors_[predecessor_index] = this;
891 predecessor->successors_[successor_index] = this;
892 successors_.push_back(successor);
893 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100894 }
895
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100896 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000897 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100898 }
899
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000900 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000901 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000902 }
903
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100904 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000905 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100906 }
907
908 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000909 predecessors_.push_back(block);
910 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100911 }
912
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100913 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000914 DCHECK_EQ(predecessors_.size(), 2u);
915 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100916 }
917
David Brazdil769c9e52015-04-27 13:54:09 +0100918 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000919 DCHECK_EQ(successors_.size(), 2u);
920 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100921 }
922
David Brazdilfc6a86a2015-06-26 10:33:45 +0000923 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000924 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100925 }
926
David Brazdilfc6a86a2015-06-26 10:33:45 +0000927 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000928 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100929 }
930
David Brazdilfc6a86a2015-06-26 10:33:45 +0000931 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000932 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100933 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000934 }
935
936 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000937 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100938 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000939 }
940
941 // Returns whether the first occurrence of `predecessor` in the list of
942 // predecessors is at index `idx`.
943 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100944 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000945 return GetPredecessorIndexOf(predecessor) == idx;
946 }
947
David Brazdild7558da2015-09-22 13:04:14 +0100948 // Create a new block between this block and its predecessors. The new block
949 // is added to the graph, all predecessor edges are relinked to it and an edge
950 // is created to `this`. Returns the new empty block. Reverse post order or
951 // loop and try/catch information are not updated.
952 HBasicBlock* CreateImmediateDominator();
953
David Brazdilfc6a86a2015-06-26 10:33:45 +0000954 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100955 // created, latter block. Note that this method will add the block to the
956 // graph, create a Goto at the end of the former block and will create an edge
957 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100958 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000959 HBasicBlock* SplitBefore(HInstruction* cursor);
960
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000961 // Split the block into two blocks just after `cursor`. Returns the newly
962 // created block. Note that this method just updates raw block information,
963 // like predecessors, successors, dominators, and instruction list. It does not
964 // update the graph, reverse post order, loop information, nor make sure the
965 // blocks are consistent (for example ending with a control flow instruction).
966 HBasicBlock* SplitAfter(HInstruction* cursor);
967
David Brazdil9bc43612015-11-05 21:25:24 +0000968 // Split catch block into two blocks after the original move-exception bytecode
969 // instruction, or at the beginning if not present. Returns the newly created,
970 // latter block, or nullptr if such block could not be created (must be dead
971 // in that case). Note that this method just updates raw block information,
972 // like predecessors, successors, dominators, and instruction list. It does not
973 // update the graph, reverse post order, loop information, nor make sure the
974 // blocks are consistent (for example ending with a control flow instruction).
975 HBasicBlock* SplitCatchBlockAfterMoveException();
976
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000977 // Merge `other` at the end of `this`. Successors and dominated blocks of
978 // `other` are changed to be successors and dominated blocks of `this`. Note
979 // that this method does not update the graph, reverse post order, loop
980 // information, nor make sure the blocks are consistent (for example ending
981 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100982 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000983
984 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
985 // of `this` are moved to `other`.
986 // Note that this method does not update the graph, reverse post order, loop
987 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000988 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000989 void ReplaceWith(HBasicBlock* other);
990
David Brazdil2d7352b2015-04-20 14:52:42 +0100991 // Merge `other` at the end of `this`. This method updates loops, reverse post
992 // order, links to predecessors, successors, dominators and deletes the block
993 // from the graph. The two blocks must be successive, i.e. `this` the only
994 // predecessor of `other` and vice versa.
995 void MergeWith(HBasicBlock* other);
996
997 // Disconnects `this` from all its predecessors, successors and dominator,
998 // removes it from all loops it is included in and eventually from the graph.
999 // The block must not dominate any other block. Predecessors and successors
1000 // are safely updated.
1001 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001002
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001003 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001004 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001005 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001006 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001007 // Replace instruction `initial` with `replacement` within this block.
1008 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1009 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001010 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001011 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001012 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1013 // instruction list. With 'ensure_safety' set to true, it verifies that the
1014 // instruction is not in use and removes it from the use lists of its inputs.
1015 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1016 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001017 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001018
1019 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001020 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001021 }
1022
Roland Levillain6b879dd2014-09-22 17:13:44 +01001023 bool IsLoopPreHeaderFirstPredecessor() const {
1024 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001025 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001026 }
1027
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001028 bool IsFirstPredecessorBackEdge() const {
1029 DCHECK(IsLoopHeader());
1030 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1031 }
1032
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001033 HLoopInformation* GetLoopInformation() const {
1034 return loop_information_;
1035 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001036
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001037 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001038 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001039 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001040 void SetInLoop(HLoopInformation* info) {
1041 if (IsLoopHeader()) {
1042 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001043 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001044 loop_information_ = info;
1045 } else if (loop_information_->Contains(*info->GetHeader())) {
1046 // Block is currently part of an outer loop. Make it part of this inner loop.
1047 // Note that a non loop header having a loop information means this loop information
1048 // has already been populated
1049 loop_information_ = info;
1050 } else {
1051 // Block is part of an inner loop. Do not update the loop information.
1052 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1053 // at this point, because this method is being called while populating `info`.
1054 }
1055 }
1056
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001057 // Raw update of the loop information.
1058 void SetLoopInformation(HLoopInformation* info) {
1059 loop_information_ = info;
1060 }
1061
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001062 bool IsInLoop() const { return loop_information_ != nullptr; }
1063
David Brazdilec16f792015-08-19 15:04:01 +01001064 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1065
1066 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1067 try_catch_information_ = try_catch_information;
1068 }
1069
1070 bool IsTryBlock() const {
1071 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1072 }
1073
1074 bool IsCatchBlock() const {
1075 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1076 }
David Brazdilffee3d32015-07-06 11:48:53 +01001077
1078 // Returns the try entry that this block's successors should have. They will
1079 // be in the same try, unless the block ends in a try boundary. In that case,
1080 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001081 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001082
David Brazdild7558da2015-09-22 13:04:14 +01001083 bool HasThrowingInstructions() const;
1084
David Brazdila4b8c212015-05-07 09:59:30 +01001085 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001086 bool Dominates(HBasicBlock* block) const;
1087
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001088 size_t GetLifetimeStart() const { return lifetime_start_; }
1089 size_t GetLifetimeEnd() const { return lifetime_end_; }
1090
1091 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1092 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1093
David Brazdil8d5b8b22015-03-24 10:51:52 +00001094 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001095 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001096 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001097 bool HasSinglePhi() const;
1098
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001099 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001100 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001101 ArenaVector<HBasicBlock*> predecessors_;
1102 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001103 HInstructionList instructions_;
1104 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001105 HLoopInformation* loop_information_;
1106 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001107 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001108 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001109 // The dex program counter of the first instruction of this block.
1110 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001111 size_t lifetime_start_;
1112 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001113 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001114
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001115 friend class HGraph;
1116 friend class HInstruction;
1117
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001118 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1119};
1120
David Brazdilb2bd1c52015-03-25 11:17:37 +00001121// Iterates over the LoopInformation of all loops which contain 'block'
1122// from the innermost to the outermost.
1123class HLoopInformationOutwardIterator : public ValueObject {
1124 public:
1125 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1126 : current_(block.GetLoopInformation()) {}
1127
1128 bool Done() const { return current_ == nullptr; }
1129
1130 void Advance() {
1131 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001132 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001133 }
1134
1135 HLoopInformation* Current() const {
1136 DCHECK(!Done());
1137 return current_;
1138 }
1139
1140 private:
1141 HLoopInformation* current_;
1142
1143 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1144};
1145
Alexandre Ramesef20f712015-06-09 10:29:30 +01001146#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001147 M(Above, Condition) \
1148 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001149 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001150 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001151 M(ArrayGet, Instruction) \
1152 M(ArrayLength, Instruction) \
1153 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001154 M(Below, Condition) \
1155 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001156 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001157 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001158 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001159 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001160 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001161 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001162 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001163 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001164 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001165 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001166 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001167 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001168 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001169 M(Exit, Instruction) \
1170 M(FloatConstant, Constant) \
1171 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001172 M(GreaterThan, Condition) \
1173 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001174 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001175 M(InstanceFieldGet, Instruction) \
1176 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001177 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001178 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001179 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001180 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001181 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001182 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001183 M(LessThan, Condition) \
1184 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001185 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001186 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001187 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001188 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001189 M(Local, Instruction) \
1190 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001191 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001192 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001193 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001194 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001195 M(Neg, UnaryOperation) \
1196 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001197 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001198 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001199 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001200 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001201 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001202 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001203 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001204 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001205 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001206 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001207 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001208 M(Return, Instruction) \
1209 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001210 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001211 M(Shl, BinaryOperation) \
1212 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001213 M(StaticFieldGet, Instruction) \
1214 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001215 M(UnresolvedInstanceFieldGet, Instruction) \
1216 M(UnresolvedInstanceFieldSet, Instruction) \
1217 M(UnresolvedStaticFieldGet, Instruction) \
1218 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001219 M(StoreLocal, Instruction) \
1220 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001221 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001222 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001223 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001224 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001225 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001226 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001227 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001228
Vladimir Markob4536b72015-11-24 13:45:23 +00001229#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001230#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001231#else
1232#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1233 M(ArmDexCacheArraysBase, Instruction)
1234#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001235
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001236#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001237#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001238#else
1239#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001240 M(Arm64DataProcWithShifterOp, Instruction) \
Nicolas Geoffray6b5afdd2016-01-22 09:31:52 +00001241 M(Arm64IntermediateAddress, Instruction) \
1242 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001243#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001244
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001245#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1246
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001247#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1248
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001249#ifndef ART_ENABLE_CODEGEN_x86
1250#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1251#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001252#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1253 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001254 M(X86LoadFromConstantTable, Instruction) \
1255 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001256#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001257
1258#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1259
1260#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1261 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1262 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1263 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001264 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001265 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001266 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1267 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1268
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001269#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1270 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001271 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001272 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001273 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001274 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001275
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001276#define FOR_EACH_INSTRUCTION(M) \
1277 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1278 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1279
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001280#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001281FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1282#undef FORWARD_DECLARATION
1283
Roland Levillainccc07a92014-09-16 14:48:16 +01001284#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001285 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001286 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001287 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001288 return other->Is##type(); \
1289 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001290 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001291
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001292#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1293 bool Is##type() const { return As##type() != nullptr; } \
1294 const H##type* As##type() const { return this; } \
1295 H##type* As##type() { return this; }
1296
David Brazdiled596192015-01-23 10:39:45 +00001297template <typename T> class HUseList;
1298
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001299template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001300class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001301 public:
David Brazdiled596192015-01-23 10:39:45 +00001302 HUseListNode* GetPrevious() const { return prev_; }
1303 HUseListNode* GetNext() const { return next_; }
1304 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001305 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001306 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001307
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001308 private:
David Brazdiled596192015-01-23 10:39:45 +00001309 HUseListNode(T user, size_t index)
1310 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1311
1312 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001313 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001314 HUseListNode<T>* prev_;
1315 HUseListNode<T>* next_;
1316
1317 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001318
1319 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1320};
1321
David Brazdiled596192015-01-23 10:39:45 +00001322template <typename T>
1323class HUseList : public ValueObject {
1324 public:
1325 HUseList() : first_(nullptr) {}
1326
1327 void Clear() {
1328 first_ = nullptr;
1329 }
1330
1331 // Adds a new entry at the beginning of the use list and returns
1332 // the newly created node.
1333 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001334 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001335 if (IsEmpty()) {
1336 first_ = new_node;
1337 } else {
1338 first_->prev_ = new_node;
1339 new_node->next_ = first_;
1340 first_ = new_node;
1341 }
1342 return new_node;
1343 }
1344
1345 HUseListNode<T>* GetFirst() const {
1346 return first_;
1347 }
1348
1349 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001350 DCHECK(node != nullptr);
1351 DCHECK(Contains(node));
1352
David Brazdiled596192015-01-23 10:39:45 +00001353 if (node->prev_ != nullptr) {
1354 node->prev_->next_ = node->next_;
1355 }
1356 if (node->next_ != nullptr) {
1357 node->next_->prev_ = node->prev_;
1358 }
1359 if (node == first_) {
1360 first_ = node->next_;
1361 }
1362 }
1363
David Brazdil1abb4192015-02-17 18:33:36 +00001364 bool Contains(const HUseListNode<T>* node) const {
1365 if (node == nullptr) {
1366 return false;
1367 }
1368 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1369 if (current == node) {
1370 return true;
1371 }
1372 }
1373 return false;
1374 }
1375
David Brazdiled596192015-01-23 10:39:45 +00001376 bool IsEmpty() const {
1377 return first_ == nullptr;
1378 }
1379
1380 bool HasOnlyOneUse() const {
1381 return first_ != nullptr && first_->next_ == nullptr;
1382 }
1383
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001384 size_t SizeSlow() const {
1385 size_t count = 0;
1386 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1387 ++count;
1388 }
1389 return count;
1390 }
1391
David Brazdiled596192015-01-23 10:39:45 +00001392 private:
1393 HUseListNode<T>* first_;
1394};
1395
1396template<typename T>
1397class HUseIterator : public ValueObject {
1398 public:
1399 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1400
1401 bool Done() const { return current_ == nullptr; }
1402
1403 void Advance() {
1404 DCHECK(!Done());
1405 current_ = current_->GetNext();
1406 }
1407
1408 HUseListNode<T>* Current() const {
1409 DCHECK(!Done());
1410 return current_;
1411 }
1412
1413 private:
1414 HUseListNode<T>* current_;
1415
1416 friend class HValue;
1417};
1418
David Brazdil1abb4192015-02-17 18:33:36 +00001419// This class is used by HEnvironment and HInstruction classes to record the
1420// instructions they use and pointers to the corresponding HUseListNodes kept
1421// by the used instructions.
1422template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001423class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001424 public:
1425 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1426 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1427
1428 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1429 : instruction_(old_record.instruction_), use_node_(use_node) {
1430 DCHECK(instruction_ != nullptr);
1431 DCHECK(use_node_ != nullptr);
1432 DCHECK(old_record.use_node_ == nullptr);
1433 }
1434
1435 HInstruction* GetInstruction() const { return instruction_; }
1436 HUseListNode<T>* GetUseNode() const { return use_node_; }
1437
1438 private:
1439 // Instruction used by the user.
1440 HInstruction* instruction_;
1441
1442 // Corresponding entry in the use list kept by 'instruction_'.
1443 HUseListNode<T>* use_node_;
1444};
1445
Aart Bik854a02b2015-07-14 16:07:00 -07001446/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001447 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001448 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001449 * For write/read dependences on fields/arrays, the dependence analysis uses
1450 * type disambiguation (e.g. a float field write cannot modify the value of an
1451 * integer field read) and the access type (e.g. a reference array write cannot
1452 * modify the value of a reference field read [although it may modify the
1453 * reference fetch prior to reading the field, which is represented by its own
1454 * write/read dependence]). The analysis makes conservative points-to
1455 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1456 * the same, and any reference read depends on any reference read without
1457 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001458 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001459 * The internal representation uses 38-bit and is described in the table below.
1460 * The first line indicates the side effect, and for field/array accesses the
1461 * second line indicates the type of the access (in the order of the
1462 * Primitive::Type enum).
1463 * The two numbered lines below indicate the bit position in the bitfield (read
1464 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001465 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001466 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1467 * +-------------+---------+---------+--------------+---------+---------+
1468 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1469 * | 3 |333333322|222222221| 1 |111111110|000000000|
1470 * | 7 |654321098|765432109| 8 |765432109|876543210|
1471 *
1472 * Note that, to ease the implementation, 'changes' bits are least significant
1473 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001474 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001475class SideEffects : public ValueObject {
1476 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001477 SideEffects() : flags_(0) {}
1478
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001479 static SideEffects None() {
1480 return SideEffects(0);
1481 }
1482
1483 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001484 return SideEffects(kAllChangeBits | kAllDependOnBits);
1485 }
1486
1487 static SideEffects AllChanges() {
1488 return SideEffects(kAllChangeBits);
1489 }
1490
1491 static SideEffects AllDependencies() {
1492 return SideEffects(kAllDependOnBits);
1493 }
1494
1495 static SideEffects AllExceptGCDependency() {
1496 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1497 }
1498
1499 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001500 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001501 }
1502
Aart Bik34c3ba92015-07-20 14:08:59 -07001503 static SideEffects AllWrites() {
1504 return SideEffects(kAllWrites);
1505 }
1506
1507 static SideEffects AllReads() {
1508 return SideEffects(kAllReads);
1509 }
1510
1511 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1512 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001513 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001514 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001515 }
1516
Aart Bik854a02b2015-07-14 16:07:00 -07001517 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1518 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001519 }
1520
Aart Bik34c3ba92015-07-20 14:08:59 -07001521 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1522 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001523 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001524 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001525 }
1526
1527 static SideEffects ArrayReadOfType(Primitive::Type type) {
1528 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1529 }
1530
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001531 static SideEffects CanTriggerGC() {
1532 return SideEffects(1ULL << kCanTriggerGCBit);
1533 }
1534
1535 static SideEffects DependsOnGC() {
1536 return SideEffects(1ULL << kDependsOnGCBit);
1537 }
1538
Aart Bik854a02b2015-07-14 16:07:00 -07001539 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001540 SideEffects Union(SideEffects other) const {
1541 return SideEffects(flags_ | other.flags_);
1542 }
1543
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001544 SideEffects Exclusion(SideEffects other) const {
1545 return SideEffects(flags_ & ~other.flags_);
1546 }
1547
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001548 void Add(SideEffects other) {
1549 flags_ |= other.flags_;
1550 }
1551
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001552 bool Includes(SideEffects other) const {
1553 return (other.flags_ & flags_) == other.flags_;
1554 }
1555
1556 bool HasSideEffects() const {
1557 return (flags_ & kAllChangeBits);
1558 }
1559
1560 bool HasDependencies() const {
1561 return (flags_ & kAllDependOnBits);
1562 }
1563
1564 // Returns true if there are no side effects or dependencies.
1565 bool DoesNothing() const {
1566 return flags_ == 0;
1567 }
1568
Aart Bik854a02b2015-07-14 16:07:00 -07001569 // Returns true if something is written.
1570 bool DoesAnyWrite() const {
1571 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001572 }
1573
Aart Bik854a02b2015-07-14 16:07:00 -07001574 // Returns true if something is read.
1575 bool DoesAnyRead() const {
1576 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001577 }
1578
Aart Bik854a02b2015-07-14 16:07:00 -07001579 // Returns true if potentially everything is written and read
1580 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001581 bool DoesAllReadWrite() const {
1582 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1583 }
1584
Aart Bik854a02b2015-07-14 16:07:00 -07001585 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001586 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001587 }
1588
Roland Levillain0d5a2812015-11-13 10:07:31 +00001589 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001590 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001591 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1592 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001593 }
1594
1595 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001596 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001597 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001598 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001599 for (int s = kLastBit; s >= 0; s--) {
1600 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1601 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1602 // This is a bit for the GC side effect.
1603 if (current_bit_is_set) {
1604 flags += "GC";
1605 }
Aart Bik854a02b2015-07-14 16:07:00 -07001606 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001607 } else {
1608 // This is a bit for the array/field analysis.
1609 // The underscore character stands for the 'can trigger GC' bit.
1610 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1611 if (current_bit_is_set) {
1612 flags += kDebug[s];
1613 }
1614 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1615 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1616 flags += "|";
1617 }
1618 }
Aart Bik854a02b2015-07-14 16:07:00 -07001619 }
1620 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001621 }
1622
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001623 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001624
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001625 private:
1626 static constexpr int kFieldArrayAnalysisBits = 9;
1627
1628 static constexpr int kFieldWriteOffset = 0;
1629 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1630 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1631 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1632
1633 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1634
1635 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1636 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1637 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1638 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1639
1640 static constexpr int kLastBit = kDependsOnGCBit;
1641 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1642
1643 // Aliases.
1644
1645 static_assert(kChangeBits == kDependOnBits,
1646 "the 'change' bits should match the 'depend on' bits.");
1647
1648 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1649 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1650 static constexpr uint64_t kAllWrites =
1651 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1652 static constexpr uint64_t kAllReads =
1653 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001654
Aart Bik854a02b2015-07-14 16:07:00 -07001655 // Work around the fact that HIR aliases I/F and J/D.
1656 // TODO: remove this interceptor once HIR types are clean
1657 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1658 switch (type) {
1659 case Primitive::kPrimInt:
1660 case Primitive::kPrimFloat:
1661 return TypeFlag(Primitive::kPrimInt, offset) |
1662 TypeFlag(Primitive::kPrimFloat, offset);
1663 case Primitive::kPrimLong:
1664 case Primitive::kPrimDouble:
1665 return TypeFlag(Primitive::kPrimLong, offset) |
1666 TypeFlag(Primitive::kPrimDouble, offset);
1667 default:
1668 return TypeFlag(type, offset);
1669 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001670 }
1671
Aart Bik854a02b2015-07-14 16:07:00 -07001672 // Translates type to bit flag.
1673 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1674 CHECK_NE(type, Primitive::kPrimVoid);
1675 const uint64_t one = 1;
1676 const int shift = type; // 0-based consecutive enum
1677 DCHECK_LE(kFieldWriteOffset, shift);
1678 DCHECK_LT(shift, kArrayWriteOffset);
1679 return one << (type + offset);
1680 }
1681
1682 // Private constructor on direct flags value.
1683 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1684
1685 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001686};
1687
David Brazdiled596192015-01-23 10:39:45 +00001688// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001689class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001690 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001691 HEnvironment(ArenaAllocator* arena,
1692 size_t number_of_vregs,
1693 const DexFile& dex_file,
1694 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001695 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001696 InvokeType invoke_type,
1697 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001698 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1699 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001700 parent_(nullptr),
1701 dex_file_(dex_file),
1702 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001703 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001704 invoke_type_(invoke_type),
1705 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001706 }
1707
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001708 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001709 : HEnvironment(arena,
1710 to_copy.Size(),
1711 to_copy.GetDexFile(),
1712 to_copy.GetMethodIdx(),
1713 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001714 to_copy.GetInvokeType(),
1715 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001716
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001717 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001718 if (parent_ != nullptr) {
1719 parent_->SetAndCopyParentChain(allocator, parent);
1720 } else {
1721 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1722 parent_->CopyFrom(parent);
1723 if (parent->GetParent() != nullptr) {
1724 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1725 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001726 }
David Brazdiled596192015-01-23 10:39:45 +00001727 }
1728
Vladimir Marko71bf8092015-09-15 15:33:14 +01001729 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001730 void CopyFrom(HEnvironment* environment);
1731
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001732 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1733 // input to the loop phi instead. This is for inserting instructions that
1734 // require an environment (like HDeoptimization) in the loop pre-header.
1735 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001736
1737 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001738 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001739 }
1740
1741 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001742 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001743 }
1744
David Brazdil1abb4192015-02-17 18:33:36 +00001745 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001746
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001747 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001748
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001749 HEnvironment* GetParent() const { return parent_; }
1750
1751 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001752 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001753 }
1754
1755 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001756 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001757 }
1758
1759 uint32_t GetDexPc() const {
1760 return dex_pc_;
1761 }
1762
1763 uint32_t GetMethodIdx() const {
1764 return method_idx_;
1765 }
1766
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001767 InvokeType GetInvokeType() const {
1768 return invoke_type_;
1769 }
1770
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001771 const DexFile& GetDexFile() const {
1772 return dex_file_;
1773 }
1774
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001775 HInstruction* GetHolder() const {
1776 return holder_;
1777 }
1778
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001779
1780 bool IsFromInlinedInvoke() const {
1781 return GetParent() != nullptr;
1782 }
1783
David Brazdiled596192015-01-23 10:39:45 +00001784 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001785 // Record instructions' use entries of this environment for constant-time removal.
1786 // It should only be called by HInstruction when a new environment use is added.
1787 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1788 DCHECK(env_use->GetUser() == this);
1789 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001790 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001791 }
David Brazdiled596192015-01-23 10:39:45 +00001792
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001793 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1794 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001795 HEnvironment* parent_;
1796 const DexFile& dex_file_;
1797 const uint32_t method_idx_;
1798 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001799 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001800
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001801 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001802 HInstruction* const holder_;
1803
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001804 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001805
1806 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1807};
1808
Vladimir Markof9f64412015-09-02 14:05:49 +01001809class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001810 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001811 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001812 : previous_(nullptr),
1813 next_(nullptr),
1814 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001815 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001816 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001817 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001818 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001819 locations_(nullptr),
1820 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001821 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001822 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001823 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001824
Dave Allison20dfc792014-06-16 20:44:29 -07001825 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001826
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001827#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001828 enum InstructionKind {
1829 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1830 };
1831#undef DECLARE_KIND
1832
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001833 HInstruction* GetNext() const { return next_; }
1834 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001835
Calin Juravle77520bc2015-01-12 18:45:46 +00001836 HInstruction* GetNextDisregardingMoves() const;
1837 HInstruction* GetPreviousDisregardingMoves() const;
1838
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001839 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001840 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001841 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001842 bool IsInBlock() const { return block_ != nullptr; }
1843 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001844 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1845 bool IsIrreducibleLoopHeaderPhi() const {
1846 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1847 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001848
Roland Levillain6b879dd2014-09-22 17:13:44 +01001849 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001850 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001851
1852 virtual void Accept(HGraphVisitor* visitor) = 0;
1853 virtual const char* DebugName() const = 0;
1854
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001855 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001856 void SetRawInputAt(size_t index, HInstruction* input) {
1857 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1858 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001859
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001860 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001861
1862 uint32_t GetDexPc() const { return dex_pc_; }
1863
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001864 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001865
Roland Levillaine161a2a2014-10-03 12:45:18 +01001866 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001867 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001868
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001869 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001870 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001871
Calin Juravle10e244f2015-01-26 18:54:32 +00001872 // Does not apply for all instructions, but having this at top level greatly
1873 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001874 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001875 virtual bool CanBeNull() const {
1876 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1877 return true;
1878 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001879
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001880 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001881 return false;
1882 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001883
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001884 virtual bool IsActualObject() const {
1885 return GetType() == Primitive::kPrimNot;
1886 }
1887
Calin Juravle2e768302015-07-28 14:41:11 +00001888 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001889
Calin Juravle61d544b2015-02-23 16:46:57 +00001890 ReferenceTypeInfo GetReferenceTypeInfo() const {
1891 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1892 return reference_type_info_;
1893 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001894
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001895 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001896 DCHECK(user != nullptr);
1897 HUseListNode<HInstruction*>* use =
1898 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1899 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001900 }
1901
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001902 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001903 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001904 HUseListNode<HEnvironment*>* env_use =
1905 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1906 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001907 }
1908
David Brazdil1abb4192015-02-17 18:33:36 +00001909 void RemoveAsUserOfInput(size_t input) {
1910 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1911 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1912 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001913
David Brazdil1abb4192015-02-17 18:33:36 +00001914 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1915 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001916
David Brazdiled596192015-01-23 10:39:45 +00001917 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1918 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001919 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001920 bool HasOnlyOneNonEnvironmentUse() const {
1921 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1922 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001923
Roland Levillain6c82d402014-10-13 16:10:27 +01001924 // Does this instruction strictly dominate `other_instruction`?
1925 // Returns false if this instruction and `other_instruction` are the same.
1926 // Aborts if this instruction and `other_instruction` are both phis.
1927 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001928
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001929 int GetId() const { return id_; }
1930 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001931
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001932 int GetSsaIndex() const { return ssa_index_; }
1933 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1934 bool HasSsaIndex() const { return ssa_index_ != -1; }
1935
1936 bool HasEnvironment() const { return environment_ != nullptr; }
1937 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001938 // Set the `environment_` field. Raw because this method does not
1939 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001940 void SetRawEnvironment(HEnvironment* environment) {
1941 DCHECK(environment_ == nullptr);
1942 DCHECK_EQ(environment->GetHolder(), this);
1943 environment_ = environment;
1944 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001945
1946 // Set the environment of this instruction, copying it from `environment`. While
1947 // copying, the uses lists are being updated.
1948 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001949 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001950 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001951 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001952 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001953 if (environment->GetParent() != nullptr) {
1954 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1955 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001956 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001957
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001958 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1959 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001960 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001961 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001962 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001963 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001964 if (environment->GetParent() != nullptr) {
1965 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1966 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001967 }
1968
Nicolas Geoffray39468442014-09-02 15:17:15 +01001969 // Returns the number of entries in the environment. Typically, that is the
1970 // number of dex registers in a method. It could be more in case of inlining.
1971 size_t EnvironmentSize() const;
1972
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001973 LocationSummary* GetLocations() const { return locations_; }
1974 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001975
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001976 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001977 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001978
Alexandre Rames188d4312015-04-09 18:30:21 +01001979 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1980 // uses of this instruction by `other` are *not* updated.
1981 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1982 ReplaceWith(other);
1983 other->ReplaceInput(this, use_index);
1984 }
1985
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001986 // Move `this` instruction before `cursor`.
1987 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001988
Vladimir Markofb337ea2015-11-25 15:25:10 +00001989 // Move `this` before its first user and out of any loops. If there is no
1990 // out-of-loop user that dominates all other users, move the instruction
1991 // to the end of the out-of-loop common dominator of the user's blocks.
1992 //
1993 // This can be used only on non-throwing instructions with no side effects that
1994 // have at least one use but no environment uses.
1995 void MoveBeforeFirstUserAndOutOfLoops();
1996
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001997#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001998 bool Is##type() const; \
1999 const H##type* As##type() const; \
2000 H##type* As##type();
2001
2002 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2003#undef INSTRUCTION_TYPE_CHECK
2004
2005#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002006 bool Is##type() const { return (As##type() != nullptr); } \
2007 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002008 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002009 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002010#undef INSTRUCTION_TYPE_CHECK
2011
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002012 // Returns whether the instruction can be moved within the graph.
2013 virtual bool CanBeMoved() const { return false; }
2014
2015 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002016 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002017 return false;
2018 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002019
2020 // Returns whether any data encoded in the two instructions is equal.
2021 // This method does not look at the inputs. Both instructions must be
2022 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002023 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002024 return false;
2025 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002026
2027 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002028 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002029 // 2) Their inputs are identical.
2030 bool Equals(HInstruction* other) const;
2031
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002032 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2033 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2034 // the virtual function because the __attribute__((__pure__)) doesn't really
2035 // apply the strong requirement for virtual functions, preventing optimizations.
2036 InstructionKind GetKind() const PURE;
2037 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002038
2039 virtual size_t ComputeHashCode() const {
2040 size_t result = GetKind();
2041 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2042 result = (result * 31) + InputAt(i)->GetId();
2043 }
2044 return result;
2045 }
2046
2047 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002048 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002049
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002050 size_t GetLifetimePosition() const { return lifetime_position_; }
2051 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2052 LiveInterval* GetLiveInterval() const { return live_interval_; }
2053 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2054 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2055
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002056 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2057
2058 // Returns whether the code generation of the instruction will require to have access
2059 // to the current method. Such instructions are:
2060 // (1): Instructions that require an environment, as calling the runtime requires
2061 // to walk the stack and have the current method stored at a specific stack address.
2062 // (2): Object literals like classes and strings, that are loaded from the dex cache
2063 // fields of the current method.
2064 bool NeedsCurrentMethod() const {
2065 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2066 }
2067
Vladimir Markodc151b22015-10-15 18:02:30 +01002068 // Returns whether the code generation of the instruction will require to have access
2069 // to the dex cache of the current method's declaring class via the current method.
2070 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002071
Mark Mendellc4701932015-04-10 13:18:51 -04002072 // Does this instruction have any use in an environment before
2073 // control flow hits 'other'?
2074 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2075
2076 // Remove all references to environment uses of this instruction.
2077 // The caller must ensure that this is safe to do.
2078 void RemoveEnvironmentUsers();
2079
David Brazdil1abb4192015-02-17 18:33:36 +00002080 protected:
2081 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2082 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
Aart Bik5d75afe2015-12-14 11:57:01 -08002083 void SetSideEffects(SideEffects other) { side_effects_ = other; }
David Brazdil1abb4192015-02-17 18:33:36 +00002084
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002085 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002086 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2087
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002088 HInstruction* previous_;
2089 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002090 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002091 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002092
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002093 // An instruction gets an id when it is added to the graph.
2094 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002095 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002096 int id_;
2097
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002098 // When doing liveness analysis, instructions that have uses get an SSA index.
2099 int ssa_index_;
2100
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002101 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002102 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002103
2104 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002105 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002106
Nicolas Geoffray39468442014-09-02 15:17:15 +01002107 // The environment associated with this instruction. Not null if the instruction
2108 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002109 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002110
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002111 // Set by the code generator.
2112 LocationSummary* locations_;
2113
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002114 // Set by the liveness analysis.
2115 LiveInterval* live_interval_;
2116
2117 // Set by the liveness analysis, this is the position in a linear
2118 // order of blocks where this instruction's live interval start.
2119 size_t lifetime_position_;
2120
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002121 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002122
Calin Juravleacf735c2015-02-12 15:25:22 +00002123 // TODO: for primitive types this should be marked as invalid.
2124 ReferenceTypeInfo reference_type_info_;
2125
David Brazdil1abb4192015-02-17 18:33:36 +00002126 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002127 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002128 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002129 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002130 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002131
2132 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2133};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002134std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002135
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002136class HInputIterator : public ValueObject {
2137 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002138 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002139
2140 bool Done() const { return index_ == instruction_->InputCount(); }
2141 HInstruction* Current() const { return instruction_->InputAt(index_); }
2142 void Advance() { index_++; }
2143
2144 private:
2145 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002146 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002147
2148 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2149};
2150
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002151class HInstructionIterator : public ValueObject {
2152 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002153 explicit HInstructionIterator(const HInstructionList& instructions)
2154 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002155 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002156 }
2157
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002158 bool Done() const { return instruction_ == nullptr; }
2159 HInstruction* Current() const { return instruction_; }
2160 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002161 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002162 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002163 }
2164
2165 private:
2166 HInstruction* instruction_;
2167 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002168
2169 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002170};
2171
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002172class HBackwardInstructionIterator : public ValueObject {
2173 public:
2174 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2175 : instruction_(instructions.last_instruction_) {
2176 next_ = Done() ? nullptr : instruction_->GetPrevious();
2177 }
2178
2179 bool Done() const { return instruction_ == nullptr; }
2180 HInstruction* Current() const { return instruction_; }
2181 void Advance() {
2182 instruction_ = next_;
2183 next_ = Done() ? nullptr : instruction_->GetPrevious();
2184 }
2185
2186 private:
2187 HInstruction* instruction_;
2188 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002189
2190 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002191};
2192
Vladimir Markof9f64412015-09-02 14:05:49 +01002193template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002194class HTemplateInstruction: public HInstruction {
2195 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002196 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002197 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002198 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002199
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002200 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002201
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002202 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002203 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2204 DCHECK_LT(i, N);
2205 return inputs_[i];
2206 }
David Brazdil1abb4192015-02-17 18:33:36 +00002207
2208 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002209 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002210 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002211 }
2212
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002213 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002214 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002215
2216 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002217};
2218
Vladimir Markof9f64412015-09-02 14:05:49 +01002219// HTemplateInstruction specialization for N=0.
2220template<>
2221class HTemplateInstruction<0>: public HInstruction {
2222 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002223 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002224 : HInstruction(side_effects, dex_pc) {}
2225
Vladimir Markof9f64412015-09-02 14:05:49 +01002226 virtual ~HTemplateInstruction() {}
2227
2228 size_t InputCount() const OVERRIDE { return 0; }
2229
2230 protected:
2231 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2232 LOG(FATAL) << "Unreachable";
2233 UNREACHABLE();
2234 }
2235
2236 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2237 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2238 LOG(FATAL) << "Unreachable";
2239 UNREACHABLE();
2240 }
2241
2242 private:
2243 friend class SsaBuilder;
2244};
2245
Dave Allison20dfc792014-06-16 20:44:29 -07002246template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002247class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002248 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002249 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002250 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002251 virtual ~HExpression() {}
2252
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002253 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002254
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002255 protected:
2256 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002257};
2258
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002259// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2260// instruction that branches to the exit block.
2261class HReturnVoid : public HTemplateInstruction<0> {
2262 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002263 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2264 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002265
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002266 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002267
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002268 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002269
2270 private:
2271 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2272};
2273
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002274// Represents dex's RETURN opcodes. A HReturn is a control flow
2275// instruction that branches to the exit block.
2276class HReturn : public HTemplateInstruction<1> {
2277 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002278 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2279 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002280 SetRawInputAt(0, value);
2281 }
2282
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002283 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002284
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002285 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002286
2287 private:
2288 DISALLOW_COPY_AND_ASSIGN(HReturn);
2289};
2290
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002291// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002292// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002293// exit block.
2294class HExit : public HTemplateInstruction<0> {
2295 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002296 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002297
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002298 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002299
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002300 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002301
2302 private:
2303 DISALLOW_COPY_AND_ASSIGN(HExit);
2304};
2305
2306// Jumps from one block to another.
2307class HGoto : public HTemplateInstruction<0> {
2308 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002309 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002310
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002311 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002312
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002313 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002314 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002315 }
2316
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002317 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002318
2319 private:
2320 DISALLOW_COPY_AND_ASSIGN(HGoto);
2321};
2322
Roland Levillain9867bc72015-08-05 10:21:34 +01002323class HConstant : public HExpression<0> {
2324 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002325 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2326 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002327
2328 bool CanBeMoved() const OVERRIDE { return true; }
2329
2330 virtual bool IsMinusOne() const { return false; }
2331 virtual bool IsZero() const { return false; }
2332 virtual bool IsOne() const { return false; }
2333
David Brazdil77a48ae2015-09-15 12:34:04 +00002334 virtual uint64_t GetValueAsUint64() const = 0;
2335
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002336 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002337
2338 private:
2339 DISALLOW_COPY_AND_ASSIGN(HConstant);
2340};
2341
2342class HNullConstant : public HConstant {
2343 public:
2344 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2345 return true;
2346 }
2347
David Brazdil77a48ae2015-09-15 12:34:04 +00002348 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2349
Roland Levillain9867bc72015-08-05 10:21:34 +01002350 size_t ComputeHashCode() const OVERRIDE { return 0; }
2351
2352 DECLARE_INSTRUCTION(NullConstant);
2353
2354 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002355 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002356
2357 friend class HGraph;
2358 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2359};
2360
2361// Constants of the type int. Those can be from Dex instructions, or
2362// synthesized (for example with the if-eqz instruction).
2363class HIntConstant : public HConstant {
2364 public:
2365 int32_t GetValue() const { return value_; }
2366
David Brazdil9f389d42015-10-01 14:32:56 +01002367 uint64_t GetValueAsUint64() const OVERRIDE {
2368 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2369 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002370
Roland Levillain9867bc72015-08-05 10:21:34 +01002371 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2372 DCHECK(other->IsIntConstant());
2373 return other->AsIntConstant()->value_ == value_;
2374 }
2375
2376 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2377
2378 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2379 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2380 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2381
2382 DECLARE_INSTRUCTION(IntConstant);
2383
2384 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002385 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2386 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2387 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2388 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002389
2390 const int32_t value_;
2391
2392 friend class HGraph;
2393 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2394 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2395 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2396};
2397
2398class HLongConstant : public HConstant {
2399 public:
2400 int64_t GetValue() const { return value_; }
2401
David Brazdil77a48ae2015-09-15 12:34:04 +00002402 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2403
Roland Levillain9867bc72015-08-05 10:21:34 +01002404 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2405 DCHECK(other->IsLongConstant());
2406 return other->AsLongConstant()->value_ == value_;
2407 }
2408
2409 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2410
2411 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2412 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2413 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2414
2415 DECLARE_INSTRUCTION(LongConstant);
2416
2417 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002418 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2419 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002420
2421 const int64_t value_;
2422
2423 friend class HGraph;
2424 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2425};
Dave Allison20dfc792014-06-16 20:44:29 -07002426
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002427// Conditional branch. A block ending with an HIf instruction must have
2428// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002429class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002430 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002431 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2432 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002433 SetRawInputAt(0, input);
2434 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002435
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002436 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002437
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002438 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002439 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002440 }
2441
2442 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002443 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002444 }
2445
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002446 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002447
2448 private:
2449 DISALLOW_COPY_AND_ASSIGN(HIf);
2450};
2451
David Brazdilfc6a86a2015-06-26 10:33:45 +00002452
2453// Abstract instruction which marks the beginning and/or end of a try block and
2454// links it to the respective exception handlers. Behaves the same as a Goto in
2455// non-exceptional control flow.
2456// Normal-flow successor is stored at index zero, exception handlers under
2457// higher indices in no particular order.
2458class HTryBoundary : public HTemplateInstruction<0> {
2459 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002460 enum BoundaryKind {
2461 kEntry,
2462 kExit,
2463 };
2464
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002465 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2466 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002467
2468 bool IsControlFlow() const OVERRIDE { return true; }
2469
2470 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002471 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002472
David Brazdild26a4112015-11-10 11:07:31 +00002473 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2474 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2475 }
2476
David Brazdilfc6a86a2015-06-26 10:33:45 +00002477 // Returns whether `handler` is among its exception handlers (non-zero index
2478 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002479 bool HasExceptionHandler(const HBasicBlock& handler) const {
2480 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002481 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002482 }
2483
2484 // If not present already, adds `handler` to its block's list of exception
2485 // handlers.
2486 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002487 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002488 GetBlock()->AddSuccessor(handler);
2489 }
2490 }
2491
David Brazdil56e1acc2015-06-30 15:41:36 +01002492 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002493
David Brazdilffee3d32015-07-06 11:48:53 +01002494 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2495
David Brazdilfc6a86a2015-06-26 10:33:45 +00002496 DECLARE_INSTRUCTION(TryBoundary);
2497
2498 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002499 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002500
2501 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2502};
2503
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002504// Deoptimize to interpreter, upon checking a condition.
2505class HDeoptimize : public HTemplateInstruction<1> {
2506 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002507 // We set CanTriggerGC to prevent any intermediate address to be live
2508 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002509 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002510 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002511 SetRawInputAt(0, cond);
2512 }
2513
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002514 bool CanBeMoved() const OVERRIDE { return true; }
2515 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2516 return true;
2517 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002518 bool NeedsEnvironment() const OVERRIDE { return true; }
2519 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002520
2521 DECLARE_INSTRUCTION(Deoptimize);
2522
2523 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002524 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2525};
2526
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002527// Represents the ArtMethod that was passed as a first argument to
2528// the method. It is used by instructions that depend on it, like
2529// instructions that work with the dex cache.
2530class HCurrentMethod : public HExpression<0> {
2531 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002532 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2533 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002534
2535 DECLARE_INSTRUCTION(CurrentMethod);
2536
2537 private:
2538 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2539};
2540
Mark Mendellfe57faa2015-09-18 09:26:15 -04002541// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2542// have one successor for each entry in the switch table, and the final successor
2543// will be the block containing the next Dex opcode.
2544class HPackedSwitch : public HTemplateInstruction<1> {
2545 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002546 HPackedSwitch(int32_t start_value,
2547 uint32_t num_entries,
2548 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002549 uint32_t dex_pc = kNoDexPc)
2550 : HTemplateInstruction(SideEffects::None(), dex_pc),
2551 start_value_(start_value),
2552 num_entries_(num_entries) {
2553 SetRawInputAt(0, input);
2554 }
2555
2556 bool IsControlFlow() const OVERRIDE { return true; }
2557
2558 int32_t GetStartValue() const { return start_value_; }
2559
Vladimir Marko211c2112015-09-24 16:52:33 +01002560 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002561
2562 HBasicBlock* GetDefaultBlock() const {
2563 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002564 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002565 }
2566 DECLARE_INSTRUCTION(PackedSwitch);
2567
2568 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002569 const int32_t start_value_;
2570 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002571
2572 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2573};
2574
Roland Levillain88cb1752014-10-20 16:36:47 +01002575class HUnaryOperation : public HExpression<1> {
2576 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002577 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2578 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002579 SetRawInputAt(0, input);
2580 }
2581
2582 HInstruction* GetInput() const { return InputAt(0); }
2583 Primitive::Type GetResultType() const { return GetType(); }
2584
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002585 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002586 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002587 return true;
2588 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002589
Roland Levillain9240d6a2014-10-20 16:47:04 +01002590 // Try to statically evaluate `operation` and return a HConstant
2591 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002592 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002593 HConstant* TryStaticEvaluation() const;
2594
2595 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002596 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2597 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002598
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002599 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002600
2601 private:
2602 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2603};
2604
Dave Allison20dfc792014-06-16 20:44:29 -07002605class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002606 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002607 HBinaryOperation(Primitive::Type result_type,
2608 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002609 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002610 SideEffects side_effects = SideEffects::None(),
2611 uint32_t dex_pc = kNoDexPc)
2612 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002613 SetRawInputAt(0, left);
2614 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002615 }
2616
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002617 HInstruction* GetLeft() const { return InputAt(0); }
2618 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002619 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002620
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002621 virtual bool IsCommutative() const { return false; }
2622
2623 // Put constant on the right.
2624 // Returns whether order is changed.
2625 bool OrderInputsWithConstantOnTheRight() {
2626 HInstruction* left = InputAt(0);
2627 HInstruction* right = InputAt(1);
2628 if (left->IsConstant() && !right->IsConstant()) {
2629 ReplaceInput(right, 0);
2630 ReplaceInput(left, 1);
2631 return true;
2632 }
2633 return false;
2634 }
2635
2636 // Order inputs by instruction id, but favor constant on the right side.
2637 // This helps GVN for commutative ops.
2638 void OrderInputs() {
2639 DCHECK(IsCommutative());
2640 HInstruction* left = InputAt(0);
2641 HInstruction* right = InputAt(1);
2642 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2643 return;
2644 }
2645 if (OrderInputsWithConstantOnTheRight()) {
2646 return;
2647 }
2648 // Order according to instruction id.
2649 if (left->GetId() > right->GetId()) {
2650 ReplaceInput(right, 0);
2651 ReplaceInput(left, 1);
2652 }
2653 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002654
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002655 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002656 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002657 return true;
2658 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002659
Roland Levillain9240d6a2014-10-20 16:47:04 +01002660 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002661 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002662 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002663 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002664
2665 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002666 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2667 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2668 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2669 HLongConstant* y ATTRIBUTE_UNUSED) const {
2670 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2671 return nullptr;
2672 }
2673 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2674 HIntConstant* y ATTRIBUTE_UNUSED) const {
2675 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2676 return nullptr;
2677 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002678 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2679 HNullConstant* y ATTRIBUTE_UNUSED) const {
2680 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2681 return nullptr;
2682 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002683
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002684 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002685 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002686 HConstant* GetConstantRight() const;
2687
2688 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002689 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002690 HInstruction* GetLeastConstantLeft() const;
2691
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002692 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002693
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002694 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002695 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2696};
2697
Mark Mendellc4701932015-04-10 13:18:51 -04002698// The comparison bias applies for floating point operations and indicates how NaN
2699// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002700enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002701 kNoBias, // bias is not applicable (i.e. for long operation)
2702 kGtBias, // return 1 for NaN comparisons
2703 kLtBias, // return -1 for NaN comparisons
2704};
2705
Dave Allison20dfc792014-06-16 20:44:29 -07002706class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002707 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002708 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2709 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002710 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002711 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002712
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002713 bool NeedsMaterialization() const { return needs_materialization_; }
2714 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002715
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002716 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002717 // `instruction`, and disregard moves in between.
2718 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002719
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002720 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002721
2722 virtual IfCondition GetCondition() const = 0;
2723
Mark Mendellc4701932015-04-10 13:18:51 -04002724 virtual IfCondition GetOppositeCondition() const = 0;
2725
Roland Levillain4fa13f62015-07-06 18:11:54 +01002726 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002727
2728 void SetBias(ComparisonBias bias) { bias_ = bias; }
2729
2730 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2731 return bias_ == other->AsCondition()->bias_;
2732 }
2733
Roland Levillain4fa13f62015-07-06 18:11:54 +01002734 bool IsFPConditionTrueIfNaN() const {
2735 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2736 IfCondition if_cond = GetCondition();
2737 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2738 }
2739
2740 bool IsFPConditionFalseIfNaN() const {
2741 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2742 IfCondition if_cond = GetCondition();
2743 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2744 }
2745
Dave Allison20dfc792014-06-16 20:44:29 -07002746 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002747 // For register allocation purposes, returns whether this instruction needs to be
2748 // materialized (that is, not just be in the processor flags).
2749 bool needs_materialization_;
2750
Mark Mendellc4701932015-04-10 13:18:51 -04002751 // Needed if we merge a HCompare into a HCondition.
2752 ComparisonBias bias_;
2753
Dave Allison20dfc792014-06-16 20:44:29 -07002754 DISALLOW_COPY_AND_ASSIGN(HCondition);
2755};
2756
2757// Instruction to check if two inputs are equal to each other.
2758class HEqual : public HCondition {
2759 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002760 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2761 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002762
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002763 bool IsCommutative() const OVERRIDE { return true; }
2764
Roland Levillain9867bc72015-08-05 10:21:34 +01002765 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002766 return GetBlock()->GetGraph()->GetIntConstant(
2767 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002768 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002769 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002770 return GetBlock()->GetGraph()->GetIntConstant(
2771 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002772 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002773 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2774 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002775 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002776 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002777
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002778 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002779
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002780 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002781 return kCondEQ;
2782 }
2783
Mark Mendellc4701932015-04-10 13:18:51 -04002784 IfCondition GetOppositeCondition() const OVERRIDE {
2785 return kCondNE;
2786 }
2787
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002788 private:
Aart Bike9f37602015-10-09 11:15:55 -07002789 template <typename T> bool Compute(T x, T y) const { return x == y; }
2790
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002791 DISALLOW_COPY_AND_ASSIGN(HEqual);
2792};
2793
Dave Allison20dfc792014-06-16 20:44:29 -07002794class HNotEqual : public HCondition {
2795 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002796 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2797 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002798
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002799 bool IsCommutative() const OVERRIDE { return true; }
2800
Roland Levillain9867bc72015-08-05 10:21:34 +01002801 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002802 return GetBlock()->GetGraph()->GetIntConstant(
2803 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002804 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002805 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002806 return GetBlock()->GetGraph()->GetIntConstant(
2807 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002808 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002809 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2810 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002811 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002812 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002813
Dave Allison20dfc792014-06-16 20:44:29 -07002814 DECLARE_INSTRUCTION(NotEqual);
2815
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002816 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002817 return kCondNE;
2818 }
2819
Mark Mendellc4701932015-04-10 13:18:51 -04002820 IfCondition GetOppositeCondition() const OVERRIDE {
2821 return kCondEQ;
2822 }
2823
Dave Allison20dfc792014-06-16 20:44:29 -07002824 private:
Aart Bike9f37602015-10-09 11:15:55 -07002825 template <typename T> bool Compute(T x, T y) const { return x != y; }
2826
Dave Allison20dfc792014-06-16 20:44:29 -07002827 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2828};
2829
2830class HLessThan : public HCondition {
2831 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002832 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2833 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002834
Roland Levillain9867bc72015-08-05 10:21:34 +01002835 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002836 return GetBlock()->GetGraph()->GetIntConstant(
2837 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002838 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002839 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002840 return GetBlock()->GetGraph()->GetIntConstant(
2841 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002842 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002843
Dave Allison20dfc792014-06-16 20:44:29 -07002844 DECLARE_INSTRUCTION(LessThan);
2845
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002846 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002847 return kCondLT;
2848 }
2849
Mark Mendellc4701932015-04-10 13:18:51 -04002850 IfCondition GetOppositeCondition() const OVERRIDE {
2851 return kCondGE;
2852 }
2853
Dave Allison20dfc792014-06-16 20:44:29 -07002854 private:
Aart Bike9f37602015-10-09 11:15:55 -07002855 template <typename T> bool Compute(T x, T y) const { return x < y; }
2856
Dave Allison20dfc792014-06-16 20:44:29 -07002857 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2858};
2859
2860class HLessThanOrEqual : public HCondition {
2861 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002862 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2863 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002864
Roland Levillain9867bc72015-08-05 10:21:34 +01002865 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002866 return GetBlock()->GetGraph()->GetIntConstant(
2867 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002868 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002869 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002870 return GetBlock()->GetGraph()->GetIntConstant(
2871 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002872 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002873
Dave Allison20dfc792014-06-16 20:44:29 -07002874 DECLARE_INSTRUCTION(LessThanOrEqual);
2875
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002876 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002877 return kCondLE;
2878 }
2879
Mark Mendellc4701932015-04-10 13:18:51 -04002880 IfCondition GetOppositeCondition() const OVERRIDE {
2881 return kCondGT;
2882 }
2883
Dave Allison20dfc792014-06-16 20:44:29 -07002884 private:
Aart Bike9f37602015-10-09 11:15:55 -07002885 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2886
Dave Allison20dfc792014-06-16 20:44:29 -07002887 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2888};
2889
2890class HGreaterThan : public HCondition {
2891 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002892 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2893 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002894
Roland Levillain9867bc72015-08-05 10:21:34 +01002895 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002896 return GetBlock()->GetGraph()->GetIntConstant(
2897 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002898 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002899 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002900 return GetBlock()->GetGraph()->GetIntConstant(
2901 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002902 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002903
Dave Allison20dfc792014-06-16 20:44:29 -07002904 DECLARE_INSTRUCTION(GreaterThan);
2905
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002906 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002907 return kCondGT;
2908 }
2909
Mark Mendellc4701932015-04-10 13:18:51 -04002910 IfCondition GetOppositeCondition() const OVERRIDE {
2911 return kCondLE;
2912 }
2913
Dave Allison20dfc792014-06-16 20:44:29 -07002914 private:
Aart Bike9f37602015-10-09 11:15:55 -07002915 template <typename T> bool Compute(T x, T y) const { return x > y; }
2916
Dave Allison20dfc792014-06-16 20:44:29 -07002917 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2918};
2919
2920class HGreaterThanOrEqual : public HCondition {
2921 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002922 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2923 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002924
Roland Levillain9867bc72015-08-05 10:21:34 +01002925 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002926 return GetBlock()->GetGraph()->GetIntConstant(
2927 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002928 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002929 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002930 return GetBlock()->GetGraph()->GetIntConstant(
2931 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002932 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002933
Dave Allison20dfc792014-06-16 20:44:29 -07002934 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2935
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002936 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002937 return kCondGE;
2938 }
2939
Mark Mendellc4701932015-04-10 13:18:51 -04002940 IfCondition GetOppositeCondition() const OVERRIDE {
2941 return kCondLT;
2942 }
2943
Dave Allison20dfc792014-06-16 20:44:29 -07002944 private:
Aart Bike9f37602015-10-09 11:15:55 -07002945 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2946
Dave Allison20dfc792014-06-16 20:44:29 -07002947 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2948};
2949
Aart Bike9f37602015-10-09 11:15:55 -07002950class HBelow : public HCondition {
2951 public:
2952 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2953 : HCondition(first, second, dex_pc) {}
2954
2955 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2956 return GetBlock()->GetGraph()->GetIntConstant(
2957 Compute(static_cast<uint32_t>(x->GetValue()),
2958 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2959 }
2960 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2961 return GetBlock()->GetGraph()->GetIntConstant(
2962 Compute(static_cast<uint64_t>(x->GetValue()),
2963 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2964 }
2965
2966 DECLARE_INSTRUCTION(Below);
2967
2968 IfCondition GetCondition() const OVERRIDE {
2969 return kCondB;
2970 }
2971
2972 IfCondition GetOppositeCondition() const OVERRIDE {
2973 return kCondAE;
2974 }
2975
2976 private:
2977 template <typename T> bool Compute(T x, T y) const { return x < y; }
2978
2979 DISALLOW_COPY_AND_ASSIGN(HBelow);
2980};
2981
2982class HBelowOrEqual : public HCondition {
2983 public:
2984 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2985 : HCondition(first, second, dex_pc) {}
2986
2987 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2988 return GetBlock()->GetGraph()->GetIntConstant(
2989 Compute(static_cast<uint32_t>(x->GetValue()),
2990 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2991 }
2992 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2993 return GetBlock()->GetGraph()->GetIntConstant(
2994 Compute(static_cast<uint64_t>(x->GetValue()),
2995 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2996 }
2997
2998 DECLARE_INSTRUCTION(BelowOrEqual);
2999
3000 IfCondition GetCondition() const OVERRIDE {
3001 return kCondBE;
3002 }
3003
3004 IfCondition GetOppositeCondition() const OVERRIDE {
3005 return kCondA;
3006 }
3007
3008 private:
3009 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3010
3011 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3012};
3013
3014class HAbove : public HCondition {
3015 public:
3016 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3017 : HCondition(first, second, dex_pc) {}
3018
3019 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3020 return GetBlock()->GetGraph()->GetIntConstant(
3021 Compute(static_cast<uint32_t>(x->GetValue()),
3022 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3023 }
3024 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3025 return GetBlock()->GetGraph()->GetIntConstant(
3026 Compute(static_cast<uint64_t>(x->GetValue()),
3027 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3028 }
3029
3030 DECLARE_INSTRUCTION(Above);
3031
3032 IfCondition GetCondition() const OVERRIDE {
3033 return kCondA;
3034 }
3035
3036 IfCondition GetOppositeCondition() const OVERRIDE {
3037 return kCondBE;
3038 }
3039
3040 private:
3041 template <typename T> bool Compute(T x, T y) const { return x > y; }
3042
3043 DISALLOW_COPY_AND_ASSIGN(HAbove);
3044};
3045
3046class HAboveOrEqual : public HCondition {
3047 public:
3048 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3049 : HCondition(first, second, dex_pc) {}
3050
3051 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3052 return GetBlock()->GetGraph()->GetIntConstant(
3053 Compute(static_cast<uint32_t>(x->GetValue()),
3054 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3055 }
3056 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3057 return GetBlock()->GetGraph()->GetIntConstant(
3058 Compute(static_cast<uint64_t>(x->GetValue()),
3059 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3060 }
3061
3062 DECLARE_INSTRUCTION(AboveOrEqual);
3063
3064 IfCondition GetCondition() const OVERRIDE {
3065 return kCondAE;
3066 }
3067
3068 IfCondition GetOppositeCondition() const OVERRIDE {
3069 return kCondB;
3070 }
3071
3072 private:
3073 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3074
3075 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3076};
Dave Allison20dfc792014-06-16 20:44:29 -07003077
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003078// Instruction to check how two inputs compare to each other.
3079// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3080class HCompare : public HBinaryOperation {
3081 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003082 HCompare(Primitive::Type type,
3083 HInstruction* first,
3084 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003085 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003086 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003087 : HBinaryOperation(Primitive::kPrimInt,
3088 first,
3089 second,
3090 SideEffectsForArchRuntimeCalls(type),
3091 dex_pc),
3092 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003093 DCHECK_EQ(type, first->GetType());
3094 DCHECK_EQ(type, second->GetType());
3095 }
3096
Roland Levillain9867bc72015-08-05 10:21:34 +01003097 template <typename T>
3098 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003099
Roland Levillain9867bc72015-08-05 10:21:34 +01003100 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003101 return GetBlock()->GetGraph()->GetIntConstant(
3102 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003103 }
3104 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003105 return GetBlock()->GetGraph()->GetIntConstant(
3106 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003107 }
3108
Calin Juravleddb7df22014-11-25 20:56:51 +00003109 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3110 return bias_ == other->AsCompare()->bias_;
3111 }
3112
Mark Mendellc4701932015-04-10 13:18:51 -04003113 ComparisonBias GetBias() const { return bias_; }
3114
Roland Levillain4fa13f62015-07-06 18:11:54 +01003115 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003116
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003117
3118 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3119 // MIPS64 uses a runtime call for FP comparisons.
3120 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3121 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003122
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003123 DECLARE_INSTRUCTION(Compare);
3124
3125 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003126 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003127
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003128 DISALLOW_COPY_AND_ASSIGN(HCompare);
3129};
3130
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003131// A local in the graph. Corresponds to a Dex register.
3132class HLocal : public HTemplateInstruction<0> {
3133 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003134 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003135 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003136
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003137 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003138
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003139 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003140
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003141 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003142 // The Dex register number.
3143 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003144
3145 DISALLOW_COPY_AND_ASSIGN(HLocal);
3146};
3147
3148// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003149class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003150 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003151 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3152 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003153 SetRawInputAt(0, local);
3154 }
3155
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003156 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3157
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003158 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003159
3160 private:
3161 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3162};
3163
3164// Store a value in a given local. This instruction has two inputs: the value
3165// and the local.
3166class HStoreLocal : public HTemplateInstruction<2> {
3167 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003168 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3169 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003170 SetRawInputAt(0, local);
3171 SetRawInputAt(1, value);
3172 }
3173
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003174 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3175
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003176 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003177
3178 private:
3179 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3180};
3181
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003182class HFloatConstant : public HConstant {
3183 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003184 float GetValue() const { return value_; }
3185
David Brazdil77a48ae2015-09-15 12:34:04 +00003186 uint64_t GetValueAsUint64() const OVERRIDE {
3187 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3188 }
3189
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003190 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003191 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003192 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003193 }
3194
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003195 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003196
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003197 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003198 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003199 }
3200 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003201 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003202 }
3203 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003204 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3205 }
3206 bool IsNaN() const {
3207 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003208 }
3209
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003210 DECLARE_INSTRUCTION(FloatConstant);
3211
3212 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003213 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3214 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3215 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3216 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003217
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003218 const float value_;
3219
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003220 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003221 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003222 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003223 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3224};
3225
3226class HDoubleConstant : public HConstant {
3227 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003228 double GetValue() const { return value_; }
3229
David Brazdil77a48ae2015-09-15 12:34:04 +00003230 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3231
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003232 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003233 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003234 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003235 }
3236
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003237 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003238
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003239 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003240 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003241 }
3242 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003243 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003244 }
3245 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003246 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3247 }
3248 bool IsNaN() const {
3249 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003250 }
3251
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003252 DECLARE_INSTRUCTION(DoubleConstant);
3253
3254 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003255 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3256 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3257 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3258 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003259
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003260 const double value_;
3261
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003262 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003263 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003264 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003265 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3266};
3267
David Brazdil6de19382016-01-08 17:37:10 +00003268class HNewInstance : public HExpression<2> {
3269 public:
3270 HNewInstance(HInstruction* cls,
3271 HCurrentMethod* current_method,
3272 uint32_t dex_pc,
3273 uint16_t type_index,
3274 const DexFile& dex_file,
3275 bool can_throw,
3276 bool finalizable,
3277 QuickEntrypointEnum entrypoint)
3278 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3279 type_index_(type_index),
3280 dex_file_(dex_file),
3281 can_throw_(can_throw),
3282 finalizable_(finalizable),
3283 entrypoint_(entrypoint) {
3284 SetRawInputAt(0, cls);
3285 SetRawInputAt(1, current_method);
3286 }
3287
3288 uint16_t GetTypeIndex() const { return type_index_; }
3289 const DexFile& GetDexFile() const { return dex_file_; }
3290
3291 // Calls runtime so needs an environment.
3292 bool NeedsEnvironment() const OVERRIDE { return true; }
3293
3294 // It may throw when called on type that's not instantiable/accessible.
3295 // It can throw OOME.
3296 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3297 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3298
3299 bool IsFinalizable() const { return finalizable_; }
3300
3301 bool CanBeNull() const OVERRIDE { return false; }
3302
3303 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3304
3305 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3306 entrypoint_ = entrypoint;
3307 }
3308
3309 bool IsStringAlloc() const;
3310
3311 DECLARE_INSTRUCTION(NewInstance);
3312
3313 private:
3314 const uint16_t type_index_;
3315 const DexFile& dex_file_;
3316 const bool can_throw_;
3317 const bool finalizable_;
3318 QuickEntrypointEnum entrypoint_;
3319
3320 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3321};
3322
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003323enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003324#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3325 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003326#include "intrinsics_list.h"
3327 kNone,
3328 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3329#undef INTRINSICS_LIST
3330#undef OPTIMIZING_INTRINSICS
3331};
3332std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3333
Agi Csaki05f20562015-08-19 14:58:14 -07003334enum IntrinsicNeedsEnvironmentOrCache {
3335 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3336 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003337};
3338
Aart Bik5d75afe2015-12-14 11:57:01 -08003339enum IntrinsicSideEffects {
3340 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3341 kReadSideEffects, // Intrinsic may read heap memory.
3342 kWriteSideEffects, // Intrinsic may write heap memory.
3343 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3344};
3345
3346enum IntrinsicExceptions {
3347 kNoThrow, // Intrinsic does not throw any exceptions.
3348 kCanThrow // Intrinsic may throw exceptions.
3349};
3350
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003351class HInvoke : public HInstruction {
3352 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003353 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003354
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003355 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003356
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003357 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003358 SetRawInputAt(index, argument);
3359 }
3360
Roland Levillain3e3d7332015-04-28 11:00:54 +01003361 // Return the number of arguments. This number can be lower than
3362 // the number of inputs returned by InputCount(), as some invoke
3363 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3364 // inputs at the end of their list of inputs.
3365 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3366
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003367 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003368
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003369 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003370 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003371
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003372 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3373
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003374 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003375 return intrinsic_;
3376 }
3377
Aart Bik5d75afe2015-12-14 11:57:01 -08003378 void SetIntrinsic(Intrinsics intrinsic,
3379 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3380 IntrinsicSideEffects side_effects,
3381 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003382
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003383 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003384 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003385 }
3386
Aart Bik5d75afe2015-12-14 11:57:01 -08003387 bool CanThrow() const OVERRIDE { return can_throw_; }
3388
3389 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3390
3391 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3392 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3393 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003394
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003395 uint32_t* GetIntrinsicOptimizations() {
3396 return &intrinsic_optimizations_;
3397 }
3398
3399 const uint32_t* GetIntrinsicOptimizations() const {
3400 return &intrinsic_optimizations_;
3401 }
3402
3403 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3404
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003405 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003406
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003407 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003408 HInvoke(ArenaAllocator* arena,
3409 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003410 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003411 Primitive::Type return_type,
3412 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003413 uint32_t dex_method_index,
3414 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003415 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003416 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003417 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003418 inputs_(number_of_arguments + number_of_other_inputs,
3419 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003420 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003421 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003422 original_invoke_type_(original_invoke_type),
Aart Bik5d75afe2015-12-14 11:57:01 -08003423 can_throw_(true),
agicsaki57b81ec2015-08-11 17:39:37 -07003424 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003425 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003426 }
3427
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003428 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003429 return inputs_[index];
3430 }
3431
David Brazdil1abb4192015-02-17 18:33:36 +00003432 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003433 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003434 }
3435
Aart Bik5d75afe2015-12-14 11:57:01 -08003436 void SetCanThrow(bool can_throw) { can_throw_ = can_throw; }
3437
Roland Levillain3e3d7332015-04-28 11:00:54 +01003438 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003439 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003440 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003441 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003442 const InvokeType original_invoke_type_;
Aart Bik5d75afe2015-12-14 11:57:01 -08003443 bool can_throw_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003444 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003445
3446 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3447 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003448
3449 private:
3450 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3451};
3452
Calin Juravle175dc732015-08-25 15:42:32 +01003453class HInvokeUnresolved : public HInvoke {
3454 public:
3455 HInvokeUnresolved(ArenaAllocator* arena,
3456 uint32_t number_of_arguments,
3457 Primitive::Type return_type,
3458 uint32_t dex_pc,
3459 uint32_t dex_method_index,
3460 InvokeType invoke_type)
3461 : HInvoke(arena,
3462 number_of_arguments,
3463 0u /* number_of_other_inputs */,
3464 return_type,
3465 dex_pc,
3466 dex_method_index,
3467 invoke_type) {
3468 }
3469
3470 DECLARE_INSTRUCTION(InvokeUnresolved);
3471
3472 private:
3473 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3474};
3475
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003476class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003477 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003478 // Requirements of this method call regarding the class
3479 // initialization (clinit) check of its declaring class.
3480 enum class ClinitCheckRequirement {
3481 kNone, // Class already initialized.
3482 kExplicit, // Static call having explicit clinit check as last input.
3483 kImplicit, // Static call implicitly requiring a clinit check.
3484 };
3485
Vladimir Marko58155012015-08-19 12:49:41 +00003486 // Determines how to load the target ArtMethod*.
3487 enum class MethodLoadKind {
3488 // Use a String init ArtMethod* loaded from Thread entrypoints.
3489 kStringInit,
3490
3491 // Use the method's own ArtMethod* loaded by the register allocator.
3492 kRecursive,
3493
3494 // Use ArtMethod* at a known address, embed the direct address in the code.
3495 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3496 kDirectAddress,
3497
3498 // Use ArtMethod* at an address that will be known at link time, embed the direct
3499 // address in the code. If the image is relocatable, emit .patch_oat entry.
3500 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3501 // the image relocatable or not.
3502 kDirectAddressWithFixup,
3503
3504 // Load from resoved methods array in the dex cache using a PC-relative load.
3505 // Used when we need to use the dex cache, for example for invoke-static that
3506 // may cause class initialization (the entry may point to a resolution method),
3507 // and we know that we can access the dex cache arrays using a PC-relative load.
3508 kDexCachePcRelative,
3509
3510 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3511 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3512 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3513 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3514 kDexCacheViaMethod,
3515 };
3516
3517 // Determines the location of the code pointer.
3518 enum class CodePtrLocation {
3519 // Recursive call, use local PC-relative call instruction.
3520 kCallSelf,
3521
3522 // Use PC-relative call instruction patched at link time.
3523 // Used for calls within an oat file, boot->boot or app->app.
3524 kCallPCRelative,
3525
3526 // Call to a known target address, embed the direct address in code.
3527 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3528 kCallDirect,
3529
3530 // Call to a target address that will be known at link time, embed the direct
3531 // address in code. If the image is relocatable, emit .patch_oat entry.
3532 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3533 // the image relocatable or not.
3534 kCallDirectWithFixup,
3535
3536 // Use code pointer from the ArtMethod*.
3537 // Used when we don't know the target code. This is also the last-resort-kind used when
3538 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3539 kCallArtMethod,
3540 };
3541
3542 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003543 MethodLoadKind method_load_kind;
3544 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003545 // The method load data holds
3546 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3547 // Note that there are multiple string init methods, each having its own offset.
3548 // - the method address for kDirectAddress
3549 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003550 uint64_t method_load_data;
3551 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003552 };
3553
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003554 HInvokeStaticOrDirect(ArenaAllocator* arena,
3555 uint32_t number_of_arguments,
3556 Primitive::Type return_type,
3557 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003558 uint32_t method_index,
3559 MethodReference target_method,
3560 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003561 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003562 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003563 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003564 : HInvoke(arena,
3565 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003566 // There is potentially one extra argument for the HCurrentMethod node, and
3567 // potentially one other if the clinit check is explicit, and potentially
3568 // one other if the method is a string factory.
3569 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3570 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3571 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003572 return_type,
3573 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003574 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003575 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003576 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003577 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003578 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003579 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003580
Vladimir Markodc151b22015-10-15 18:02:30 +01003581 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003582 bool had_current_method_input = HasCurrentMethodInput();
3583 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3584
3585 // Using the current method is the default and once we find a better
3586 // method load kind, we should not go back to using the current method.
3587 DCHECK(had_current_method_input || !needs_current_method_input);
3588
3589 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003590 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3591 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003592 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003593 dispatch_info_ = dispatch_info;
3594 }
3595
Vladimir Markoc53c0792015-11-19 15:48:33 +00003596 void AddSpecialInput(HInstruction* input) {
3597 // We allow only one special input.
3598 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3599 DCHECK(InputCount() == GetSpecialInputIndex() ||
3600 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3601 InsertInputAt(GetSpecialInputIndex(), input);
3602 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003603
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003604 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003605 // We access the method via the dex cache so we can't do an implicit null check.
3606 // TODO: for intrinsics we can generate implicit null checks.
3607 return false;
3608 }
3609
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003610 bool CanBeNull() const OVERRIDE {
3611 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3612 }
3613
Vladimir Markoc53c0792015-11-19 15:48:33 +00003614 // Get the index of the special input, if any.
3615 //
David Brazdil6de19382016-01-08 17:37:10 +00003616 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3617 // method pointer; otherwise there may be one platform-specific special input,
3618 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003619 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3620
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003621 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3622 void SetOptimizedInvokeType(InvokeType invoke_type) {
3623 optimized_invoke_type_ = invoke_type;
3624 }
3625
Vladimir Marko58155012015-08-19 12:49:41 +00003626 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3627 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3628 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003629 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003630 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003631 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003632 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003633 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3634 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003635 bool HasCurrentMethodInput() const {
3636 // This function can be called only after the invoke has been fully initialized by the builder.
3637 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003638 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003639 return true;
3640 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003641 DCHECK(InputCount() == GetSpecialInputIndex() ||
3642 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003643 return false;
3644 }
3645 }
Vladimir Marko58155012015-08-19 12:49:41 +00003646 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3647 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003648 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003649
3650 int32_t GetStringInitOffset() const {
3651 DCHECK(IsStringInit());
3652 return dispatch_info_.method_load_data;
3653 }
3654
3655 uint64_t GetMethodAddress() const {
3656 DCHECK(HasMethodAddress());
3657 return dispatch_info_.method_load_data;
3658 }
3659
3660 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003661 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003662 return dispatch_info_.method_load_data;
3663 }
3664
3665 uint64_t GetDirectCodePtr() const {
3666 DCHECK(HasDirectCodePtr());
3667 return dispatch_info_.direct_code_ptr;
3668 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003669
Calin Juravle68ad6492015-08-18 17:08:12 +01003670 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3671
Roland Levillain4c0eb422015-04-24 16:43:49 +01003672 // Is this instruction a call to a static method?
3673 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003674 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003675 }
3676
Vladimir Markofbb184a2015-11-13 14:47:00 +00003677 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3678 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3679 // instruction; only relevant for static calls with explicit clinit check.
3680 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003681 DCHECK(IsStaticWithExplicitClinitCheck());
3682 size_t last_input_index = InputCount() - 1;
3683 HInstruction* last_input = InputAt(last_input_index);
3684 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003685 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003686 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003687 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003688 clinit_check_requirement_ = new_requirement;
3689 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003690 }
3691
David Brazdilbc9ab162016-01-20 14:50:19 +00003692 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00003693 DCHECK(IsStringInit());
3694 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00003695 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00003696 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003697 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00003698 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003699 }
3700
Roland Levillain4c0eb422015-04-24 16:43:49 +01003701 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003702 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003703 bool IsStaticWithExplicitClinitCheck() const {
3704 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3705 }
3706
3707 // Is this a call to a static method whose declaring class has an
3708 // implicit intialization check requirement?
3709 bool IsStaticWithImplicitClinitCheck() const {
3710 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3711 }
3712
Vladimir Markob554b5a2015-11-06 12:57:55 +00003713 // Does this method load kind need the current method as an input?
3714 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3715 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3716 }
3717
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003718 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003719
Roland Levillain4c0eb422015-04-24 16:43:49 +01003720 protected:
3721 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3722 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3723 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3724 HInstruction* input = input_record.GetInstruction();
3725 // `input` is the last input of a static invoke marked as having
3726 // an explicit clinit check. It must either be:
3727 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3728 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3729 DCHECK(input != nullptr);
3730 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3731 }
3732 return input_record;
3733 }
3734
Vladimir Markoc53c0792015-11-19 15:48:33 +00003735 void InsertInputAt(size_t index, HInstruction* input);
3736 void RemoveInputAt(size_t index);
3737
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003738 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003739 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003740 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003741 // The target method may refer to different dex file or method index than the original
3742 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3743 // in derived class to increase visibility.
3744 MethodReference target_method_;
3745 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003746
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003747 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003748};
Vladimir Markof64242a2015-12-01 14:58:23 +00003749std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003750std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003751
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003752class HInvokeVirtual : public HInvoke {
3753 public:
3754 HInvokeVirtual(ArenaAllocator* arena,
3755 uint32_t number_of_arguments,
3756 Primitive::Type return_type,
3757 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003758 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003759 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003760 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003761 vtable_index_(vtable_index) {}
3762
Calin Juravle641547a2015-04-21 22:08:51 +01003763 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003764 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003765 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003766 }
3767
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003768 uint32_t GetVTableIndex() const { return vtable_index_; }
3769
3770 DECLARE_INSTRUCTION(InvokeVirtual);
3771
3772 private:
3773 const uint32_t vtable_index_;
3774
3775 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3776};
3777
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003778class HInvokeInterface : public HInvoke {
3779 public:
3780 HInvokeInterface(ArenaAllocator* arena,
3781 uint32_t number_of_arguments,
3782 Primitive::Type return_type,
3783 uint32_t dex_pc,
3784 uint32_t dex_method_index,
3785 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003786 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003787 imt_index_(imt_index) {}
3788
Calin Juravle641547a2015-04-21 22:08:51 +01003789 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003790 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003791 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003792 }
3793
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003794 uint32_t GetImtIndex() const { return imt_index_; }
3795 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3796
3797 DECLARE_INSTRUCTION(InvokeInterface);
3798
3799 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003800 const uint32_t imt_index_;
3801
3802 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3803};
3804
Roland Levillain88cb1752014-10-20 16:36:47 +01003805class HNeg : public HUnaryOperation {
3806 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003807 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3808 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003809
Roland Levillain9867bc72015-08-05 10:21:34 +01003810 template <typename T> T Compute(T x) const { return -x; }
3811
3812 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003813 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003814 }
3815 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003816 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003817 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003818
Roland Levillain88cb1752014-10-20 16:36:47 +01003819 DECLARE_INSTRUCTION(Neg);
3820
3821 private:
3822 DISALLOW_COPY_AND_ASSIGN(HNeg);
3823};
3824
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003825class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003826 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003827 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003828 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003829 uint32_t dex_pc,
3830 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003831 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003832 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003833 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003834 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003835 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003836 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003837 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003838 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003839 }
3840
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003841 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003842 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003843
3844 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003845 bool NeedsEnvironment() const OVERRIDE { return true; }
3846
Mingyao Yang0c365e62015-03-31 15:09:29 -07003847 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3848 bool CanThrow() const OVERRIDE { return true; }
3849
Calin Juravle10e244f2015-01-26 18:54:32 +00003850 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003851
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003852 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3853
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003854 DECLARE_INSTRUCTION(NewArray);
3855
3856 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003857 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003858 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003859 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003860
3861 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3862};
3863
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003864class HAdd : public HBinaryOperation {
3865 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003866 HAdd(Primitive::Type result_type,
3867 HInstruction* left,
3868 HInstruction* right,
3869 uint32_t dex_pc = kNoDexPc)
3870 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003871
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003872 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003873
Roland Levillain9867bc72015-08-05 10:21:34 +01003874 template <typename T> T Compute(T x, T y) const { return x + y; }
3875
3876 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003877 return GetBlock()->GetGraph()->GetIntConstant(
3878 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003879 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003880 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003881 return GetBlock()->GetGraph()->GetLongConstant(
3882 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003883 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003884
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003885 DECLARE_INSTRUCTION(Add);
3886
3887 private:
3888 DISALLOW_COPY_AND_ASSIGN(HAdd);
3889};
3890
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003891class HSub : public HBinaryOperation {
3892 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003893 HSub(Primitive::Type result_type,
3894 HInstruction* left,
3895 HInstruction* right,
3896 uint32_t dex_pc = kNoDexPc)
3897 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003898
Roland Levillain9867bc72015-08-05 10:21:34 +01003899 template <typename T> T Compute(T x, T y) const { return x - y; }
3900
3901 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003902 return GetBlock()->GetGraph()->GetIntConstant(
3903 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003904 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003905 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003906 return GetBlock()->GetGraph()->GetLongConstant(
3907 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003908 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003909
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003910 DECLARE_INSTRUCTION(Sub);
3911
3912 private:
3913 DISALLOW_COPY_AND_ASSIGN(HSub);
3914};
3915
Calin Juravle34bacdf2014-10-07 20:23:36 +01003916class HMul : public HBinaryOperation {
3917 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003918 HMul(Primitive::Type result_type,
3919 HInstruction* left,
3920 HInstruction* right,
3921 uint32_t dex_pc = kNoDexPc)
3922 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003923
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003924 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003925
Roland Levillain9867bc72015-08-05 10:21:34 +01003926 template <typename T> T Compute(T x, T y) const { return x * y; }
3927
3928 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003929 return GetBlock()->GetGraph()->GetIntConstant(
3930 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003931 }
3932 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003933 return GetBlock()->GetGraph()->GetLongConstant(
3934 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003935 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003936
3937 DECLARE_INSTRUCTION(Mul);
3938
3939 private:
3940 DISALLOW_COPY_AND_ASSIGN(HMul);
3941};
3942
Calin Juravle7c4954d2014-10-28 16:57:40 +00003943class HDiv : public HBinaryOperation {
3944 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003945 HDiv(Primitive::Type result_type,
3946 HInstruction* left,
3947 HInstruction* right,
3948 uint32_t dex_pc)
3949 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003950
Roland Levillain9867bc72015-08-05 10:21:34 +01003951 template <typename T>
3952 T Compute(T x, T y) const {
3953 // Our graph structure ensures we never have 0 for `y` during
3954 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003955 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003956 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003957 return (y == -1) ? -x : x / y;
3958 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003959
Roland Levillain9867bc72015-08-05 10:21:34 +01003960 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003961 return GetBlock()->GetGraph()->GetIntConstant(
3962 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003963 }
3964 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003965 return GetBlock()->GetGraph()->GetLongConstant(
3966 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003967 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003968
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003969 static SideEffects SideEffectsForArchRuntimeCalls() {
3970 // The generated code can use a runtime call.
3971 return SideEffects::CanTriggerGC();
3972 }
3973
Calin Juravle7c4954d2014-10-28 16:57:40 +00003974 DECLARE_INSTRUCTION(Div);
3975
3976 private:
3977 DISALLOW_COPY_AND_ASSIGN(HDiv);
3978};
3979
Calin Juravlebacfec32014-11-14 15:54:36 +00003980class HRem : public HBinaryOperation {
3981 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003982 HRem(Primitive::Type result_type,
3983 HInstruction* left,
3984 HInstruction* right,
3985 uint32_t dex_pc)
3986 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003987
Roland Levillain9867bc72015-08-05 10:21:34 +01003988 template <typename T>
3989 T Compute(T x, T y) const {
3990 // Our graph structure ensures we never have 0 for `y` during
3991 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003992 DCHECK_NE(y, 0);
3993 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3994 return (y == -1) ? 0 : x % y;
3995 }
3996
Roland Levillain9867bc72015-08-05 10:21:34 +01003997 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003998 return GetBlock()->GetGraph()->GetIntConstant(
3999 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004000 }
4001 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004002 return GetBlock()->GetGraph()->GetLongConstant(
4003 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004004 }
4005
Calin Juravlebacfec32014-11-14 15:54:36 +00004006
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004007 static SideEffects SideEffectsForArchRuntimeCalls() {
4008 return SideEffects::CanTriggerGC();
4009 }
4010
Calin Juravlebacfec32014-11-14 15:54:36 +00004011 DECLARE_INSTRUCTION(Rem);
4012
4013 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004014 DISALLOW_COPY_AND_ASSIGN(HRem);
4015};
4016
Calin Juravled0d48522014-11-04 16:40:20 +00004017class HDivZeroCheck : public HExpression<1> {
4018 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004019 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4020 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004021 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004022 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004023 SetRawInputAt(0, value);
4024 }
4025
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004026 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4027
Calin Juravled0d48522014-11-04 16:40:20 +00004028 bool CanBeMoved() const OVERRIDE { return true; }
4029
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004030 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004031 return true;
4032 }
4033
4034 bool NeedsEnvironment() const OVERRIDE { return true; }
4035 bool CanThrow() const OVERRIDE { return true; }
4036
Calin Juravled0d48522014-11-04 16:40:20 +00004037 DECLARE_INSTRUCTION(DivZeroCheck);
4038
4039 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004040 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4041};
4042
Calin Juravle9aec02f2014-11-18 23:06:35 +00004043class HShl : public HBinaryOperation {
4044 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004045 HShl(Primitive::Type result_type,
4046 HInstruction* left,
4047 HInstruction* right,
4048 uint32_t dex_pc = kNoDexPc)
4049 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004050
Roland Levillain9867bc72015-08-05 10:21:34 +01004051 template <typename T, typename U, typename V>
4052 T Compute(T x, U y, V max_shift_value) const {
4053 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4054 "V is not the unsigned integer type corresponding to T");
4055 return x << (y & max_shift_value);
4056 }
4057
4058 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4059 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004060 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004061 }
4062 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4063 // case is handled as `x << static_cast<int>(y)`.
4064 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4065 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004066 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004067 }
4068 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4069 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004070 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004071 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004072
4073 DECLARE_INSTRUCTION(Shl);
4074
4075 private:
4076 DISALLOW_COPY_AND_ASSIGN(HShl);
4077};
4078
4079class HShr : public HBinaryOperation {
4080 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004081 HShr(Primitive::Type result_type,
4082 HInstruction* left,
4083 HInstruction* right,
4084 uint32_t dex_pc = kNoDexPc)
4085 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004086
Roland Levillain9867bc72015-08-05 10:21:34 +01004087 template <typename T, typename U, typename V>
4088 T Compute(T x, U y, V max_shift_value) const {
4089 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4090 "V is not the unsigned integer type corresponding to T");
4091 return x >> (y & max_shift_value);
4092 }
4093
4094 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4095 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004096 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004097 }
4098 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4099 // case is handled as `x >> static_cast<int>(y)`.
4100 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4101 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004102 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004103 }
4104 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4105 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004106 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004107 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004108
4109 DECLARE_INSTRUCTION(Shr);
4110
4111 private:
4112 DISALLOW_COPY_AND_ASSIGN(HShr);
4113};
4114
4115class HUShr : public HBinaryOperation {
4116 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004117 HUShr(Primitive::Type result_type,
4118 HInstruction* left,
4119 HInstruction* right,
4120 uint32_t dex_pc = kNoDexPc)
4121 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004122
Roland Levillain9867bc72015-08-05 10:21:34 +01004123 template <typename T, typename U, typename V>
4124 T Compute(T x, U y, V max_shift_value) const {
4125 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4126 "V is not the unsigned integer type corresponding to T");
4127 V ux = static_cast<V>(x);
4128 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004129 }
4130
Roland Levillain9867bc72015-08-05 10:21:34 +01004131 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4132 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004133 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004134 }
4135 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4136 // case is handled as `x >>> static_cast<int>(y)`.
4137 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4138 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004139 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004140 }
4141 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4142 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004143 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004144 }
4145
4146 DECLARE_INSTRUCTION(UShr);
4147
4148 private:
4149 DISALLOW_COPY_AND_ASSIGN(HUShr);
4150};
4151
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004152class HAnd : public HBinaryOperation {
4153 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004154 HAnd(Primitive::Type result_type,
4155 HInstruction* left,
4156 HInstruction* right,
4157 uint32_t dex_pc = kNoDexPc)
4158 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004159
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004160 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004161
Roland Levillain9867bc72015-08-05 10:21:34 +01004162 template <typename T, typename U>
4163 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4164
4165 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004166 return GetBlock()->GetGraph()->GetIntConstant(
4167 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004168 }
4169 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004170 return GetBlock()->GetGraph()->GetLongConstant(
4171 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004172 }
4173 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004174 return GetBlock()->GetGraph()->GetLongConstant(
4175 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004176 }
4177 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004178 return GetBlock()->GetGraph()->GetLongConstant(
4179 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004180 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004181
4182 DECLARE_INSTRUCTION(And);
4183
4184 private:
4185 DISALLOW_COPY_AND_ASSIGN(HAnd);
4186};
4187
4188class HOr : public HBinaryOperation {
4189 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004190 HOr(Primitive::Type result_type,
4191 HInstruction* left,
4192 HInstruction* right,
4193 uint32_t dex_pc = kNoDexPc)
4194 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004195
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004196 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004197
Roland Levillain9867bc72015-08-05 10:21:34 +01004198 template <typename T, typename U>
4199 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4200
4201 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004202 return GetBlock()->GetGraph()->GetIntConstant(
4203 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004204 }
4205 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004206 return GetBlock()->GetGraph()->GetLongConstant(
4207 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004208 }
4209 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004210 return GetBlock()->GetGraph()->GetLongConstant(
4211 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004212 }
4213 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004214 return GetBlock()->GetGraph()->GetLongConstant(
4215 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004216 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004217
4218 DECLARE_INSTRUCTION(Or);
4219
4220 private:
4221 DISALLOW_COPY_AND_ASSIGN(HOr);
4222};
4223
4224class HXor : public HBinaryOperation {
4225 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004226 HXor(Primitive::Type result_type,
4227 HInstruction* left,
4228 HInstruction* right,
4229 uint32_t dex_pc = kNoDexPc)
4230 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004231
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004232 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004233
Roland Levillain9867bc72015-08-05 10:21:34 +01004234 template <typename T, typename U>
4235 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4236
4237 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004238 return GetBlock()->GetGraph()->GetIntConstant(
4239 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004240 }
4241 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004242 return GetBlock()->GetGraph()->GetLongConstant(
4243 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004244 }
4245 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004246 return GetBlock()->GetGraph()->GetLongConstant(
4247 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004248 }
4249 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004250 return GetBlock()->GetGraph()->GetLongConstant(
4251 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004252 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004253
4254 DECLARE_INSTRUCTION(Xor);
4255
4256 private:
4257 DISALLOW_COPY_AND_ASSIGN(HXor);
4258};
4259
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004260class HRor : public HBinaryOperation {
4261 public:
4262 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4263 : HBinaryOperation(result_type, value, distance) {}
4264
4265 template <typename T, typename U, typename V>
4266 T Compute(T x, U y, V max_shift_value) const {
4267 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4268 "V is not the unsigned integer type corresponding to T");
4269 V ux = static_cast<V>(x);
4270 if ((y & max_shift_value) == 0) {
4271 return static_cast<T>(ux);
4272 } else {
4273 const V reg_bits = sizeof(T) * 8;
4274 return static_cast<T>(ux >> (y & max_shift_value)) |
4275 (x << (reg_bits - (y & max_shift_value)));
4276 }
4277 }
4278
4279 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4280 return GetBlock()->GetGraph()->GetIntConstant(
4281 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4282 }
4283 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4284 return GetBlock()->GetGraph()->GetLongConstant(
4285 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4286 }
4287 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4288 return GetBlock()->GetGraph()->GetLongConstant(
4289 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4290 }
4291
4292 DECLARE_INSTRUCTION(Ror);
4293
4294 private:
4295 DISALLOW_COPY_AND_ASSIGN(HRor);
4296};
4297
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004298// The value of a parameter in this method. Its location depends on
4299// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004300class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004301 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004302 HParameterValue(const DexFile& dex_file,
4303 uint16_t type_index,
4304 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004305 Primitive::Type parameter_type,
4306 bool is_this = false)
4307 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004308 dex_file_(dex_file),
4309 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004310 index_(index),
4311 is_this_(is_this),
4312 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004313
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004314 const DexFile& GetDexFile() const { return dex_file_; }
4315 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004316 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004317 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004318
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004319 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4320 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004321
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004322 DECLARE_INSTRUCTION(ParameterValue);
4323
4324 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004325 const DexFile& dex_file_;
4326 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004327 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004328 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004329 const uint8_t index_;
4330
Calin Juravle10e244f2015-01-26 18:54:32 +00004331 // Whether or not the parameter value corresponds to 'this' argument.
4332 const bool is_this_;
4333
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004334 bool can_be_null_;
4335
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004336 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4337};
4338
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004339class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004340 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004341 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4342 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004343
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004344 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004345 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004346 return true;
4347 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004348
Roland Levillain9867bc72015-08-05 10:21:34 +01004349 template <typename T> T Compute(T x) const { return ~x; }
4350
4351 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004352 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004353 }
4354 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004355 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004356 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004357
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004358 DECLARE_INSTRUCTION(Not);
4359
4360 private:
4361 DISALLOW_COPY_AND_ASSIGN(HNot);
4362};
4363
David Brazdil66d126e2015-04-03 16:02:44 +01004364class HBooleanNot : public HUnaryOperation {
4365 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004366 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4367 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004368
4369 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004370 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004371 return true;
4372 }
4373
Roland Levillain9867bc72015-08-05 10:21:34 +01004374 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004375 DCHECK(IsUint<1>(x));
4376 return !x;
4377 }
4378
Roland Levillain9867bc72015-08-05 10:21:34 +01004379 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004380 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004381 }
4382 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4383 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004384 UNREACHABLE();
4385 }
4386
4387 DECLARE_INSTRUCTION(BooleanNot);
4388
4389 private:
4390 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4391};
4392
Roland Levillaindff1f282014-11-05 14:15:05 +00004393class HTypeConversion : public HExpression<1> {
4394 public:
4395 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004396 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004397 : HExpression(result_type,
4398 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4399 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004400 SetRawInputAt(0, input);
4401 DCHECK_NE(input->GetType(), result_type);
4402 }
4403
4404 HInstruction* GetInput() const { return InputAt(0); }
4405 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4406 Primitive::Type GetResultType() const { return GetType(); }
4407
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004408 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004409 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004410
Roland Levillaindff1f282014-11-05 14:15:05 +00004411 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004412 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004413
Mark Mendelle82549b2015-05-06 10:55:34 -04004414 // Try to statically evaluate the conversion and return a HConstant
4415 // containing the result. If the input cannot be converted, return nullptr.
4416 HConstant* TryStaticEvaluation() const;
4417
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004418 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4419 Primitive::Type result_type) {
4420 // Some architectures may not require the 'GC' side effects, but at this point
4421 // in the compilation process we do not know what architecture we will
4422 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004423 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4424 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004425 return SideEffects::CanTriggerGC();
4426 }
4427 return SideEffects::None();
4428 }
4429
Roland Levillaindff1f282014-11-05 14:15:05 +00004430 DECLARE_INSTRUCTION(TypeConversion);
4431
4432 private:
4433 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4434};
4435
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004436static constexpr uint32_t kNoRegNumber = -1;
4437
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004438class HPhi : public HInstruction {
4439 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004440 HPhi(ArenaAllocator* arena,
4441 uint32_t reg_number,
4442 size_t number_of_inputs,
4443 Primitive::Type type,
4444 uint32_t dex_pc = kNoDexPc)
4445 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004446 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004447 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004448 type_(ToPhiType(type)),
4449 // Phis are constructed live and marked dead if conflicting or unused.
4450 // Individual steps of SsaBuilder should assume that if a phi has been
4451 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4452 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004453 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004454 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004455 }
4456
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004457 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4458 static Primitive::Type ToPhiType(Primitive::Type type) {
4459 switch (type) {
4460 case Primitive::kPrimBoolean:
4461 case Primitive::kPrimByte:
4462 case Primitive::kPrimShort:
4463 case Primitive::kPrimChar:
4464 return Primitive::kPrimInt;
4465 default:
4466 return type;
4467 }
4468 }
4469
David Brazdilffee3d32015-07-06 11:48:53 +01004470 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4471
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004472 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004473
4474 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004475 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004476
Calin Juravle10e244f2015-01-26 18:54:32 +00004477 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil4833f5a2015-12-16 10:37:39 +00004478 void SetType(Primitive::Type new_type) {
4479 // Make sure that only valid type changes occur. The following are allowed:
4480 // (1) int -> float/ref (primitive type propagation),
4481 // (2) long -> double (primitive type propagation).
4482 DCHECK(type_ == new_type ||
4483 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4484 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4485 (type_ == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4486 type_ = new_type;
4487 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004488
Calin Juravle10e244f2015-01-26 18:54:32 +00004489 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4490 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4491
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004492 uint32_t GetRegNumber() const { return reg_number_; }
4493
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004494 void SetDead() { is_live_ = false; }
4495 void SetLive() { is_live_ = true; }
4496 bool IsDead() const { return !is_live_; }
4497 bool IsLive() const { return is_live_; }
4498
David Brazdil77a48ae2015-09-15 12:34:04 +00004499 bool IsVRegEquivalentOf(HInstruction* other) const {
4500 return other != nullptr
4501 && other->IsPhi()
4502 && other->AsPhi()->GetBlock() == GetBlock()
4503 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4504 }
4505
Calin Juravlea4f88312015-04-16 12:57:19 +01004506 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4507 // An equivalent phi is a phi having the same dex register and type.
4508 // It assumes that phis with the same dex register are adjacent.
4509 HPhi* GetNextEquivalentPhiWithSameType() {
4510 HInstruction* next = GetNext();
4511 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4512 if (next->GetType() == GetType()) {
4513 return next->AsPhi();
4514 }
4515 next = next->GetNext();
4516 }
4517 return nullptr;
4518 }
4519
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004520 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004521
David Brazdil1abb4192015-02-17 18:33:36 +00004522 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004523 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004524 return inputs_[index];
4525 }
David Brazdil1abb4192015-02-17 18:33:36 +00004526
4527 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004528 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004529 }
4530
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004531 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004532 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004533 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004534 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004535 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004536 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004537
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004538 DISALLOW_COPY_AND_ASSIGN(HPhi);
4539};
4540
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004541class HNullCheck : public HExpression<1> {
4542 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004543 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4544 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004545 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004546 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004547 SetRawInputAt(0, value);
4548 }
4549
Calin Juravle10e244f2015-01-26 18:54:32 +00004550 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004551 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004552 return true;
4553 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004554
Calin Juravle10e244f2015-01-26 18:54:32 +00004555 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004556
Calin Juravle10e244f2015-01-26 18:54:32 +00004557 bool CanThrow() const OVERRIDE { return true; }
4558
4559 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004560
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004561
4562 DECLARE_INSTRUCTION(NullCheck);
4563
4564 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004565 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4566};
4567
4568class FieldInfo : public ValueObject {
4569 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004570 FieldInfo(MemberOffset field_offset,
4571 Primitive::Type field_type,
4572 bool is_volatile,
4573 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004574 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004575 const DexFile& dex_file,
4576 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004577 : field_offset_(field_offset),
4578 field_type_(field_type),
4579 is_volatile_(is_volatile),
4580 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004581 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004582 dex_file_(dex_file),
4583 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004584
4585 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004586 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004587 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004588 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004589 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004590 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004591 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004592
4593 private:
4594 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004595 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004596 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004597 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004598 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004599 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004600 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004601};
4602
4603class HInstanceFieldGet : public HExpression<1> {
4604 public:
4605 HInstanceFieldGet(HInstruction* value,
4606 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004607 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004608 bool is_volatile,
4609 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004610 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004611 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004612 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004613 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004614 : HExpression(field_type,
4615 SideEffects::FieldReadOfType(field_type, is_volatile),
4616 dex_pc),
4617 field_info_(field_offset,
4618 field_type,
4619 is_volatile,
4620 field_idx,
4621 declaring_class_def_index,
4622 dex_file,
4623 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004624 SetRawInputAt(0, value);
4625 }
4626
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004627 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004628
4629 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4630 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4631 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004632 }
4633
Calin Juravle641547a2015-04-21 22:08:51 +01004634 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4635 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004636 }
4637
4638 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004639 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4640 }
4641
Calin Juravle52c48962014-12-16 17:02:57 +00004642 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004643 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004644 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004645 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004646
4647 DECLARE_INSTRUCTION(InstanceFieldGet);
4648
4649 private:
4650 const FieldInfo field_info_;
4651
4652 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4653};
4654
4655class HInstanceFieldSet : public HTemplateInstruction<2> {
4656 public:
4657 HInstanceFieldSet(HInstruction* object,
4658 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004659 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004660 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004661 bool is_volatile,
4662 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004663 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004664 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004665 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004666 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004667 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4668 dex_pc),
4669 field_info_(field_offset,
4670 field_type,
4671 is_volatile,
4672 field_idx,
4673 declaring_class_def_index,
4674 dex_file,
4675 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004676 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004677 SetRawInputAt(0, object);
4678 SetRawInputAt(1, value);
4679 }
4680
Calin Juravle641547a2015-04-21 22:08:51 +01004681 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4682 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004683 }
4684
Calin Juravle52c48962014-12-16 17:02:57 +00004685 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004686 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004687 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004688 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004689 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004690 bool GetValueCanBeNull() const { return value_can_be_null_; }
4691 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004692
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004693 DECLARE_INSTRUCTION(InstanceFieldSet);
4694
4695 private:
4696 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004697 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004698
4699 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4700};
4701
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004702class HArrayGet : public HExpression<2> {
4703 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004704 HArrayGet(HInstruction* array,
4705 HInstruction* index,
4706 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004707 uint32_t dex_pc,
4708 SideEffects additional_side_effects = SideEffects::None())
4709 : HExpression(type,
4710 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004711 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004712 SetRawInputAt(0, array);
4713 SetRawInputAt(1, index);
4714 }
4715
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004716 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004717 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004718 return true;
4719 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004720 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004721 // TODO: We can be smarter here.
4722 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4723 // which generates the implicit null check. There are cases when these can be removed
4724 // to produce better code. If we ever add optimizations to do so we should allow an
4725 // implicit check here (as long as the address falls in the first page).
4726 return false;
4727 }
4728
David Brazdil4833f5a2015-12-16 10:37:39 +00004729 bool IsEquivalentOf(HArrayGet* other) const {
4730 bool result = (GetDexPc() == other->GetDexPc());
4731 if (kIsDebugBuild && result) {
4732 DCHECK_EQ(GetBlock(), other->GetBlock());
4733 DCHECK_EQ(GetArray(), other->GetArray());
4734 DCHECK_EQ(GetIndex(), other->GetIndex());
4735 if (Primitive::IsIntOrLongType(GetType())) {
4736 DCHECK(Primitive::IsFloatingPointType(other->GetType()));
4737 } else {
4738 DCHECK(Primitive::IsFloatingPointType(GetType()));
4739 DCHECK(Primitive::IsIntOrLongType(other->GetType()));
4740 }
4741 }
4742 return result;
4743 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004744
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004745 HInstruction* GetArray() const { return InputAt(0); }
4746 HInstruction* GetIndex() const { return InputAt(1); }
4747
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004748 DECLARE_INSTRUCTION(ArrayGet);
4749
4750 private:
4751 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4752};
4753
4754class HArraySet : public HTemplateInstruction<3> {
4755 public:
4756 HArraySet(HInstruction* array,
4757 HInstruction* index,
4758 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004759 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004760 uint32_t dex_pc,
4761 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004762 : HTemplateInstruction(
4763 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004764 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4765 additional_side_effects),
4766 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004767 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004768 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004769 value_can_be_null_(true),
4770 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004771 SetRawInputAt(0, array);
4772 SetRawInputAt(1, index);
4773 SetRawInputAt(2, value);
4774 }
4775
Calin Juravle77520bc2015-01-12 18:45:46 +00004776 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004777 // We currently always call a runtime method to catch array store
4778 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004779 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004780 }
4781
Mingyao Yang81014cb2015-06-02 03:16:27 -07004782 // Can throw ArrayStoreException.
4783 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4784
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004785 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004786 // TODO: Same as for ArrayGet.
4787 return false;
4788 }
4789
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004790 void ClearNeedsTypeCheck() {
4791 needs_type_check_ = false;
4792 }
4793
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004794 void ClearValueCanBeNull() {
4795 value_can_be_null_ = false;
4796 }
4797
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004798 void SetStaticTypeOfArrayIsObjectArray() {
4799 static_type_of_array_is_object_array_ = true;
4800 }
4801
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004802 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004803 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004804 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004805
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004806 HInstruction* GetArray() const { return InputAt(0); }
4807 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004808 HInstruction* GetValue() const { return InputAt(2); }
4809
4810 Primitive::Type GetComponentType() const {
4811 // The Dex format does not type floating point index operations. Since the
4812 // `expected_component_type_` is set during building and can therefore not
4813 // be correct, we also check what is the value type. If it is a floating
4814 // point type, we must use that type.
4815 Primitive::Type value_type = GetValue()->GetType();
4816 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4817 ? value_type
4818 : expected_component_type_;
4819 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004820
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004821 Primitive::Type GetRawExpectedComponentType() const {
4822 return expected_component_type_;
4823 }
4824
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004825 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4826 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4827 }
4828
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004829 DECLARE_INSTRUCTION(ArraySet);
4830
4831 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004832 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004833 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004834 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004835 // Cached information for the reference_type_info_ so that codegen
4836 // does not need to inspect the static type.
4837 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004838
4839 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4840};
4841
4842class HArrayLength : public HExpression<1> {
4843 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004844 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004845 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004846 // Note that arrays do not change length, so the instruction does not
4847 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004848 SetRawInputAt(0, array);
4849 }
4850
Calin Juravle77520bc2015-01-12 18:45:46 +00004851 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004852 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004853 return true;
4854 }
Calin Juravle641547a2015-04-21 22:08:51 +01004855 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4856 return obj == InputAt(0);
4857 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004858
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004859 DECLARE_INSTRUCTION(ArrayLength);
4860
4861 private:
4862 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4863};
4864
4865class HBoundsCheck : public HExpression<2> {
4866 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004867 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
4868 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004869 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004870 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004871 DCHECK(index->GetType() == Primitive::kPrimInt);
4872 SetRawInputAt(0, index);
4873 SetRawInputAt(1, length);
4874 }
4875
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004876 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004877 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004878 return true;
4879 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004880
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004881 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004882
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004883 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004884
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004885 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004886
4887 DECLARE_INSTRUCTION(BoundsCheck);
4888
4889 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004890 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4891};
4892
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004893/**
4894 * Some DEX instructions are folded into multiple HInstructions that need
4895 * to stay live until the last HInstruction. This class
4896 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004897 * HInstruction stays live. `index` represents the stack location index of the
4898 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004899 */
4900class HTemporary : public HTemplateInstruction<0> {
4901 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004902 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4903 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004904
4905 size_t GetIndex() const { return index_; }
4906
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004907 Primitive::Type GetType() const OVERRIDE {
4908 // The previous instruction is the one that will be stored in the temporary location.
4909 DCHECK(GetPrevious() != nullptr);
4910 return GetPrevious()->GetType();
4911 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004912
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004913 DECLARE_INSTRUCTION(Temporary);
4914
4915 private:
4916 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004917 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4918};
4919
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004920class HSuspendCheck : public HTemplateInstruction<0> {
4921 public:
4922 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004923 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004924
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004925 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004926 return true;
4927 }
4928
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004929 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4930 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004931
4932 DECLARE_INSTRUCTION(SuspendCheck);
4933
4934 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004935 // Only used for code generation, in order to share the same slow path between back edges
4936 // of a same loop.
4937 SlowPathCode* slow_path_;
4938
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004939 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4940};
4941
David Srbecky0cf44932015-12-09 14:09:59 +00004942// Pseudo-instruction which provides the native debugger with mapping information.
4943// It ensures that we can generate line number and local variables at this point.
4944class HNativeDebugInfo : public HTemplateInstruction<0> {
4945 public:
4946 explicit HNativeDebugInfo(uint32_t dex_pc)
4947 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
4948
4949 bool NeedsEnvironment() const OVERRIDE {
4950 return true;
4951 }
4952
4953 DECLARE_INSTRUCTION(NativeDebugInfo);
4954
4955 private:
4956 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
4957};
4958
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004959/**
4960 * Instruction to load a Class object.
4961 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004962class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004963 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004964 HLoadClass(HCurrentMethod* current_method,
4965 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004966 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004967 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004968 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004969 bool needs_access_check,
4970 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004971 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004972 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004973 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004974 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004975 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004976 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004977 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00004978 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004979 // Referrers class should not need access check. We never inline unverified
4980 // methods so we can't possibly end up in this situation.
4981 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004982 SetRawInputAt(0, current_method);
4983 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004984
4985 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004986
4987 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004988 // Note that we don't need to test for generate_clinit_check_.
4989 // Whether or not we need to generate the clinit check is processed in
4990 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004991 return other->AsLoadClass()->type_index_ == type_index_ &&
4992 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004993 }
4994
4995 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
4996
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004997 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004998 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01004999 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005000
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005001 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005002 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005003 }
5004
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005005 bool MustGenerateClinitCheck() const {
5006 return generate_clinit_check_;
5007 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005008
Calin Juravle0ba218d2015-05-19 18:46:01 +01005009 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005010 // The entrypoint the code generator is going to call does not do
5011 // clinit of the class.
5012 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01005013 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005014 }
5015
5016 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005017 return MustGenerateClinitCheck() ||
5018 (!is_referrers_class_ && !is_in_dex_cache_) ||
5019 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01005020 }
5021
5022 bool NeedsAccessCheck() const {
5023 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005024 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005025
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005026 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005027 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005028 }
5029
Calin Juravleacf735c2015-02-12 15:25:22 +00005030 ReferenceTypeInfo GetLoadedClassRTI() {
5031 return loaded_class_rti_;
5032 }
5033
5034 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5035 // Make sure we only set exact types (the loaded class should never be merged).
5036 DCHECK(rti.IsExact());
5037 loaded_class_rti_ = rti;
5038 }
5039
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005040 const DexFile& GetDexFile() { return dex_file_; }
5041
Vladimir Markodc151b22015-10-15 18:02:30 +01005042 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005043
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005044 static SideEffects SideEffectsForArchRuntimeCalls() {
5045 return SideEffects::CanTriggerGC();
5046 }
5047
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005048 bool IsInDexCache() const { return is_in_dex_cache_; }
5049
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005050 DECLARE_INSTRUCTION(LoadClass);
5051
5052 private:
5053 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005054 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005055 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005056 // Whether this instruction must generate the initialization check.
5057 // Used for code generation.
5058 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005059 const bool needs_access_check_;
5060 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005061
Calin Juravleacf735c2015-02-12 15:25:22 +00005062 ReferenceTypeInfo loaded_class_rti_;
5063
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005064 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5065};
5066
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005067class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005068 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005069 HLoadString(HCurrentMethod* current_method,
5070 uint32_t string_index,
5071 uint32_t dex_pc,
5072 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005073 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005074 string_index_(string_index),
5075 is_in_dex_cache_(is_in_dex_cache) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005076 SetRawInputAt(0, current_method);
5077 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005078
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005079 bool CanBeMoved() const OVERRIDE { return true; }
5080
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005081 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5082 return other->AsLoadString()->string_index_ == string_index_;
5083 }
5084
5085 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5086
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005087 uint32_t GetStringIndex() const { return string_index_; }
5088
5089 // TODO: Can we deopt or debug when we resolve a string?
5090 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01005091 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005092 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005093 bool IsInDexCache() const { return is_in_dex_cache_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005094
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005095 static SideEffects SideEffectsForArchRuntimeCalls() {
5096 return SideEffects::CanTriggerGC();
5097 }
5098
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005099 DECLARE_INSTRUCTION(LoadString);
5100
5101 private:
5102 const uint32_t string_index_;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005103 const bool is_in_dex_cache_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005104
5105 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5106};
5107
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005108/**
5109 * Performs an initialization check on its Class object input.
5110 */
5111class HClinitCheck : public HExpression<1> {
5112 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005113 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005114 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005115 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005116 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5117 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005118 SetRawInputAt(0, constant);
5119 }
5120
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005121 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005122 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005123 return true;
5124 }
5125
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005126 bool NeedsEnvironment() const OVERRIDE {
5127 // May call runtime to initialize the class.
5128 return true;
5129 }
5130
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005131 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005132
5133 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5134
5135 DECLARE_INSTRUCTION(ClinitCheck);
5136
5137 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005138 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5139};
5140
5141class HStaticFieldGet : public HExpression<1> {
5142 public:
5143 HStaticFieldGet(HInstruction* cls,
5144 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005145 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005146 bool is_volatile,
5147 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005148 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005149 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005150 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005151 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005152 : HExpression(field_type,
5153 SideEffects::FieldReadOfType(field_type, is_volatile),
5154 dex_pc),
5155 field_info_(field_offset,
5156 field_type,
5157 is_volatile,
5158 field_idx,
5159 declaring_class_def_index,
5160 dex_file,
5161 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005162 SetRawInputAt(0, cls);
5163 }
5164
Calin Juravle52c48962014-12-16 17:02:57 +00005165
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005166 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005167
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005168 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005169 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5170 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005171 }
5172
5173 size_t ComputeHashCode() const OVERRIDE {
5174 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5175 }
5176
Calin Juravle52c48962014-12-16 17:02:57 +00005177 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005178 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5179 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005180 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005181
5182 DECLARE_INSTRUCTION(StaticFieldGet);
5183
5184 private:
5185 const FieldInfo field_info_;
5186
5187 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5188};
5189
5190class HStaticFieldSet : public HTemplateInstruction<2> {
5191 public:
5192 HStaticFieldSet(HInstruction* cls,
5193 HInstruction* value,
5194 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005195 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005196 bool is_volatile,
5197 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005198 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005199 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005200 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005201 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005202 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5203 dex_pc),
5204 field_info_(field_offset,
5205 field_type,
5206 is_volatile,
5207 field_idx,
5208 declaring_class_def_index,
5209 dex_file,
5210 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005211 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005212 SetRawInputAt(0, cls);
5213 SetRawInputAt(1, value);
5214 }
5215
Calin Juravle52c48962014-12-16 17:02:57 +00005216 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005217 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5218 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005219 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005220
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005221 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005222 bool GetValueCanBeNull() const { return value_can_be_null_; }
5223 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005224
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005225 DECLARE_INSTRUCTION(StaticFieldSet);
5226
5227 private:
5228 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005229 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005230
5231 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5232};
5233
Calin Juravlee460d1d2015-09-29 04:52:17 +01005234class HUnresolvedInstanceFieldGet : public HExpression<1> {
5235 public:
5236 HUnresolvedInstanceFieldGet(HInstruction* obj,
5237 Primitive::Type field_type,
5238 uint32_t field_index,
5239 uint32_t dex_pc)
5240 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5241 field_index_(field_index) {
5242 SetRawInputAt(0, obj);
5243 }
5244
5245 bool NeedsEnvironment() const OVERRIDE { return true; }
5246 bool CanThrow() const OVERRIDE { return true; }
5247
5248 Primitive::Type GetFieldType() const { return GetType(); }
5249 uint32_t GetFieldIndex() const { return field_index_; }
5250
5251 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5252
5253 private:
5254 const uint32_t field_index_;
5255
5256 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5257};
5258
5259class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5260 public:
5261 HUnresolvedInstanceFieldSet(HInstruction* obj,
5262 HInstruction* value,
5263 Primitive::Type field_type,
5264 uint32_t field_index,
5265 uint32_t dex_pc)
5266 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5267 field_type_(field_type),
5268 field_index_(field_index) {
5269 DCHECK_EQ(field_type, value->GetType());
5270 SetRawInputAt(0, obj);
5271 SetRawInputAt(1, value);
5272 }
5273
5274 bool NeedsEnvironment() const OVERRIDE { return true; }
5275 bool CanThrow() const OVERRIDE { return true; }
5276
5277 Primitive::Type GetFieldType() const { return field_type_; }
5278 uint32_t GetFieldIndex() const { return field_index_; }
5279
5280 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5281
5282 private:
5283 const Primitive::Type field_type_;
5284 const uint32_t field_index_;
5285
5286 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5287};
5288
5289class HUnresolvedStaticFieldGet : public HExpression<0> {
5290 public:
5291 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5292 uint32_t field_index,
5293 uint32_t dex_pc)
5294 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5295 field_index_(field_index) {
5296 }
5297
5298 bool NeedsEnvironment() const OVERRIDE { return true; }
5299 bool CanThrow() const OVERRIDE { return true; }
5300
5301 Primitive::Type GetFieldType() const { return GetType(); }
5302 uint32_t GetFieldIndex() const { return field_index_; }
5303
5304 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5305
5306 private:
5307 const uint32_t field_index_;
5308
5309 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5310};
5311
5312class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5313 public:
5314 HUnresolvedStaticFieldSet(HInstruction* value,
5315 Primitive::Type field_type,
5316 uint32_t field_index,
5317 uint32_t dex_pc)
5318 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5319 field_type_(field_type),
5320 field_index_(field_index) {
5321 DCHECK_EQ(field_type, value->GetType());
5322 SetRawInputAt(0, value);
5323 }
5324
5325 bool NeedsEnvironment() const OVERRIDE { return true; }
5326 bool CanThrow() const OVERRIDE { return true; }
5327
5328 Primitive::Type GetFieldType() const { return field_type_; }
5329 uint32_t GetFieldIndex() const { return field_index_; }
5330
5331 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5332
5333 private:
5334 const Primitive::Type field_type_;
5335 const uint32_t field_index_;
5336
5337 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5338};
5339
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005340// Implement the move-exception DEX instruction.
5341class HLoadException : public HExpression<0> {
5342 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005343 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5344 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005345
David Brazdilbbd733e2015-08-18 17:48:17 +01005346 bool CanBeNull() const OVERRIDE { return false; }
5347
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005348 DECLARE_INSTRUCTION(LoadException);
5349
5350 private:
5351 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5352};
5353
David Brazdilcb1c0552015-08-04 16:22:25 +01005354// Implicit part of move-exception which clears thread-local exception storage.
5355// Must not be removed because the runtime expects the TLS to get cleared.
5356class HClearException : public HTemplateInstruction<0> {
5357 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005358 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5359 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005360
5361 DECLARE_INSTRUCTION(ClearException);
5362
5363 private:
5364 DISALLOW_COPY_AND_ASSIGN(HClearException);
5365};
5366
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005367class HThrow : public HTemplateInstruction<1> {
5368 public:
5369 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005370 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005371 SetRawInputAt(0, exception);
5372 }
5373
5374 bool IsControlFlow() const OVERRIDE { return true; }
5375
5376 bool NeedsEnvironment() const OVERRIDE { return true; }
5377
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005378 bool CanThrow() const OVERRIDE { return true; }
5379
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005380
5381 DECLARE_INSTRUCTION(Throw);
5382
5383 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005384 DISALLOW_COPY_AND_ASSIGN(HThrow);
5385};
5386
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005387/**
5388 * Implementation strategies for the code generator of a HInstanceOf
5389 * or `HCheckCast`.
5390 */
5391enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005392 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005393 kExactCheck, // Can do a single class compare.
5394 kClassHierarchyCheck, // Can just walk the super class chain.
5395 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5396 kInterfaceCheck, // No optimization yet when checking against an interface.
5397 kArrayObjectCheck, // Can just check if the array is not primitive.
5398 kArrayCheck // No optimization yet when checking against a generic array.
5399};
5400
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005401class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005402 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005403 HInstanceOf(HInstruction* object,
5404 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005405 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005406 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005407 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005408 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005409 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005410 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005411 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005412 SetRawInputAt(0, object);
5413 SetRawInputAt(1, constant);
5414 }
5415
5416 bool CanBeMoved() const OVERRIDE { return true; }
5417
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005418 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005419 return true;
5420 }
5421
5422 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005423 return false;
5424 }
5425
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005426 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5427
5428 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005429
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005430 // Used only in code generation.
5431 bool MustDoNullCheck() const { return must_do_null_check_; }
5432 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5433
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005434 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5435 return (check_kind == TypeCheckKind::kExactCheck)
5436 ? SideEffects::None()
5437 // Mips currently does runtime calls for any other checks.
5438 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005439 }
5440
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005441 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005442
5443 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005444 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005445 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005446
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005447 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5448};
5449
Calin Juravleb1498f62015-02-16 13:13:29 +00005450class HBoundType : public HExpression<1> {
5451 public:
David Brazdilf5552582015-12-27 13:36:12 +00005452 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005453 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
David Brazdilf5552582015-12-27 13:36:12 +00005454 upper_bound_(ReferenceTypeInfo::CreateInvalid()),
5455 upper_can_be_null_(true),
5456 can_be_null_(true) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005457 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005458 SetRawInputAt(0, input);
5459 }
5460
David Brazdilf5552582015-12-27 13:36:12 +00005461 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005462 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5463 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
David Brazdilf5552582015-12-27 13:36:12 +00005464 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005465
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005466 void SetCanBeNull(bool can_be_null) {
5467 DCHECK(upper_can_be_null_ || !can_be_null);
5468 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005469 }
5470
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005471 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5472
Calin Juravleb1498f62015-02-16 13:13:29 +00005473 DECLARE_INSTRUCTION(BoundType);
5474
5475 private:
5476 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005477 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5478 // It is used to bound the type in cases like:
5479 // if (x instanceof ClassX) {
5480 // // uper_bound_ will be ClassX
5481 // }
David Brazdilf5552582015-12-27 13:36:12 +00005482 ReferenceTypeInfo upper_bound_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005483 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5484 // is false then can_be_null_ cannot be true).
David Brazdilf5552582015-12-27 13:36:12 +00005485 bool upper_can_be_null_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005486 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005487
5488 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5489};
5490
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005491class HCheckCast : public HTemplateInstruction<2> {
5492 public:
5493 HCheckCast(HInstruction* object,
5494 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005495 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005496 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005497 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005498 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005499 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005500 SetRawInputAt(0, object);
5501 SetRawInputAt(1, constant);
5502 }
5503
5504 bool CanBeMoved() const OVERRIDE { return true; }
5505
5506 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5507 return true;
5508 }
5509
5510 bool NeedsEnvironment() const OVERRIDE {
5511 // Instruction may throw a CheckCastError.
5512 return true;
5513 }
5514
5515 bool CanThrow() const OVERRIDE { return true; }
5516
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005517 bool MustDoNullCheck() const { return must_do_null_check_; }
5518 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005519 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005520
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005521 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005522
5523 DECLARE_INSTRUCTION(CheckCast);
5524
5525 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005526 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005527 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005528
5529 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005530};
5531
Calin Juravle27df7582015-04-17 19:12:31 +01005532class HMemoryBarrier : public HTemplateInstruction<0> {
5533 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005534 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005535 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005536 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005537 barrier_kind_(barrier_kind) {}
5538
5539 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5540
5541 DECLARE_INSTRUCTION(MemoryBarrier);
5542
5543 private:
5544 const MemBarrierKind barrier_kind_;
5545
5546 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5547};
5548
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005549class HMonitorOperation : public HTemplateInstruction<1> {
5550 public:
5551 enum OperationKind {
5552 kEnter,
5553 kExit,
5554 };
5555
5556 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005557 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005558 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5559 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005560 SetRawInputAt(0, object);
5561 }
5562
5563 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005564 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5565
5566 bool CanThrow() const OVERRIDE {
5567 // Verifier guarantees that monitor-exit cannot throw.
5568 // This is important because it allows the HGraphBuilder to remove
5569 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5570 return IsEnter();
5571 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005572
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005573
5574 bool IsEnter() const { return kind_ == kEnter; }
5575
5576 DECLARE_INSTRUCTION(MonitorOperation);
5577
Calin Juravle52c48962014-12-16 17:02:57 +00005578 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005579 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005580
5581 private:
5582 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5583};
5584
Vladimir Markof9f64412015-09-02 14:05:49 +01005585class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005586 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005587 MoveOperands(Location source,
5588 Location destination,
5589 Primitive::Type type,
5590 HInstruction* instruction)
5591 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005592
5593 Location GetSource() const { return source_; }
5594 Location GetDestination() const { return destination_; }
5595
5596 void SetSource(Location value) { source_ = value; }
5597 void SetDestination(Location value) { destination_ = value; }
5598
5599 // The parallel move resolver marks moves as "in-progress" by clearing the
5600 // destination (but not the source).
5601 Location MarkPending() {
5602 DCHECK(!IsPending());
5603 Location dest = destination_;
5604 destination_ = Location::NoLocation();
5605 return dest;
5606 }
5607
5608 void ClearPending(Location dest) {
5609 DCHECK(IsPending());
5610 destination_ = dest;
5611 }
5612
5613 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00005614 DCHECK(source_.IsValid() || destination_.IsInvalid());
5615 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005616 }
5617
5618 // True if this blocks a move from the given location.
5619 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005620 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005621 }
5622
5623 // A move is redundant if it's been eliminated, if its source and
5624 // destination are the same, or if its destination is unneeded.
5625 bool IsRedundant() const {
5626 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5627 }
5628
5629 // We clear both operands to indicate move that's been eliminated.
5630 void Eliminate() {
5631 source_ = destination_ = Location::NoLocation();
5632 }
5633
5634 bool IsEliminated() const {
5635 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5636 return source_.IsInvalid();
5637 }
5638
Alexey Frunze4dda3372015-06-01 18:31:49 -07005639 Primitive::Type GetType() const { return type_; }
5640
Nicolas Geoffray90218252015-04-15 11:56:51 +01005641 bool Is64BitMove() const {
5642 return Primitive::Is64BitType(type_);
5643 }
5644
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005645 HInstruction* GetInstruction() const { return instruction_; }
5646
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005647 private:
5648 Location source_;
5649 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005650 // The type this move is for.
5651 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005652 // The instruction this move is assocatied with. Null when this move is
5653 // for moving an input in the expected locations of user (including a phi user).
5654 // This is only used in debug mode, to ensure we do not connect interval siblings
5655 // in the same parallel move.
5656 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005657};
5658
Roland Levillainc9285912015-12-18 10:38:42 +00005659std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
5660
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005661static constexpr size_t kDefaultNumberOfMoves = 4;
5662
5663class HParallelMove : public HTemplateInstruction<0> {
5664 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005665 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005666 : HTemplateInstruction(SideEffects::None(), dex_pc),
5667 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5668 moves_.reserve(kDefaultNumberOfMoves);
5669 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005670
Nicolas Geoffray90218252015-04-15 11:56:51 +01005671 void AddMove(Location source,
5672 Location destination,
5673 Primitive::Type type,
5674 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005675 DCHECK(source.IsValid());
5676 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005677 if (kIsDebugBuild) {
5678 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005679 for (const MoveOperands& move : moves_) {
5680 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005681 // Special case the situation where the move is for the spill slot
5682 // of the instruction.
5683 if ((GetPrevious() == instruction)
5684 || ((GetPrevious() == nullptr)
5685 && instruction->IsPhi()
5686 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005687 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005688 << "Doing parallel moves for the same instruction.";
5689 } else {
5690 DCHECK(false) << "Doing parallel moves for the same instruction.";
5691 }
5692 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005693 }
5694 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005695 for (const MoveOperands& move : moves_) {
5696 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005697 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005698 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005699 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005700 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005701 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005702 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005703 }
5704
Vladimir Marko225b6462015-09-28 12:17:40 +01005705 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005706 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005707 }
5708
Vladimir Marko225b6462015-09-28 12:17:40 +01005709 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005710
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005711 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005712
5713 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005714 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005715
5716 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5717};
5718
Mark Mendell0616ae02015-04-17 12:49:27 -04005719} // namespace art
5720
Vladimir Markob4536b72015-11-24 13:45:23 +00005721#ifdef ART_ENABLE_CODEGEN_arm
5722#include "nodes_arm.h"
5723#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005724#ifdef ART_ENABLE_CODEGEN_arm64
5725#include "nodes_arm64.h"
5726#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005727#ifdef ART_ENABLE_CODEGEN_x86
5728#include "nodes_x86.h"
5729#endif
5730
5731namespace art {
5732
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005733class HGraphVisitor : public ValueObject {
5734 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005735 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5736 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005737
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005738 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005739 virtual void VisitBasicBlock(HBasicBlock* block);
5740
Roland Levillain633021e2014-10-01 14:12:25 +01005741 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005742 void VisitInsertionOrder();
5743
Roland Levillain633021e2014-10-01 14:12:25 +01005744 // Visit the graph following dominator tree reverse post-order.
5745 void VisitReversePostOrder();
5746
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005747 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005748
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005749 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005750#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005751 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5752
5753 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5754
5755#undef DECLARE_VISIT_INSTRUCTION
5756
5757 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005758 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005759
5760 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5761};
5762
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005763class HGraphDelegateVisitor : public HGraphVisitor {
5764 public:
5765 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5766 virtual ~HGraphDelegateVisitor() {}
5767
5768 // Visit functions that delegate to to super class.
5769#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005770 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005771
5772 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5773
5774#undef DECLARE_VISIT_INSTRUCTION
5775
5776 private:
5777 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5778};
5779
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005780class HInsertionOrderIterator : public ValueObject {
5781 public:
5782 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5783
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005784 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005785 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005786 void Advance() { ++index_; }
5787
5788 private:
5789 const HGraph& graph_;
5790 size_t index_;
5791
5792 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5793};
5794
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005795class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005796 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005797 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5798 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005799 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005800 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005801
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005802 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5803 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005804 void Advance() { ++index_; }
5805
5806 private:
5807 const HGraph& graph_;
5808 size_t index_;
5809
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005810 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005811};
5812
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005813class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005814 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005815 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005816 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005817 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005818 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005819 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005820
5821 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005822 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005823 void Advance() { --index_; }
5824
5825 private:
5826 const HGraph& graph_;
5827 size_t index_;
5828
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005829 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005830};
5831
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005832class HLinearPostOrderIterator : public ValueObject {
5833 public:
5834 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005835 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005836
5837 bool Done() const { return index_ == 0; }
5838
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005839 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005840
5841 void Advance() {
5842 --index_;
5843 DCHECK_GE(index_, 0U);
5844 }
5845
5846 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005847 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005848 size_t index_;
5849
5850 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5851};
5852
5853class HLinearOrderIterator : public ValueObject {
5854 public:
5855 explicit HLinearOrderIterator(const HGraph& graph)
5856 : order_(graph.GetLinearOrder()), index_(0) {}
5857
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005858 bool Done() const { return index_ == order_.size(); }
5859 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005860 void Advance() { ++index_; }
5861
5862 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005863 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005864 size_t index_;
5865
5866 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5867};
5868
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005869// Iterator over the blocks that art part of the loop. Includes blocks part
5870// of an inner loop. The order in which the blocks are iterated is on their
5871// block id.
5872class HBlocksInLoopIterator : public ValueObject {
5873 public:
5874 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5875 : blocks_in_loop_(info.GetBlocks()),
5876 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5877 index_(0) {
5878 if (!blocks_in_loop_.IsBitSet(index_)) {
5879 Advance();
5880 }
5881 }
5882
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005883 bool Done() const { return index_ == blocks_.size(); }
5884 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005885 void Advance() {
5886 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005887 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005888 if (blocks_in_loop_.IsBitSet(index_)) {
5889 break;
5890 }
5891 }
5892 }
5893
5894 private:
5895 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005896 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005897 size_t index_;
5898
5899 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5900};
5901
Mingyao Yang3584bce2015-05-19 16:01:59 -07005902// Iterator over the blocks that art part of the loop. Includes blocks part
5903// of an inner loop. The order in which the blocks are iterated is reverse
5904// post order.
5905class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5906 public:
5907 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5908 : blocks_in_loop_(info.GetBlocks()),
5909 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5910 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005911 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005912 Advance();
5913 }
5914 }
5915
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005916 bool Done() const { return index_ == blocks_.size(); }
5917 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005918 void Advance() {
5919 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005920 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5921 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005922 break;
5923 }
5924 }
5925 }
5926
5927 private:
5928 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005929 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005930 size_t index_;
5931
5932 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5933};
5934
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005935inline int64_t Int64FromConstant(HConstant* constant) {
5936 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5937 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5938 : constant->AsLongConstant()->GetValue();
5939}
5940
Vladimir Marko58155012015-08-19 12:49:41 +00005941inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5942 // For the purposes of the compiler, the dex files must actually be the same object
5943 // if we want to safely treat them as the same. This is especially important for JIT
5944 // as custom class loaders can open the same underlying file (or memory) multiple
5945 // times and provide different class resolution but no two class loaders should ever
5946 // use the same DexFile object - doing so is an unsupported hack that can lead to
5947 // all sorts of weird failures.
5948 return &lhs == &rhs;
5949}
5950
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005951#define INSTRUCTION_TYPE_CHECK(type, super) \
5952 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
5953 inline const H##type* HInstruction::As##type() const { \
5954 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
5955 } \
5956 inline H##type* HInstruction::As##type() { \
5957 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
5958 }
5959
5960 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
5961#undef INSTRUCTION_TYPE_CHECK
5962
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005963} // namespace art
5964
5965#endif // ART_COMPILER_OPTIMIZING_NODES_H_