blob: 859d570b29735fe316ddc9b50e06f475932f7a8f [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
863 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000864 successors_.push_back(block);
865 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000866 }
867
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100868 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
869 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100870 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000871 new_block->predecessors_.push_back(this);
872 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000873 }
874
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000875 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
876 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000877 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000878 new_block->successors_.push_back(this);
879 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000880 }
881
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100882 // Insert `this` between `predecessor` and `successor. This method
883 // preserves the indicies, and will update the first edge found between
884 // `predecessor` and `successor`.
885 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
886 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100887 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000888 successor->predecessors_[predecessor_index] = this;
889 predecessor->successors_[successor_index] = this;
890 successors_.push_back(successor);
891 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100892 }
893
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100894 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000895 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100896 }
897
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000898 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000899 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000900 }
901
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100902 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000903 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100904 }
905
906 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000907 predecessors_.push_back(block);
908 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100909 }
910
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100911 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000912 DCHECK_EQ(predecessors_.size(), 2u);
913 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100914 }
915
David Brazdil769c9e52015-04-27 13:54:09 +0100916 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000917 DCHECK_EQ(successors_.size(), 2u);
918 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100919 }
920
David Brazdilfc6a86a2015-06-26 10:33:45 +0000921 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000922 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100923 }
924
David Brazdilfc6a86a2015-06-26 10:33:45 +0000925 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000926 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100927 }
928
David Brazdilfc6a86a2015-06-26 10:33:45 +0000929 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000930 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100931 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000932 }
933
934 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000935 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100936 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000937 }
938
939 // Returns whether the first occurrence of `predecessor` in the list of
940 // predecessors is at index `idx`.
941 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100942 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000943 return GetPredecessorIndexOf(predecessor) == idx;
944 }
945
David Brazdild7558da2015-09-22 13:04:14 +0100946 // Create a new block between this block and its predecessors. The new block
947 // is added to the graph, all predecessor edges are relinked to it and an edge
948 // is created to `this`. Returns the new empty block. Reverse post order or
949 // loop and try/catch information are not updated.
950 HBasicBlock* CreateImmediateDominator();
951
David Brazdilfc6a86a2015-06-26 10:33:45 +0000952 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100953 // created, latter block. Note that this method will add the block to the
954 // graph, create a Goto at the end of the former block and will create an edge
955 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100956 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000957 HBasicBlock* SplitBefore(HInstruction* cursor);
958
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000959 // Split the block into two blocks just after `cursor`. Returns the newly
960 // created block. Note that this method just updates raw block information,
961 // like predecessors, successors, dominators, and instruction list. It does not
962 // update the graph, reverse post order, loop information, nor make sure the
963 // blocks are consistent (for example ending with a control flow instruction).
964 HBasicBlock* SplitAfter(HInstruction* cursor);
965
David Brazdil9bc43612015-11-05 21:25:24 +0000966 // Split catch block into two blocks after the original move-exception bytecode
967 // instruction, or at the beginning if not present. Returns the newly created,
968 // latter block, or nullptr if such block could not be created (must be dead
969 // in that case). Note that this method just updates raw block information,
970 // like predecessors, successors, dominators, and instruction list. It does not
971 // update the graph, reverse post order, loop information, nor make sure the
972 // blocks are consistent (for example ending with a control flow instruction).
973 HBasicBlock* SplitCatchBlockAfterMoveException();
974
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000975 // Merge `other` at the end of `this`. Successors and dominated blocks of
976 // `other` are changed to be successors and dominated blocks of `this`. Note
977 // that this method does not update the graph, reverse post order, loop
978 // information, nor make sure the blocks are consistent (for example ending
979 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100980 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000981
982 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
983 // of `this` are moved to `other`.
984 // Note that this method does not update the graph, reverse post order, loop
985 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000986 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000987 void ReplaceWith(HBasicBlock* other);
988
David Brazdil2d7352b2015-04-20 14:52:42 +0100989 // Merge `other` at the end of `this`. This method updates loops, reverse post
990 // order, links to predecessors, successors, dominators and deletes the block
991 // from the graph. The two blocks must be successive, i.e. `this` the only
992 // predecessor of `other` and vice versa.
993 void MergeWith(HBasicBlock* other);
994
995 // Disconnects `this` from all its predecessors, successors and dominator,
996 // removes it from all loops it is included in and eventually from the graph.
997 // The block must not dominate any other block. Predecessors and successors
998 // are safely updated.
999 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001000
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001001 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001002 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001003 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001004 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001005 // Replace instruction `initial` with `replacement` within this block.
1006 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1007 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001008 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001009 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001010 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1011 // instruction list. With 'ensure_safety' set to true, it verifies that the
1012 // instruction is not in use and removes it from the use lists of its inputs.
1013 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1014 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001015 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001016
1017 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001018 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001019 }
1020
Roland Levillain6b879dd2014-09-22 17:13:44 +01001021 bool IsLoopPreHeaderFirstPredecessor() const {
1022 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001023 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001024 }
1025
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001026 bool IsFirstPredecessorBackEdge() const {
1027 DCHECK(IsLoopHeader());
1028 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1029 }
1030
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001031 HLoopInformation* GetLoopInformation() const {
1032 return loop_information_;
1033 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001034
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001035 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001036 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001037 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001038 void SetInLoop(HLoopInformation* info) {
1039 if (IsLoopHeader()) {
1040 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001041 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001042 loop_information_ = info;
1043 } else if (loop_information_->Contains(*info->GetHeader())) {
1044 // Block is currently part of an outer loop. Make it part of this inner loop.
1045 // Note that a non loop header having a loop information means this loop information
1046 // has already been populated
1047 loop_information_ = info;
1048 } else {
1049 // Block is part of an inner loop. Do not update the loop information.
1050 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1051 // at this point, because this method is being called while populating `info`.
1052 }
1053 }
1054
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001055 // Raw update of the loop information.
1056 void SetLoopInformation(HLoopInformation* info) {
1057 loop_information_ = info;
1058 }
1059
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001060 bool IsInLoop() const { return loop_information_ != nullptr; }
1061
David Brazdilec16f792015-08-19 15:04:01 +01001062 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1063
1064 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1065 try_catch_information_ = try_catch_information;
1066 }
1067
1068 bool IsTryBlock() const {
1069 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1070 }
1071
1072 bool IsCatchBlock() const {
1073 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1074 }
David Brazdilffee3d32015-07-06 11:48:53 +01001075
1076 // Returns the try entry that this block's successors should have. They will
1077 // be in the same try, unless the block ends in a try boundary. In that case,
1078 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001079 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001080
David Brazdild7558da2015-09-22 13:04:14 +01001081 bool HasThrowingInstructions() const;
1082
David Brazdila4b8c212015-05-07 09:59:30 +01001083 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001084 bool Dominates(HBasicBlock* block) const;
1085
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001086 size_t GetLifetimeStart() const { return lifetime_start_; }
1087 size_t GetLifetimeEnd() const { return lifetime_end_; }
1088
1089 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1090 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1091
David Brazdil8d5b8b22015-03-24 10:51:52 +00001092 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001093 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001094 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001095 bool HasSinglePhi() const;
1096
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001097 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001098 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001099 ArenaVector<HBasicBlock*> predecessors_;
1100 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001101 HInstructionList instructions_;
1102 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001103 HLoopInformation* loop_information_;
1104 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001105 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001106 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001107 // The dex program counter of the first instruction of this block.
1108 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001109 size_t lifetime_start_;
1110 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001111 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001112
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001113 friend class HGraph;
1114 friend class HInstruction;
1115
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001116 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1117};
1118
David Brazdilb2bd1c52015-03-25 11:17:37 +00001119// Iterates over the LoopInformation of all loops which contain 'block'
1120// from the innermost to the outermost.
1121class HLoopInformationOutwardIterator : public ValueObject {
1122 public:
1123 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1124 : current_(block.GetLoopInformation()) {}
1125
1126 bool Done() const { return current_ == nullptr; }
1127
1128 void Advance() {
1129 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001130 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001131 }
1132
1133 HLoopInformation* Current() const {
1134 DCHECK(!Done());
1135 return current_;
1136 }
1137
1138 private:
1139 HLoopInformation* current_;
1140
1141 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1142};
1143
Alexandre Ramesef20f712015-06-09 10:29:30 +01001144#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001145 M(Above, Condition) \
1146 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001147 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001148 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001149 M(ArrayGet, Instruction) \
1150 M(ArrayLength, Instruction) \
1151 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001152 M(Below, Condition) \
1153 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001154 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001155 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001156 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001157 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001158 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001159 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001160 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001161 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001162 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001163 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001164 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001165 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001166 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001167 M(Exit, Instruction) \
1168 M(FloatConstant, Constant) \
1169 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001170 M(GreaterThan, Condition) \
1171 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001172 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001173 M(InstanceFieldGet, Instruction) \
1174 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001175 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001176 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001177 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001178 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001179 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001180 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001181 M(LessThan, Condition) \
1182 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001183 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001184 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001185 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001186 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001187 M(Local, Instruction) \
1188 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001189 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001190 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001191 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001192 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001193 M(Neg, UnaryOperation) \
1194 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001195 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001196 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001197 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001198 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001199 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001200 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001201 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001202 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001203 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001204 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001205 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001206 M(Return, Instruction) \
1207 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001208 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001209 M(Shl, BinaryOperation) \
1210 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001211 M(StaticFieldGet, Instruction) \
1212 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001213 M(UnresolvedInstanceFieldGet, Instruction) \
1214 M(UnresolvedInstanceFieldSet, Instruction) \
1215 M(UnresolvedStaticFieldGet, Instruction) \
1216 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001217 M(StoreLocal, Instruction) \
1218 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001219 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001220 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001221 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001222 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001223 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001224 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001225 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001226
Vladimir Markob4536b72015-11-24 13:45:23 +00001227#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001228#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001229#else
1230#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1231 M(ArmDexCacheArraysBase, Instruction)
1232#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001233
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001234#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001235#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001236#else
1237#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001238 M(Arm64DataProcWithShifterOp, Instruction) \
Alexandre Rames418318f2015-11-20 15:55:47 +00001239 M(Arm64IntermediateAddress, Instruction) \
1240 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001241#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001242
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001243#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1244
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001245#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1246
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001247#ifndef ART_ENABLE_CODEGEN_x86
1248#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1249#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001250#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1251 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001252 M(X86LoadFromConstantTable, Instruction) \
1253 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001254#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001255
1256#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1257
1258#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1259 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1260 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1261 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001262 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001263 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001264 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1265 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1266
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001267#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1268 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001269 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001270 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001271 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001272 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001273
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001274#define FOR_EACH_INSTRUCTION(M) \
1275 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1276 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1277
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001278#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001279FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1280#undef FORWARD_DECLARATION
1281
Roland Levillainccc07a92014-09-16 14:48:16 +01001282#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001283 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001284 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001285 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001286 return other->Is##type(); \
1287 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001288 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001289
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001290#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1291 bool Is##type() const { return As##type() != nullptr; } \
1292 const H##type* As##type() const { return this; } \
1293 H##type* As##type() { return this; }
1294
David Brazdiled596192015-01-23 10:39:45 +00001295template <typename T> class HUseList;
1296
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001297template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001298class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001299 public:
David Brazdiled596192015-01-23 10:39:45 +00001300 HUseListNode* GetPrevious() const { return prev_; }
1301 HUseListNode* GetNext() const { return next_; }
1302 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001303 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001304 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001305
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001306 private:
David Brazdiled596192015-01-23 10:39:45 +00001307 HUseListNode(T user, size_t index)
1308 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1309
1310 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001311 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001312 HUseListNode<T>* prev_;
1313 HUseListNode<T>* next_;
1314
1315 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001316
1317 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1318};
1319
David Brazdiled596192015-01-23 10:39:45 +00001320template <typename T>
1321class HUseList : public ValueObject {
1322 public:
1323 HUseList() : first_(nullptr) {}
1324
1325 void Clear() {
1326 first_ = nullptr;
1327 }
1328
1329 // Adds a new entry at the beginning of the use list and returns
1330 // the newly created node.
1331 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001332 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001333 if (IsEmpty()) {
1334 first_ = new_node;
1335 } else {
1336 first_->prev_ = new_node;
1337 new_node->next_ = first_;
1338 first_ = new_node;
1339 }
1340 return new_node;
1341 }
1342
1343 HUseListNode<T>* GetFirst() const {
1344 return first_;
1345 }
1346
1347 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001348 DCHECK(node != nullptr);
1349 DCHECK(Contains(node));
1350
David Brazdiled596192015-01-23 10:39:45 +00001351 if (node->prev_ != nullptr) {
1352 node->prev_->next_ = node->next_;
1353 }
1354 if (node->next_ != nullptr) {
1355 node->next_->prev_ = node->prev_;
1356 }
1357 if (node == first_) {
1358 first_ = node->next_;
1359 }
1360 }
1361
David Brazdil1abb4192015-02-17 18:33:36 +00001362 bool Contains(const HUseListNode<T>* node) const {
1363 if (node == nullptr) {
1364 return false;
1365 }
1366 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1367 if (current == node) {
1368 return true;
1369 }
1370 }
1371 return false;
1372 }
1373
David Brazdiled596192015-01-23 10:39:45 +00001374 bool IsEmpty() const {
1375 return first_ == nullptr;
1376 }
1377
1378 bool HasOnlyOneUse() const {
1379 return first_ != nullptr && first_->next_ == nullptr;
1380 }
1381
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001382 size_t SizeSlow() const {
1383 size_t count = 0;
1384 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1385 ++count;
1386 }
1387 return count;
1388 }
1389
David Brazdiled596192015-01-23 10:39:45 +00001390 private:
1391 HUseListNode<T>* first_;
1392};
1393
1394template<typename T>
1395class HUseIterator : public ValueObject {
1396 public:
1397 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1398
1399 bool Done() const { return current_ == nullptr; }
1400
1401 void Advance() {
1402 DCHECK(!Done());
1403 current_ = current_->GetNext();
1404 }
1405
1406 HUseListNode<T>* Current() const {
1407 DCHECK(!Done());
1408 return current_;
1409 }
1410
1411 private:
1412 HUseListNode<T>* current_;
1413
1414 friend class HValue;
1415};
1416
David Brazdil1abb4192015-02-17 18:33:36 +00001417// This class is used by HEnvironment and HInstruction classes to record the
1418// instructions they use and pointers to the corresponding HUseListNodes kept
1419// by the used instructions.
1420template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001421class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001422 public:
1423 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1424 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1425
1426 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1427 : instruction_(old_record.instruction_), use_node_(use_node) {
1428 DCHECK(instruction_ != nullptr);
1429 DCHECK(use_node_ != nullptr);
1430 DCHECK(old_record.use_node_ == nullptr);
1431 }
1432
1433 HInstruction* GetInstruction() const { return instruction_; }
1434 HUseListNode<T>* GetUseNode() const { return use_node_; }
1435
1436 private:
1437 // Instruction used by the user.
1438 HInstruction* instruction_;
1439
1440 // Corresponding entry in the use list kept by 'instruction_'.
1441 HUseListNode<T>* use_node_;
1442};
1443
Aart Bik854a02b2015-07-14 16:07:00 -07001444/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001445 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001446 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001447 * For write/read dependences on fields/arrays, the dependence analysis uses
1448 * type disambiguation (e.g. a float field write cannot modify the value of an
1449 * integer field read) and the access type (e.g. a reference array write cannot
1450 * modify the value of a reference field read [although it may modify the
1451 * reference fetch prior to reading the field, which is represented by its own
1452 * write/read dependence]). The analysis makes conservative points-to
1453 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1454 * the same, and any reference read depends on any reference read without
1455 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001456 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001457 * The internal representation uses 38-bit and is described in the table below.
1458 * The first line indicates the side effect, and for field/array accesses the
1459 * second line indicates the type of the access (in the order of the
1460 * Primitive::Type enum).
1461 * The two numbered lines below indicate the bit position in the bitfield (read
1462 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001463 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001464 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1465 * +-------------+---------+---------+--------------+---------+---------+
1466 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1467 * | 3 |333333322|222222221| 1 |111111110|000000000|
1468 * | 7 |654321098|765432109| 8 |765432109|876543210|
1469 *
1470 * Note that, to ease the implementation, 'changes' bits are least significant
1471 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001472 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001473class SideEffects : public ValueObject {
1474 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001475 SideEffects() : flags_(0) {}
1476
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001477 static SideEffects None() {
1478 return SideEffects(0);
1479 }
1480
1481 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001482 return SideEffects(kAllChangeBits | kAllDependOnBits);
1483 }
1484
1485 static SideEffects AllChanges() {
1486 return SideEffects(kAllChangeBits);
1487 }
1488
1489 static SideEffects AllDependencies() {
1490 return SideEffects(kAllDependOnBits);
1491 }
1492
1493 static SideEffects AllExceptGCDependency() {
1494 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1495 }
1496
1497 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001498 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001499 }
1500
Aart Bik34c3ba92015-07-20 14:08:59 -07001501 static SideEffects AllWrites() {
1502 return SideEffects(kAllWrites);
1503 }
1504
1505 static SideEffects AllReads() {
1506 return SideEffects(kAllReads);
1507 }
1508
1509 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1510 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001511 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001512 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001513 }
1514
Aart Bik854a02b2015-07-14 16:07:00 -07001515 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1516 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001517 }
1518
Aart Bik34c3ba92015-07-20 14:08:59 -07001519 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1520 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001521 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001522 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001523 }
1524
1525 static SideEffects ArrayReadOfType(Primitive::Type type) {
1526 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1527 }
1528
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001529 static SideEffects CanTriggerGC() {
1530 return SideEffects(1ULL << kCanTriggerGCBit);
1531 }
1532
1533 static SideEffects DependsOnGC() {
1534 return SideEffects(1ULL << kDependsOnGCBit);
1535 }
1536
Aart Bik854a02b2015-07-14 16:07:00 -07001537 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001538 SideEffects Union(SideEffects other) const {
1539 return SideEffects(flags_ | other.flags_);
1540 }
1541
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001542 SideEffects Exclusion(SideEffects other) const {
1543 return SideEffects(flags_ & ~other.flags_);
1544 }
1545
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001546 void Add(SideEffects other) {
1547 flags_ |= other.flags_;
1548 }
1549
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001550 bool Includes(SideEffects other) const {
1551 return (other.flags_ & flags_) == other.flags_;
1552 }
1553
1554 bool HasSideEffects() const {
1555 return (flags_ & kAllChangeBits);
1556 }
1557
1558 bool HasDependencies() const {
1559 return (flags_ & kAllDependOnBits);
1560 }
1561
1562 // Returns true if there are no side effects or dependencies.
1563 bool DoesNothing() const {
1564 return flags_ == 0;
1565 }
1566
Aart Bik854a02b2015-07-14 16:07:00 -07001567 // Returns true if something is written.
1568 bool DoesAnyWrite() const {
1569 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001570 }
1571
Aart Bik854a02b2015-07-14 16:07:00 -07001572 // Returns true if something is read.
1573 bool DoesAnyRead() const {
1574 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001575 }
1576
Aart Bik854a02b2015-07-14 16:07:00 -07001577 // Returns true if potentially everything is written and read
1578 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001579 bool DoesAllReadWrite() const {
1580 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1581 }
1582
Aart Bik854a02b2015-07-14 16:07:00 -07001583 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001584 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001585 }
1586
Roland Levillain0d5a2812015-11-13 10:07:31 +00001587 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001588 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001589 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1590 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001591 }
1592
1593 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001594 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001595 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001596 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001597 for (int s = kLastBit; s >= 0; s--) {
1598 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1599 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1600 // This is a bit for the GC side effect.
1601 if (current_bit_is_set) {
1602 flags += "GC";
1603 }
Aart Bik854a02b2015-07-14 16:07:00 -07001604 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001605 } else {
1606 // This is a bit for the array/field analysis.
1607 // The underscore character stands for the 'can trigger GC' bit.
1608 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1609 if (current_bit_is_set) {
1610 flags += kDebug[s];
1611 }
1612 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1613 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1614 flags += "|";
1615 }
1616 }
Aart Bik854a02b2015-07-14 16:07:00 -07001617 }
1618 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001619 }
1620
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001621 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001622
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001623 private:
1624 static constexpr int kFieldArrayAnalysisBits = 9;
1625
1626 static constexpr int kFieldWriteOffset = 0;
1627 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1628 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1629 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1630
1631 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1632
1633 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1634 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1635 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1636 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1637
1638 static constexpr int kLastBit = kDependsOnGCBit;
1639 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1640
1641 // Aliases.
1642
1643 static_assert(kChangeBits == kDependOnBits,
1644 "the 'change' bits should match the 'depend on' bits.");
1645
1646 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1647 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1648 static constexpr uint64_t kAllWrites =
1649 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1650 static constexpr uint64_t kAllReads =
1651 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001652
Aart Bik854a02b2015-07-14 16:07:00 -07001653 // Work around the fact that HIR aliases I/F and J/D.
1654 // TODO: remove this interceptor once HIR types are clean
1655 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1656 switch (type) {
1657 case Primitive::kPrimInt:
1658 case Primitive::kPrimFloat:
1659 return TypeFlag(Primitive::kPrimInt, offset) |
1660 TypeFlag(Primitive::kPrimFloat, offset);
1661 case Primitive::kPrimLong:
1662 case Primitive::kPrimDouble:
1663 return TypeFlag(Primitive::kPrimLong, offset) |
1664 TypeFlag(Primitive::kPrimDouble, offset);
1665 default:
1666 return TypeFlag(type, offset);
1667 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001668 }
1669
Aart Bik854a02b2015-07-14 16:07:00 -07001670 // Translates type to bit flag.
1671 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1672 CHECK_NE(type, Primitive::kPrimVoid);
1673 const uint64_t one = 1;
1674 const int shift = type; // 0-based consecutive enum
1675 DCHECK_LE(kFieldWriteOffset, shift);
1676 DCHECK_LT(shift, kArrayWriteOffset);
1677 return one << (type + offset);
1678 }
1679
1680 // Private constructor on direct flags value.
1681 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1682
1683 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001684};
1685
David Brazdiled596192015-01-23 10:39:45 +00001686// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001687class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001688 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001689 HEnvironment(ArenaAllocator* arena,
1690 size_t number_of_vregs,
1691 const DexFile& dex_file,
1692 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001693 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001694 InvokeType invoke_type,
1695 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001696 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1697 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001698 parent_(nullptr),
1699 dex_file_(dex_file),
1700 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001701 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001702 invoke_type_(invoke_type),
1703 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001704 }
1705
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001706 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001707 : HEnvironment(arena,
1708 to_copy.Size(),
1709 to_copy.GetDexFile(),
1710 to_copy.GetMethodIdx(),
1711 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001712 to_copy.GetInvokeType(),
1713 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001714
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001715 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001716 if (parent_ != nullptr) {
1717 parent_->SetAndCopyParentChain(allocator, parent);
1718 } else {
1719 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1720 parent_->CopyFrom(parent);
1721 if (parent->GetParent() != nullptr) {
1722 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1723 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001724 }
David Brazdiled596192015-01-23 10:39:45 +00001725 }
1726
Vladimir Marko71bf8092015-09-15 15:33:14 +01001727 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001728 void CopyFrom(HEnvironment* environment);
1729
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001730 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1731 // input to the loop phi instead. This is for inserting instructions that
1732 // require an environment (like HDeoptimization) in the loop pre-header.
1733 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001734
1735 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001736 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001737 }
1738
1739 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001740 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001741 }
1742
David Brazdil1abb4192015-02-17 18:33:36 +00001743 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001744
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001745 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001746
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001747 HEnvironment* GetParent() const { return parent_; }
1748
1749 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001750 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001751 }
1752
1753 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001754 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001755 }
1756
1757 uint32_t GetDexPc() const {
1758 return dex_pc_;
1759 }
1760
1761 uint32_t GetMethodIdx() const {
1762 return method_idx_;
1763 }
1764
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001765 InvokeType GetInvokeType() const {
1766 return invoke_type_;
1767 }
1768
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001769 const DexFile& GetDexFile() const {
1770 return dex_file_;
1771 }
1772
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001773 HInstruction* GetHolder() const {
1774 return holder_;
1775 }
1776
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001777
1778 bool IsFromInlinedInvoke() const {
1779 return GetParent() != nullptr;
1780 }
1781
David Brazdiled596192015-01-23 10:39:45 +00001782 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001783 // Record instructions' use entries of this environment for constant-time removal.
1784 // It should only be called by HInstruction when a new environment use is added.
1785 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1786 DCHECK(env_use->GetUser() == this);
1787 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001788 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001789 }
David Brazdiled596192015-01-23 10:39:45 +00001790
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001791 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1792 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001793 HEnvironment* parent_;
1794 const DexFile& dex_file_;
1795 const uint32_t method_idx_;
1796 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001797 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001798
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001799 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001800 HInstruction* const holder_;
1801
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001802 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001803
1804 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1805};
1806
Vladimir Markof9f64412015-09-02 14:05:49 +01001807class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001808 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001809 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001810 : previous_(nullptr),
1811 next_(nullptr),
1812 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001813 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001814 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001815 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001816 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001817 locations_(nullptr),
1818 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001819 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001820 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001821 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001822
Dave Allison20dfc792014-06-16 20:44:29 -07001823 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001824
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001825#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001826 enum InstructionKind {
1827 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1828 };
1829#undef DECLARE_KIND
1830
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001831 HInstruction* GetNext() const { return next_; }
1832 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001833
Calin Juravle77520bc2015-01-12 18:45:46 +00001834 HInstruction* GetNextDisregardingMoves() const;
1835 HInstruction* GetPreviousDisregardingMoves() const;
1836
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001837 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001838 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001839 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001840 bool IsInBlock() const { return block_ != nullptr; }
1841 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001842 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1843 bool IsIrreducibleLoopHeaderPhi() const {
1844 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1845 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001846
Roland Levillain6b879dd2014-09-22 17:13:44 +01001847 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001848 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001849
1850 virtual void Accept(HGraphVisitor* visitor) = 0;
1851 virtual const char* DebugName() const = 0;
1852
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001853 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001854 void SetRawInputAt(size_t index, HInstruction* input) {
1855 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1856 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001857
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001858 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001859
1860 uint32_t GetDexPc() const { return dex_pc_; }
1861
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001862 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001863
Roland Levillaine161a2a2014-10-03 12:45:18 +01001864 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001865 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001866
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001867 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001868 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001869
Calin Juravle10e244f2015-01-26 18:54:32 +00001870 // Does not apply for all instructions, but having this at top level greatly
1871 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001872 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001873 virtual bool CanBeNull() const {
1874 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1875 return true;
1876 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001877
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001878 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001879 return false;
1880 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001881
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001882 virtual bool IsActualObject() const {
1883 return GetType() == Primitive::kPrimNot;
1884 }
1885
Calin Juravle2e768302015-07-28 14:41:11 +00001886 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001887
Calin Juravle61d544b2015-02-23 16:46:57 +00001888 ReferenceTypeInfo GetReferenceTypeInfo() const {
1889 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1890 return reference_type_info_;
1891 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001892
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001893 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001894 DCHECK(user != nullptr);
1895 HUseListNode<HInstruction*>* use =
1896 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1897 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001898 }
1899
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001900 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001901 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001902 HUseListNode<HEnvironment*>* env_use =
1903 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1904 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001905 }
1906
David Brazdil1abb4192015-02-17 18:33:36 +00001907 void RemoveAsUserOfInput(size_t input) {
1908 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1909 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1910 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001911
David Brazdil1abb4192015-02-17 18:33:36 +00001912 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1913 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001914
David Brazdiled596192015-01-23 10:39:45 +00001915 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1916 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001917 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001918 bool HasOnlyOneNonEnvironmentUse() const {
1919 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1920 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001921
Roland Levillain6c82d402014-10-13 16:10:27 +01001922 // Does this instruction strictly dominate `other_instruction`?
1923 // Returns false if this instruction and `other_instruction` are the same.
1924 // Aborts if this instruction and `other_instruction` are both phis.
1925 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001926
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001927 int GetId() const { return id_; }
1928 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001929
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001930 int GetSsaIndex() const { return ssa_index_; }
1931 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1932 bool HasSsaIndex() const { return ssa_index_ != -1; }
1933
1934 bool HasEnvironment() const { return environment_ != nullptr; }
1935 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001936 // Set the `environment_` field. Raw because this method does not
1937 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001938 void SetRawEnvironment(HEnvironment* environment) {
1939 DCHECK(environment_ == nullptr);
1940 DCHECK_EQ(environment->GetHolder(), this);
1941 environment_ = environment;
1942 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001943
1944 // Set the environment of this instruction, copying it from `environment`. While
1945 // copying, the uses lists are being updated.
1946 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001947 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001948 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001949 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001950 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001951 if (environment->GetParent() != nullptr) {
1952 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1953 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001954 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001955
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001956 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1957 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001958 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001959 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001960 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001961 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001962 if (environment->GetParent() != nullptr) {
1963 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1964 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001965 }
1966
Nicolas Geoffray39468442014-09-02 15:17:15 +01001967 // Returns the number of entries in the environment. Typically, that is the
1968 // number of dex registers in a method. It could be more in case of inlining.
1969 size_t EnvironmentSize() const;
1970
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001971 LocationSummary* GetLocations() const { return locations_; }
1972 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001973
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001974 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001975 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001976
Alexandre Rames188d4312015-04-09 18:30:21 +01001977 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1978 // uses of this instruction by `other` are *not* updated.
1979 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1980 ReplaceWith(other);
1981 other->ReplaceInput(this, use_index);
1982 }
1983
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001984 // Move `this` instruction before `cursor`.
1985 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001986
Vladimir Markofb337ea2015-11-25 15:25:10 +00001987 // Move `this` before its first user and out of any loops. If there is no
1988 // out-of-loop user that dominates all other users, move the instruction
1989 // to the end of the out-of-loop common dominator of the user's blocks.
1990 //
1991 // This can be used only on non-throwing instructions with no side effects that
1992 // have at least one use but no environment uses.
1993 void MoveBeforeFirstUserAndOutOfLoops();
1994
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001995#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001996 bool Is##type() const; \
1997 const H##type* As##type() const; \
1998 H##type* As##type();
1999
2000 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2001#undef INSTRUCTION_TYPE_CHECK
2002
2003#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002004 bool Is##type() const { return (As##type() != nullptr); } \
2005 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002006 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002007 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002008#undef INSTRUCTION_TYPE_CHECK
2009
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002010 // Returns whether the instruction can be moved within the graph.
2011 virtual bool CanBeMoved() const { return false; }
2012
2013 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002014 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002015 return false;
2016 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002017
2018 // Returns whether any data encoded in the two instructions is equal.
2019 // This method does not look at the inputs. Both instructions must be
2020 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002021 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002022 return false;
2023 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002024
2025 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002026 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002027 // 2) Their inputs are identical.
2028 bool Equals(HInstruction* other) const;
2029
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002030 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2031 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2032 // the virtual function because the __attribute__((__pure__)) doesn't really
2033 // apply the strong requirement for virtual functions, preventing optimizations.
2034 InstructionKind GetKind() const PURE;
2035 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002036
2037 virtual size_t ComputeHashCode() const {
2038 size_t result = GetKind();
2039 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2040 result = (result * 31) + InputAt(i)->GetId();
2041 }
2042 return result;
2043 }
2044
2045 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002046 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002047
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002048 size_t GetLifetimePosition() const { return lifetime_position_; }
2049 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2050 LiveInterval* GetLiveInterval() const { return live_interval_; }
2051 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2052 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2053
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002054 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2055
2056 // Returns whether the code generation of the instruction will require to have access
2057 // to the current method. Such instructions are:
2058 // (1): Instructions that require an environment, as calling the runtime requires
2059 // to walk the stack and have the current method stored at a specific stack address.
2060 // (2): Object literals like classes and strings, that are loaded from the dex cache
2061 // fields of the current method.
2062 bool NeedsCurrentMethod() const {
2063 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2064 }
2065
Vladimir Markodc151b22015-10-15 18:02:30 +01002066 // Returns whether the code generation of the instruction will require to have access
2067 // to the dex cache of the current method's declaring class via the current method.
2068 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002069
Mark Mendellc4701932015-04-10 13:18:51 -04002070 // Does this instruction have any use in an environment before
2071 // control flow hits 'other'?
2072 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2073
2074 // Remove all references to environment uses of this instruction.
2075 // The caller must ensure that this is safe to do.
2076 void RemoveEnvironmentUsers();
2077
David Brazdil1abb4192015-02-17 18:33:36 +00002078 protected:
2079 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2080 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
Aart Bik5d75afe2015-12-14 11:57:01 -08002081 void SetSideEffects(SideEffects other) { side_effects_ = other; }
David Brazdil1abb4192015-02-17 18:33:36 +00002082
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002083 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002084 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2085
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002086 HInstruction* previous_;
2087 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002088 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002089 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002090
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002091 // An instruction gets an id when it is added to the graph.
2092 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002093 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002094 int id_;
2095
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002096 // When doing liveness analysis, instructions that have uses get an SSA index.
2097 int ssa_index_;
2098
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002099 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002100 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002101
2102 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002103 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002104
Nicolas Geoffray39468442014-09-02 15:17:15 +01002105 // The environment associated with this instruction. Not null if the instruction
2106 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002107 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002108
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002109 // Set by the code generator.
2110 LocationSummary* locations_;
2111
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002112 // Set by the liveness analysis.
2113 LiveInterval* live_interval_;
2114
2115 // Set by the liveness analysis, this is the position in a linear
2116 // order of blocks where this instruction's live interval start.
2117 size_t lifetime_position_;
2118
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002119 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002120
Calin Juravleacf735c2015-02-12 15:25:22 +00002121 // TODO: for primitive types this should be marked as invalid.
2122 ReferenceTypeInfo reference_type_info_;
2123
David Brazdil1abb4192015-02-17 18:33:36 +00002124 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002125 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002126 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002127 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002128 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002129
2130 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2131};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002132std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002133
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002134class HInputIterator : public ValueObject {
2135 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002136 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002137
2138 bool Done() const { return index_ == instruction_->InputCount(); }
2139 HInstruction* Current() const { return instruction_->InputAt(index_); }
2140 void Advance() { index_++; }
2141
2142 private:
2143 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002144 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002145
2146 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2147};
2148
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002149class HInstructionIterator : public ValueObject {
2150 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002151 explicit HInstructionIterator(const HInstructionList& instructions)
2152 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002153 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002154 }
2155
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002156 bool Done() const { return instruction_ == nullptr; }
2157 HInstruction* Current() const { return instruction_; }
2158 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002159 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002160 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002161 }
2162
2163 private:
2164 HInstruction* instruction_;
2165 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002166
2167 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002168};
2169
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002170class HBackwardInstructionIterator : public ValueObject {
2171 public:
2172 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2173 : instruction_(instructions.last_instruction_) {
2174 next_ = Done() ? nullptr : instruction_->GetPrevious();
2175 }
2176
2177 bool Done() const { return instruction_ == nullptr; }
2178 HInstruction* Current() const { return instruction_; }
2179 void Advance() {
2180 instruction_ = next_;
2181 next_ = Done() ? nullptr : instruction_->GetPrevious();
2182 }
2183
2184 private:
2185 HInstruction* instruction_;
2186 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002187
2188 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002189};
2190
Vladimir Markof9f64412015-09-02 14:05:49 +01002191template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002192class HTemplateInstruction: public HInstruction {
2193 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002194 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002195 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002196 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002197
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002198 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002199
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002200 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002201 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2202 DCHECK_LT(i, N);
2203 return inputs_[i];
2204 }
David Brazdil1abb4192015-02-17 18:33:36 +00002205
2206 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002207 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002208 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002209 }
2210
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002211 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002212 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002213
2214 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002215};
2216
Vladimir Markof9f64412015-09-02 14:05:49 +01002217// HTemplateInstruction specialization for N=0.
2218template<>
2219class HTemplateInstruction<0>: public HInstruction {
2220 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002221 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002222 : HInstruction(side_effects, dex_pc) {}
2223
Vladimir Markof9f64412015-09-02 14:05:49 +01002224 virtual ~HTemplateInstruction() {}
2225
2226 size_t InputCount() const OVERRIDE { return 0; }
2227
2228 protected:
2229 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2230 LOG(FATAL) << "Unreachable";
2231 UNREACHABLE();
2232 }
2233
2234 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2235 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2236 LOG(FATAL) << "Unreachable";
2237 UNREACHABLE();
2238 }
2239
2240 private:
2241 friend class SsaBuilder;
2242};
2243
Dave Allison20dfc792014-06-16 20:44:29 -07002244template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002245class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002246 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002247 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002248 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002249 virtual ~HExpression() {}
2250
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002251 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002252
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002253 protected:
2254 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002255};
2256
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002257// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2258// instruction that branches to the exit block.
2259class HReturnVoid : public HTemplateInstruction<0> {
2260 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002261 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2262 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002263
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002264 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002265
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002266 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002267
2268 private:
2269 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2270};
2271
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002272// Represents dex's RETURN opcodes. A HReturn is a control flow
2273// instruction that branches to the exit block.
2274class HReturn : public HTemplateInstruction<1> {
2275 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002276 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2277 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002278 SetRawInputAt(0, value);
2279 }
2280
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002281 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002282
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002283 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002284
2285 private:
2286 DISALLOW_COPY_AND_ASSIGN(HReturn);
2287};
2288
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002289// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002290// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002291// exit block.
2292class HExit : public HTemplateInstruction<0> {
2293 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002294 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002295
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002296 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002297
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002298 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002299
2300 private:
2301 DISALLOW_COPY_AND_ASSIGN(HExit);
2302};
2303
2304// Jumps from one block to another.
2305class HGoto : public HTemplateInstruction<0> {
2306 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002307 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002308
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002309 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002310
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002311 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002312 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002313 }
2314
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002315 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002316
2317 private:
2318 DISALLOW_COPY_AND_ASSIGN(HGoto);
2319};
2320
Roland Levillain9867bc72015-08-05 10:21:34 +01002321class HConstant : public HExpression<0> {
2322 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002323 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2324 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002325
2326 bool CanBeMoved() const OVERRIDE { return true; }
2327
2328 virtual bool IsMinusOne() const { return false; }
2329 virtual bool IsZero() const { return false; }
2330 virtual bool IsOne() const { return false; }
2331
David Brazdil77a48ae2015-09-15 12:34:04 +00002332 virtual uint64_t GetValueAsUint64() const = 0;
2333
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002334 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002335
2336 private:
2337 DISALLOW_COPY_AND_ASSIGN(HConstant);
2338};
2339
2340class HNullConstant : public HConstant {
2341 public:
2342 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2343 return true;
2344 }
2345
David Brazdil77a48ae2015-09-15 12:34:04 +00002346 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2347
Roland Levillain9867bc72015-08-05 10:21:34 +01002348 size_t ComputeHashCode() const OVERRIDE { return 0; }
2349
2350 DECLARE_INSTRUCTION(NullConstant);
2351
2352 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002353 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002354
2355 friend class HGraph;
2356 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2357};
2358
2359// Constants of the type int. Those can be from Dex instructions, or
2360// synthesized (for example with the if-eqz instruction).
2361class HIntConstant : public HConstant {
2362 public:
2363 int32_t GetValue() const { return value_; }
2364
David Brazdil9f389d42015-10-01 14:32:56 +01002365 uint64_t GetValueAsUint64() const OVERRIDE {
2366 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2367 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002368
Roland Levillain9867bc72015-08-05 10:21:34 +01002369 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2370 DCHECK(other->IsIntConstant());
2371 return other->AsIntConstant()->value_ == value_;
2372 }
2373
2374 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2375
2376 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2377 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2378 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2379
2380 DECLARE_INSTRUCTION(IntConstant);
2381
2382 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002383 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2384 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2385 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2386 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002387
2388 const int32_t value_;
2389
2390 friend class HGraph;
2391 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2392 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2393 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2394};
2395
2396class HLongConstant : public HConstant {
2397 public:
2398 int64_t GetValue() const { return value_; }
2399
David Brazdil77a48ae2015-09-15 12:34:04 +00002400 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2401
Roland Levillain9867bc72015-08-05 10:21:34 +01002402 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2403 DCHECK(other->IsLongConstant());
2404 return other->AsLongConstant()->value_ == value_;
2405 }
2406
2407 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2408
2409 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2410 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2411 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2412
2413 DECLARE_INSTRUCTION(LongConstant);
2414
2415 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002416 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2417 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002418
2419 const int64_t value_;
2420
2421 friend class HGraph;
2422 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2423};
Dave Allison20dfc792014-06-16 20:44:29 -07002424
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002425// Conditional branch. A block ending with an HIf instruction must have
2426// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002427class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002428 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002429 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2430 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002431 SetRawInputAt(0, input);
2432 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002433
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002434 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002435
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002436 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002437 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002438 }
2439
2440 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002441 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002442 }
2443
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002444 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002445
2446 private:
2447 DISALLOW_COPY_AND_ASSIGN(HIf);
2448};
2449
David Brazdilfc6a86a2015-06-26 10:33:45 +00002450
2451// Abstract instruction which marks the beginning and/or end of a try block and
2452// links it to the respective exception handlers. Behaves the same as a Goto in
2453// non-exceptional control flow.
2454// Normal-flow successor is stored at index zero, exception handlers under
2455// higher indices in no particular order.
2456class HTryBoundary : public HTemplateInstruction<0> {
2457 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002458 enum BoundaryKind {
2459 kEntry,
2460 kExit,
2461 };
2462
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002463 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2464 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002465
2466 bool IsControlFlow() const OVERRIDE { return true; }
2467
2468 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002469 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002470
David Brazdild26a4112015-11-10 11:07:31 +00002471 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2472 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2473 }
2474
David Brazdilfc6a86a2015-06-26 10:33:45 +00002475 // Returns whether `handler` is among its exception handlers (non-zero index
2476 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002477 bool HasExceptionHandler(const HBasicBlock& handler) const {
2478 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002479 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002480 }
2481
2482 // If not present already, adds `handler` to its block's list of exception
2483 // handlers.
2484 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002485 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002486 GetBlock()->AddSuccessor(handler);
2487 }
2488 }
2489
David Brazdil56e1acc2015-06-30 15:41:36 +01002490 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002491
David Brazdilffee3d32015-07-06 11:48:53 +01002492 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2493
David Brazdilfc6a86a2015-06-26 10:33:45 +00002494 DECLARE_INSTRUCTION(TryBoundary);
2495
2496 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002497 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002498
2499 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2500};
2501
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002502// Deoptimize to interpreter, upon checking a condition.
2503class HDeoptimize : public HTemplateInstruction<1> {
2504 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002505 // We set CanTriggerGC to prevent any intermediate address to be live
2506 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002507 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002508 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002509 SetRawInputAt(0, cond);
2510 }
2511
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002512 bool CanBeMoved() const OVERRIDE { return true; }
2513 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2514 return true;
2515 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002516 bool NeedsEnvironment() const OVERRIDE { return true; }
2517 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002518
2519 DECLARE_INSTRUCTION(Deoptimize);
2520
2521 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002522 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2523};
2524
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002525// Represents the ArtMethod that was passed as a first argument to
2526// the method. It is used by instructions that depend on it, like
2527// instructions that work with the dex cache.
2528class HCurrentMethod : public HExpression<0> {
2529 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002530 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2531 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002532
2533 DECLARE_INSTRUCTION(CurrentMethod);
2534
2535 private:
2536 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2537};
2538
Mark Mendellfe57faa2015-09-18 09:26:15 -04002539// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2540// have one successor for each entry in the switch table, and the final successor
2541// will be the block containing the next Dex opcode.
2542class HPackedSwitch : public HTemplateInstruction<1> {
2543 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002544 HPackedSwitch(int32_t start_value,
2545 uint32_t num_entries,
2546 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002547 uint32_t dex_pc = kNoDexPc)
2548 : HTemplateInstruction(SideEffects::None(), dex_pc),
2549 start_value_(start_value),
2550 num_entries_(num_entries) {
2551 SetRawInputAt(0, input);
2552 }
2553
2554 bool IsControlFlow() const OVERRIDE { return true; }
2555
2556 int32_t GetStartValue() const { return start_value_; }
2557
Vladimir Marko211c2112015-09-24 16:52:33 +01002558 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002559
2560 HBasicBlock* GetDefaultBlock() const {
2561 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002562 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002563 }
2564 DECLARE_INSTRUCTION(PackedSwitch);
2565
2566 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002567 const int32_t start_value_;
2568 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002569
2570 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2571};
2572
Roland Levillain88cb1752014-10-20 16:36:47 +01002573class HUnaryOperation : public HExpression<1> {
2574 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002575 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2576 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002577 SetRawInputAt(0, input);
2578 }
2579
2580 HInstruction* GetInput() const { return InputAt(0); }
2581 Primitive::Type GetResultType() const { return GetType(); }
2582
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002583 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002584 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002585 return true;
2586 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002587
Roland Levillain9240d6a2014-10-20 16:47:04 +01002588 // Try to statically evaluate `operation` and return a HConstant
2589 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002590 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002591 HConstant* TryStaticEvaluation() const;
2592
2593 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002594 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2595 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002596
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002597 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002598
2599 private:
2600 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2601};
2602
Dave Allison20dfc792014-06-16 20:44:29 -07002603class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002604 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002605 HBinaryOperation(Primitive::Type result_type,
2606 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002607 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002608 SideEffects side_effects = SideEffects::None(),
2609 uint32_t dex_pc = kNoDexPc)
2610 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002611 SetRawInputAt(0, left);
2612 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002613 }
2614
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002615 HInstruction* GetLeft() const { return InputAt(0); }
2616 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002617 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002618
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002619 virtual bool IsCommutative() const { return false; }
2620
2621 // Put constant on the right.
2622 // Returns whether order is changed.
2623 bool OrderInputsWithConstantOnTheRight() {
2624 HInstruction* left = InputAt(0);
2625 HInstruction* right = InputAt(1);
2626 if (left->IsConstant() && !right->IsConstant()) {
2627 ReplaceInput(right, 0);
2628 ReplaceInput(left, 1);
2629 return true;
2630 }
2631 return false;
2632 }
2633
2634 // Order inputs by instruction id, but favor constant on the right side.
2635 // This helps GVN for commutative ops.
2636 void OrderInputs() {
2637 DCHECK(IsCommutative());
2638 HInstruction* left = InputAt(0);
2639 HInstruction* right = InputAt(1);
2640 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2641 return;
2642 }
2643 if (OrderInputsWithConstantOnTheRight()) {
2644 return;
2645 }
2646 // Order according to instruction id.
2647 if (left->GetId() > right->GetId()) {
2648 ReplaceInput(right, 0);
2649 ReplaceInput(left, 1);
2650 }
2651 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002652
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002653 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002654 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002655 return true;
2656 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002657
Roland Levillain9240d6a2014-10-20 16:47:04 +01002658 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002659 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002660 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002661 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002662
2663 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002664 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2665 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2666 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2667 HLongConstant* y ATTRIBUTE_UNUSED) const {
2668 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2669 return nullptr;
2670 }
2671 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2672 HIntConstant* y ATTRIBUTE_UNUSED) const {
2673 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2674 return nullptr;
2675 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002676 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2677 HNullConstant* y ATTRIBUTE_UNUSED) const {
2678 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2679 return nullptr;
2680 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002681
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002682 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002683 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002684 HConstant* GetConstantRight() const;
2685
2686 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002687 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002688 HInstruction* GetLeastConstantLeft() const;
2689
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002690 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002691
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002692 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002693 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2694};
2695
Mark Mendellc4701932015-04-10 13:18:51 -04002696// The comparison bias applies for floating point operations and indicates how NaN
2697// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002698enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002699 kNoBias, // bias is not applicable (i.e. for long operation)
2700 kGtBias, // return 1 for NaN comparisons
2701 kLtBias, // return -1 for NaN comparisons
2702};
2703
Dave Allison20dfc792014-06-16 20:44:29 -07002704class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002705 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002706 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2707 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002708 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002709 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002710
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002711 bool NeedsMaterialization() const { return needs_materialization_; }
2712 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002713
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002714 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002715 // `instruction`, and disregard moves in between.
2716 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002717
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002718 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002719
2720 virtual IfCondition GetCondition() const = 0;
2721
Mark Mendellc4701932015-04-10 13:18:51 -04002722 virtual IfCondition GetOppositeCondition() const = 0;
2723
Roland Levillain4fa13f62015-07-06 18:11:54 +01002724 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002725
2726 void SetBias(ComparisonBias bias) { bias_ = bias; }
2727
2728 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2729 return bias_ == other->AsCondition()->bias_;
2730 }
2731
Roland Levillain4fa13f62015-07-06 18:11:54 +01002732 bool IsFPConditionTrueIfNaN() const {
2733 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2734 IfCondition if_cond = GetCondition();
2735 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2736 }
2737
2738 bool IsFPConditionFalseIfNaN() const {
2739 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2740 IfCondition if_cond = GetCondition();
2741 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2742 }
2743
Dave Allison20dfc792014-06-16 20:44:29 -07002744 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002745 // For register allocation purposes, returns whether this instruction needs to be
2746 // materialized (that is, not just be in the processor flags).
2747 bool needs_materialization_;
2748
Mark Mendellc4701932015-04-10 13:18:51 -04002749 // Needed if we merge a HCompare into a HCondition.
2750 ComparisonBias bias_;
2751
Dave Allison20dfc792014-06-16 20:44:29 -07002752 DISALLOW_COPY_AND_ASSIGN(HCondition);
2753};
2754
2755// Instruction to check if two inputs are equal to each other.
2756class HEqual : public HCondition {
2757 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002758 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2759 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002760
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002761 bool IsCommutative() const OVERRIDE { return true; }
2762
Roland Levillain9867bc72015-08-05 10:21:34 +01002763 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002764 return GetBlock()->GetGraph()->GetIntConstant(
2765 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002766 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002767 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002768 return GetBlock()->GetGraph()->GetIntConstant(
2769 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002770 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002771 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2772 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002773 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002774 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002775
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002776 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002777
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002778 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002779 return kCondEQ;
2780 }
2781
Mark Mendellc4701932015-04-10 13:18:51 -04002782 IfCondition GetOppositeCondition() const OVERRIDE {
2783 return kCondNE;
2784 }
2785
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002786 private:
Aart Bike9f37602015-10-09 11:15:55 -07002787 template <typename T> bool Compute(T x, T y) const { return x == y; }
2788
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002789 DISALLOW_COPY_AND_ASSIGN(HEqual);
2790};
2791
Dave Allison20dfc792014-06-16 20:44:29 -07002792class HNotEqual : public HCondition {
2793 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002794 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2795 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002796
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002797 bool IsCommutative() const OVERRIDE { return true; }
2798
Roland Levillain9867bc72015-08-05 10:21:34 +01002799 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002800 return GetBlock()->GetGraph()->GetIntConstant(
2801 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002802 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002803 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002804 return GetBlock()->GetGraph()->GetIntConstant(
2805 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002806 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002807 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2808 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002809 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002810 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002811
Dave Allison20dfc792014-06-16 20:44:29 -07002812 DECLARE_INSTRUCTION(NotEqual);
2813
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002814 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002815 return kCondNE;
2816 }
2817
Mark Mendellc4701932015-04-10 13:18:51 -04002818 IfCondition GetOppositeCondition() const OVERRIDE {
2819 return kCondEQ;
2820 }
2821
Dave Allison20dfc792014-06-16 20:44:29 -07002822 private:
Aart Bike9f37602015-10-09 11:15:55 -07002823 template <typename T> bool Compute(T x, T y) const { return x != y; }
2824
Dave Allison20dfc792014-06-16 20:44:29 -07002825 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2826};
2827
2828class HLessThan : public HCondition {
2829 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002830 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2831 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002832
Roland Levillain9867bc72015-08-05 10:21:34 +01002833 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002834 return GetBlock()->GetGraph()->GetIntConstant(
2835 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002836 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002837 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002838 return GetBlock()->GetGraph()->GetIntConstant(
2839 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002840 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002841
Dave Allison20dfc792014-06-16 20:44:29 -07002842 DECLARE_INSTRUCTION(LessThan);
2843
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002844 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002845 return kCondLT;
2846 }
2847
Mark Mendellc4701932015-04-10 13:18:51 -04002848 IfCondition GetOppositeCondition() const OVERRIDE {
2849 return kCondGE;
2850 }
2851
Dave Allison20dfc792014-06-16 20:44:29 -07002852 private:
Aart Bike9f37602015-10-09 11:15:55 -07002853 template <typename T> bool Compute(T x, T y) const { return x < y; }
2854
Dave Allison20dfc792014-06-16 20:44:29 -07002855 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2856};
2857
2858class HLessThanOrEqual : public HCondition {
2859 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002860 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2861 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002862
Roland Levillain9867bc72015-08-05 10:21:34 +01002863 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002864 return GetBlock()->GetGraph()->GetIntConstant(
2865 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002866 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002867 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002868 return GetBlock()->GetGraph()->GetIntConstant(
2869 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002870 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002871
Dave Allison20dfc792014-06-16 20:44:29 -07002872 DECLARE_INSTRUCTION(LessThanOrEqual);
2873
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002874 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002875 return kCondLE;
2876 }
2877
Mark Mendellc4701932015-04-10 13:18:51 -04002878 IfCondition GetOppositeCondition() const OVERRIDE {
2879 return kCondGT;
2880 }
2881
Dave Allison20dfc792014-06-16 20:44:29 -07002882 private:
Aart Bike9f37602015-10-09 11:15:55 -07002883 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2884
Dave Allison20dfc792014-06-16 20:44:29 -07002885 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2886};
2887
2888class HGreaterThan : public HCondition {
2889 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002890 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2891 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002892
Roland Levillain9867bc72015-08-05 10:21:34 +01002893 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002894 return GetBlock()->GetGraph()->GetIntConstant(
2895 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002896 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002897 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002898 return GetBlock()->GetGraph()->GetIntConstant(
2899 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002900 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002901
Dave Allison20dfc792014-06-16 20:44:29 -07002902 DECLARE_INSTRUCTION(GreaterThan);
2903
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002904 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002905 return kCondGT;
2906 }
2907
Mark Mendellc4701932015-04-10 13:18:51 -04002908 IfCondition GetOppositeCondition() const OVERRIDE {
2909 return kCondLE;
2910 }
2911
Dave Allison20dfc792014-06-16 20:44:29 -07002912 private:
Aart Bike9f37602015-10-09 11:15:55 -07002913 template <typename T> bool Compute(T x, T y) const { return x > y; }
2914
Dave Allison20dfc792014-06-16 20:44:29 -07002915 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2916};
2917
2918class HGreaterThanOrEqual : public HCondition {
2919 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002920 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2921 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002922
Roland Levillain9867bc72015-08-05 10:21:34 +01002923 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002924 return GetBlock()->GetGraph()->GetIntConstant(
2925 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002926 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002927 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002928 return GetBlock()->GetGraph()->GetIntConstant(
2929 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002930 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002931
Dave Allison20dfc792014-06-16 20:44:29 -07002932 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2933
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002934 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002935 return kCondGE;
2936 }
2937
Mark Mendellc4701932015-04-10 13:18:51 -04002938 IfCondition GetOppositeCondition() const OVERRIDE {
2939 return kCondLT;
2940 }
2941
Dave Allison20dfc792014-06-16 20:44:29 -07002942 private:
Aart Bike9f37602015-10-09 11:15:55 -07002943 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2944
Dave Allison20dfc792014-06-16 20:44:29 -07002945 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2946};
2947
Aart Bike9f37602015-10-09 11:15:55 -07002948class HBelow : public HCondition {
2949 public:
2950 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2951 : HCondition(first, second, dex_pc) {}
2952
2953 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2954 return GetBlock()->GetGraph()->GetIntConstant(
2955 Compute(static_cast<uint32_t>(x->GetValue()),
2956 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2957 }
2958 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2959 return GetBlock()->GetGraph()->GetIntConstant(
2960 Compute(static_cast<uint64_t>(x->GetValue()),
2961 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2962 }
2963
2964 DECLARE_INSTRUCTION(Below);
2965
2966 IfCondition GetCondition() const OVERRIDE {
2967 return kCondB;
2968 }
2969
2970 IfCondition GetOppositeCondition() const OVERRIDE {
2971 return kCondAE;
2972 }
2973
2974 private:
2975 template <typename T> bool Compute(T x, T y) const { return x < y; }
2976
2977 DISALLOW_COPY_AND_ASSIGN(HBelow);
2978};
2979
2980class HBelowOrEqual : public HCondition {
2981 public:
2982 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2983 : HCondition(first, second, dex_pc) {}
2984
2985 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2986 return GetBlock()->GetGraph()->GetIntConstant(
2987 Compute(static_cast<uint32_t>(x->GetValue()),
2988 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2989 }
2990 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2991 return GetBlock()->GetGraph()->GetIntConstant(
2992 Compute(static_cast<uint64_t>(x->GetValue()),
2993 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2994 }
2995
2996 DECLARE_INSTRUCTION(BelowOrEqual);
2997
2998 IfCondition GetCondition() const OVERRIDE {
2999 return kCondBE;
3000 }
3001
3002 IfCondition GetOppositeCondition() const OVERRIDE {
3003 return kCondA;
3004 }
3005
3006 private:
3007 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3008
3009 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3010};
3011
3012class HAbove : public HCondition {
3013 public:
3014 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3015 : HCondition(first, second, dex_pc) {}
3016
3017 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3018 return GetBlock()->GetGraph()->GetIntConstant(
3019 Compute(static_cast<uint32_t>(x->GetValue()),
3020 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3021 }
3022 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3023 return GetBlock()->GetGraph()->GetIntConstant(
3024 Compute(static_cast<uint64_t>(x->GetValue()),
3025 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3026 }
3027
3028 DECLARE_INSTRUCTION(Above);
3029
3030 IfCondition GetCondition() const OVERRIDE {
3031 return kCondA;
3032 }
3033
3034 IfCondition GetOppositeCondition() const OVERRIDE {
3035 return kCondBE;
3036 }
3037
3038 private:
3039 template <typename T> bool Compute(T x, T y) const { return x > y; }
3040
3041 DISALLOW_COPY_AND_ASSIGN(HAbove);
3042};
3043
3044class HAboveOrEqual : public HCondition {
3045 public:
3046 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3047 : HCondition(first, second, dex_pc) {}
3048
3049 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3050 return GetBlock()->GetGraph()->GetIntConstant(
3051 Compute(static_cast<uint32_t>(x->GetValue()),
3052 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3053 }
3054 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3055 return GetBlock()->GetGraph()->GetIntConstant(
3056 Compute(static_cast<uint64_t>(x->GetValue()),
3057 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3058 }
3059
3060 DECLARE_INSTRUCTION(AboveOrEqual);
3061
3062 IfCondition GetCondition() const OVERRIDE {
3063 return kCondAE;
3064 }
3065
3066 IfCondition GetOppositeCondition() const OVERRIDE {
3067 return kCondB;
3068 }
3069
3070 private:
3071 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3072
3073 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3074};
Dave Allison20dfc792014-06-16 20:44:29 -07003075
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003076// Instruction to check how two inputs compare to each other.
3077// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3078class HCompare : public HBinaryOperation {
3079 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003080 HCompare(Primitive::Type type,
3081 HInstruction* first,
3082 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003083 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003084 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003085 : HBinaryOperation(Primitive::kPrimInt,
3086 first,
3087 second,
3088 SideEffectsForArchRuntimeCalls(type),
3089 dex_pc),
3090 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003091 DCHECK_EQ(type, first->GetType());
3092 DCHECK_EQ(type, second->GetType());
3093 }
3094
Roland Levillain9867bc72015-08-05 10:21:34 +01003095 template <typename T>
3096 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003097
Roland Levillain9867bc72015-08-05 10:21:34 +01003098 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003099 return GetBlock()->GetGraph()->GetIntConstant(
3100 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003101 }
3102 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003103 return GetBlock()->GetGraph()->GetIntConstant(
3104 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003105 }
3106
Calin Juravleddb7df22014-11-25 20:56:51 +00003107 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3108 return bias_ == other->AsCompare()->bias_;
3109 }
3110
Mark Mendellc4701932015-04-10 13:18:51 -04003111 ComparisonBias GetBias() const { return bias_; }
3112
Roland Levillain4fa13f62015-07-06 18:11:54 +01003113 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003114
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003115
3116 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3117 // MIPS64 uses a runtime call for FP comparisons.
3118 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3119 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003120
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003121 DECLARE_INSTRUCTION(Compare);
3122
3123 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003124 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003125
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003126 DISALLOW_COPY_AND_ASSIGN(HCompare);
3127};
3128
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003129// A local in the graph. Corresponds to a Dex register.
3130class HLocal : public HTemplateInstruction<0> {
3131 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003132 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003133 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003134
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003135 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003136
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003137 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003138
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003139 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003140 // The Dex register number.
3141 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003142
3143 DISALLOW_COPY_AND_ASSIGN(HLocal);
3144};
3145
3146// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003147class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003148 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003149 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3150 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003151 SetRawInputAt(0, local);
3152 }
3153
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003154 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3155
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003156 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003157
3158 private:
3159 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3160};
3161
3162// Store a value in a given local. This instruction has two inputs: the value
3163// and the local.
3164class HStoreLocal : public HTemplateInstruction<2> {
3165 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003166 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3167 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003168 SetRawInputAt(0, local);
3169 SetRawInputAt(1, value);
3170 }
3171
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003172 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3173
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003174 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003175
3176 private:
3177 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3178};
3179
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003180class HFloatConstant : public HConstant {
3181 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003182 float GetValue() const { return value_; }
3183
David Brazdil77a48ae2015-09-15 12:34:04 +00003184 uint64_t GetValueAsUint64() const OVERRIDE {
3185 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3186 }
3187
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003188 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003189 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003190 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003191 }
3192
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003193 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003194
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003195 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003196 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003197 }
3198 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003199 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003200 }
3201 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003202 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3203 }
3204 bool IsNaN() const {
3205 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003206 }
3207
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003208 DECLARE_INSTRUCTION(FloatConstant);
3209
3210 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003211 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3212 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3213 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3214 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003215
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003216 const float value_;
3217
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003218 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003219 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003220 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003221 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3222};
3223
3224class HDoubleConstant : public HConstant {
3225 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003226 double GetValue() const { return value_; }
3227
David Brazdil77a48ae2015-09-15 12:34:04 +00003228 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3229
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003230 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003231 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003232 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003233 }
3234
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003235 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003236
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003237 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003238 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003239 }
3240 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003241 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003242 }
3243 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003244 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3245 }
3246 bool IsNaN() const {
3247 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003248 }
3249
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003250 DECLARE_INSTRUCTION(DoubleConstant);
3251
3252 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003253 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3254 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3255 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3256 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003257
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003258 const double value_;
3259
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003260 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003261 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003262 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003263 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3264};
3265
David Brazdil6de19382016-01-08 17:37:10 +00003266class HNewInstance : public HExpression<2> {
3267 public:
3268 HNewInstance(HInstruction* cls,
3269 HCurrentMethod* current_method,
3270 uint32_t dex_pc,
3271 uint16_t type_index,
3272 const DexFile& dex_file,
3273 bool can_throw,
3274 bool finalizable,
3275 QuickEntrypointEnum entrypoint)
3276 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3277 type_index_(type_index),
3278 dex_file_(dex_file),
3279 can_throw_(can_throw),
3280 finalizable_(finalizable),
3281 entrypoint_(entrypoint) {
3282 SetRawInputAt(0, cls);
3283 SetRawInputAt(1, current_method);
3284 }
3285
3286 uint16_t GetTypeIndex() const { return type_index_; }
3287 const DexFile& GetDexFile() const { return dex_file_; }
3288
3289 // Calls runtime so needs an environment.
3290 bool NeedsEnvironment() const OVERRIDE { return true; }
3291
3292 // It may throw when called on type that's not instantiable/accessible.
3293 // It can throw OOME.
3294 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3295 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3296
3297 bool IsFinalizable() const { return finalizable_; }
3298
3299 bool CanBeNull() const OVERRIDE { return false; }
3300
3301 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3302
3303 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3304 entrypoint_ = entrypoint;
3305 }
3306
3307 bool IsStringAlloc() const;
3308
3309 DECLARE_INSTRUCTION(NewInstance);
3310
3311 private:
3312 const uint16_t type_index_;
3313 const DexFile& dex_file_;
3314 const bool can_throw_;
3315 const bool finalizable_;
3316 QuickEntrypointEnum entrypoint_;
3317
3318 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3319};
3320
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003321enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003322#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3323 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003324#include "intrinsics_list.h"
3325 kNone,
3326 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3327#undef INTRINSICS_LIST
3328#undef OPTIMIZING_INTRINSICS
3329};
3330std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3331
Agi Csaki05f20562015-08-19 14:58:14 -07003332enum IntrinsicNeedsEnvironmentOrCache {
3333 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3334 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003335};
3336
Aart Bik5d75afe2015-12-14 11:57:01 -08003337enum IntrinsicSideEffects {
3338 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3339 kReadSideEffects, // Intrinsic may read heap memory.
3340 kWriteSideEffects, // Intrinsic may write heap memory.
3341 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3342};
3343
3344enum IntrinsicExceptions {
3345 kNoThrow, // Intrinsic does not throw any exceptions.
3346 kCanThrow // Intrinsic may throw exceptions.
3347};
3348
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003349class HInvoke : public HInstruction {
3350 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003351 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003352
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003353 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003354
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003355 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003356 SetRawInputAt(index, argument);
3357 }
3358
Roland Levillain3e3d7332015-04-28 11:00:54 +01003359 // Return the number of arguments. This number can be lower than
3360 // the number of inputs returned by InputCount(), as some invoke
3361 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3362 // inputs at the end of their list of inputs.
3363 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3364
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003365 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003366
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003367 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003368 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003369
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003370 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3371
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003372 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003373 return intrinsic_;
3374 }
3375
Aart Bik5d75afe2015-12-14 11:57:01 -08003376 void SetIntrinsic(Intrinsics intrinsic,
3377 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3378 IntrinsicSideEffects side_effects,
3379 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003380
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003381 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003382 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003383 }
3384
Aart Bik5d75afe2015-12-14 11:57:01 -08003385 bool CanThrow() const OVERRIDE { return can_throw_; }
3386
3387 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3388
3389 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3390 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3391 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003392
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003393 uint32_t* GetIntrinsicOptimizations() {
3394 return &intrinsic_optimizations_;
3395 }
3396
3397 const uint32_t* GetIntrinsicOptimizations() const {
3398 return &intrinsic_optimizations_;
3399 }
3400
3401 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3402
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003403 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003404
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003405 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003406 HInvoke(ArenaAllocator* arena,
3407 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003408 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003409 Primitive::Type return_type,
3410 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003411 uint32_t dex_method_index,
3412 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003413 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003414 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003415 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003416 inputs_(number_of_arguments + number_of_other_inputs,
3417 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003418 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003419 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003420 original_invoke_type_(original_invoke_type),
Aart Bik5d75afe2015-12-14 11:57:01 -08003421 can_throw_(true),
agicsaki57b81ec2015-08-11 17:39:37 -07003422 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003423 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003424 }
3425
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003426 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003427 return inputs_[index];
3428 }
3429
David Brazdil1abb4192015-02-17 18:33:36 +00003430 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003431 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003432 }
3433
Aart Bik5d75afe2015-12-14 11:57:01 -08003434 void SetCanThrow(bool can_throw) { can_throw_ = can_throw; }
3435
Roland Levillain3e3d7332015-04-28 11:00:54 +01003436 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003437 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003438 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003439 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003440 const InvokeType original_invoke_type_;
Aart Bik5d75afe2015-12-14 11:57:01 -08003441 bool can_throw_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003442 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003443
3444 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3445 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003446
3447 private:
3448 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3449};
3450
Calin Juravle175dc732015-08-25 15:42:32 +01003451class HInvokeUnresolved : public HInvoke {
3452 public:
3453 HInvokeUnresolved(ArenaAllocator* arena,
3454 uint32_t number_of_arguments,
3455 Primitive::Type return_type,
3456 uint32_t dex_pc,
3457 uint32_t dex_method_index,
3458 InvokeType invoke_type)
3459 : HInvoke(arena,
3460 number_of_arguments,
3461 0u /* number_of_other_inputs */,
3462 return_type,
3463 dex_pc,
3464 dex_method_index,
3465 invoke_type) {
3466 }
3467
3468 DECLARE_INSTRUCTION(InvokeUnresolved);
3469
3470 private:
3471 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3472};
3473
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003474class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003475 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003476 // Requirements of this method call regarding the class
3477 // initialization (clinit) check of its declaring class.
3478 enum class ClinitCheckRequirement {
3479 kNone, // Class already initialized.
3480 kExplicit, // Static call having explicit clinit check as last input.
3481 kImplicit, // Static call implicitly requiring a clinit check.
3482 };
3483
Vladimir Marko58155012015-08-19 12:49:41 +00003484 // Determines how to load the target ArtMethod*.
3485 enum class MethodLoadKind {
3486 // Use a String init ArtMethod* loaded from Thread entrypoints.
3487 kStringInit,
3488
3489 // Use the method's own ArtMethod* loaded by the register allocator.
3490 kRecursive,
3491
3492 // Use ArtMethod* at a known address, embed the direct address in the code.
3493 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3494 kDirectAddress,
3495
3496 // Use ArtMethod* at an address that will be known at link time, embed the direct
3497 // address in the code. If the image is relocatable, emit .patch_oat entry.
3498 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3499 // the image relocatable or not.
3500 kDirectAddressWithFixup,
3501
3502 // Load from resoved methods array in the dex cache using a PC-relative load.
3503 // Used when we need to use the dex cache, for example for invoke-static that
3504 // may cause class initialization (the entry may point to a resolution method),
3505 // and we know that we can access the dex cache arrays using a PC-relative load.
3506 kDexCachePcRelative,
3507
3508 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3509 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3510 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3511 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3512 kDexCacheViaMethod,
3513 };
3514
3515 // Determines the location of the code pointer.
3516 enum class CodePtrLocation {
3517 // Recursive call, use local PC-relative call instruction.
3518 kCallSelf,
3519
3520 // Use PC-relative call instruction patched at link time.
3521 // Used for calls within an oat file, boot->boot or app->app.
3522 kCallPCRelative,
3523
3524 // Call to a known target address, embed the direct address in code.
3525 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3526 kCallDirect,
3527
3528 // Call to a target address that will be known at link time, embed the direct
3529 // address in code. If the image is relocatable, emit .patch_oat entry.
3530 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3531 // the image relocatable or not.
3532 kCallDirectWithFixup,
3533
3534 // Use code pointer from the ArtMethod*.
3535 // Used when we don't know the target code. This is also the last-resort-kind used when
3536 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3537 kCallArtMethod,
3538 };
3539
3540 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003541 MethodLoadKind method_load_kind;
3542 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003543 // The method load data holds
3544 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3545 // Note that there are multiple string init methods, each having its own offset.
3546 // - the method address for kDirectAddress
3547 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003548 uint64_t method_load_data;
3549 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003550 };
3551
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003552 HInvokeStaticOrDirect(ArenaAllocator* arena,
3553 uint32_t number_of_arguments,
3554 Primitive::Type return_type,
3555 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003556 uint32_t method_index,
3557 MethodReference target_method,
3558 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003559 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003560 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003561 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003562 : HInvoke(arena,
3563 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003564 // There is potentially one extra argument for the HCurrentMethod node, and
3565 // potentially one other if the clinit check is explicit, and potentially
3566 // one other if the method is a string factory.
3567 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3568 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3569 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003570 return_type,
3571 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003572 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003573 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003574 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003575 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003576 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003577 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003578
Vladimir Markodc151b22015-10-15 18:02:30 +01003579 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003580 bool had_current_method_input = HasCurrentMethodInput();
3581 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3582
3583 // Using the current method is the default and once we find a better
3584 // method load kind, we should not go back to using the current method.
3585 DCHECK(had_current_method_input || !needs_current_method_input);
3586
3587 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003588 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3589 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003590 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003591 dispatch_info_ = dispatch_info;
3592 }
3593
Vladimir Markoc53c0792015-11-19 15:48:33 +00003594 void AddSpecialInput(HInstruction* input) {
3595 // We allow only one special input.
3596 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3597 DCHECK(InputCount() == GetSpecialInputIndex() ||
3598 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3599 InsertInputAt(GetSpecialInputIndex(), input);
3600 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003601
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003602 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003603 // We access the method via the dex cache so we can't do an implicit null check.
3604 // TODO: for intrinsics we can generate implicit null checks.
3605 return false;
3606 }
3607
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003608 bool CanBeNull() const OVERRIDE {
3609 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3610 }
3611
Vladimir Markoc53c0792015-11-19 15:48:33 +00003612 // Get the index of the special input, if any.
3613 //
David Brazdil6de19382016-01-08 17:37:10 +00003614 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3615 // method pointer; otherwise there may be one platform-specific special input,
3616 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003617 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3618
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003619 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3620 void SetOptimizedInvokeType(InvokeType invoke_type) {
3621 optimized_invoke_type_ = invoke_type;
3622 }
3623
Vladimir Marko58155012015-08-19 12:49:41 +00003624 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3625 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3626 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003627 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003628 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003629 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003630 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003631 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3632 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003633 bool HasCurrentMethodInput() const {
3634 // This function can be called only after the invoke has been fully initialized by the builder.
3635 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003636 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003637 return true;
3638 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003639 DCHECK(InputCount() == GetSpecialInputIndex() ||
3640 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003641 return false;
3642 }
3643 }
Vladimir Marko58155012015-08-19 12:49:41 +00003644 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3645 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003646 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003647
3648 int32_t GetStringInitOffset() const {
3649 DCHECK(IsStringInit());
3650 return dispatch_info_.method_load_data;
3651 }
3652
3653 uint64_t GetMethodAddress() const {
3654 DCHECK(HasMethodAddress());
3655 return dispatch_info_.method_load_data;
3656 }
3657
3658 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003659 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003660 return dispatch_info_.method_load_data;
3661 }
3662
3663 uint64_t GetDirectCodePtr() const {
3664 DCHECK(HasDirectCodePtr());
3665 return dispatch_info_.direct_code_ptr;
3666 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003667
Calin Juravle68ad6492015-08-18 17:08:12 +01003668 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3669
Roland Levillain4c0eb422015-04-24 16:43:49 +01003670 // Is this instruction a call to a static method?
3671 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003672 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003673 }
3674
Vladimir Markofbb184a2015-11-13 14:47:00 +00003675 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3676 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3677 // instruction; only relevant for static calls with explicit clinit check.
3678 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003679 DCHECK(IsStaticWithExplicitClinitCheck());
3680 size_t last_input_index = InputCount() - 1;
3681 HInstruction* last_input = InputAt(last_input_index);
3682 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003683 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003684 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003685 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003686 clinit_check_requirement_ = new_requirement;
3687 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003688 }
3689
David Brazdil6de19382016-01-08 17:37:10 +00003690 HNewInstance* GetThisArgumentOfStringInit() const {
3691 DCHECK(IsStringInit());
3692 size_t index = InputCount() - 1;
3693 DCHECK(InputAt(index)->IsNewInstance());
3694 return InputAt(index)->AsNewInstance();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003695 }
3696
David Brazdil6de19382016-01-08 17:37:10 +00003697 void RemoveThisArgumentOfStringInit() {
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003698 DCHECK(IsStringInit());
David Brazdil6de19382016-01-08 17:37:10 +00003699 size_t index = InputCount() - 1;
3700 DCHECK(InputAt(index)->IsNewInstance());
3701 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003702 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003703 }
3704
Roland Levillain4c0eb422015-04-24 16:43:49 +01003705 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003706 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003707 bool IsStaticWithExplicitClinitCheck() const {
3708 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3709 }
3710
3711 // Is this a call to a static method whose declaring class has an
3712 // implicit intialization check requirement?
3713 bool IsStaticWithImplicitClinitCheck() const {
3714 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3715 }
3716
Vladimir Markob554b5a2015-11-06 12:57:55 +00003717 // Does this method load kind need the current method as an input?
3718 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3719 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3720 }
3721
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003722 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003723
Roland Levillain4c0eb422015-04-24 16:43:49 +01003724 protected:
3725 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3726 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3727 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3728 HInstruction* input = input_record.GetInstruction();
3729 // `input` is the last input of a static invoke marked as having
3730 // an explicit clinit check. It must either be:
3731 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3732 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3733 DCHECK(input != nullptr);
3734 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3735 }
3736 return input_record;
3737 }
3738
Vladimir Markoc53c0792015-11-19 15:48:33 +00003739 void InsertInputAt(size_t index, HInstruction* input);
3740 void RemoveInputAt(size_t index);
3741
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003742 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003743 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003744 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003745 // The target method may refer to different dex file or method index than the original
3746 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3747 // in derived class to increase visibility.
3748 MethodReference target_method_;
3749 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003750
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003751 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003752};
Vladimir Markof64242a2015-12-01 14:58:23 +00003753std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003754std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003755
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003756class HInvokeVirtual : public HInvoke {
3757 public:
3758 HInvokeVirtual(ArenaAllocator* arena,
3759 uint32_t number_of_arguments,
3760 Primitive::Type return_type,
3761 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003762 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003763 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003764 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003765 vtable_index_(vtable_index) {}
3766
Calin Juravle641547a2015-04-21 22:08:51 +01003767 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003768 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003769 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003770 }
3771
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003772 uint32_t GetVTableIndex() const { return vtable_index_; }
3773
3774 DECLARE_INSTRUCTION(InvokeVirtual);
3775
3776 private:
3777 const uint32_t vtable_index_;
3778
3779 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3780};
3781
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003782class HInvokeInterface : public HInvoke {
3783 public:
3784 HInvokeInterface(ArenaAllocator* arena,
3785 uint32_t number_of_arguments,
3786 Primitive::Type return_type,
3787 uint32_t dex_pc,
3788 uint32_t dex_method_index,
3789 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003790 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003791 imt_index_(imt_index) {}
3792
Calin Juravle641547a2015-04-21 22:08:51 +01003793 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003794 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003795 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003796 }
3797
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003798 uint32_t GetImtIndex() const { return imt_index_; }
3799 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3800
3801 DECLARE_INSTRUCTION(InvokeInterface);
3802
3803 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003804 const uint32_t imt_index_;
3805
3806 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3807};
3808
Roland Levillain88cb1752014-10-20 16:36:47 +01003809class HNeg : public HUnaryOperation {
3810 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003811 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3812 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003813
Roland Levillain9867bc72015-08-05 10:21:34 +01003814 template <typename T> T Compute(T x) const { return -x; }
3815
3816 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003817 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003818 }
3819 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003820 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003821 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003822
Roland Levillain88cb1752014-10-20 16:36:47 +01003823 DECLARE_INSTRUCTION(Neg);
3824
3825 private:
3826 DISALLOW_COPY_AND_ASSIGN(HNeg);
3827};
3828
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003829class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003830 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003831 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003832 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003833 uint32_t dex_pc,
3834 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003835 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003836 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003837 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003838 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003839 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003840 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003841 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003842 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003843 }
3844
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003845 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003846 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003847
3848 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003849 bool NeedsEnvironment() const OVERRIDE { return true; }
3850
Mingyao Yang0c365e62015-03-31 15:09:29 -07003851 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3852 bool CanThrow() const OVERRIDE { return true; }
3853
Calin Juravle10e244f2015-01-26 18:54:32 +00003854 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003855
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003856 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3857
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003858 DECLARE_INSTRUCTION(NewArray);
3859
3860 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003861 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003862 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003863 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003864
3865 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3866};
3867
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003868class HAdd : public HBinaryOperation {
3869 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003870 HAdd(Primitive::Type result_type,
3871 HInstruction* left,
3872 HInstruction* right,
3873 uint32_t dex_pc = kNoDexPc)
3874 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003875
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003876 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003877
Roland Levillain9867bc72015-08-05 10:21:34 +01003878 template <typename T> T Compute(T x, T y) const { return x + y; }
3879
3880 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003881 return GetBlock()->GetGraph()->GetIntConstant(
3882 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003883 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003884 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003885 return GetBlock()->GetGraph()->GetLongConstant(
3886 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003887 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003888
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003889 DECLARE_INSTRUCTION(Add);
3890
3891 private:
3892 DISALLOW_COPY_AND_ASSIGN(HAdd);
3893};
3894
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003895class HSub : public HBinaryOperation {
3896 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003897 HSub(Primitive::Type result_type,
3898 HInstruction* left,
3899 HInstruction* right,
3900 uint32_t dex_pc = kNoDexPc)
3901 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003902
Roland Levillain9867bc72015-08-05 10:21:34 +01003903 template <typename T> T Compute(T x, T y) const { return x - y; }
3904
3905 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003906 return GetBlock()->GetGraph()->GetIntConstant(
3907 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003908 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003909 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003910 return GetBlock()->GetGraph()->GetLongConstant(
3911 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003912 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003913
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003914 DECLARE_INSTRUCTION(Sub);
3915
3916 private:
3917 DISALLOW_COPY_AND_ASSIGN(HSub);
3918};
3919
Calin Juravle34bacdf2014-10-07 20:23:36 +01003920class HMul : public HBinaryOperation {
3921 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003922 HMul(Primitive::Type result_type,
3923 HInstruction* left,
3924 HInstruction* right,
3925 uint32_t dex_pc = kNoDexPc)
3926 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003927
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003928 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003929
Roland Levillain9867bc72015-08-05 10:21:34 +01003930 template <typename T> T Compute(T x, T y) const { return x * y; }
3931
3932 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003933 return GetBlock()->GetGraph()->GetIntConstant(
3934 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003935 }
3936 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003937 return GetBlock()->GetGraph()->GetLongConstant(
3938 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003939 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003940
3941 DECLARE_INSTRUCTION(Mul);
3942
3943 private:
3944 DISALLOW_COPY_AND_ASSIGN(HMul);
3945};
3946
Calin Juravle7c4954d2014-10-28 16:57:40 +00003947class HDiv : public HBinaryOperation {
3948 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003949 HDiv(Primitive::Type result_type,
3950 HInstruction* left,
3951 HInstruction* right,
3952 uint32_t dex_pc)
3953 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003954
Roland Levillain9867bc72015-08-05 10:21:34 +01003955 template <typename T>
3956 T Compute(T x, T y) const {
3957 // Our graph structure ensures we never have 0 for `y` during
3958 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003959 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003960 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003961 return (y == -1) ? -x : x / y;
3962 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003963
Roland Levillain9867bc72015-08-05 10:21:34 +01003964 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003965 return GetBlock()->GetGraph()->GetIntConstant(
3966 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003967 }
3968 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003969 return GetBlock()->GetGraph()->GetLongConstant(
3970 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003971 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003972
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003973 static SideEffects SideEffectsForArchRuntimeCalls() {
3974 // The generated code can use a runtime call.
3975 return SideEffects::CanTriggerGC();
3976 }
3977
Calin Juravle7c4954d2014-10-28 16:57:40 +00003978 DECLARE_INSTRUCTION(Div);
3979
3980 private:
3981 DISALLOW_COPY_AND_ASSIGN(HDiv);
3982};
3983
Calin Juravlebacfec32014-11-14 15:54:36 +00003984class HRem : public HBinaryOperation {
3985 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003986 HRem(Primitive::Type result_type,
3987 HInstruction* left,
3988 HInstruction* right,
3989 uint32_t dex_pc)
3990 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003991
Roland Levillain9867bc72015-08-05 10:21:34 +01003992 template <typename T>
3993 T Compute(T x, T y) const {
3994 // Our graph structure ensures we never have 0 for `y` during
3995 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003996 DCHECK_NE(y, 0);
3997 // Special case -1 to avoid getting a SIGFPE on x86(_64).
3998 return (y == -1) ? 0 : x % y;
3999 }
4000
Roland Levillain9867bc72015-08-05 10:21:34 +01004001 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004002 return GetBlock()->GetGraph()->GetIntConstant(
4003 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004004 }
4005 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004006 return GetBlock()->GetGraph()->GetLongConstant(
4007 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004008 }
4009
Calin Juravlebacfec32014-11-14 15:54:36 +00004010
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004011 static SideEffects SideEffectsForArchRuntimeCalls() {
4012 return SideEffects::CanTriggerGC();
4013 }
4014
Calin Juravlebacfec32014-11-14 15:54:36 +00004015 DECLARE_INSTRUCTION(Rem);
4016
4017 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004018 DISALLOW_COPY_AND_ASSIGN(HRem);
4019};
4020
Calin Juravled0d48522014-11-04 16:40:20 +00004021class HDivZeroCheck : public HExpression<1> {
4022 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004023 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4024 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004025 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004026 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004027 SetRawInputAt(0, value);
4028 }
4029
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004030 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4031
Calin Juravled0d48522014-11-04 16:40:20 +00004032 bool CanBeMoved() const OVERRIDE { return true; }
4033
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004034 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004035 return true;
4036 }
4037
4038 bool NeedsEnvironment() const OVERRIDE { return true; }
4039 bool CanThrow() const OVERRIDE { return true; }
4040
Calin Juravled0d48522014-11-04 16:40:20 +00004041 DECLARE_INSTRUCTION(DivZeroCheck);
4042
4043 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004044 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4045};
4046
Calin Juravle9aec02f2014-11-18 23:06:35 +00004047class HShl : public HBinaryOperation {
4048 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004049 HShl(Primitive::Type result_type,
4050 HInstruction* left,
4051 HInstruction* right,
4052 uint32_t dex_pc = kNoDexPc)
4053 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004054
Roland Levillain9867bc72015-08-05 10:21:34 +01004055 template <typename T, typename U, typename V>
4056 T Compute(T x, U y, V max_shift_value) const {
4057 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4058 "V is not the unsigned integer type corresponding to T");
4059 return x << (y & max_shift_value);
4060 }
4061
4062 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4063 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004064 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004065 }
4066 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4067 // case is handled as `x << static_cast<int>(y)`.
4068 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4069 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004070 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004071 }
4072 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4073 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004074 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004075 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004076
4077 DECLARE_INSTRUCTION(Shl);
4078
4079 private:
4080 DISALLOW_COPY_AND_ASSIGN(HShl);
4081};
4082
4083class HShr : public HBinaryOperation {
4084 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004085 HShr(Primitive::Type result_type,
4086 HInstruction* left,
4087 HInstruction* right,
4088 uint32_t dex_pc = kNoDexPc)
4089 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004090
Roland Levillain9867bc72015-08-05 10:21:34 +01004091 template <typename T, typename U, typename V>
4092 T Compute(T x, U y, V max_shift_value) const {
4093 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4094 "V is not the unsigned integer type corresponding to T");
4095 return x >> (y & max_shift_value);
4096 }
4097
4098 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4099 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004100 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004101 }
4102 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4103 // case is handled as `x >> static_cast<int>(y)`.
4104 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4105 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004106 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004107 }
4108 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4109 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004110 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004111 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004112
4113 DECLARE_INSTRUCTION(Shr);
4114
4115 private:
4116 DISALLOW_COPY_AND_ASSIGN(HShr);
4117};
4118
4119class HUShr : public HBinaryOperation {
4120 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004121 HUShr(Primitive::Type result_type,
4122 HInstruction* left,
4123 HInstruction* right,
4124 uint32_t dex_pc = kNoDexPc)
4125 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004126
Roland Levillain9867bc72015-08-05 10:21:34 +01004127 template <typename T, typename U, typename V>
4128 T Compute(T x, U y, V max_shift_value) const {
4129 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4130 "V is not the unsigned integer type corresponding to T");
4131 V ux = static_cast<V>(x);
4132 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004133 }
4134
Roland Levillain9867bc72015-08-05 10:21:34 +01004135 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4136 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004137 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004138 }
4139 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4140 // case is handled as `x >>> static_cast<int>(y)`.
4141 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4142 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004143 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004144 }
4145 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4146 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004147 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004148 }
4149
4150 DECLARE_INSTRUCTION(UShr);
4151
4152 private:
4153 DISALLOW_COPY_AND_ASSIGN(HUShr);
4154};
4155
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004156class HAnd : public HBinaryOperation {
4157 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004158 HAnd(Primitive::Type result_type,
4159 HInstruction* left,
4160 HInstruction* right,
4161 uint32_t dex_pc = kNoDexPc)
4162 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004163
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004164 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004165
Roland Levillain9867bc72015-08-05 10:21:34 +01004166 template <typename T, typename U>
4167 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4168
4169 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004170 return GetBlock()->GetGraph()->GetIntConstant(
4171 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004172 }
4173 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004174 return GetBlock()->GetGraph()->GetLongConstant(
4175 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004176 }
4177 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004178 return GetBlock()->GetGraph()->GetLongConstant(
4179 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004180 }
4181 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004182 return GetBlock()->GetGraph()->GetLongConstant(
4183 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004184 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004185
4186 DECLARE_INSTRUCTION(And);
4187
4188 private:
4189 DISALLOW_COPY_AND_ASSIGN(HAnd);
4190};
4191
4192class HOr : public HBinaryOperation {
4193 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004194 HOr(Primitive::Type result_type,
4195 HInstruction* left,
4196 HInstruction* right,
4197 uint32_t dex_pc = kNoDexPc)
4198 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004199
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004200 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004201
Roland Levillain9867bc72015-08-05 10:21:34 +01004202 template <typename T, typename U>
4203 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4204
4205 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004206 return GetBlock()->GetGraph()->GetIntConstant(
4207 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004208 }
4209 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004210 return GetBlock()->GetGraph()->GetLongConstant(
4211 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004212 }
4213 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004214 return GetBlock()->GetGraph()->GetLongConstant(
4215 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004216 }
4217 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004218 return GetBlock()->GetGraph()->GetLongConstant(
4219 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004220 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004221
4222 DECLARE_INSTRUCTION(Or);
4223
4224 private:
4225 DISALLOW_COPY_AND_ASSIGN(HOr);
4226};
4227
4228class HXor : public HBinaryOperation {
4229 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004230 HXor(Primitive::Type result_type,
4231 HInstruction* left,
4232 HInstruction* right,
4233 uint32_t dex_pc = kNoDexPc)
4234 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004235
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004236 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004237
Roland Levillain9867bc72015-08-05 10:21:34 +01004238 template <typename T, typename U>
4239 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4240
4241 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004242 return GetBlock()->GetGraph()->GetIntConstant(
4243 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004244 }
4245 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004246 return GetBlock()->GetGraph()->GetLongConstant(
4247 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004248 }
4249 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004250 return GetBlock()->GetGraph()->GetLongConstant(
4251 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004252 }
4253 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004254 return GetBlock()->GetGraph()->GetLongConstant(
4255 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004256 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004257
4258 DECLARE_INSTRUCTION(Xor);
4259
4260 private:
4261 DISALLOW_COPY_AND_ASSIGN(HXor);
4262};
4263
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004264class HRor : public HBinaryOperation {
4265 public:
4266 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4267 : HBinaryOperation(result_type, value, distance) {}
4268
4269 template <typename T, typename U, typename V>
4270 T Compute(T x, U y, V max_shift_value) const {
4271 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4272 "V is not the unsigned integer type corresponding to T");
4273 V ux = static_cast<V>(x);
4274 if ((y & max_shift_value) == 0) {
4275 return static_cast<T>(ux);
4276 } else {
4277 const V reg_bits = sizeof(T) * 8;
4278 return static_cast<T>(ux >> (y & max_shift_value)) |
4279 (x << (reg_bits - (y & max_shift_value)));
4280 }
4281 }
4282
4283 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4284 return GetBlock()->GetGraph()->GetIntConstant(
4285 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4286 }
4287 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4288 return GetBlock()->GetGraph()->GetLongConstant(
4289 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4290 }
4291 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4292 return GetBlock()->GetGraph()->GetLongConstant(
4293 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4294 }
4295
4296 DECLARE_INSTRUCTION(Ror);
4297
4298 private:
4299 DISALLOW_COPY_AND_ASSIGN(HRor);
4300};
4301
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004302// The value of a parameter in this method. Its location depends on
4303// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004304class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004305 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004306 HParameterValue(const DexFile& dex_file,
4307 uint16_t type_index,
4308 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004309 Primitive::Type parameter_type,
4310 bool is_this = false)
4311 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004312 dex_file_(dex_file),
4313 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004314 index_(index),
4315 is_this_(is_this),
4316 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004317
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004318 const DexFile& GetDexFile() const { return dex_file_; }
4319 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004320 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004321 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004322
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004323 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4324 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004325
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004326 DECLARE_INSTRUCTION(ParameterValue);
4327
4328 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004329 const DexFile& dex_file_;
4330 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004331 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004332 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004333 const uint8_t index_;
4334
Calin Juravle10e244f2015-01-26 18:54:32 +00004335 // Whether or not the parameter value corresponds to 'this' argument.
4336 const bool is_this_;
4337
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004338 bool can_be_null_;
4339
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004340 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4341};
4342
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004343class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004344 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004345 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4346 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004347
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004348 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004349 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004350 return true;
4351 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004352
Roland Levillain9867bc72015-08-05 10:21:34 +01004353 template <typename T> T Compute(T x) const { return ~x; }
4354
4355 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004356 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004357 }
4358 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004359 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004360 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004361
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004362 DECLARE_INSTRUCTION(Not);
4363
4364 private:
4365 DISALLOW_COPY_AND_ASSIGN(HNot);
4366};
4367
David Brazdil66d126e2015-04-03 16:02:44 +01004368class HBooleanNot : public HUnaryOperation {
4369 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004370 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4371 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004372
4373 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004374 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004375 return true;
4376 }
4377
Roland Levillain9867bc72015-08-05 10:21:34 +01004378 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004379 DCHECK(IsUint<1>(x));
4380 return !x;
4381 }
4382
Roland Levillain9867bc72015-08-05 10:21:34 +01004383 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004384 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004385 }
4386 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4387 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004388 UNREACHABLE();
4389 }
4390
4391 DECLARE_INSTRUCTION(BooleanNot);
4392
4393 private:
4394 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4395};
4396
Roland Levillaindff1f282014-11-05 14:15:05 +00004397class HTypeConversion : public HExpression<1> {
4398 public:
4399 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004400 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004401 : HExpression(result_type,
4402 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4403 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004404 SetRawInputAt(0, input);
4405 DCHECK_NE(input->GetType(), result_type);
4406 }
4407
4408 HInstruction* GetInput() const { return InputAt(0); }
4409 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4410 Primitive::Type GetResultType() const { return GetType(); }
4411
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004412 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004413 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004414
Roland Levillaindff1f282014-11-05 14:15:05 +00004415 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004416 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004417
Mark Mendelle82549b2015-05-06 10:55:34 -04004418 // Try to statically evaluate the conversion and return a HConstant
4419 // containing the result. If the input cannot be converted, return nullptr.
4420 HConstant* TryStaticEvaluation() const;
4421
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004422 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4423 Primitive::Type result_type) {
4424 // Some architectures may not require the 'GC' side effects, but at this point
4425 // in the compilation process we do not know what architecture we will
4426 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004427 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4428 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004429 return SideEffects::CanTriggerGC();
4430 }
4431 return SideEffects::None();
4432 }
4433
Roland Levillaindff1f282014-11-05 14:15:05 +00004434 DECLARE_INSTRUCTION(TypeConversion);
4435
4436 private:
4437 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4438};
4439
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004440static constexpr uint32_t kNoRegNumber = -1;
4441
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004442class HPhi : public HInstruction {
4443 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004444 HPhi(ArenaAllocator* arena,
4445 uint32_t reg_number,
4446 size_t number_of_inputs,
4447 Primitive::Type type,
4448 uint32_t dex_pc = kNoDexPc)
4449 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004450 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004451 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004452 type_(ToPhiType(type)),
4453 // Phis are constructed live and marked dead if conflicting or unused.
4454 // Individual steps of SsaBuilder should assume that if a phi has been
4455 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4456 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004457 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004458 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004459 }
4460
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004461 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4462 static Primitive::Type ToPhiType(Primitive::Type type) {
4463 switch (type) {
4464 case Primitive::kPrimBoolean:
4465 case Primitive::kPrimByte:
4466 case Primitive::kPrimShort:
4467 case Primitive::kPrimChar:
4468 return Primitive::kPrimInt;
4469 default:
4470 return type;
4471 }
4472 }
4473
David Brazdilffee3d32015-07-06 11:48:53 +01004474 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4475
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004476 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004477
4478 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004479 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004480
Calin Juravle10e244f2015-01-26 18:54:32 +00004481 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil4833f5a2015-12-16 10:37:39 +00004482 void SetType(Primitive::Type new_type) {
4483 // Make sure that only valid type changes occur. The following are allowed:
4484 // (1) int -> float/ref (primitive type propagation),
4485 // (2) long -> double (primitive type propagation).
4486 DCHECK(type_ == new_type ||
4487 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4488 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4489 (type_ == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4490 type_ = new_type;
4491 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004492
Calin Juravle10e244f2015-01-26 18:54:32 +00004493 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4494 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4495
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004496 uint32_t GetRegNumber() const { return reg_number_; }
4497
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004498 void SetDead() { is_live_ = false; }
4499 void SetLive() { is_live_ = true; }
4500 bool IsDead() const { return !is_live_; }
4501 bool IsLive() const { return is_live_; }
4502
David Brazdil77a48ae2015-09-15 12:34:04 +00004503 bool IsVRegEquivalentOf(HInstruction* other) const {
4504 return other != nullptr
4505 && other->IsPhi()
4506 && other->AsPhi()->GetBlock() == GetBlock()
4507 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4508 }
4509
Calin Juravlea4f88312015-04-16 12:57:19 +01004510 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4511 // An equivalent phi is a phi having the same dex register and type.
4512 // It assumes that phis with the same dex register are adjacent.
4513 HPhi* GetNextEquivalentPhiWithSameType() {
4514 HInstruction* next = GetNext();
4515 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4516 if (next->GetType() == GetType()) {
4517 return next->AsPhi();
4518 }
4519 next = next->GetNext();
4520 }
4521 return nullptr;
4522 }
4523
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004524 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004525
David Brazdil1abb4192015-02-17 18:33:36 +00004526 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004527 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004528 return inputs_[index];
4529 }
David Brazdil1abb4192015-02-17 18:33:36 +00004530
4531 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004532 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004533 }
4534
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004535 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004536 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004537 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004538 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004539 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004540 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004541
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004542 DISALLOW_COPY_AND_ASSIGN(HPhi);
4543};
4544
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004545class HNullCheck : public HExpression<1> {
4546 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004547 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4548 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004549 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004550 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004551 SetRawInputAt(0, value);
4552 }
4553
Calin Juravle10e244f2015-01-26 18:54:32 +00004554 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004555 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004556 return true;
4557 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004558
Calin Juravle10e244f2015-01-26 18:54:32 +00004559 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004560
Calin Juravle10e244f2015-01-26 18:54:32 +00004561 bool CanThrow() const OVERRIDE { return true; }
4562
4563 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004564
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004565
4566 DECLARE_INSTRUCTION(NullCheck);
4567
4568 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004569 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4570};
4571
4572class FieldInfo : public ValueObject {
4573 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004574 FieldInfo(MemberOffset field_offset,
4575 Primitive::Type field_type,
4576 bool is_volatile,
4577 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004578 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004579 const DexFile& dex_file,
4580 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004581 : field_offset_(field_offset),
4582 field_type_(field_type),
4583 is_volatile_(is_volatile),
4584 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004585 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004586 dex_file_(dex_file),
4587 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004588
4589 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004590 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004591 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004592 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004593 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004594 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004595 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004596
4597 private:
4598 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004599 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004600 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004601 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004602 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004603 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004604 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004605};
4606
4607class HInstanceFieldGet : public HExpression<1> {
4608 public:
4609 HInstanceFieldGet(HInstruction* value,
4610 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004611 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004612 bool is_volatile,
4613 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004614 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004615 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004616 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004617 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004618 : HExpression(field_type,
4619 SideEffects::FieldReadOfType(field_type, is_volatile),
4620 dex_pc),
4621 field_info_(field_offset,
4622 field_type,
4623 is_volatile,
4624 field_idx,
4625 declaring_class_def_index,
4626 dex_file,
4627 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004628 SetRawInputAt(0, value);
4629 }
4630
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004631 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004632
4633 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4634 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4635 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004636 }
4637
Calin Juravle641547a2015-04-21 22:08:51 +01004638 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4639 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004640 }
4641
4642 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004643 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4644 }
4645
Calin Juravle52c48962014-12-16 17:02:57 +00004646 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004647 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004648 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004649 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004650
4651 DECLARE_INSTRUCTION(InstanceFieldGet);
4652
4653 private:
4654 const FieldInfo field_info_;
4655
4656 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4657};
4658
4659class HInstanceFieldSet : public HTemplateInstruction<2> {
4660 public:
4661 HInstanceFieldSet(HInstruction* object,
4662 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004663 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004664 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004665 bool is_volatile,
4666 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004667 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004668 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004669 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004670 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004671 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4672 dex_pc),
4673 field_info_(field_offset,
4674 field_type,
4675 is_volatile,
4676 field_idx,
4677 declaring_class_def_index,
4678 dex_file,
4679 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004680 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004681 SetRawInputAt(0, object);
4682 SetRawInputAt(1, value);
4683 }
4684
Calin Juravle641547a2015-04-21 22:08:51 +01004685 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4686 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004687 }
4688
Calin Juravle52c48962014-12-16 17:02:57 +00004689 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004690 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004691 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004692 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004693 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004694 bool GetValueCanBeNull() const { return value_can_be_null_; }
4695 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004696
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004697 DECLARE_INSTRUCTION(InstanceFieldSet);
4698
4699 private:
4700 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004701 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004702
4703 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4704};
4705
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004706class HArrayGet : public HExpression<2> {
4707 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004708 HArrayGet(HInstruction* array,
4709 HInstruction* index,
4710 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004711 uint32_t dex_pc,
4712 SideEffects additional_side_effects = SideEffects::None())
4713 : HExpression(type,
4714 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004715 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004716 SetRawInputAt(0, array);
4717 SetRawInputAt(1, index);
4718 }
4719
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004720 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004721 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004722 return true;
4723 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004724 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004725 // TODO: We can be smarter here.
4726 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4727 // which generates the implicit null check. There are cases when these can be removed
4728 // to produce better code. If we ever add optimizations to do so we should allow an
4729 // implicit check here (as long as the address falls in the first page).
4730 return false;
4731 }
4732
David Brazdil4833f5a2015-12-16 10:37:39 +00004733 bool IsEquivalentOf(HArrayGet* other) const {
4734 bool result = (GetDexPc() == other->GetDexPc());
4735 if (kIsDebugBuild && result) {
4736 DCHECK_EQ(GetBlock(), other->GetBlock());
4737 DCHECK_EQ(GetArray(), other->GetArray());
4738 DCHECK_EQ(GetIndex(), other->GetIndex());
4739 if (Primitive::IsIntOrLongType(GetType())) {
4740 DCHECK(Primitive::IsFloatingPointType(other->GetType()));
4741 } else {
4742 DCHECK(Primitive::IsFloatingPointType(GetType()));
4743 DCHECK(Primitive::IsIntOrLongType(other->GetType()));
4744 }
4745 }
4746 return result;
4747 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004748
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004749 HInstruction* GetArray() const { return InputAt(0); }
4750 HInstruction* GetIndex() const { return InputAt(1); }
4751
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004752 DECLARE_INSTRUCTION(ArrayGet);
4753
4754 private:
4755 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4756};
4757
4758class HArraySet : public HTemplateInstruction<3> {
4759 public:
4760 HArraySet(HInstruction* array,
4761 HInstruction* index,
4762 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004763 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004764 uint32_t dex_pc,
4765 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004766 : HTemplateInstruction(
4767 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004768 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4769 additional_side_effects),
4770 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004771 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004772 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004773 value_can_be_null_(true),
4774 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004775 SetRawInputAt(0, array);
4776 SetRawInputAt(1, index);
4777 SetRawInputAt(2, value);
4778 }
4779
Calin Juravle77520bc2015-01-12 18:45:46 +00004780 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004781 // We currently always call a runtime method to catch array store
4782 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004783 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004784 }
4785
Mingyao Yang81014cb2015-06-02 03:16:27 -07004786 // Can throw ArrayStoreException.
4787 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4788
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004789 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004790 // TODO: Same as for ArrayGet.
4791 return false;
4792 }
4793
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004794 void ClearNeedsTypeCheck() {
4795 needs_type_check_ = false;
4796 }
4797
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004798 void ClearValueCanBeNull() {
4799 value_can_be_null_ = false;
4800 }
4801
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004802 void SetStaticTypeOfArrayIsObjectArray() {
4803 static_type_of_array_is_object_array_ = true;
4804 }
4805
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004806 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004807 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004808 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004809
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004810 HInstruction* GetArray() const { return InputAt(0); }
4811 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004812 HInstruction* GetValue() const { return InputAt(2); }
4813
4814 Primitive::Type GetComponentType() const {
4815 // The Dex format does not type floating point index operations. Since the
4816 // `expected_component_type_` is set during building and can therefore not
4817 // be correct, we also check what is the value type. If it is a floating
4818 // point type, we must use that type.
4819 Primitive::Type value_type = GetValue()->GetType();
4820 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4821 ? value_type
4822 : expected_component_type_;
4823 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004824
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004825 Primitive::Type GetRawExpectedComponentType() const {
4826 return expected_component_type_;
4827 }
4828
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004829 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4830 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4831 }
4832
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004833 DECLARE_INSTRUCTION(ArraySet);
4834
4835 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004836 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004837 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004838 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004839 // Cached information for the reference_type_info_ so that codegen
4840 // does not need to inspect the static type.
4841 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004842
4843 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4844};
4845
4846class HArrayLength : public HExpression<1> {
4847 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004848 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004849 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004850 // Note that arrays do not change length, so the instruction does not
4851 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004852 SetRawInputAt(0, array);
4853 }
4854
Calin Juravle77520bc2015-01-12 18:45:46 +00004855 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004856 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004857 return true;
4858 }
Calin Juravle641547a2015-04-21 22:08:51 +01004859 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4860 return obj == InputAt(0);
4861 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004862
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004863 DECLARE_INSTRUCTION(ArrayLength);
4864
4865 private:
4866 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4867};
4868
4869class HBoundsCheck : public HExpression<2> {
4870 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004871 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
4872 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004873 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004874 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004875 DCHECK(index->GetType() == Primitive::kPrimInt);
4876 SetRawInputAt(0, index);
4877 SetRawInputAt(1, length);
4878 }
4879
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004880 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004881 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004882 return true;
4883 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004884
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004885 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004886
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004887 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004888
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004889 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004890
4891 DECLARE_INSTRUCTION(BoundsCheck);
4892
4893 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004894 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4895};
4896
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004897/**
4898 * Some DEX instructions are folded into multiple HInstructions that need
4899 * to stay live until the last HInstruction. This class
4900 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004901 * HInstruction stays live. `index` represents the stack location index of the
4902 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004903 */
4904class HTemporary : public HTemplateInstruction<0> {
4905 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004906 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4907 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004908
4909 size_t GetIndex() const { return index_; }
4910
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004911 Primitive::Type GetType() const OVERRIDE {
4912 // The previous instruction is the one that will be stored in the temporary location.
4913 DCHECK(GetPrevious() != nullptr);
4914 return GetPrevious()->GetType();
4915 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004916
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004917 DECLARE_INSTRUCTION(Temporary);
4918
4919 private:
4920 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004921 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4922};
4923
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004924class HSuspendCheck : public HTemplateInstruction<0> {
4925 public:
4926 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004927 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004928
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004929 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004930 return true;
4931 }
4932
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004933 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4934 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004935
4936 DECLARE_INSTRUCTION(SuspendCheck);
4937
4938 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004939 // Only used for code generation, in order to share the same slow path between back edges
4940 // of a same loop.
4941 SlowPathCode* slow_path_;
4942
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004943 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4944};
4945
David Srbecky0cf44932015-12-09 14:09:59 +00004946// Pseudo-instruction which provides the native debugger with mapping information.
4947// It ensures that we can generate line number and local variables at this point.
4948class HNativeDebugInfo : public HTemplateInstruction<0> {
4949 public:
4950 explicit HNativeDebugInfo(uint32_t dex_pc)
4951 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
4952
4953 bool NeedsEnvironment() const OVERRIDE {
4954 return true;
4955 }
4956
4957 DECLARE_INSTRUCTION(NativeDebugInfo);
4958
4959 private:
4960 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
4961};
4962
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004963/**
4964 * Instruction to load a Class object.
4965 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004966class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004967 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004968 HLoadClass(HCurrentMethod* current_method,
4969 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004970 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004971 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004972 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004973 bool needs_access_check,
4974 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004975 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004976 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004977 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004978 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004979 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004980 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004981 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00004982 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004983 // Referrers class should not need access check. We never inline unverified
4984 // methods so we can't possibly end up in this situation.
4985 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004986 SetRawInputAt(0, current_method);
4987 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004988
4989 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004990
4991 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004992 // Note that we don't need to test for generate_clinit_check_.
4993 // Whether or not we need to generate the clinit check is processed in
4994 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004995 return other->AsLoadClass()->type_index_ == type_index_ &&
4996 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004997 }
4998
4999 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5000
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005001 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005002 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005003 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005004
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005005 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005006 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005007 }
5008
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005009 bool MustGenerateClinitCheck() const {
5010 return generate_clinit_check_;
5011 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005012
Calin Juravle0ba218d2015-05-19 18:46:01 +01005013 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005014 // The entrypoint the code generator is going to call does not do
5015 // clinit of the class.
5016 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01005017 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005018 }
5019
5020 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005021 return MustGenerateClinitCheck() ||
5022 (!is_referrers_class_ && !is_in_dex_cache_) ||
5023 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01005024 }
5025
5026 bool NeedsAccessCheck() const {
5027 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005028 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005029
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005030 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005031 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005032 }
5033
Calin Juravleacf735c2015-02-12 15:25:22 +00005034 ReferenceTypeInfo GetLoadedClassRTI() {
5035 return loaded_class_rti_;
5036 }
5037
5038 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5039 // Make sure we only set exact types (the loaded class should never be merged).
5040 DCHECK(rti.IsExact());
5041 loaded_class_rti_ = rti;
5042 }
5043
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005044 const DexFile& GetDexFile() { return dex_file_; }
5045
Vladimir Markodc151b22015-10-15 18:02:30 +01005046 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005047
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005048 static SideEffects SideEffectsForArchRuntimeCalls() {
5049 return SideEffects::CanTriggerGC();
5050 }
5051
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005052 bool IsInDexCache() const { return is_in_dex_cache_; }
5053
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005054 DECLARE_INSTRUCTION(LoadClass);
5055
5056 private:
5057 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005058 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005059 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005060 // Whether this instruction must generate the initialization check.
5061 // Used for code generation.
5062 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005063 const bool needs_access_check_;
5064 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005065
Calin Juravleacf735c2015-02-12 15:25:22 +00005066 ReferenceTypeInfo loaded_class_rti_;
5067
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005068 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5069};
5070
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005071class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005072 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005073 HLoadString(HCurrentMethod* current_method,
5074 uint32_t string_index,
5075 uint32_t dex_pc,
5076 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005077 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005078 string_index_(string_index),
5079 is_in_dex_cache_(is_in_dex_cache) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005080 SetRawInputAt(0, current_method);
5081 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005082
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005083 bool CanBeMoved() const OVERRIDE { return true; }
5084
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005085 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5086 return other->AsLoadString()->string_index_ == string_index_;
5087 }
5088
5089 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5090
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005091 uint32_t GetStringIndex() const { return string_index_; }
5092
5093 // TODO: Can we deopt or debug when we resolve a string?
5094 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01005095 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005096 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005097 bool IsInDexCache() const { return is_in_dex_cache_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005098
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005099 static SideEffects SideEffectsForArchRuntimeCalls() {
5100 return SideEffects::CanTriggerGC();
5101 }
5102
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005103 DECLARE_INSTRUCTION(LoadString);
5104
5105 private:
5106 const uint32_t string_index_;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005107 const bool is_in_dex_cache_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005108
5109 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5110};
5111
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005112/**
5113 * Performs an initialization check on its Class object input.
5114 */
5115class HClinitCheck : public HExpression<1> {
5116 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005117 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005118 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005119 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005120 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5121 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005122 SetRawInputAt(0, constant);
5123 }
5124
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005125 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005126 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005127 return true;
5128 }
5129
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005130 bool NeedsEnvironment() const OVERRIDE {
5131 // May call runtime to initialize the class.
5132 return true;
5133 }
5134
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005135 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005136
5137 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5138
5139 DECLARE_INSTRUCTION(ClinitCheck);
5140
5141 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005142 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5143};
5144
5145class HStaticFieldGet : public HExpression<1> {
5146 public:
5147 HStaticFieldGet(HInstruction* cls,
5148 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005149 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005150 bool is_volatile,
5151 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005152 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005153 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005154 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005155 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005156 : HExpression(field_type,
5157 SideEffects::FieldReadOfType(field_type, is_volatile),
5158 dex_pc),
5159 field_info_(field_offset,
5160 field_type,
5161 is_volatile,
5162 field_idx,
5163 declaring_class_def_index,
5164 dex_file,
5165 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005166 SetRawInputAt(0, cls);
5167 }
5168
Calin Juravle52c48962014-12-16 17:02:57 +00005169
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005170 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005171
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005172 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005173 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5174 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005175 }
5176
5177 size_t ComputeHashCode() const OVERRIDE {
5178 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5179 }
5180
Calin Juravle52c48962014-12-16 17:02:57 +00005181 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005182 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5183 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005184 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005185
5186 DECLARE_INSTRUCTION(StaticFieldGet);
5187
5188 private:
5189 const FieldInfo field_info_;
5190
5191 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5192};
5193
5194class HStaticFieldSet : public HTemplateInstruction<2> {
5195 public:
5196 HStaticFieldSet(HInstruction* cls,
5197 HInstruction* value,
5198 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005199 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005200 bool is_volatile,
5201 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005202 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005203 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005204 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005205 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005206 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5207 dex_pc),
5208 field_info_(field_offset,
5209 field_type,
5210 is_volatile,
5211 field_idx,
5212 declaring_class_def_index,
5213 dex_file,
5214 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005215 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005216 SetRawInputAt(0, cls);
5217 SetRawInputAt(1, value);
5218 }
5219
Calin Juravle52c48962014-12-16 17:02:57 +00005220 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005221 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5222 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005223 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005224
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005225 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005226 bool GetValueCanBeNull() const { return value_can_be_null_; }
5227 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005228
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005229 DECLARE_INSTRUCTION(StaticFieldSet);
5230
5231 private:
5232 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005233 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005234
5235 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5236};
5237
Calin Juravlee460d1d2015-09-29 04:52:17 +01005238class HUnresolvedInstanceFieldGet : public HExpression<1> {
5239 public:
5240 HUnresolvedInstanceFieldGet(HInstruction* obj,
5241 Primitive::Type field_type,
5242 uint32_t field_index,
5243 uint32_t dex_pc)
5244 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5245 field_index_(field_index) {
5246 SetRawInputAt(0, obj);
5247 }
5248
5249 bool NeedsEnvironment() const OVERRIDE { return true; }
5250 bool CanThrow() const OVERRIDE { return true; }
5251
5252 Primitive::Type GetFieldType() const { return GetType(); }
5253 uint32_t GetFieldIndex() const { return field_index_; }
5254
5255 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5256
5257 private:
5258 const uint32_t field_index_;
5259
5260 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5261};
5262
5263class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5264 public:
5265 HUnresolvedInstanceFieldSet(HInstruction* obj,
5266 HInstruction* value,
5267 Primitive::Type field_type,
5268 uint32_t field_index,
5269 uint32_t dex_pc)
5270 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5271 field_type_(field_type),
5272 field_index_(field_index) {
5273 DCHECK_EQ(field_type, value->GetType());
5274 SetRawInputAt(0, obj);
5275 SetRawInputAt(1, value);
5276 }
5277
5278 bool NeedsEnvironment() const OVERRIDE { return true; }
5279 bool CanThrow() const OVERRIDE { return true; }
5280
5281 Primitive::Type GetFieldType() const { return field_type_; }
5282 uint32_t GetFieldIndex() const { return field_index_; }
5283
5284 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5285
5286 private:
5287 const Primitive::Type field_type_;
5288 const uint32_t field_index_;
5289
5290 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5291};
5292
5293class HUnresolvedStaticFieldGet : public HExpression<0> {
5294 public:
5295 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5296 uint32_t field_index,
5297 uint32_t dex_pc)
5298 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5299 field_index_(field_index) {
5300 }
5301
5302 bool NeedsEnvironment() const OVERRIDE { return true; }
5303 bool CanThrow() const OVERRIDE { return true; }
5304
5305 Primitive::Type GetFieldType() const { return GetType(); }
5306 uint32_t GetFieldIndex() const { return field_index_; }
5307
5308 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5309
5310 private:
5311 const uint32_t field_index_;
5312
5313 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5314};
5315
5316class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5317 public:
5318 HUnresolvedStaticFieldSet(HInstruction* value,
5319 Primitive::Type field_type,
5320 uint32_t field_index,
5321 uint32_t dex_pc)
5322 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5323 field_type_(field_type),
5324 field_index_(field_index) {
5325 DCHECK_EQ(field_type, value->GetType());
5326 SetRawInputAt(0, value);
5327 }
5328
5329 bool NeedsEnvironment() const OVERRIDE { return true; }
5330 bool CanThrow() const OVERRIDE { return true; }
5331
5332 Primitive::Type GetFieldType() const { return field_type_; }
5333 uint32_t GetFieldIndex() const { return field_index_; }
5334
5335 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5336
5337 private:
5338 const Primitive::Type field_type_;
5339 const uint32_t field_index_;
5340
5341 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5342};
5343
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005344// Implement the move-exception DEX instruction.
5345class HLoadException : public HExpression<0> {
5346 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005347 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5348 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005349
David Brazdilbbd733e2015-08-18 17:48:17 +01005350 bool CanBeNull() const OVERRIDE { return false; }
5351
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005352 DECLARE_INSTRUCTION(LoadException);
5353
5354 private:
5355 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5356};
5357
David Brazdilcb1c0552015-08-04 16:22:25 +01005358// Implicit part of move-exception which clears thread-local exception storage.
5359// Must not be removed because the runtime expects the TLS to get cleared.
5360class HClearException : public HTemplateInstruction<0> {
5361 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005362 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5363 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005364
5365 DECLARE_INSTRUCTION(ClearException);
5366
5367 private:
5368 DISALLOW_COPY_AND_ASSIGN(HClearException);
5369};
5370
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005371class HThrow : public HTemplateInstruction<1> {
5372 public:
5373 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005374 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005375 SetRawInputAt(0, exception);
5376 }
5377
5378 bool IsControlFlow() const OVERRIDE { return true; }
5379
5380 bool NeedsEnvironment() const OVERRIDE { return true; }
5381
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005382 bool CanThrow() const OVERRIDE { return true; }
5383
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005384
5385 DECLARE_INSTRUCTION(Throw);
5386
5387 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005388 DISALLOW_COPY_AND_ASSIGN(HThrow);
5389};
5390
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005391/**
5392 * Implementation strategies for the code generator of a HInstanceOf
5393 * or `HCheckCast`.
5394 */
5395enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005396 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005397 kExactCheck, // Can do a single class compare.
5398 kClassHierarchyCheck, // Can just walk the super class chain.
5399 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5400 kInterfaceCheck, // No optimization yet when checking against an interface.
5401 kArrayObjectCheck, // Can just check if the array is not primitive.
5402 kArrayCheck // No optimization yet when checking against a generic array.
5403};
5404
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005405class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005406 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005407 HInstanceOf(HInstruction* object,
5408 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005409 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005410 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005411 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005412 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005413 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005414 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005415 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005416 SetRawInputAt(0, object);
5417 SetRawInputAt(1, constant);
5418 }
5419
5420 bool CanBeMoved() const OVERRIDE { return true; }
5421
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005422 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005423 return true;
5424 }
5425
5426 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005427 return false;
5428 }
5429
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005430 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5431
5432 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005433
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005434 // Used only in code generation.
5435 bool MustDoNullCheck() const { return must_do_null_check_; }
5436 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5437
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005438 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5439 return (check_kind == TypeCheckKind::kExactCheck)
5440 ? SideEffects::None()
5441 // Mips currently does runtime calls for any other checks.
5442 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005443 }
5444
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005445 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005446
5447 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005448 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005449 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005450
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005451 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5452};
5453
Calin Juravleb1498f62015-02-16 13:13:29 +00005454class HBoundType : public HExpression<1> {
5455 public:
David Brazdilf5552582015-12-27 13:36:12 +00005456 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005457 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
David Brazdilf5552582015-12-27 13:36:12 +00005458 upper_bound_(ReferenceTypeInfo::CreateInvalid()),
5459 upper_can_be_null_(true),
5460 can_be_null_(true) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005461 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005462 SetRawInputAt(0, input);
5463 }
5464
David Brazdilf5552582015-12-27 13:36:12 +00005465 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005466 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5467 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
David Brazdilf5552582015-12-27 13:36:12 +00005468 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005469
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005470 void SetCanBeNull(bool can_be_null) {
5471 DCHECK(upper_can_be_null_ || !can_be_null);
5472 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005473 }
5474
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005475 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5476
Calin Juravleb1498f62015-02-16 13:13:29 +00005477 DECLARE_INSTRUCTION(BoundType);
5478
5479 private:
5480 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005481 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5482 // It is used to bound the type in cases like:
5483 // if (x instanceof ClassX) {
5484 // // uper_bound_ will be ClassX
5485 // }
David Brazdilf5552582015-12-27 13:36:12 +00005486 ReferenceTypeInfo upper_bound_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005487 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5488 // is false then can_be_null_ cannot be true).
David Brazdilf5552582015-12-27 13:36:12 +00005489 bool upper_can_be_null_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005490 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005491
5492 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5493};
5494
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005495class HCheckCast : public HTemplateInstruction<2> {
5496 public:
5497 HCheckCast(HInstruction* object,
5498 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005499 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005500 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005501 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005502 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005503 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005504 SetRawInputAt(0, object);
5505 SetRawInputAt(1, constant);
5506 }
5507
5508 bool CanBeMoved() const OVERRIDE { return true; }
5509
5510 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5511 return true;
5512 }
5513
5514 bool NeedsEnvironment() const OVERRIDE {
5515 // Instruction may throw a CheckCastError.
5516 return true;
5517 }
5518
5519 bool CanThrow() const OVERRIDE { return true; }
5520
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005521 bool MustDoNullCheck() const { return must_do_null_check_; }
5522 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005523 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005524
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005525 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005526
5527 DECLARE_INSTRUCTION(CheckCast);
5528
5529 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005530 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005531 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005532
5533 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005534};
5535
Calin Juravle27df7582015-04-17 19:12:31 +01005536class HMemoryBarrier : public HTemplateInstruction<0> {
5537 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005538 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005539 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005540 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005541 barrier_kind_(barrier_kind) {}
5542
5543 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5544
5545 DECLARE_INSTRUCTION(MemoryBarrier);
5546
5547 private:
5548 const MemBarrierKind barrier_kind_;
5549
5550 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5551};
5552
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005553class HMonitorOperation : public HTemplateInstruction<1> {
5554 public:
5555 enum OperationKind {
5556 kEnter,
5557 kExit,
5558 };
5559
5560 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005561 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005562 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5563 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005564 SetRawInputAt(0, object);
5565 }
5566
5567 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005568 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5569
5570 bool CanThrow() const OVERRIDE {
5571 // Verifier guarantees that monitor-exit cannot throw.
5572 // This is important because it allows the HGraphBuilder to remove
5573 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5574 return IsEnter();
5575 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005576
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005577
5578 bool IsEnter() const { return kind_ == kEnter; }
5579
5580 DECLARE_INSTRUCTION(MonitorOperation);
5581
Calin Juravle52c48962014-12-16 17:02:57 +00005582 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005583 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005584
5585 private:
5586 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5587};
5588
Vladimir Markof9f64412015-09-02 14:05:49 +01005589class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005590 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005591 MoveOperands(Location source,
5592 Location destination,
5593 Primitive::Type type,
5594 HInstruction* instruction)
5595 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005596
5597 Location GetSource() const { return source_; }
5598 Location GetDestination() const { return destination_; }
5599
5600 void SetSource(Location value) { source_ = value; }
5601 void SetDestination(Location value) { destination_ = value; }
5602
5603 // The parallel move resolver marks moves as "in-progress" by clearing the
5604 // destination (but not the source).
5605 Location MarkPending() {
5606 DCHECK(!IsPending());
5607 Location dest = destination_;
5608 destination_ = Location::NoLocation();
5609 return dest;
5610 }
5611
5612 void ClearPending(Location dest) {
5613 DCHECK(IsPending());
5614 destination_ = dest;
5615 }
5616
5617 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00005618 DCHECK(source_.IsValid() || destination_.IsInvalid());
5619 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005620 }
5621
5622 // True if this blocks a move from the given location.
5623 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005624 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005625 }
5626
5627 // A move is redundant if it's been eliminated, if its source and
5628 // destination are the same, or if its destination is unneeded.
5629 bool IsRedundant() const {
5630 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5631 }
5632
5633 // We clear both operands to indicate move that's been eliminated.
5634 void Eliminate() {
5635 source_ = destination_ = Location::NoLocation();
5636 }
5637
5638 bool IsEliminated() const {
5639 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5640 return source_.IsInvalid();
5641 }
5642
Alexey Frunze4dda3372015-06-01 18:31:49 -07005643 Primitive::Type GetType() const { return type_; }
5644
Nicolas Geoffray90218252015-04-15 11:56:51 +01005645 bool Is64BitMove() const {
5646 return Primitive::Is64BitType(type_);
5647 }
5648
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005649 HInstruction* GetInstruction() const { return instruction_; }
5650
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005651 private:
5652 Location source_;
5653 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005654 // The type this move is for.
5655 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005656 // The instruction this move is assocatied with. Null when this move is
5657 // for moving an input in the expected locations of user (including a phi user).
5658 // This is only used in debug mode, to ensure we do not connect interval siblings
5659 // in the same parallel move.
5660 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005661};
5662
Roland Levillainc9285912015-12-18 10:38:42 +00005663std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
5664
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005665static constexpr size_t kDefaultNumberOfMoves = 4;
5666
5667class HParallelMove : public HTemplateInstruction<0> {
5668 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005669 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005670 : HTemplateInstruction(SideEffects::None(), dex_pc),
5671 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5672 moves_.reserve(kDefaultNumberOfMoves);
5673 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005674
Nicolas Geoffray90218252015-04-15 11:56:51 +01005675 void AddMove(Location source,
5676 Location destination,
5677 Primitive::Type type,
5678 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005679 DCHECK(source.IsValid());
5680 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005681 if (kIsDebugBuild) {
5682 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005683 for (const MoveOperands& move : moves_) {
5684 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005685 // Special case the situation where the move is for the spill slot
5686 // of the instruction.
5687 if ((GetPrevious() == instruction)
5688 || ((GetPrevious() == nullptr)
5689 && instruction->IsPhi()
5690 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005691 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005692 << "Doing parallel moves for the same instruction.";
5693 } else {
5694 DCHECK(false) << "Doing parallel moves for the same instruction.";
5695 }
5696 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005697 }
5698 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005699 for (const MoveOperands& move : moves_) {
5700 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005701 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005702 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005703 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005704 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005705 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005706 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005707 }
5708
Vladimir Marko225b6462015-09-28 12:17:40 +01005709 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005710 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005711 }
5712
Vladimir Marko225b6462015-09-28 12:17:40 +01005713 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005714
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005715 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005716
5717 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005718 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005719
5720 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5721};
5722
Mark Mendell0616ae02015-04-17 12:49:27 -04005723} // namespace art
5724
Vladimir Markob4536b72015-11-24 13:45:23 +00005725#ifdef ART_ENABLE_CODEGEN_arm
5726#include "nodes_arm.h"
5727#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005728#ifdef ART_ENABLE_CODEGEN_arm64
5729#include "nodes_arm64.h"
5730#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005731#ifdef ART_ENABLE_CODEGEN_x86
5732#include "nodes_x86.h"
5733#endif
5734
5735namespace art {
5736
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005737class HGraphVisitor : public ValueObject {
5738 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005739 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5740 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005741
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005742 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005743 virtual void VisitBasicBlock(HBasicBlock* block);
5744
Roland Levillain633021e2014-10-01 14:12:25 +01005745 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005746 void VisitInsertionOrder();
5747
Roland Levillain633021e2014-10-01 14:12:25 +01005748 // Visit the graph following dominator tree reverse post-order.
5749 void VisitReversePostOrder();
5750
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005751 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005752
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005753 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005754#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005755 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5756
5757 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5758
5759#undef DECLARE_VISIT_INSTRUCTION
5760
5761 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005762 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005763
5764 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5765};
5766
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005767class HGraphDelegateVisitor : public HGraphVisitor {
5768 public:
5769 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5770 virtual ~HGraphDelegateVisitor() {}
5771
5772 // Visit functions that delegate to to super class.
5773#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005774 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005775
5776 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5777
5778#undef DECLARE_VISIT_INSTRUCTION
5779
5780 private:
5781 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5782};
5783
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005784class HInsertionOrderIterator : public ValueObject {
5785 public:
5786 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5787
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005788 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005789 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005790 void Advance() { ++index_; }
5791
5792 private:
5793 const HGraph& graph_;
5794 size_t index_;
5795
5796 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5797};
5798
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005799class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005800 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005801 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5802 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005803 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005804 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005805
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005806 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5807 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005808 void Advance() { ++index_; }
5809
5810 private:
5811 const HGraph& graph_;
5812 size_t index_;
5813
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005814 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005815};
5816
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005817class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005818 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005819 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005820 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005821 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005822 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005823 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005824
5825 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005826 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005827 void Advance() { --index_; }
5828
5829 private:
5830 const HGraph& graph_;
5831 size_t index_;
5832
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005833 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005834};
5835
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005836class HLinearPostOrderIterator : public ValueObject {
5837 public:
5838 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005839 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005840
5841 bool Done() const { return index_ == 0; }
5842
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005843 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005844
5845 void Advance() {
5846 --index_;
5847 DCHECK_GE(index_, 0U);
5848 }
5849
5850 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005851 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005852 size_t index_;
5853
5854 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5855};
5856
5857class HLinearOrderIterator : public ValueObject {
5858 public:
5859 explicit HLinearOrderIterator(const HGraph& graph)
5860 : order_(graph.GetLinearOrder()), index_(0) {}
5861
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005862 bool Done() const { return index_ == order_.size(); }
5863 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005864 void Advance() { ++index_; }
5865
5866 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005867 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005868 size_t index_;
5869
5870 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5871};
5872
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005873// Iterator over the blocks that art part of the loop. Includes blocks part
5874// of an inner loop. The order in which the blocks are iterated is on their
5875// block id.
5876class HBlocksInLoopIterator : public ValueObject {
5877 public:
5878 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5879 : blocks_in_loop_(info.GetBlocks()),
5880 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5881 index_(0) {
5882 if (!blocks_in_loop_.IsBitSet(index_)) {
5883 Advance();
5884 }
5885 }
5886
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005887 bool Done() const { return index_ == blocks_.size(); }
5888 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005889 void Advance() {
5890 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005891 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005892 if (blocks_in_loop_.IsBitSet(index_)) {
5893 break;
5894 }
5895 }
5896 }
5897
5898 private:
5899 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005900 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005901 size_t index_;
5902
5903 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5904};
5905
Mingyao Yang3584bce2015-05-19 16:01:59 -07005906// Iterator over the blocks that art part of the loop. Includes blocks part
5907// of an inner loop. The order in which the blocks are iterated is reverse
5908// post order.
5909class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5910 public:
5911 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5912 : blocks_in_loop_(info.GetBlocks()),
5913 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5914 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005915 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005916 Advance();
5917 }
5918 }
5919
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005920 bool Done() const { return index_ == blocks_.size(); }
5921 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005922 void Advance() {
5923 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005924 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5925 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005926 break;
5927 }
5928 }
5929 }
5930
5931 private:
5932 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005933 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005934 size_t index_;
5935
5936 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5937};
5938
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005939inline int64_t Int64FromConstant(HConstant* constant) {
5940 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5941 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5942 : constant->AsLongConstant()->GetValue();
5943}
5944
Vladimir Marko58155012015-08-19 12:49:41 +00005945inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5946 // For the purposes of the compiler, the dex files must actually be the same object
5947 // if we want to safely treat them as the same. This is especially important for JIT
5948 // as custom class loaders can open the same underlying file (or memory) multiple
5949 // times and provide different class resolution but no two class loaders should ever
5950 // use the same DexFile object - doing so is an unsupported hack that can lead to
5951 // all sorts of weird failures.
5952 return &lhs == &rhs;
5953}
5954
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005955#define INSTRUCTION_TYPE_CHECK(type, super) \
5956 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
5957 inline const H##type* HInstruction::As##type() const { \
5958 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
5959 } \
5960 inline H##type* HInstruction::As##type() { \
5961 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
5962 }
5963
5964 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
5965#undef INSTRUCTION_TYPE_CHECK
5966
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005967} // namespace art
5968
5969#endif // ART_COMPILER_OPTIMIZING_NODES_H_