blob: e222ef72607e71f21193fe7220a5dd2bc5f54e2e [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
Vladimir Marko60584552015-09-03 13:35:12 +000027#include "base/stl_util.h"
Calin Juravle27df7582015-04-17 19:12:31 +010028#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000029#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000030#include "handle.h"
31#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000032#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010033#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000034#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010036#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "primitive.h"
David Brazdild26a4112015-11-10 11:07:31 +000038#include "utils/array_ref.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000039
40namespace art {
41
David Brazdil1abb4192015-02-17 18:33:36 +000042class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010046class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000047class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000048class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000049class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000051class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000052class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000054class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010056class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010057class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000059class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010060class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062
Mathieu Chartier736b5602015-09-02 14:54:11 -070063namespace mirror {
64class DexCache;
65} // namespace mirror
66
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067static const int kDefaultNumberOfBlocks = 8;
68static const int kDefaultNumberOfSuccessors = 2;
69static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010070static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010071static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000072static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Calin Juravle9aec02f2014-11-18 23:06:35 +000074static constexpr uint32_t kMaxIntShiftValue = 0x1f;
75static constexpr uint64_t kMaxLongShiftValue = 0x3f;
76
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010077static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070078static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010079
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010080static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
81
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060082static constexpr uint32_t kNoDexPc = -1;
83
Dave Allison20dfc792014-06-16 20:44:29 -070084enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -070085 // All types.
86 kCondEQ, // ==
87 kCondNE, // !=
88 // Signed integers and floating-point numbers.
89 kCondLT, // <
90 kCondLE, // <=
91 kCondGT, // >
92 kCondGE, // >=
93 // Unsigned integers.
94 kCondB, // <
95 kCondBE, // <=
96 kCondA, // >
97 kCondAE, // >=
Dave Allison20dfc792014-06-16 20:44:29 -070098};
99
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000100enum GraphAnalysisResult {
101 kAnalysisFailThrowCatchLoop,
102 kAnalysisFailAmbiguousArrayOp,
103 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000104};
105
Vladimir Markof9f64412015-09-02 14:05:49 +0100106class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100107 public:
108 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
109
110 void AddInstruction(HInstruction* instruction);
111 void RemoveInstruction(HInstruction* instruction);
112
David Brazdilc3d743f2015-04-22 13:40:50 +0100113 // Insert `instruction` before/after an existing instruction `cursor`.
114 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
115 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
116
Roland Levillain6b469232014-09-25 10:10:38 +0100117 // Return true if this list contains `instruction`.
118 bool Contains(HInstruction* instruction) const;
119
Roland Levillainccc07a92014-09-16 14:48:16 +0100120 // Return true if `instruction1` is found before `instruction2` in
121 // this instruction list and false otherwise. Abort if none
122 // of these instructions is found.
123 bool FoundBefore(const HInstruction* instruction1,
124 const HInstruction* instruction2) const;
125
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000126 bool IsEmpty() const { return first_instruction_ == nullptr; }
127 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
128
129 // Update the block of all instructions to be `block`.
130 void SetBlockOfInstructions(HBasicBlock* block) const;
131
132 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
133 void Add(const HInstructionList& instruction_list);
134
David Brazdil2d7352b2015-04-20 14:52:42 +0100135 // Return the number of instructions in the list. This is an expensive operation.
136 size_t CountSize() const;
137
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100138 private:
139 HInstruction* first_instruction_;
140 HInstruction* last_instruction_;
141
142 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000143 friend class HGraph;
144 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100145 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100146 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100147
148 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
149};
150
David Brazdil4833f5a2015-12-16 10:37:39 +0000151class ReferenceTypeInfo : ValueObject {
152 public:
153 typedef Handle<mirror::Class> TypeHandle;
154
155 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
156 // The constructor will check that the type_handle is valid.
157 return ReferenceTypeInfo(type_handle, is_exact);
158 }
159
160 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
161
162 static bool IsValidHandle(TypeHandle handle) SHARED_REQUIRES(Locks::mutator_lock_) {
163 return handle.GetReference() != nullptr;
164 }
165
166 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
167 return IsValidHandle(type_handle_);
168 }
169
170 bool IsExact() const { return is_exact_; }
171
172 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
173 DCHECK(IsValid());
174 return GetTypeHandle()->IsObjectClass();
175 }
176
177 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
178 DCHECK(IsValid());
179 return GetTypeHandle()->IsStringClass();
180 }
181
182 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
183 DCHECK(IsValid());
184 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
185 }
186
187 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
188 DCHECK(IsValid());
189 return GetTypeHandle()->IsInterface();
190 }
191
192 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
193 DCHECK(IsValid());
194 return GetTypeHandle()->IsArrayClass();
195 }
196
197 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
198 DCHECK(IsValid());
199 return GetTypeHandle()->IsPrimitiveArray();
200 }
201
202 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
203 DCHECK(IsValid());
204 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
205 }
206
207 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
208 DCHECK(IsValid());
209 if (!IsExact()) return false;
210 if (!IsArrayClass()) return false;
211 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
212 }
213
214 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
215 DCHECK(IsValid());
216 if (!IsExact()) return false;
217 if (!IsArrayClass()) return false;
218 if (!rti.IsArrayClass()) return false;
219 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
220 rti.GetTypeHandle()->GetComponentType());
221 }
222
223 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
224
225 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
226 DCHECK(IsValid());
227 DCHECK(rti.IsValid());
228 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
229 }
230
231 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
232 DCHECK(IsValid());
233 DCHECK(rti.IsValid());
234 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
235 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
236 }
237
238 // Returns true if the type information provide the same amount of details.
239 // Note that it does not mean that the instructions have the same actual type
240 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000241 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000242 if (!IsValid() && !rti.IsValid()) {
243 // Invalid types are equal.
244 return true;
245 }
246 if (!IsValid() || !rti.IsValid()) {
247 // One is valid, the other not.
248 return false;
249 }
250 return IsExact() == rti.IsExact()
251 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
252 }
253
254 private:
255 ReferenceTypeInfo();
256 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
257
258 // The class of the object.
259 TypeHandle type_handle_;
260 // Whether or not the type is exact or a superclass of the actual type.
261 // Whether or not we have any information about this type.
262 bool is_exact_;
263};
264
265std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
266
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000267// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100268class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000269 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100270 HGraph(ArenaAllocator* arena,
271 const DexFile& dex_file,
272 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100273 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700274 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100275 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100276 bool debuggable = false,
277 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000278 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100279 blocks_(arena->Adapter(kArenaAllocBlockList)),
280 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
281 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700282 entry_block_(nullptr),
283 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100284 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100285 number_of_vregs_(0),
286 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000287 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400288 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000289 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000290 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000291 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000292 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100293 dex_file_(dex_file),
294 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100295 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100296 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100297 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700298 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000299 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100300 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
301 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
302 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
303 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000304 cached_current_method_(nullptr),
305 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100306 blocks_.reserve(kDefaultNumberOfBlocks);
307 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000308
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000309 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100310 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
311
David Brazdil69ba7b72015-06-23 18:27:30 +0100312 bool IsInSsaForm() const { return in_ssa_form_; }
313
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000314 HBasicBlock* GetEntryBlock() const { return entry_block_; }
315 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100316 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000317
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000318 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
319 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000320
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000321 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100322
David Brazdil4833f5a2015-12-16 10:37:39 +0000323 // Try building the SSA form of this graph, with dominance computation and
324 // loop recognition. Returns a code specifying that it was successful or the
325 // reason for failure.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000326 GraphAnalysisResult TryBuildingSsa(StackHandleScopeCollection* handles);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000327
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100328 void ComputeDominanceInformation();
329 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000330 void ClearLoopInformation();
331 void FindBackEdges(ArenaBitVector* visited);
332 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100333 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100334 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000335
David Brazdil4833f5a2015-12-16 10:37:39 +0000336 // Analyze all natural loops in this graph. Returns a code specifying that it
337 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000338 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000339 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100340
David Brazdilffee3d32015-07-06 11:48:53 +0100341 // Iterate over blocks to compute try block membership. Needs reverse post
342 // order and loop information.
343 void ComputeTryBlockInformation();
344
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000345 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000346 // Returns the instruction used to replace the invoke expression or null if the
347 // invoke is for a void method.
348 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000349
Mingyao Yang3584bce2015-05-19 16:01:59 -0700350 // Need to add a couple of blocks to test if the loop body is entered and
351 // put deoptimization instructions, etc.
352 void TransformLoopHeaderForBCE(HBasicBlock* header);
353
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000354 // Removes `block` from the graph. Assumes `block` has been disconnected from
355 // other blocks and has no instructions or phis.
356 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000357
David Brazdilfc6a86a2015-06-26 10:33:45 +0000358 // Splits the edge between `block` and `successor` while preserving the
359 // indices in the predecessor/successor lists. If there are multiple edges
360 // between the blocks, the lowest indices are used.
361 // Returns the new block which is empty and has the same dex pc as `successor`.
362 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
363
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100364 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
365 void SimplifyLoop(HBasicBlock* header);
366
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000367 int32_t GetNextInstructionId() {
368 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000369 return current_instruction_id_++;
370 }
371
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000372 int32_t GetCurrentInstructionId() const {
373 return current_instruction_id_;
374 }
375
376 void SetCurrentInstructionId(int32_t id) {
377 current_instruction_id_ = id;
378 }
379
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100380 uint16_t GetMaximumNumberOfOutVRegs() const {
381 return maximum_number_of_out_vregs_;
382 }
383
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000384 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
385 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100386 }
387
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100388 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
389 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
390 }
391
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000392 void UpdateTemporariesVRegSlots(size_t slots) {
393 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100394 }
395
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000396 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100397 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000398 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100399 }
400
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100401 void SetNumberOfVRegs(uint16_t number_of_vregs) {
402 number_of_vregs_ = number_of_vregs;
403 }
404
405 uint16_t GetNumberOfVRegs() const {
406 return number_of_vregs_;
407 }
408
409 void SetNumberOfInVRegs(uint16_t value) {
410 number_of_in_vregs_ = value;
411 }
412
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100413 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100414 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100415 return number_of_vregs_ - number_of_in_vregs_;
416 }
417
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100418 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100419 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100420 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100421
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100422 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100423 return linear_order_;
424 }
425
Mark Mendell1152c922015-04-24 17:06:35 -0400426 bool HasBoundsChecks() const {
427 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800428 }
429
Mark Mendell1152c922015-04-24 17:06:35 -0400430 void SetHasBoundsChecks(bool value) {
431 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800432 }
433
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100434 bool ShouldGenerateConstructorBarrier() const {
435 return should_generate_constructor_barrier_;
436 }
437
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000438 bool IsDebuggable() const { return debuggable_; }
439
David Brazdil8d5b8b22015-03-24 10:51:52 +0000440 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000441 // already, it is created and inserted into the graph. This method is only for
442 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600443 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000444
445 // TODO: This is problematic for the consistency of reference type propagation
446 // because it can be created anytime after the pass and thus it will be left
447 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600448 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000449
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600450 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
451 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000452 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600453 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
454 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000455 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600456 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
457 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000458 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600459 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
460 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000461 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000462
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100463 HCurrentMethod* GetCurrentMethod();
464
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100465 const DexFile& GetDexFile() const {
466 return dex_file_;
467 }
468
469 uint32_t GetMethodIdx() const {
470 return method_idx_;
471 }
472
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100473 InvokeType GetInvokeType() const {
474 return invoke_type_;
475 }
476
Mark Mendellc4701932015-04-10 13:18:51 -0400477 InstructionSet GetInstructionSet() const {
478 return instruction_set_;
479 }
480
David Brazdil77a48ae2015-09-15 12:34:04 +0000481 bool HasTryCatch() const { return has_try_catch_; }
482 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100483
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000484 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
485 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
486
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100487 ArtMethod* GetArtMethod() const { return art_method_; }
488 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
489
Mark Mendellf6529172015-11-17 11:16:56 -0500490 // Returns an instruction with the opposite boolean value from 'cond'.
491 // The instruction has been inserted into the graph, either as a constant, or
492 // before cursor.
493 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
494
David Brazdil2d7352b2015-04-20 14:52:42 +0100495 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000496 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100497 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000498
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000499 template <class InstructionType, typename ValueType>
500 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600501 ArenaSafeMap<ValueType, InstructionType*>* cache,
502 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000503 // Try to find an existing constant of the given value.
504 InstructionType* constant = nullptr;
505 auto cached_constant = cache->find(value);
506 if (cached_constant != cache->end()) {
507 constant = cached_constant->second;
508 }
509
510 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100511 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000512 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600513 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000514 cache->Overwrite(value, constant);
515 InsertConstant(constant);
516 }
517 return constant;
518 }
519
David Brazdil8d5b8b22015-03-24 10:51:52 +0000520 void InsertConstant(HConstant* instruction);
521
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000522 // Cache a float constant into the graph. This method should only be
523 // called by the SsaBuilder when creating "equivalent" instructions.
524 void CacheFloatConstant(HFloatConstant* constant);
525
526 // See CacheFloatConstant comment.
527 void CacheDoubleConstant(HDoubleConstant* constant);
528
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000529 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000530
531 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100532 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000533
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100534 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100535 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000536
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100537 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100538 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100539
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000540 HBasicBlock* entry_block_;
541 HBasicBlock* exit_block_;
542
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100543 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100544 uint16_t maximum_number_of_out_vregs_;
545
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100546 // The number of virtual registers in this method. Contains the parameters.
547 uint16_t number_of_vregs_;
548
549 // The number of virtual registers used by parameters of this method.
550 uint16_t number_of_in_vregs_;
551
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000552 // Number of vreg size slots that the temporaries use (used in baseline compiler).
553 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100554
Mark Mendell1152c922015-04-24 17:06:35 -0400555 // Has bounds checks. We can totally skip BCE if it's false.
556 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800557
David Brazdil77a48ae2015-09-15 12:34:04 +0000558 // Flag whether there are any try/catch blocks in the graph. We will skip
559 // try/catch-related passes if false.
560 bool has_try_catch_;
561
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000562 // Flag whether there are any irreducible loops in the graph.
563 bool has_irreducible_loops_;
564
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000565 // Indicates whether the graph should be compiled in a way that
566 // ensures full debuggability. If false, we can apply more
567 // aggressive optimizations that may limit the level of debugging.
568 const bool debuggable_;
569
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000570 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000571 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000572
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100573 // The dex file from which the method is from.
574 const DexFile& dex_file_;
575
576 // The method index in the dex file.
577 const uint32_t method_idx_;
578
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100579 // If inlined, this encodes how the callee is being invoked.
580 const InvokeType invoke_type_;
581
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100582 // Whether the graph has been transformed to SSA form. Only used
583 // in debug mode to ensure we are not using properties only valid
584 // for non-SSA form (like the number of temporaries).
585 bool in_ssa_form_;
586
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100587 const bool should_generate_constructor_barrier_;
588
Mathieu Chartiere401d142015-04-22 13:56:20 -0700589 const InstructionSet instruction_set_;
590
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000591 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000592 HNullConstant* cached_null_constant_;
593 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000594 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000595 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000596 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000597
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100598 HCurrentMethod* cached_current_method_;
599
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100600 // The ArtMethod this graph is for. Note that for AOT, it may be null,
601 // for example for methods whose declaring class could not be resolved
602 // (such as when the superclass could not be found).
603 ArtMethod* art_method_;
604
David Brazdil4833f5a2015-12-16 10:37:39 +0000605 // Keep the RTI of inexact Object to avoid having to pass stack handle
606 // collection pointer to passes which may create NullConstant.
607 ReferenceTypeInfo inexact_object_rti_;
608
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000609 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100610 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000611 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000612 DISALLOW_COPY_AND_ASSIGN(HGraph);
613};
614
Vladimir Markof9f64412015-09-02 14:05:49 +0100615class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000616 public:
617 HLoopInformation(HBasicBlock* header, HGraph* graph)
618 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100619 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000620 irreducible_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100621 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100622 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100623 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
624 back_edges_.reserve(kDefaultNumberOfBackEdges);
625 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100626
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000627 bool IsIrreducible() const { return irreducible_; }
628
629 void Dump(std::ostream& os);
630
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100631 HBasicBlock* GetHeader() const {
632 return header_;
633 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000634
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000635 void SetHeader(HBasicBlock* block) {
636 header_ = block;
637 }
638
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100639 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
640 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
641 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
642
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000643 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100644 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000645 }
646
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100647 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100648 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100649 }
650
David Brazdil46e2a392015-03-16 17:31:52 +0000651 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100652 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100653 }
654
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000655 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100656 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000657 }
658
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100659 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100660
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100661 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100662 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100663 }
664
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100665 // Returns the lifetime position of the back edge that has the
666 // greatest lifetime position.
667 size_t GetLifetimeEnd() const;
668
669 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100670 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100671 }
672
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000673 // Finds blocks that are part of this loop.
674 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100675
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100676 // Returns whether this loop information contains `block`.
677 // Note that this loop information *must* be populated before entering this function.
678 bool Contains(const HBasicBlock& block) const;
679
680 // Returns whether this loop information is an inner loop of `other`.
681 // Note that `other` *must* be populated before entering this function.
682 bool IsIn(const HLoopInformation& other) const;
683
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800684 // Returns true if instruction is not defined within this loop.
685 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700686
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100687 const ArenaBitVector& GetBlocks() const { return blocks_; }
688
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000689 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000690 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000691
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000692 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100693 // Internal recursive implementation of `Populate`.
694 void PopulateRecursive(HBasicBlock* block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000695 void PopulateIrreducibleRecursive(HBasicBlock* block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100696
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000697 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100698 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000699 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100700 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100701 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000702
703 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
704};
705
David Brazdilec16f792015-08-19 15:04:01 +0100706// Stores try/catch information for basic blocks.
707// Note that HGraph is constructed so that catch blocks cannot simultaneously
708// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100709class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100710 public:
711 // Try block information constructor.
712 explicit TryCatchInformation(const HTryBoundary& try_entry)
713 : try_entry_(&try_entry),
714 catch_dex_file_(nullptr),
715 catch_type_index_(DexFile::kDexNoIndex16) {
716 DCHECK(try_entry_ != nullptr);
717 }
718
719 // Catch block information constructor.
720 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
721 : try_entry_(nullptr),
722 catch_dex_file_(&dex_file),
723 catch_type_index_(catch_type_index) {}
724
725 bool IsTryBlock() const { return try_entry_ != nullptr; }
726
727 const HTryBoundary& GetTryEntry() const {
728 DCHECK(IsTryBlock());
729 return *try_entry_;
730 }
731
732 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
733
734 bool IsCatchAllTypeIndex() const {
735 DCHECK(IsCatchBlock());
736 return catch_type_index_ == DexFile::kDexNoIndex16;
737 }
738
739 uint16_t GetCatchTypeIndex() const {
740 DCHECK(IsCatchBlock());
741 return catch_type_index_;
742 }
743
744 const DexFile& GetCatchDexFile() const {
745 DCHECK(IsCatchBlock());
746 return *catch_dex_file_;
747 }
748
749 private:
750 // One of possibly several TryBoundary instructions entering the block's try.
751 // Only set for try blocks.
752 const HTryBoundary* try_entry_;
753
754 // Exception type information. Only set for catch blocks.
755 const DexFile* catch_dex_file_;
756 const uint16_t catch_type_index_;
757};
758
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100759static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100760static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100761
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000762// A block in a method. Contains the list of instructions represented
763// as a double linked list. Each block knows its predecessors and
764// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100765
Vladimir Markof9f64412015-09-02 14:05:49 +0100766class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000767 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600768 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000769 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000770 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
771 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000772 loop_information_(nullptr),
773 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000774 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100775 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100776 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100777 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000778 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000779 try_catch_information_(nullptr) {
780 predecessors_.reserve(kDefaultNumberOfPredecessors);
781 successors_.reserve(kDefaultNumberOfSuccessors);
782 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
783 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000784
Vladimir Marko60584552015-09-03 13:35:12 +0000785 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100786 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000787 }
788
Vladimir Marko60584552015-09-03 13:35:12 +0000789 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100790 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000791 }
792
David Brazdild26a4112015-11-10 11:07:31 +0000793 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
794 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
795
Vladimir Marko60584552015-09-03 13:35:12 +0000796 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
797 return ContainsElement(successors_, block, start_from);
798 }
799
800 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100801 return dominated_blocks_;
802 }
803
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100804 bool IsEntryBlock() const {
805 return graph_->GetEntryBlock() == this;
806 }
807
808 bool IsExitBlock() const {
809 return graph_->GetExitBlock() == this;
810 }
811
David Brazdil46e2a392015-03-16 17:31:52 +0000812 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000813 bool IsSingleTryBoundary() const;
814
815 // Returns true if this block emits nothing but a jump.
816 bool IsSingleJump() const {
817 HLoopInformation* loop_info = GetLoopInformation();
818 return (IsSingleGoto() || IsSingleTryBoundary())
819 // Back edges generate a suspend check.
820 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
821 }
David Brazdil46e2a392015-03-16 17:31:52 +0000822
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000823 void AddBackEdge(HBasicBlock* back_edge) {
824 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000825 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000826 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100827 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000828 loop_information_->AddBackEdge(back_edge);
829 }
830
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000831 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000832 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000833
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100834 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000835 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600836 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000837
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000838 HBasicBlock* GetDominator() const { return dominator_; }
839 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000840 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
841
842 void RemoveDominatedBlock(HBasicBlock* block) {
843 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100844 }
Vladimir Marko60584552015-09-03 13:35:12 +0000845
846 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
847 ReplaceElement(dominated_blocks_, existing, new_block);
848 }
849
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100850 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000851
852 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100853 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000854 }
855
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100856 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
857 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100858 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100859 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100860 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
861 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000862
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000863 HInstruction* GetFirstInstructionDisregardMoves() const;
864
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000865 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000866 successors_.push_back(block);
867 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000868 }
869
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100870 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
871 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100872 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000873 new_block->predecessors_.push_back(this);
874 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000875 }
876
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000877 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
878 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000879 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000880 new_block->successors_.push_back(this);
881 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000882 }
883
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100884 // Insert `this` between `predecessor` and `successor. This method
885 // preserves the indicies, and will update the first edge found between
886 // `predecessor` and `successor`.
887 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
888 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100889 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000890 successor->predecessors_[predecessor_index] = this;
891 predecessor->successors_[successor_index] = this;
892 successors_.push_back(successor);
893 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100894 }
895
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100896 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000897 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100898 }
899
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000900 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000901 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000902 }
903
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100904 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000905 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100906 }
907
908 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000909 predecessors_.push_back(block);
910 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100911 }
912
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100913 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000914 DCHECK_EQ(predecessors_.size(), 2u);
915 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100916 }
917
David Brazdil769c9e52015-04-27 13:54:09 +0100918 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000919 DCHECK_EQ(successors_.size(), 2u);
920 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100921 }
922
David Brazdilfc6a86a2015-06-26 10:33:45 +0000923 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000924 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100925 }
926
David Brazdilfc6a86a2015-06-26 10:33:45 +0000927 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000928 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100929 }
930
David Brazdilfc6a86a2015-06-26 10:33:45 +0000931 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000932 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100933 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000934 }
935
936 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000937 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100938 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000939 }
940
941 // Returns whether the first occurrence of `predecessor` in the list of
942 // predecessors is at index `idx`.
943 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100944 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000945 return GetPredecessorIndexOf(predecessor) == idx;
946 }
947
David Brazdild7558da2015-09-22 13:04:14 +0100948 // Create a new block between this block and its predecessors. The new block
949 // is added to the graph, all predecessor edges are relinked to it and an edge
950 // is created to `this`. Returns the new empty block. Reverse post order or
951 // loop and try/catch information are not updated.
952 HBasicBlock* CreateImmediateDominator();
953
David Brazdilfc6a86a2015-06-26 10:33:45 +0000954 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100955 // created, latter block. Note that this method will add the block to the
956 // graph, create a Goto at the end of the former block and will create an edge
957 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100958 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000959 HBasicBlock* SplitBefore(HInstruction* cursor);
960
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000961 // Split the block into two blocks just after `cursor`. Returns the newly
962 // created block. Note that this method just updates raw block information,
963 // like predecessors, successors, dominators, and instruction list. It does not
964 // update the graph, reverse post order, loop information, nor make sure the
965 // blocks are consistent (for example ending with a control flow instruction).
966 HBasicBlock* SplitAfter(HInstruction* cursor);
967
David Brazdil9bc43612015-11-05 21:25:24 +0000968 // Split catch block into two blocks after the original move-exception bytecode
969 // instruction, or at the beginning if not present. Returns the newly created,
970 // latter block, or nullptr if such block could not be created (must be dead
971 // in that case). Note that this method just updates raw block information,
972 // like predecessors, successors, dominators, and instruction list. It does not
973 // update the graph, reverse post order, loop information, nor make sure the
974 // blocks are consistent (for example ending with a control flow instruction).
975 HBasicBlock* SplitCatchBlockAfterMoveException();
976
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000977 // Merge `other` at the end of `this`. Successors and dominated blocks of
978 // `other` are changed to be successors and dominated blocks of `this`. Note
979 // that this method does not update the graph, reverse post order, loop
980 // information, nor make sure the blocks are consistent (for example ending
981 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100982 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000983
984 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
985 // of `this` are moved to `other`.
986 // Note that this method does not update the graph, reverse post order, loop
987 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000988 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000989 void ReplaceWith(HBasicBlock* other);
990
David Brazdil2d7352b2015-04-20 14:52:42 +0100991 // Merge `other` at the end of `this`. This method updates loops, reverse post
992 // order, links to predecessors, successors, dominators and deletes the block
993 // from the graph. The two blocks must be successive, i.e. `this` the only
994 // predecessor of `other` and vice versa.
995 void MergeWith(HBasicBlock* other);
996
997 // Disconnects `this` from all its predecessors, successors and dominator,
998 // removes it from all loops it is included in and eventually from the graph.
999 // The block must not dominate any other block. Predecessors and successors
1000 // are safely updated.
1001 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001002
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001003 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001004 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001005 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001006 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001007 // Replace instruction `initial` with `replacement` within this block.
1008 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1009 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001010 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001011 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001012 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1013 // instruction list. With 'ensure_safety' set to true, it verifies that the
1014 // instruction is not in use and removes it from the use lists of its inputs.
1015 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1016 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001017 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001018
1019 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001020 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001021 }
1022
Roland Levillain6b879dd2014-09-22 17:13:44 +01001023 bool IsLoopPreHeaderFirstPredecessor() const {
1024 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001025 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001026 }
1027
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001028 bool IsFirstPredecessorBackEdge() const {
1029 DCHECK(IsLoopHeader());
1030 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1031 }
1032
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001033 HLoopInformation* GetLoopInformation() const {
1034 return loop_information_;
1035 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001036
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001037 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001038 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001039 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001040 void SetInLoop(HLoopInformation* info) {
1041 if (IsLoopHeader()) {
1042 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001043 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001044 loop_information_ = info;
1045 } else if (loop_information_->Contains(*info->GetHeader())) {
1046 // Block is currently part of an outer loop. Make it part of this inner loop.
1047 // Note that a non loop header having a loop information means this loop information
1048 // has already been populated
1049 loop_information_ = info;
1050 } else {
1051 // Block is part of an inner loop. Do not update the loop information.
1052 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1053 // at this point, because this method is being called while populating `info`.
1054 }
1055 }
1056
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001057 // Raw update of the loop information.
1058 void SetLoopInformation(HLoopInformation* info) {
1059 loop_information_ = info;
1060 }
1061
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001062 bool IsInLoop() const { return loop_information_ != nullptr; }
1063
David Brazdilec16f792015-08-19 15:04:01 +01001064 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1065
1066 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1067 try_catch_information_ = try_catch_information;
1068 }
1069
1070 bool IsTryBlock() const {
1071 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1072 }
1073
1074 bool IsCatchBlock() const {
1075 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1076 }
David Brazdilffee3d32015-07-06 11:48:53 +01001077
1078 // Returns the try entry that this block's successors should have. They will
1079 // be in the same try, unless the block ends in a try boundary. In that case,
1080 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001081 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001082
David Brazdild7558da2015-09-22 13:04:14 +01001083 bool HasThrowingInstructions() const;
1084
David Brazdila4b8c212015-05-07 09:59:30 +01001085 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001086 bool Dominates(HBasicBlock* block) const;
1087
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001088 size_t GetLifetimeStart() const { return lifetime_start_; }
1089 size_t GetLifetimeEnd() const { return lifetime_end_; }
1090
1091 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1092 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1093
David Brazdil8d5b8b22015-03-24 10:51:52 +00001094 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001095 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001096 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001097 bool HasSinglePhi() const;
1098
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001099 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001100 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001101 ArenaVector<HBasicBlock*> predecessors_;
1102 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001103 HInstructionList instructions_;
1104 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001105 HLoopInformation* loop_information_;
1106 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001107 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001108 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001109 // The dex program counter of the first instruction of this block.
1110 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001111 size_t lifetime_start_;
1112 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001113 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001114
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001115 friend class HGraph;
1116 friend class HInstruction;
1117
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001118 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1119};
1120
David Brazdilb2bd1c52015-03-25 11:17:37 +00001121// Iterates over the LoopInformation of all loops which contain 'block'
1122// from the innermost to the outermost.
1123class HLoopInformationOutwardIterator : public ValueObject {
1124 public:
1125 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1126 : current_(block.GetLoopInformation()) {}
1127
1128 bool Done() const { return current_ == nullptr; }
1129
1130 void Advance() {
1131 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001132 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001133 }
1134
1135 HLoopInformation* Current() const {
1136 DCHECK(!Done());
1137 return current_;
1138 }
1139
1140 private:
1141 HLoopInformation* current_;
1142
1143 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1144};
1145
Alexandre Ramesef20f712015-06-09 10:29:30 +01001146#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001147 M(Above, Condition) \
1148 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001149 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001150 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001151 M(ArrayGet, Instruction) \
1152 M(ArrayLength, Instruction) \
1153 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001154 M(Below, Condition) \
1155 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001156 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001157 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001158 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001159 M(CheckCast, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001160 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001161 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001162 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001163 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001164 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001165 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001166 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001167 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001168 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001169 M(Exit, Instruction) \
1170 M(FloatConstant, Constant) \
1171 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001172 M(GreaterThan, Condition) \
1173 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001174 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001175 M(InstanceFieldGet, Instruction) \
1176 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001177 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001178 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001179 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001180 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001181 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001182 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001183 M(LessThan, Condition) \
1184 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001185 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001186 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001187 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001188 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001189 M(Local, Instruction) \
1190 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001191 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001192 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001193 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001194 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001195 M(Neg, UnaryOperation) \
1196 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001197 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001198 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001199 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001200 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001201 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001202 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001203 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001204 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001205 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001206 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001207 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001208 M(Return, Instruction) \
1209 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001210 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001211 M(Shl, BinaryOperation) \
1212 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001213 M(StaticFieldGet, Instruction) \
1214 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001215 M(UnresolvedInstanceFieldGet, Instruction) \
1216 M(UnresolvedInstanceFieldSet, Instruction) \
1217 M(UnresolvedStaticFieldGet, Instruction) \
1218 M(UnresolvedStaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001219 M(StoreLocal, Instruction) \
1220 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001221 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001222 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001223 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001224 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001225 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001226 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001227 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001228
Vladimir Markob4536b72015-11-24 13:45:23 +00001229#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001230#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001231#else
1232#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1233 M(ArmDexCacheArraysBase, Instruction)
1234#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001235
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001236#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001237#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001238#else
1239#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001240 M(Arm64DataProcWithShifterOp, Instruction) \
Alexandre Rames418318f2015-11-20 15:55:47 +00001241 M(Arm64IntermediateAddress, Instruction) \
1242 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001243#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001244
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001245#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1246
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001247#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1248
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001249#ifndef ART_ENABLE_CODEGEN_x86
1250#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1251#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001252#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1253 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001254 M(X86LoadFromConstantTable, Instruction) \
1255 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001256#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001257
1258#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1259
1260#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1261 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1262 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1263 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001264 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001265 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001266 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1267 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1268
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001269#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1270 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001271 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001272 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001273 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001274 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001275
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001276#define FOR_EACH_INSTRUCTION(M) \
1277 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1278 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1279
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001280#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001281FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1282#undef FORWARD_DECLARATION
1283
Roland Levillainccc07a92014-09-16 14:48:16 +01001284#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001285 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001286 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001287 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001288 return other->Is##type(); \
1289 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001290 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001291
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001292#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1293 bool Is##type() const { return As##type() != nullptr; } \
1294 const H##type* As##type() const { return this; } \
1295 H##type* As##type() { return this; }
1296
David Brazdiled596192015-01-23 10:39:45 +00001297template <typename T> class HUseList;
1298
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001299template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001300class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001301 public:
David Brazdiled596192015-01-23 10:39:45 +00001302 HUseListNode* GetPrevious() const { return prev_; }
1303 HUseListNode* GetNext() const { return next_; }
1304 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001305 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001306 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001307
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001308 private:
David Brazdiled596192015-01-23 10:39:45 +00001309 HUseListNode(T user, size_t index)
1310 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1311
1312 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001313 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001314 HUseListNode<T>* prev_;
1315 HUseListNode<T>* next_;
1316
1317 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001318
1319 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1320};
1321
David Brazdiled596192015-01-23 10:39:45 +00001322template <typename T>
1323class HUseList : public ValueObject {
1324 public:
1325 HUseList() : first_(nullptr) {}
1326
1327 void Clear() {
1328 first_ = nullptr;
1329 }
1330
1331 // Adds a new entry at the beginning of the use list and returns
1332 // the newly created node.
1333 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001334 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001335 if (IsEmpty()) {
1336 first_ = new_node;
1337 } else {
1338 first_->prev_ = new_node;
1339 new_node->next_ = first_;
1340 first_ = new_node;
1341 }
1342 return new_node;
1343 }
1344
1345 HUseListNode<T>* GetFirst() const {
1346 return first_;
1347 }
1348
1349 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001350 DCHECK(node != nullptr);
1351 DCHECK(Contains(node));
1352
David Brazdiled596192015-01-23 10:39:45 +00001353 if (node->prev_ != nullptr) {
1354 node->prev_->next_ = node->next_;
1355 }
1356 if (node->next_ != nullptr) {
1357 node->next_->prev_ = node->prev_;
1358 }
1359 if (node == first_) {
1360 first_ = node->next_;
1361 }
1362 }
1363
David Brazdil1abb4192015-02-17 18:33:36 +00001364 bool Contains(const HUseListNode<T>* node) const {
1365 if (node == nullptr) {
1366 return false;
1367 }
1368 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1369 if (current == node) {
1370 return true;
1371 }
1372 }
1373 return false;
1374 }
1375
David Brazdiled596192015-01-23 10:39:45 +00001376 bool IsEmpty() const {
1377 return first_ == nullptr;
1378 }
1379
1380 bool HasOnlyOneUse() const {
1381 return first_ != nullptr && first_->next_ == nullptr;
1382 }
1383
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001384 size_t SizeSlow() const {
1385 size_t count = 0;
1386 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1387 ++count;
1388 }
1389 return count;
1390 }
1391
David Brazdiled596192015-01-23 10:39:45 +00001392 private:
1393 HUseListNode<T>* first_;
1394};
1395
1396template<typename T>
1397class HUseIterator : public ValueObject {
1398 public:
1399 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1400
1401 bool Done() const { return current_ == nullptr; }
1402
1403 void Advance() {
1404 DCHECK(!Done());
1405 current_ = current_->GetNext();
1406 }
1407
1408 HUseListNode<T>* Current() const {
1409 DCHECK(!Done());
1410 return current_;
1411 }
1412
1413 private:
1414 HUseListNode<T>* current_;
1415
1416 friend class HValue;
1417};
1418
David Brazdil1abb4192015-02-17 18:33:36 +00001419// This class is used by HEnvironment and HInstruction classes to record the
1420// instructions they use and pointers to the corresponding HUseListNodes kept
1421// by the used instructions.
1422template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001423class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001424 public:
1425 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1426 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1427
1428 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1429 : instruction_(old_record.instruction_), use_node_(use_node) {
1430 DCHECK(instruction_ != nullptr);
1431 DCHECK(use_node_ != nullptr);
1432 DCHECK(old_record.use_node_ == nullptr);
1433 }
1434
1435 HInstruction* GetInstruction() const { return instruction_; }
1436 HUseListNode<T>* GetUseNode() const { return use_node_; }
1437
1438 private:
1439 // Instruction used by the user.
1440 HInstruction* instruction_;
1441
1442 // Corresponding entry in the use list kept by 'instruction_'.
1443 HUseListNode<T>* use_node_;
1444};
1445
Aart Bik854a02b2015-07-14 16:07:00 -07001446/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001447 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001448 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001449 * For write/read dependences on fields/arrays, the dependence analysis uses
1450 * type disambiguation (e.g. a float field write cannot modify the value of an
1451 * integer field read) and the access type (e.g. a reference array write cannot
1452 * modify the value of a reference field read [although it may modify the
1453 * reference fetch prior to reading the field, which is represented by its own
1454 * write/read dependence]). The analysis makes conservative points-to
1455 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1456 * the same, and any reference read depends on any reference read without
1457 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001458 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001459 * The internal representation uses 38-bit and is described in the table below.
1460 * The first line indicates the side effect, and for field/array accesses the
1461 * second line indicates the type of the access (in the order of the
1462 * Primitive::Type enum).
1463 * The two numbered lines below indicate the bit position in the bitfield (read
1464 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001465 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001466 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1467 * +-------------+---------+---------+--------------+---------+---------+
1468 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1469 * | 3 |333333322|222222221| 1 |111111110|000000000|
1470 * | 7 |654321098|765432109| 8 |765432109|876543210|
1471 *
1472 * Note that, to ease the implementation, 'changes' bits are least significant
1473 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001474 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001475class SideEffects : public ValueObject {
1476 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001477 SideEffects() : flags_(0) {}
1478
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001479 static SideEffects None() {
1480 return SideEffects(0);
1481 }
1482
1483 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001484 return SideEffects(kAllChangeBits | kAllDependOnBits);
1485 }
1486
1487 static SideEffects AllChanges() {
1488 return SideEffects(kAllChangeBits);
1489 }
1490
1491 static SideEffects AllDependencies() {
1492 return SideEffects(kAllDependOnBits);
1493 }
1494
1495 static SideEffects AllExceptGCDependency() {
1496 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1497 }
1498
1499 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001500 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001501 }
1502
Aart Bik34c3ba92015-07-20 14:08:59 -07001503 static SideEffects AllWrites() {
1504 return SideEffects(kAllWrites);
1505 }
1506
1507 static SideEffects AllReads() {
1508 return SideEffects(kAllReads);
1509 }
1510
1511 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1512 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001513 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001514 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001515 }
1516
Aart Bik854a02b2015-07-14 16:07:00 -07001517 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1518 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001519 }
1520
Aart Bik34c3ba92015-07-20 14:08:59 -07001521 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1522 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001523 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001524 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001525 }
1526
1527 static SideEffects ArrayReadOfType(Primitive::Type type) {
1528 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1529 }
1530
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001531 static SideEffects CanTriggerGC() {
1532 return SideEffects(1ULL << kCanTriggerGCBit);
1533 }
1534
1535 static SideEffects DependsOnGC() {
1536 return SideEffects(1ULL << kDependsOnGCBit);
1537 }
1538
Aart Bik854a02b2015-07-14 16:07:00 -07001539 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001540 SideEffects Union(SideEffects other) const {
1541 return SideEffects(flags_ | other.flags_);
1542 }
1543
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001544 SideEffects Exclusion(SideEffects other) const {
1545 return SideEffects(flags_ & ~other.flags_);
1546 }
1547
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001548 void Add(SideEffects other) {
1549 flags_ |= other.flags_;
1550 }
1551
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001552 bool Includes(SideEffects other) const {
1553 return (other.flags_ & flags_) == other.flags_;
1554 }
1555
1556 bool HasSideEffects() const {
1557 return (flags_ & kAllChangeBits);
1558 }
1559
1560 bool HasDependencies() const {
1561 return (flags_ & kAllDependOnBits);
1562 }
1563
1564 // Returns true if there are no side effects or dependencies.
1565 bool DoesNothing() const {
1566 return flags_ == 0;
1567 }
1568
Aart Bik854a02b2015-07-14 16:07:00 -07001569 // Returns true if something is written.
1570 bool DoesAnyWrite() const {
1571 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001572 }
1573
Aart Bik854a02b2015-07-14 16:07:00 -07001574 // Returns true if something is read.
1575 bool DoesAnyRead() const {
1576 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001577 }
1578
Aart Bik854a02b2015-07-14 16:07:00 -07001579 // Returns true if potentially everything is written and read
1580 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001581 bool DoesAllReadWrite() const {
1582 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1583 }
1584
Aart Bik854a02b2015-07-14 16:07:00 -07001585 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001586 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001587 }
1588
Roland Levillain0d5a2812015-11-13 10:07:31 +00001589 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001590 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001591 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1592 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001593 }
1594
1595 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001596 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001597 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001598 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001599 for (int s = kLastBit; s >= 0; s--) {
1600 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1601 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1602 // This is a bit for the GC side effect.
1603 if (current_bit_is_set) {
1604 flags += "GC";
1605 }
Aart Bik854a02b2015-07-14 16:07:00 -07001606 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001607 } else {
1608 // This is a bit for the array/field analysis.
1609 // The underscore character stands for the 'can trigger GC' bit.
1610 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1611 if (current_bit_is_set) {
1612 flags += kDebug[s];
1613 }
1614 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1615 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1616 flags += "|";
1617 }
1618 }
Aart Bik854a02b2015-07-14 16:07:00 -07001619 }
1620 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001621 }
1622
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001623 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001624
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001625 private:
1626 static constexpr int kFieldArrayAnalysisBits = 9;
1627
1628 static constexpr int kFieldWriteOffset = 0;
1629 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1630 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1631 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1632
1633 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1634
1635 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1636 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1637 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1638 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1639
1640 static constexpr int kLastBit = kDependsOnGCBit;
1641 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1642
1643 // Aliases.
1644
1645 static_assert(kChangeBits == kDependOnBits,
1646 "the 'change' bits should match the 'depend on' bits.");
1647
1648 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1649 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1650 static constexpr uint64_t kAllWrites =
1651 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1652 static constexpr uint64_t kAllReads =
1653 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001654
Aart Bik854a02b2015-07-14 16:07:00 -07001655 // Work around the fact that HIR aliases I/F and J/D.
1656 // TODO: remove this interceptor once HIR types are clean
1657 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1658 switch (type) {
1659 case Primitive::kPrimInt:
1660 case Primitive::kPrimFloat:
1661 return TypeFlag(Primitive::kPrimInt, offset) |
1662 TypeFlag(Primitive::kPrimFloat, offset);
1663 case Primitive::kPrimLong:
1664 case Primitive::kPrimDouble:
1665 return TypeFlag(Primitive::kPrimLong, offset) |
1666 TypeFlag(Primitive::kPrimDouble, offset);
1667 default:
1668 return TypeFlag(type, offset);
1669 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001670 }
1671
Aart Bik854a02b2015-07-14 16:07:00 -07001672 // Translates type to bit flag.
1673 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1674 CHECK_NE(type, Primitive::kPrimVoid);
1675 const uint64_t one = 1;
1676 const int shift = type; // 0-based consecutive enum
1677 DCHECK_LE(kFieldWriteOffset, shift);
1678 DCHECK_LT(shift, kArrayWriteOffset);
1679 return one << (type + offset);
1680 }
1681
1682 // Private constructor on direct flags value.
1683 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1684
1685 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001686};
1687
David Brazdiled596192015-01-23 10:39:45 +00001688// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001689class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001690 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001691 HEnvironment(ArenaAllocator* arena,
1692 size_t number_of_vregs,
1693 const DexFile& dex_file,
1694 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001695 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001696 InvokeType invoke_type,
1697 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001698 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1699 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001700 parent_(nullptr),
1701 dex_file_(dex_file),
1702 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001703 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001704 invoke_type_(invoke_type),
1705 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001706 }
1707
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001708 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001709 : HEnvironment(arena,
1710 to_copy.Size(),
1711 to_copy.GetDexFile(),
1712 to_copy.GetMethodIdx(),
1713 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001714 to_copy.GetInvokeType(),
1715 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001716
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001717 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001718 if (parent_ != nullptr) {
1719 parent_->SetAndCopyParentChain(allocator, parent);
1720 } else {
1721 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1722 parent_->CopyFrom(parent);
1723 if (parent->GetParent() != nullptr) {
1724 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1725 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001726 }
David Brazdiled596192015-01-23 10:39:45 +00001727 }
1728
Vladimir Marko71bf8092015-09-15 15:33:14 +01001729 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001730 void CopyFrom(HEnvironment* environment);
1731
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001732 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1733 // input to the loop phi instead. This is for inserting instructions that
1734 // require an environment (like HDeoptimization) in the loop pre-header.
1735 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001736
1737 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001738 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001739 }
1740
1741 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001742 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001743 }
1744
David Brazdil1abb4192015-02-17 18:33:36 +00001745 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001746
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001747 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001748
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001749 HEnvironment* GetParent() const { return parent_; }
1750
1751 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001752 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001753 }
1754
1755 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001756 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001757 }
1758
1759 uint32_t GetDexPc() const {
1760 return dex_pc_;
1761 }
1762
1763 uint32_t GetMethodIdx() const {
1764 return method_idx_;
1765 }
1766
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001767 InvokeType GetInvokeType() const {
1768 return invoke_type_;
1769 }
1770
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001771 const DexFile& GetDexFile() const {
1772 return dex_file_;
1773 }
1774
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001775 HInstruction* GetHolder() const {
1776 return holder_;
1777 }
1778
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001779
1780 bool IsFromInlinedInvoke() const {
1781 return GetParent() != nullptr;
1782 }
1783
David Brazdiled596192015-01-23 10:39:45 +00001784 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001785 // Record instructions' use entries of this environment for constant-time removal.
1786 // It should only be called by HInstruction when a new environment use is added.
1787 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1788 DCHECK(env_use->GetUser() == this);
1789 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001790 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001791 }
David Brazdiled596192015-01-23 10:39:45 +00001792
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001793 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1794 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001795 HEnvironment* parent_;
1796 const DexFile& dex_file_;
1797 const uint32_t method_idx_;
1798 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001799 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001800
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001801 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001802 HInstruction* const holder_;
1803
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001804 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001805
1806 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1807};
1808
Vladimir Markof9f64412015-09-02 14:05:49 +01001809class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001810 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001811 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001812 : previous_(nullptr),
1813 next_(nullptr),
1814 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001815 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001816 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001817 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001818 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001819 locations_(nullptr),
1820 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001821 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001822 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001823 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001824
Dave Allison20dfc792014-06-16 20:44:29 -07001825 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001826
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001827#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001828 enum InstructionKind {
1829 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1830 };
1831#undef DECLARE_KIND
1832
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001833 HInstruction* GetNext() const { return next_; }
1834 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001835
Calin Juravle77520bc2015-01-12 18:45:46 +00001836 HInstruction* GetNextDisregardingMoves() const;
1837 HInstruction* GetPreviousDisregardingMoves() const;
1838
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001839 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001840 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001841 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001842 bool IsInBlock() const { return block_ != nullptr; }
1843 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001844 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1845 bool IsIrreducibleLoopHeaderPhi() const {
1846 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1847 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001848
Roland Levillain6b879dd2014-09-22 17:13:44 +01001849 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001850 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001851
1852 virtual void Accept(HGraphVisitor* visitor) = 0;
1853 virtual const char* DebugName() const = 0;
1854
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001855 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001856 void SetRawInputAt(size_t index, HInstruction* input) {
1857 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1858 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001859
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001860 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001861
1862 uint32_t GetDexPc() const { return dex_pc_; }
1863
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001864 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001865
Roland Levillaine161a2a2014-10-03 12:45:18 +01001866 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001867 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001868
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001869 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001870 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001871
Calin Juravle10e244f2015-01-26 18:54:32 +00001872 // Does not apply for all instructions, but having this at top level greatly
1873 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001874 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001875 virtual bool CanBeNull() const {
1876 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1877 return true;
1878 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001879
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001880 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001881 return false;
1882 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001883
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001884 virtual bool IsActualObject() const {
1885 return GetType() == Primitive::kPrimNot;
1886 }
1887
Calin Juravle2e768302015-07-28 14:41:11 +00001888 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001889
Calin Juravle61d544b2015-02-23 16:46:57 +00001890 ReferenceTypeInfo GetReferenceTypeInfo() const {
1891 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1892 return reference_type_info_;
1893 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001894
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001895 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001896 DCHECK(user != nullptr);
1897 HUseListNode<HInstruction*>* use =
1898 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1899 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001900 }
1901
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001902 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001903 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001904 HUseListNode<HEnvironment*>* env_use =
1905 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1906 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001907 }
1908
David Brazdil1abb4192015-02-17 18:33:36 +00001909 void RemoveAsUserOfInput(size_t input) {
1910 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1911 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1912 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001913
David Brazdil1abb4192015-02-17 18:33:36 +00001914 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1915 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001916
David Brazdiled596192015-01-23 10:39:45 +00001917 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1918 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001919 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001920 bool HasOnlyOneNonEnvironmentUse() const {
1921 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1922 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001923
Roland Levillain6c82d402014-10-13 16:10:27 +01001924 // Does this instruction strictly dominate `other_instruction`?
1925 // Returns false if this instruction and `other_instruction` are the same.
1926 // Aborts if this instruction and `other_instruction` are both phis.
1927 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001928
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001929 int GetId() const { return id_; }
1930 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001931
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001932 int GetSsaIndex() const { return ssa_index_; }
1933 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1934 bool HasSsaIndex() const { return ssa_index_ != -1; }
1935
1936 bool HasEnvironment() const { return environment_ != nullptr; }
1937 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001938 // Set the `environment_` field. Raw because this method does not
1939 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001940 void SetRawEnvironment(HEnvironment* environment) {
1941 DCHECK(environment_ == nullptr);
1942 DCHECK_EQ(environment->GetHolder(), this);
1943 environment_ = environment;
1944 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001945
1946 // Set the environment of this instruction, copying it from `environment`. While
1947 // copying, the uses lists are being updated.
1948 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001949 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001950 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001951 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001952 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001953 if (environment->GetParent() != nullptr) {
1954 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1955 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001956 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001957
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001958 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1959 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001960 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001961 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001962 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001963 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001964 if (environment->GetParent() != nullptr) {
1965 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1966 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001967 }
1968
Nicolas Geoffray39468442014-09-02 15:17:15 +01001969 // Returns the number of entries in the environment. Typically, that is the
1970 // number of dex registers in a method. It could be more in case of inlining.
1971 size_t EnvironmentSize() const;
1972
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001973 LocationSummary* GetLocations() const { return locations_; }
1974 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001975
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001976 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001977 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001978
Alexandre Rames188d4312015-04-09 18:30:21 +01001979 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1980 // uses of this instruction by `other` are *not* updated.
1981 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1982 ReplaceWith(other);
1983 other->ReplaceInput(this, use_index);
1984 }
1985
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001986 // Move `this` instruction before `cursor`.
1987 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001988
Vladimir Markofb337ea2015-11-25 15:25:10 +00001989 // Move `this` before its first user and out of any loops. If there is no
1990 // out-of-loop user that dominates all other users, move the instruction
1991 // to the end of the out-of-loop common dominator of the user's blocks.
1992 //
1993 // This can be used only on non-throwing instructions with no side effects that
1994 // have at least one use but no environment uses.
1995 void MoveBeforeFirstUserAndOutOfLoops();
1996
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001997#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001998 bool Is##type() const; \
1999 const H##type* As##type() const; \
2000 H##type* As##type();
2001
2002 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2003#undef INSTRUCTION_TYPE_CHECK
2004
2005#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002006 bool Is##type() const { return (As##type() != nullptr); } \
2007 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002008 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002009 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002010#undef INSTRUCTION_TYPE_CHECK
2011
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002012 // Returns whether the instruction can be moved within the graph.
2013 virtual bool CanBeMoved() const { return false; }
2014
2015 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002016 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002017 return false;
2018 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002019
2020 // Returns whether any data encoded in the two instructions is equal.
2021 // This method does not look at the inputs. Both instructions must be
2022 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002023 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002024 return false;
2025 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002026
2027 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002028 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002029 // 2) Their inputs are identical.
2030 bool Equals(HInstruction* other) const;
2031
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002032 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2033 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2034 // the virtual function because the __attribute__((__pure__)) doesn't really
2035 // apply the strong requirement for virtual functions, preventing optimizations.
2036 InstructionKind GetKind() const PURE;
2037 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002038
2039 virtual size_t ComputeHashCode() const {
2040 size_t result = GetKind();
2041 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2042 result = (result * 31) + InputAt(i)->GetId();
2043 }
2044 return result;
2045 }
2046
2047 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002048 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002049
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002050 size_t GetLifetimePosition() const { return lifetime_position_; }
2051 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2052 LiveInterval* GetLiveInterval() const { return live_interval_; }
2053 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2054 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2055
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002056 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2057
2058 // Returns whether the code generation of the instruction will require to have access
2059 // to the current method. Such instructions are:
2060 // (1): Instructions that require an environment, as calling the runtime requires
2061 // to walk the stack and have the current method stored at a specific stack address.
2062 // (2): Object literals like classes and strings, that are loaded from the dex cache
2063 // fields of the current method.
2064 bool NeedsCurrentMethod() const {
2065 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2066 }
2067
Vladimir Markodc151b22015-10-15 18:02:30 +01002068 // Returns whether the code generation of the instruction will require to have access
2069 // to the dex cache of the current method's declaring class via the current method.
2070 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002071
Mark Mendellc4701932015-04-10 13:18:51 -04002072 // Does this instruction have any use in an environment before
2073 // control flow hits 'other'?
2074 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2075
2076 // Remove all references to environment uses of this instruction.
2077 // The caller must ensure that this is safe to do.
2078 void RemoveEnvironmentUsers();
2079
David Brazdil1abb4192015-02-17 18:33:36 +00002080 protected:
2081 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2082 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
Aart Bik5d75afe2015-12-14 11:57:01 -08002083 void SetSideEffects(SideEffects other) { side_effects_ = other; }
David Brazdil1abb4192015-02-17 18:33:36 +00002084
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002085 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002086 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2087
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002088 HInstruction* previous_;
2089 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002090 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002091 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002092
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002093 // An instruction gets an id when it is added to the graph.
2094 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002095 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002096 int id_;
2097
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002098 // When doing liveness analysis, instructions that have uses get an SSA index.
2099 int ssa_index_;
2100
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002101 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002102 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002103
2104 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002105 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002106
Nicolas Geoffray39468442014-09-02 15:17:15 +01002107 // The environment associated with this instruction. Not null if the instruction
2108 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002109 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002110
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002111 // Set by the code generator.
2112 LocationSummary* locations_;
2113
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002114 // Set by the liveness analysis.
2115 LiveInterval* live_interval_;
2116
2117 // Set by the liveness analysis, this is the position in a linear
2118 // order of blocks where this instruction's live interval start.
2119 size_t lifetime_position_;
2120
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002121 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002122
Calin Juravleacf735c2015-02-12 15:25:22 +00002123 // TODO: for primitive types this should be marked as invalid.
2124 ReferenceTypeInfo reference_type_info_;
2125
David Brazdil1abb4192015-02-17 18:33:36 +00002126 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002127 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002128 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002129 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002130 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002131
2132 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2133};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002134std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002135
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002136class HInputIterator : public ValueObject {
2137 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002138 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002139
2140 bool Done() const { return index_ == instruction_->InputCount(); }
2141 HInstruction* Current() const { return instruction_->InputAt(index_); }
2142 void Advance() { index_++; }
2143
2144 private:
2145 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002146 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002147
2148 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2149};
2150
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002151class HInstructionIterator : public ValueObject {
2152 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002153 explicit HInstructionIterator(const HInstructionList& instructions)
2154 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002155 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002156 }
2157
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002158 bool Done() const { return instruction_ == nullptr; }
2159 HInstruction* Current() const { return instruction_; }
2160 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002161 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002162 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002163 }
2164
2165 private:
2166 HInstruction* instruction_;
2167 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002168
2169 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002170};
2171
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002172class HBackwardInstructionIterator : public ValueObject {
2173 public:
2174 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2175 : instruction_(instructions.last_instruction_) {
2176 next_ = Done() ? nullptr : instruction_->GetPrevious();
2177 }
2178
2179 bool Done() const { return instruction_ == nullptr; }
2180 HInstruction* Current() const { return instruction_; }
2181 void Advance() {
2182 instruction_ = next_;
2183 next_ = Done() ? nullptr : instruction_->GetPrevious();
2184 }
2185
2186 private:
2187 HInstruction* instruction_;
2188 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002189
2190 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002191};
2192
Vladimir Markof9f64412015-09-02 14:05:49 +01002193template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002194class HTemplateInstruction: public HInstruction {
2195 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002196 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002197 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002198 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002199
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002200 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002201
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002202 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002203 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2204 DCHECK_LT(i, N);
2205 return inputs_[i];
2206 }
David Brazdil1abb4192015-02-17 18:33:36 +00002207
2208 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002209 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002210 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002211 }
2212
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002213 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002214 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002215
2216 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002217};
2218
Vladimir Markof9f64412015-09-02 14:05:49 +01002219// HTemplateInstruction specialization for N=0.
2220template<>
2221class HTemplateInstruction<0>: public HInstruction {
2222 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002223 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002224 : HInstruction(side_effects, dex_pc) {}
2225
Vladimir Markof9f64412015-09-02 14:05:49 +01002226 virtual ~HTemplateInstruction() {}
2227
2228 size_t InputCount() const OVERRIDE { return 0; }
2229
2230 protected:
2231 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2232 LOG(FATAL) << "Unreachable";
2233 UNREACHABLE();
2234 }
2235
2236 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2237 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2238 LOG(FATAL) << "Unreachable";
2239 UNREACHABLE();
2240 }
2241
2242 private:
2243 friend class SsaBuilder;
2244};
2245
Dave Allison20dfc792014-06-16 20:44:29 -07002246template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002247class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002248 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002249 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002250 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002251 virtual ~HExpression() {}
2252
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002253 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002254
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002255 protected:
2256 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002257};
2258
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002259// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2260// instruction that branches to the exit block.
2261class HReturnVoid : public HTemplateInstruction<0> {
2262 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002263 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2264 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002265
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002266 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002267
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002268 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002269
2270 private:
2271 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2272};
2273
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002274// Represents dex's RETURN opcodes. A HReturn is a control flow
2275// instruction that branches to the exit block.
2276class HReturn : public HTemplateInstruction<1> {
2277 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002278 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2279 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002280 SetRawInputAt(0, value);
2281 }
2282
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002283 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002284
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002285 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002286
2287 private:
2288 DISALLOW_COPY_AND_ASSIGN(HReturn);
2289};
2290
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002291// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002292// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002293// exit block.
2294class HExit : public HTemplateInstruction<0> {
2295 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002296 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002297
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002298 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002299
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002300 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002301
2302 private:
2303 DISALLOW_COPY_AND_ASSIGN(HExit);
2304};
2305
2306// Jumps from one block to another.
2307class HGoto : public HTemplateInstruction<0> {
2308 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002309 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002310
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002311 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002312
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002313 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002314 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002315 }
2316
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002317 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002318
2319 private:
2320 DISALLOW_COPY_AND_ASSIGN(HGoto);
2321};
2322
Roland Levillain9867bc72015-08-05 10:21:34 +01002323class HConstant : public HExpression<0> {
2324 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002325 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2326 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002327
2328 bool CanBeMoved() const OVERRIDE { return true; }
2329
2330 virtual bool IsMinusOne() const { return false; }
2331 virtual bool IsZero() const { return false; }
2332 virtual bool IsOne() const { return false; }
2333
David Brazdil77a48ae2015-09-15 12:34:04 +00002334 virtual uint64_t GetValueAsUint64() const = 0;
2335
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002336 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002337
2338 private:
2339 DISALLOW_COPY_AND_ASSIGN(HConstant);
2340};
2341
2342class HNullConstant : public HConstant {
2343 public:
2344 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2345 return true;
2346 }
2347
David Brazdil77a48ae2015-09-15 12:34:04 +00002348 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2349
Roland Levillain9867bc72015-08-05 10:21:34 +01002350 size_t ComputeHashCode() const OVERRIDE { return 0; }
2351
2352 DECLARE_INSTRUCTION(NullConstant);
2353
2354 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002355 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002356
2357 friend class HGraph;
2358 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2359};
2360
2361// Constants of the type int. Those can be from Dex instructions, or
2362// synthesized (for example with the if-eqz instruction).
2363class HIntConstant : public HConstant {
2364 public:
2365 int32_t GetValue() const { return value_; }
2366
David Brazdil9f389d42015-10-01 14:32:56 +01002367 uint64_t GetValueAsUint64() const OVERRIDE {
2368 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2369 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002370
Roland Levillain9867bc72015-08-05 10:21:34 +01002371 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2372 DCHECK(other->IsIntConstant());
2373 return other->AsIntConstant()->value_ == value_;
2374 }
2375
2376 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2377
2378 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2379 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2380 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2381
2382 DECLARE_INSTRUCTION(IntConstant);
2383
2384 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002385 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2386 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2387 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2388 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002389
2390 const int32_t value_;
2391
2392 friend class HGraph;
2393 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2394 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2395 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2396};
2397
2398class HLongConstant : public HConstant {
2399 public:
2400 int64_t GetValue() const { return value_; }
2401
David Brazdil77a48ae2015-09-15 12:34:04 +00002402 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2403
Roland Levillain9867bc72015-08-05 10:21:34 +01002404 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2405 DCHECK(other->IsLongConstant());
2406 return other->AsLongConstant()->value_ == value_;
2407 }
2408
2409 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2410
2411 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2412 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2413 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2414
2415 DECLARE_INSTRUCTION(LongConstant);
2416
2417 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002418 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2419 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002420
2421 const int64_t value_;
2422
2423 friend class HGraph;
2424 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2425};
Dave Allison20dfc792014-06-16 20:44:29 -07002426
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002427// Conditional branch. A block ending with an HIf instruction must have
2428// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002429class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002430 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002431 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2432 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002433 SetRawInputAt(0, input);
2434 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002435
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002436 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002437
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002438 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002439 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002440 }
2441
2442 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002443 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002444 }
2445
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002446 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002447
2448 private:
2449 DISALLOW_COPY_AND_ASSIGN(HIf);
2450};
2451
David Brazdilfc6a86a2015-06-26 10:33:45 +00002452
2453// Abstract instruction which marks the beginning and/or end of a try block and
2454// links it to the respective exception handlers. Behaves the same as a Goto in
2455// non-exceptional control flow.
2456// Normal-flow successor is stored at index zero, exception handlers under
2457// higher indices in no particular order.
2458class HTryBoundary : public HTemplateInstruction<0> {
2459 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002460 enum BoundaryKind {
2461 kEntry,
2462 kExit,
2463 };
2464
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002465 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2466 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002467
2468 bool IsControlFlow() const OVERRIDE { return true; }
2469
2470 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002471 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002472
David Brazdild26a4112015-11-10 11:07:31 +00002473 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2474 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2475 }
2476
David Brazdilfc6a86a2015-06-26 10:33:45 +00002477 // Returns whether `handler` is among its exception handlers (non-zero index
2478 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002479 bool HasExceptionHandler(const HBasicBlock& handler) const {
2480 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002481 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002482 }
2483
2484 // If not present already, adds `handler` to its block's list of exception
2485 // handlers.
2486 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002487 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002488 GetBlock()->AddSuccessor(handler);
2489 }
2490 }
2491
David Brazdil56e1acc2015-06-30 15:41:36 +01002492 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002493
David Brazdilffee3d32015-07-06 11:48:53 +01002494 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2495
David Brazdilfc6a86a2015-06-26 10:33:45 +00002496 DECLARE_INSTRUCTION(TryBoundary);
2497
2498 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002499 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002500
2501 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2502};
2503
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002504// Deoptimize to interpreter, upon checking a condition.
2505class HDeoptimize : public HTemplateInstruction<1> {
2506 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002507 // We set CanTriggerGC to prevent any intermediate address to be live
2508 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002509 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002510 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002511 SetRawInputAt(0, cond);
2512 }
2513
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002514 bool CanBeMoved() const OVERRIDE { return true; }
2515 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2516 return true;
2517 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002518 bool NeedsEnvironment() const OVERRIDE { return true; }
2519 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002520
2521 DECLARE_INSTRUCTION(Deoptimize);
2522
2523 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002524 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2525};
2526
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002527// Represents the ArtMethod that was passed as a first argument to
2528// the method. It is used by instructions that depend on it, like
2529// instructions that work with the dex cache.
2530class HCurrentMethod : public HExpression<0> {
2531 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002532 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2533 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002534
2535 DECLARE_INSTRUCTION(CurrentMethod);
2536
2537 private:
2538 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2539};
2540
Mark Mendellfe57faa2015-09-18 09:26:15 -04002541// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2542// have one successor for each entry in the switch table, and the final successor
2543// will be the block containing the next Dex opcode.
2544class HPackedSwitch : public HTemplateInstruction<1> {
2545 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002546 HPackedSwitch(int32_t start_value,
2547 uint32_t num_entries,
2548 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002549 uint32_t dex_pc = kNoDexPc)
2550 : HTemplateInstruction(SideEffects::None(), dex_pc),
2551 start_value_(start_value),
2552 num_entries_(num_entries) {
2553 SetRawInputAt(0, input);
2554 }
2555
2556 bool IsControlFlow() const OVERRIDE { return true; }
2557
2558 int32_t GetStartValue() const { return start_value_; }
2559
Vladimir Marko211c2112015-09-24 16:52:33 +01002560 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002561
2562 HBasicBlock* GetDefaultBlock() const {
2563 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002564 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002565 }
2566 DECLARE_INSTRUCTION(PackedSwitch);
2567
2568 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002569 const int32_t start_value_;
2570 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002571
2572 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2573};
2574
Roland Levillain88cb1752014-10-20 16:36:47 +01002575class HUnaryOperation : public HExpression<1> {
2576 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002577 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2578 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002579 SetRawInputAt(0, input);
2580 }
2581
2582 HInstruction* GetInput() const { return InputAt(0); }
2583 Primitive::Type GetResultType() const { return GetType(); }
2584
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002585 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002586 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002587 return true;
2588 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002589
Roland Levillain9240d6a2014-10-20 16:47:04 +01002590 // Try to statically evaluate `operation` and return a HConstant
2591 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002592 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002593 HConstant* TryStaticEvaluation() const;
2594
2595 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002596 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2597 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002598
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002599 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002600
2601 private:
2602 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2603};
2604
Dave Allison20dfc792014-06-16 20:44:29 -07002605class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002606 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002607 HBinaryOperation(Primitive::Type result_type,
2608 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002609 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002610 SideEffects side_effects = SideEffects::None(),
2611 uint32_t dex_pc = kNoDexPc)
2612 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002613 SetRawInputAt(0, left);
2614 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002615 }
2616
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002617 HInstruction* GetLeft() const { return InputAt(0); }
2618 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002619 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002620
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002621 virtual bool IsCommutative() const { return false; }
2622
2623 // Put constant on the right.
2624 // Returns whether order is changed.
2625 bool OrderInputsWithConstantOnTheRight() {
2626 HInstruction* left = InputAt(0);
2627 HInstruction* right = InputAt(1);
2628 if (left->IsConstant() && !right->IsConstant()) {
2629 ReplaceInput(right, 0);
2630 ReplaceInput(left, 1);
2631 return true;
2632 }
2633 return false;
2634 }
2635
2636 // Order inputs by instruction id, but favor constant on the right side.
2637 // This helps GVN for commutative ops.
2638 void OrderInputs() {
2639 DCHECK(IsCommutative());
2640 HInstruction* left = InputAt(0);
2641 HInstruction* right = InputAt(1);
2642 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2643 return;
2644 }
2645 if (OrderInputsWithConstantOnTheRight()) {
2646 return;
2647 }
2648 // Order according to instruction id.
2649 if (left->GetId() > right->GetId()) {
2650 ReplaceInput(right, 0);
2651 ReplaceInput(left, 1);
2652 }
2653 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002654
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002655 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002656 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002657 return true;
2658 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002659
Roland Levillain9240d6a2014-10-20 16:47:04 +01002660 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002661 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002662 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002663 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002664
2665 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002666 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2667 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2668 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2669 HLongConstant* y ATTRIBUTE_UNUSED) const {
2670 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2671 return nullptr;
2672 }
2673 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2674 HIntConstant* y ATTRIBUTE_UNUSED) const {
2675 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2676 return nullptr;
2677 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002678 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2679 HNullConstant* y ATTRIBUTE_UNUSED) const {
2680 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2681 return nullptr;
2682 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002683
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002684 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002685 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002686 HConstant* GetConstantRight() const;
2687
2688 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002689 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002690 HInstruction* GetLeastConstantLeft() const;
2691
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002692 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002693
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002694 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002695 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2696};
2697
Mark Mendellc4701932015-04-10 13:18:51 -04002698// The comparison bias applies for floating point operations and indicates how NaN
2699// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002700enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002701 kNoBias, // bias is not applicable (i.e. for long operation)
2702 kGtBias, // return 1 for NaN comparisons
2703 kLtBias, // return -1 for NaN comparisons
2704};
2705
Dave Allison20dfc792014-06-16 20:44:29 -07002706class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002707 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002708 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2709 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Mark Mendellc4701932015-04-10 13:18:51 -04002710 needs_materialization_(true),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002711 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002712
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002713 bool NeedsMaterialization() const { return needs_materialization_; }
2714 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002715
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002716 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002717 // `instruction`, and disregard moves in between.
2718 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002719
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002720 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002721
2722 virtual IfCondition GetCondition() const = 0;
2723
Mark Mendellc4701932015-04-10 13:18:51 -04002724 virtual IfCondition GetOppositeCondition() const = 0;
2725
Roland Levillain4fa13f62015-07-06 18:11:54 +01002726 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002727
2728 void SetBias(ComparisonBias bias) { bias_ = bias; }
2729
2730 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2731 return bias_ == other->AsCondition()->bias_;
2732 }
2733
Roland Levillain4fa13f62015-07-06 18:11:54 +01002734 bool IsFPConditionTrueIfNaN() const {
2735 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2736 IfCondition if_cond = GetCondition();
2737 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
2738 }
2739
2740 bool IsFPConditionFalseIfNaN() const {
2741 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2742 IfCondition if_cond = GetCondition();
2743 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
2744 }
2745
Dave Allison20dfc792014-06-16 20:44:29 -07002746 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002747 // For register allocation purposes, returns whether this instruction needs to be
2748 // materialized (that is, not just be in the processor flags).
2749 bool needs_materialization_;
2750
Mark Mendellc4701932015-04-10 13:18:51 -04002751 // Needed if we merge a HCompare into a HCondition.
2752 ComparisonBias bias_;
2753
Dave Allison20dfc792014-06-16 20:44:29 -07002754 DISALLOW_COPY_AND_ASSIGN(HCondition);
2755};
2756
2757// Instruction to check if two inputs are equal to each other.
2758class HEqual : public HCondition {
2759 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002760 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2761 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002762
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002763 bool IsCommutative() const OVERRIDE { return true; }
2764
Roland Levillain9867bc72015-08-05 10:21:34 +01002765 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002766 return GetBlock()->GetGraph()->GetIntConstant(
2767 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002768 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002769 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002770 return GetBlock()->GetGraph()->GetIntConstant(
2771 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002772 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002773 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2774 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002775 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002776 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002777
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002778 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002779
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002780 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002781 return kCondEQ;
2782 }
2783
Mark Mendellc4701932015-04-10 13:18:51 -04002784 IfCondition GetOppositeCondition() const OVERRIDE {
2785 return kCondNE;
2786 }
2787
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002788 private:
Aart Bike9f37602015-10-09 11:15:55 -07002789 template <typename T> bool Compute(T x, T y) const { return x == y; }
2790
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002791 DISALLOW_COPY_AND_ASSIGN(HEqual);
2792};
2793
Dave Allison20dfc792014-06-16 20:44:29 -07002794class HNotEqual : public HCondition {
2795 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002796 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2797 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002798
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002799 bool IsCommutative() const OVERRIDE { return true; }
2800
Roland Levillain9867bc72015-08-05 10:21:34 +01002801 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002802 return GetBlock()->GetGraph()->GetIntConstant(
2803 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002804 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002805 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002806 return GetBlock()->GetGraph()->GetIntConstant(
2807 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002808 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002809 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2810 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002811 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002812 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002813
Dave Allison20dfc792014-06-16 20:44:29 -07002814 DECLARE_INSTRUCTION(NotEqual);
2815
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002816 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002817 return kCondNE;
2818 }
2819
Mark Mendellc4701932015-04-10 13:18:51 -04002820 IfCondition GetOppositeCondition() const OVERRIDE {
2821 return kCondEQ;
2822 }
2823
Dave Allison20dfc792014-06-16 20:44:29 -07002824 private:
Aart Bike9f37602015-10-09 11:15:55 -07002825 template <typename T> bool Compute(T x, T y) const { return x != y; }
2826
Dave Allison20dfc792014-06-16 20:44:29 -07002827 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
2828};
2829
2830class HLessThan : public HCondition {
2831 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002832 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2833 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002834
Roland Levillain9867bc72015-08-05 10:21:34 +01002835 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002836 return GetBlock()->GetGraph()->GetIntConstant(
2837 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002838 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002839 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002840 return GetBlock()->GetGraph()->GetIntConstant(
2841 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002842 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002843
Dave Allison20dfc792014-06-16 20:44:29 -07002844 DECLARE_INSTRUCTION(LessThan);
2845
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002846 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002847 return kCondLT;
2848 }
2849
Mark Mendellc4701932015-04-10 13:18:51 -04002850 IfCondition GetOppositeCondition() const OVERRIDE {
2851 return kCondGE;
2852 }
2853
Dave Allison20dfc792014-06-16 20:44:29 -07002854 private:
Aart Bike9f37602015-10-09 11:15:55 -07002855 template <typename T> bool Compute(T x, T y) const { return x < y; }
2856
Dave Allison20dfc792014-06-16 20:44:29 -07002857 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2858};
2859
2860class HLessThanOrEqual : public HCondition {
2861 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002862 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2863 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002864
Roland Levillain9867bc72015-08-05 10:21:34 +01002865 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002866 return GetBlock()->GetGraph()->GetIntConstant(
2867 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002868 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002869 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002870 return GetBlock()->GetGraph()->GetIntConstant(
2871 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002872 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002873
Dave Allison20dfc792014-06-16 20:44:29 -07002874 DECLARE_INSTRUCTION(LessThanOrEqual);
2875
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002876 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002877 return kCondLE;
2878 }
2879
Mark Mendellc4701932015-04-10 13:18:51 -04002880 IfCondition GetOppositeCondition() const OVERRIDE {
2881 return kCondGT;
2882 }
2883
Dave Allison20dfc792014-06-16 20:44:29 -07002884 private:
Aart Bike9f37602015-10-09 11:15:55 -07002885 template <typename T> bool Compute(T x, T y) const { return x <= y; }
2886
Dave Allison20dfc792014-06-16 20:44:29 -07002887 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2888};
2889
2890class HGreaterThan : public HCondition {
2891 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002892 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2893 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002894
Roland Levillain9867bc72015-08-05 10:21:34 +01002895 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002896 return GetBlock()->GetGraph()->GetIntConstant(
2897 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002898 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002899 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002900 return GetBlock()->GetGraph()->GetIntConstant(
2901 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002902 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002903
Dave Allison20dfc792014-06-16 20:44:29 -07002904 DECLARE_INSTRUCTION(GreaterThan);
2905
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002906 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002907 return kCondGT;
2908 }
2909
Mark Mendellc4701932015-04-10 13:18:51 -04002910 IfCondition GetOppositeCondition() const OVERRIDE {
2911 return kCondLE;
2912 }
2913
Dave Allison20dfc792014-06-16 20:44:29 -07002914 private:
Aart Bike9f37602015-10-09 11:15:55 -07002915 template <typename T> bool Compute(T x, T y) const { return x > y; }
2916
Dave Allison20dfc792014-06-16 20:44:29 -07002917 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2918};
2919
2920class HGreaterThanOrEqual : public HCondition {
2921 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002922 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2923 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002924
Roland Levillain9867bc72015-08-05 10:21:34 +01002925 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002926 return GetBlock()->GetGraph()->GetIntConstant(
2927 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002928 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002929 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002930 return GetBlock()->GetGraph()->GetIntConstant(
2931 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002932 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002933
Dave Allison20dfc792014-06-16 20:44:29 -07002934 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2935
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002936 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002937 return kCondGE;
2938 }
2939
Mark Mendellc4701932015-04-10 13:18:51 -04002940 IfCondition GetOppositeCondition() const OVERRIDE {
2941 return kCondLT;
2942 }
2943
Dave Allison20dfc792014-06-16 20:44:29 -07002944 private:
Aart Bike9f37602015-10-09 11:15:55 -07002945 template <typename T> bool Compute(T x, T y) const { return x >= y; }
2946
Dave Allison20dfc792014-06-16 20:44:29 -07002947 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2948};
2949
Aart Bike9f37602015-10-09 11:15:55 -07002950class HBelow : public HCondition {
2951 public:
2952 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2953 : HCondition(first, second, dex_pc) {}
2954
2955 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2956 return GetBlock()->GetGraph()->GetIntConstant(
2957 Compute(static_cast<uint32_t>(x->GetValue()),
2958 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2959 }
2960 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2961 return GetBlock()->GetGraph()->GetIntConstant(
2962 Compute(static_cast<uint64_t>(x->GetValue()),
2963 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2964 }
2965
2966 DECLARE_INSTRUCTION(Below);
2967
2968 IfCondition GetCondition() const OVERRIDE {
2969 return kCondB;
2970 }
2971
2972 IfCondition GetOppositeCondition() const OVERRIDE {
2973 return kCondAE;
2974 }
2975
2976 private:
2977 template <typename T> bool Compute(T x, T y) const { return x < y; }
2978
2979 DISALLOW_COPY_AND_ASSIGN(HBelow);
2980};
2981
2982class HBelowOrEqual : public HCondition {
2983 public:
2984 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2985 : HCondition(first, second, dex_pc) {}
2986
2987 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2988 return GetBlock()->GetGraph()->GetIntConstant(
2989 Compute(static_cast<uint32_t>(x->GetValue()),
2990 static_cast<uint32_t>(y->GetValue())), GetDexPc());
2991 }
2992 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2993 return GetBlock()->GetGraph()->GetIntConstant(
2994 Compute(static_cast<uint64_t>(x->GetValue()),
2995 static_cast<uint64_t>(y->GetValue())), GetDexPc());
2996 }
2997
2998 DECLARE_INSTRUCTION(BelowOrEqual);
2999
3000 IfCondition GetCondition() const OVERRIDE {
3001 return kCondBE;
3002 }
3003
3004 IfCondition GetOppositeCondition() const OVERRIDE {
3005 return kCondA;
3006 }
3007
3008 private:
3009 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3010
3011 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3012};
3013
3014class HAbove : public HCondition {
3015 public:
3016 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3017 : HCondition(first, second, dex_pc) {}
3018
3019 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3020 return GetBlock()->GetGraph()->GetIntConstant(
3021 Compute(static_cast<uint32_t>(x->GetValue()),
3022 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3023 }
3024 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3025 return GetBlock()->GetGraph()->GetIntConstant(
3026 Compute(static_cast<uint64_t>(x->GetValue()),
3027 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3028 }
3029
3030 DECLARE_INSTRUCTION(Above);
3031
3032 IfCondition GetCondition() const OVERRIDE {
3033 return kCondA;
3034 }
3035
3036 IfCondition GetOppositeCondition() const OVERRIDE {
3037 return kCondBE;
3038 }
3039
3040 private:
3041 template <typename T> bool Compute(T x, T y) const { return x > y; }
3042
3043 DISALLOW_COPY_AND_ASSIGN(HAbove);
3044};
3045
3046class HAboveOrEqual : public HCondition {
3047 public:
3048 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3049 : HCondition(first, second, dex_pc) {}
3050
3051 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3052 return GetBlock()->GetGraph()->GetIntConstant(
3053 Compute(static_cast<uint32_t>(x->GetValue()),
3054 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3055 }
3056 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3057 return GetBlock()->GetGraph()->GetIntConstant(
3058 Compute(static_cast<uint64_t>(x->GetValue()),
3059 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3060 }
3061
3062 DECLARE_INSTRUCTION(AboveOrEqual);
3063
3064 IfCondition GetCondition() const OVERRIDE {
3065 return kCondAE;
3066 }
3067
3068 IfCondition GetOppositeCondition() const OVERRIDE {
3069 return kCondB;
3070 }
3071
3072 private:
3073 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3074
3075 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3076};
Dave Allison20dfc792014-06-16 20:44:29 -07003077
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003078// Instruction to check how two inputs compare to each other.
3079// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3080class HCompare : public HBinaryOperation {
3081 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003082 HCompare(Primitive::Type type,
3083 HInstruction* first,
3084 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003085 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003086 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003087 : HBinaryOperation(Primitive::kPrimInt,
3088 first,
3089 second,
3090 SideEffectsForArchRuntimeCalls(type),
3091 dex_pc),
3092 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003093 DCHECK_EQ(type, first->GetType());
3094 DCHECK_EQ(type, second->GetType());
3095 }
3096
Roland Levillain9867bc72015-08-05 10:21:34 +01003097 template <typename T>
3098 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003099
Roland Levillain9867bc72015-08-05 10:21:34 +01003100 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003101 return GetBlock()->GetGraph()->GetIntConstant(
3102 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003103 }
3104 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003105 return GetBlock()->GetGraph()->GetIntConstant(
3106 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003107 }
3108
Calin Juravleddb7df22014-11-25 20:56:51 +00003109 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3110 return bias_ == other->AsCompare()->bias_;
3111 }
3112
Mark Mendellc4701932015-04-10 13:18:51 -04003113 ComparisonBias GetBias() const { return bias_; }
3114
Roland Levillain4fa13f62015-07-06 18:11:54 +01003115 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003116
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003117
3118 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3119 // MIPS64 uses a runtime call for FP comparisons.
3120 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3121 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003122
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003123 DECLARE_INSTRUCTION(Compare);
3124
3125 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003126 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003127
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003128 DISALLOW_COPY_AND_ASSIGN(HCompare);
3129};
3130
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003131// A local in the graph. Corresponds to a Dex register.
3132class HLocal : public HTemplateInstruction<0> {
3133 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003134 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003135 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003136
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003137 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003138
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003139 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003140
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003141 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003142 // The Dex register number.
3143 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003144
3145 DISALLOW_COPY_AND_ASSIGN(HLocal);
3146};
3147
3148// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003149class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003150 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003151 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3152 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003153 SetRawInputAt(0, local);
3154 }
3155
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003156 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3157
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003158 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003159
3160 private:
3161 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3162};
3163
3164// Store a value in a given local. This instruction has two inputs: the value
3165// and the local.
3166class HStoreLocal : public HTemplateInstruction<2> {
3167 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003168 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3169 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003170 SetRawInputAt(0, local);
3171 SetRawInputAt(1, value);
3172 }
3173
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003174 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3175
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003176 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003177
3178 private:
3179 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3180};
3181
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003182class HFloatConstant : public HConstant {
3183 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003184 float GetValue() const { return value_; }
3185
David Brazdil77a48ae2015-09-15 12:34:04 +00003186 uint64_t GetValueAsUint64() const OVERRIDE {
3187 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3188 }
3189
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003190 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003191 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003192 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003193 }
3194
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003195 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003196
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003197 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003198 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003199 }
3200 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003201 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003202 }
3203 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003204 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3205 }
3206 bool IsNaN() const {
3207 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003208 }
3209
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003210 DECLARE_INSTRUCTION(FloatConstant);
3211
3212 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003213 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3214 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3215 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3216 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003217
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003218 const float value_;
3219
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003220 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003221 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003222 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003223 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3224};
3225
3226class HDoubleConstant : public HConstant {
3227 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003228 double GetValue() const { return value_; }
3229
David Brazdil77a48ae2015-09-15 12:34:04 +00003230 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3231
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003232 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003233 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003234 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003235 }
3236
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003237 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003238
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003239 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003240 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003241 }
3242 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003243 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003244 }
3245 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003246 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3247 }
3248 bool IsNaN() const {
3249 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003250 }
3251
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003252 DECLARE_INSTRUCTION(DoubleConstant);
3253
3254 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003255 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3256 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3257 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3258 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003259
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003260 const double value_;
3261
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003262 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003263 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003264 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003265 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3266};
3267
David Brazdil6de19382016-01-08 17:37:10 +00003268class HNewInstance : public HExpression<2> {
3269 public:
3270 HNewInstance(HInstruction* cls,
3271 HCurrentMethod* current_method,
3272 uint32_t dex_pc,
3273 uint16_t type_index,
3274 const DexFile& dex_file,
3275 bool can_throw,
3276 bool finalizable,
3277 QuickEntrypointEnum entrypoint)
3278 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3279 type_index_(type_index),
3280 dex_file_(dex_file),
3281 can_throw_(can_throw),
3282 finalizable_(finalizable),
3283 entrypoint_(entrypoint) {
3284 SetRawInputAt(0, cls);
3285 SetRawInputAt(1, current_method);
3286 }
3287
3288 uint16_t GetTypeIndex() const { return type_index_; }
3289 const DexFile& GetDexFile() const { return dex_file_; }
3290
3291 // Calls runtime so needs an environment.
3292 bool NeedsEnvironment() const OVERRIDE { return true; }
3293
3294 // It may throw when called on type that's not instantiable/accessible.
3295 // It can throw OOME.
3296 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3297 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3298
3299 bool IsFinalizable() const { return finalizable_; }
3300
3301 bool CanBeNull() const OVERRIDE { return false; }
3302
3303 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3304
3305 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3306 entrypoint_ = entrypoint;
3307 }
3308
3309 bool IsStringAlloc() const;
3310
3311 DECLARE_INSTRUCTION(NewInstance);
3312
3313 private:
3314 const uint16_t type_index_;
3315 const DexFile& dex_file_;
3316 const bool can_throw_;
3317 const bool finalizable_;
3318 QuickEntrypointEnum entrypoint_;
3319
3320 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3321};
3322
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003323enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003324#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3325 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003326#include "intrinsics_list.h"
3327 kNone,
3328 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3329#undef INTRINSICS_LIST
3330#undef OPTIMIZING_INTRINSICS
3331};
3332std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3333
Agi Csaki05f20562015-08-19 14:58:14 -07003334enum IntrinsicNeedsEnvironmentOrCache {
3335 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3336 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003337};
3338
Aart Bik5d75afe2015-12-14 11:57:01 -08003339enum IntrinsicSideEffects {
3340 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3341 kReadSideEffects, // Intrinsic may read heap memory.
3342 kWriteSideEffects, // Intrinsic may write heap memory.
3343 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3344};
3345
3346enum IntrinsicExceptions {
3347 kNoThrow, // Intrinsic does not throw any exceptions.
3348 kCanThrow // Intrinsic may throw exceptions.
3349};
3350
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003351class HInvoke : public HInstruction {
3352 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003353 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003354
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003355 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003356
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003357 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003358 SetRawInputAt(index, argument);
3359 }
3360
Roland Levillain3e3d7332015-04-28 11:00:54 +01003361 // Return the number of arguments. This number can be lower than
3362 // the number of inputs returned by InputCount(), as some invoke
3363 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3364 // inputs at the end of their list of inputs.
3365 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3366
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003367 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003368
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003369 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003370 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003371
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003372 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3373
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003374 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003375 return intrinsic_;
3376 }
3377
Aart Bik5d75afe2015-12-14 11:57:01 -08003378 void SetIntrinsic(Intrinsics intrinsic,
3379 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3380 IntrinsicSideEffects side_effects,
3381 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003382
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003383 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003384 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003385 }
3386
Aart Bik5d75afe2015-12-14 11:57:01 -08003387 bool CanThrow() const OVERRIDE { return can_throw_; }
3388
3389 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3390
3391 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3392 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3393 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003394
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003395 uint32_t* GetIntrinsicOptimizations() {
3396 return &intrinsic_optimizations_;
3397 }
3398
3399 const uint32_t* GetIntrinsicOptimizations() const {
3400 return &intrinsic_optimizations_;
3401 }
3402
3403 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3404
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003405 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003406
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003407 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003408 HInvoke(ArenaAllocator* arena,
3409 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003410 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003411 Primitive::Type return_type,
3412 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003413 uint32_t dex_method_index,
3414 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003415 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003416 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003417 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003418 inputs_(number_of_arguments + number_of_other_inputs,
3419 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003420 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003421 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003422 original_invoke_type_(original_invoke_type),
Aart Bik5d75afe2015-12-14 11:57:01 -08003423 can_throw_(true),
agicsaki57b81ec2015-08-11 17:39:37 -07003424 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003425 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003426 }
3427
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003428 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003429 return inputs_[index];
3430 }
3431
David Brazdil1abb4192015-02-17 18:33:36 +00003432 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003433 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003434 }
3435
Aart Bik5d75afe2015-12-14 11:57:01 -08003436 void SetCanThrow(bool can_throw) { can_throw_ = can_throw; }
3437
Roland Levillain3e3d7332015-04-28 11:00:54 +01003438 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003439 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003440 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003441 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003442 const InvokeType original_invoke_type_;
Aart Bik5d75afe2015-12-14 11:57:01 -08003443 bool can_throw_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003444 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003445
3446 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3447 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003448
3449 private:
3450 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3451};
3452
Calin Juravle175dc732015-08-25 15:42:32 +01003453class HInvokeUnresolved : public HInvoke {
3454 public:
3455 HInvokeUnresolved(ArenaAllocator* arena,
3456 uint32_t number_of_arguments,
3457 Primitive::Type return_type,
3458 uint32_t dex_pc,
3459 uint32_t dex_method_index,
3460 InvokeType invoke_type)
3461 : HInvoke(arena,
3462 number_of_arguments,
3463 0u /* number_of_other_inputs */,
3464 return_type,
3465 dex_pc,
3466 dex_method_index,
3467 invoke_type) {
3468 }
3469
3470 DECLARE_INSTRUCTION(InvokeUnresolved);
3471
3472 private:
3473 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3474};
3475
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003476class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003477 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003478 // Requirements of this method call regarding the class
3479 // initialization (clinit) check of its declaring class.
3480 enum class ClinitCheckRequirement {
3481 kNone, // Class already initialized.
3482 kExplicit, // Static call having explicit clinit check as last input.
3483 kImplicit, // Static call implicitly requiring a clinit check.
3484 };
3485
Vladimir Marko58155012015-08-19 12:49:41 +00003486 // Determines how to load the target ArtMethod*.
3487 enum class MethodLoadKind {
3488 // Use a String init ArtMethod* loaded from Thread entrypoints.
3489 kStringInit,
3490
3491 // Use the method's own ArtMethod* loaded by the register allocator.
3492 kRecursive,
3493
3494 // Use ArtMethod* at a known address, embed the direct address in the code.
3495 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3496 kDirectAddress,
3497
3498 // Use ArtMethod* at an address that will be known at link time, embed the direct
3499 // address in the code. If the image is relocatable, emit .patch_oat entry.
3500 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3501 // the image relocatable or not.
3502 kDirectAddressWithFixup,
3503
3504 // Load from resoved methods array in the dex cache using a PC-relative load.
3505 // Used when we need to use the dex cache, for example for invoke-static that
3506 // may cause class initialization (the entry may point to a resolution method),
3507 // and we know that we can access the dex cache arrays using a PC-relative load.
3508 kDexCachePcRelative,
3509
3510 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3511 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3512 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3513 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3514 kDexCacheViaMethod,
3515 };
3516
3517 // Determines the location of the code pointer.
3518 enum class CodePtrLocation {
3519 // Recursive call, use local PC-relative call instruction.
3520 kCallSelf,
3521
3522 // Use PC-relative call instruction patched at link time.
3523 // Used for calls within an oat file, boot->boot or app->app.
3524 kCallPCRelative,
3525
3526 // Call to a known target address, embed the direct address in code.
3527 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3528 kCallDirect,
3529
3530 // Call to a target address that will be known at link time, embed the direct
3531 // address in code. If the image is relocatable, emit .patch_oat entry.
3532 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3533 // the image relocatable or not.
3534 kCallDirectWithFixup,
3535
3536 // Use code pointer from the ArtMethod*.
3537 // Used when we don't know the target code. This is also the last-resort-kind used when
3538 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3539 kCallArtMethod,
3540 };
3541
3542 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003543 MethodLoadKind method_load_kind;
3544 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003545 // The method load data holds
3546 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3547 // Note that there are multiple string init methods, each having its own offset.
3548 // - the method address for kDirectAddress
3549 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003550 uint64_t method_load_data;
3551 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003552 };
3553
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003554 HInvokeStaticOrDirect(ArenaAllocator* arena,
3555 uint32_t number_of_arguments,
3556 Primitive::Type return_type,
3557 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003558 uint32_t method_index,
3559 MethodReference target_method,
3560 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003561 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003562 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003563 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003564 : HInvoke(arena,
3565 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003566 // There is potentially one extra argument for the HCurrentMethod node, and
3567 // potentially one other if the clinit check is explicit, and potentially
3568 // one other if the method is a string factory.
3569 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3570 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3571 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003572 return_type,
3573 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003574 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003575 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003576 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003577 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003578 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003579 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003580
Vladimir Markodc151b22015-10-15 18:02:30 +01003581 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003582 bool had_current_method_input = HasCurrentMethodInput();
3583 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3584
3585 // Using the current method is the default and once we find a better
3586 // method load kind, we should not go back to using the current method.
3587 DCHECK(had_current_method_input || !needs_current_method_input);
3588
3589 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003590 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3591 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003592 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003593 dispatch_info_ = dispatch_info;
3594 }
3595
Vladimir Markoc53c0792015-11-19 15:48:33 +00003596 void AddSpecialInput(HInstruction* input) {
3597 // We allow only one special input.
3598 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3599 DCHECK(InputCount() == GetSpecialInputIndex() ||
3600 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3601 InsertInputAt(GetSpecialInputIndex(), input);
3602 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003603
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003604 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003605 // We access the method via the dex cache so we can't do an implicit null check.
3606 // TODO: for intrinsics we can generate implicit null checks.
3607 return false;
3608 }
3609
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003610 bool CanBeNull() const OVERRIDE {
3611 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3612 }
3613
Vladimir Markoc53c0792015-11-19 15:48:33 +00003614 // Get the index of the special input, if any.
3615 //
David Brazdil6de19382016-01-08 17:37:10 +00003616 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3617 // method pointer; otherwise there may be one platform-specific special input,
3618 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003619 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
3620
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003621 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3622 void SetOptimizedInvokeType(InvokeType invoke_type) {
3623 optimized_invoke_type_ = invoke_type;
3624 }
3625
Vladimir Marko58155012015-08-19 12:49:41 +00003626 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3627 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3628 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003629 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003630 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003631 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003632 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003633 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3634 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003635 bool HasCurrentMethodInput() const {
3636 // This function can be called only after the invoke has been fully initialized by the builder.
3637 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003638 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003639 return true;
3640 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003641 DCHECK(InputCount() == GetSpecialInputIndex() ||
3642 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003643 return false;
3644 }
3645 }
Vladimir Marko58155012015-08-19 12:49:41 +00003646 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3647 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003648 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003649
3650 int32_t GetStringInitOffset() const {
3651 DCHECK(IsStringInit());
3652 return dispatch_info_.method_load_data;
3653 }
3654
3655 uint64_t GetMethodAddress() const {
3656 DCHECK(HasMethodAddress());
3657 return dispatch_info_.method_load_data;
3658 }
3659
3660 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003661 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003662 return dispatch_info_.method_load_data;
3663 }
3664
3665 uint64_t GetDirectCodePtr() const {
3666 DCHECK(HasDirectCodePtr());
3667 return dispatch_info_.direct_code_ptr;
3668 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003669
Calin Juravle68ad6492015-08-18 17:08:12 +01003670 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3671
Roland Levillain4c0eb422015-04-24 16:43:49 +01003672 // Is this instruction a call to a static method?
3673 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003674 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003675 }
3676
Vladimir Markofbb184a2015-11-13 14:47:00 +00003677 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3678 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3679 // instruction; only relevant for static calls with explicit clinit check.
3680 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003681 DCHECK(IsStaticWithExplicitClinitCheck());
3682 size_t last_input_index = InputCount() - 1;
3683 HInstruction* last_input = InputAt(last_input_index);
3684 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003685 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003686 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003687 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003688 clinit_check_requirement_ = new_requirement;
3689 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003690 }
3691
David Brazdil6de19382016-01-08 17:37:10 +00003692 HNewInstance* GetThisArgumentOfStringInit() const {
3693 DCHECK(IsStringInit());
3694 size_t index = InputCount() - 1;
3695 DCHECK(InputAt(index)->IsNewInstance());
3696 return InputAt(index)->AsNewInstance();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003697 }
3698
David Brazdil6de19382016-01-08 17:37:10 +00003699 void RemoveThisArgumentOfStringInit() {
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003700 DCHECK(IsStringInit());
David Brazdil6de19382016-01-08 17:37:10 +00003701 size_t index = InputCount() - 1;
3702 DCHECK(InputAt(index)->IsNewInstance());
3703 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003704 inputs_.pop_back();
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003705 }
3706
Roland Levillain4c0eb422015-04-24 16:43:49 +01003707 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003708 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003709 bool IsStaticWithExplicitClinitCheck() const {
3710 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3711 }
3712
3713 // Is this a call to a static method whose declaring class has an
3714 // implicit intialization check requirement?
3715 bool IsStaticWithImplicitClinitCheck() const {
3716 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3717 }
3718
Vladimir Markob554b5a2015-11-06 12:57:55 +00003719 // Does this method load kind need the current method as an input?
3720 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3721 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3722 }
3723
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003724 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003725
Roland Levillain4c0eb422015-04-24 16:43:49 +01003726 protected:
3727 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3728 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3729 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3730 HInstruction* input = input_record.GetInstruction();
3731 // `input` is the last input of a static invoke marked as having
3732 // an explicit clinit check. It must either be:
3733 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3734 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3735 DCHECK(input != nullptr);
3736 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3737 }
3738 return input_record;
3739 }
3740
Vladimir Markoc53c0792015-11-19 15:48:33 +00003741 void InsertInputAt(size_t index, HInstruction* input);
3742 void RemoveInputAt(size_t index);
3743
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003744 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003745 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003746 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003747 // The target method may refer to different dex file or method index than the original
3748 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3749 // in derived class to increase visibility.
3750 MethodReference target_method_;
3751 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003752
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003753 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003754};
Vladimir Markof64242a2015-12-01 14:58:23 +00003755std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003756std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003757
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003758class HInvokeVirtual : public HInvoke {
3759 public:
3760 HInvokeVirtual(ArenaAllocator* arena,
3761 uint32_t number_of_arguments,
3762 Primitive::Type return_type,
3763 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003764 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003765 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003766 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003767 vtable_index_(vtable_index) {}
3768
Calin Juravle641547a2015-04-21 22:08:51 +01003769 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003770 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003771 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003772 }
3773
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003774 uint32_t GetVTableIndex() const { return vtable_index_; }
3775
3776 DECLARE_INSTRUCTION(InvokeVirtual);
3777
3778 private:
3779 const uint32_t vtable_index_;
3780
3781 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3782};
3783
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003784class HInvokeInterface : public HInvoke {
3785 public:
3786 HInvokeInterface(ArenaAllocator* arena,
3787 uint32_t number_of_arguments,
3788 Primitive::Type return_type,
3789 uint32_t dex_pc,
3790 uint32_t dex_method_index,
3791 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003792 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003793 imt_index_(imt_index) {}
3794
Calin Juravle641547a2015-04-21 22:08:51 +01003795 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003796 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003797 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003798 }
3799
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003800 uint32_t GetImtIndex() const { return imt_index_; }
3801 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3802
3803 DECLARE_INSTRUCTION(InvokeInterface);
3804
3805 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003806 const uint32_t imt_index_;
3807
3808 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3809};
3810
Roland Levillain88cb1752014-10-20 16:36:47 +01003811class HNeg : public HUnaryOperation {
3812 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003813 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3814 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003815
Roland Levillain9867bc72015-08-05 10:21:34 +01003816 template <typename T> T Compute(T x) const { return -x; }
3817
3818 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003819 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003820 }
3821 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003822 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003823 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003824
Roland Levillain88cb1752014-10-20 16:36:47 +01003825 DECLARE_INSTRUCTION(Neg);
3826
3827 private:
3828 DISALLOW_COPY_AND_ASSIGN(HNeg);
3829};
3830
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003831class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003832 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003833 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003834 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003835 uint32_t dex_pc,
3836 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003837 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003838 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003839 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003840 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003841 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003842 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003843 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003844 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003845 }
3846
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003847 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003848 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003849
3850 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003851 bool NeedsEnvironment() const OVERRIDE { return true; }
3852
Mingyao Yang0c365e62015-03-31 15:09:29 -07003853 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3854 bool CanThrow() const OVERRIDE { return true; }
3855
Calin Juravle10e244f2015-01-26 18:54:32 +00003856 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003857
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003858 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3859
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003860 DECLARE_INSTRUCTION(NewArray);
3861
3862 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003863 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003864 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003865 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003866
3867 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3868};
3869
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003870class HAdd : public HBinaryOperation {
3871 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003872 HAdd(Primitive::Type result_type,
3873 HInstruction* left,
3874 HInstruction* right,
3875 uint32_t dex_pc = kNoDexPc)
3876 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003877
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003878 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003879
Roland Levillain9867bc72015-08-05 10:21:34 +01003880 template <typename T> T Compute(T x, T y) const { return x + y; }
3881
3882 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003883 return GetBlock()->GetGraph()->GetIntConstant(
3884 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003885 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003886 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003887 return GetBlock()->GetGraph()->GetLongConstant(
3888 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003889 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003890
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003891 DECLARE_INSTRUCTION(Add);
3892
3893 private:
3894 DISALLOW_COPY_AND_ASSIGN(HAdd);
3895};
3896
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003897class HSub : public HBinaryOperation {
3898 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003899 HSub(Primitive::Type result_type,
3900 HInstruction* left,
3901 HInstruction* right,
3902 uint32_t dex_pc = kNoDexPc)
3903 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003904
Roland Levillain9867bc72015-08-05 10:21:34 +01003905 template <typename T> T Compute(T x, T y) const { return x - y; }
3906
3907 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003908 return GetBlock()->GetGraph()->GetIntConstant(
3909 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003910 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003911 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003912 return GetBlock()->GetGraph()->GetLongConstant(
3913 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003914 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003915
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003916 DECLARE_INSTRUCTION(Sub);
3917
3918 private:
3919 DISALLOW_COPY_AND_ASSIGN(HSub);
3920};
3921
Calin Juravle34bacdf2014-10-07 20:23:36 +01003922class HMul : public HBinaryOperation {
3923 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003924 HMul(Primitive::Type result_type,
3925 HInstruction* left,
3926 HInstruction* right,
3927 uint32_t dex_pc = kNoDexPc)
3928 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003929
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003930 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003931
Roland Levillain9867bc72015-08-05 10:21:34 +01003932 template <typename T> T Compute(T x, T y) const { return x * y; }
3933
3934 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003935 return GetBlock()->GetGraph()->GetIntConstant(
3936 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003937 }
3938 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003939 return GetBlock()->GetGraph()->GetLongConstant(
3940 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003941 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003942
3943 DECLARE_INSTRUCTION(Mul);
3944
3945 private:
3946 DISALLOW_COPY_AND_ASSIGN(HMul);
3947};
3948
Calin Juravle7c4954d2014-10-28 16:57:40 +00003949class HDiv : public HBinaryOperation {
3950 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003951 HDiv(Primitive::Type result_type,
3952 HInstruction* left,
3953 HInstruction* right,
3954 uint32_t dex_pc)
3955 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00003956
Roland Levillain9867bc72015-08-05 10:21:34 +01003957 template <typename T>
3958 T Compute(T x, T y) const {
3959 // Our graph structure ensures we never have 0 for `y` during
3960 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003961 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00003962 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00003963 return (y == -1) ? -x : x / y;
3964 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003965
Roland Levillain9867bc72015-08-05 10:21:34 +01003966 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003967 return GetBlock()->GetGraph()->GetIntConstant(
3968 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003969 }
3970 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003971 return GetBlock()->GetGraph()->GetLongConstant(
3972 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00003973 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003974
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003975 static SideEffects SideEffectsForArchRuntimeCalls() {
3976 // The generated code can use a runtime call.
3977 return SideEffects::CanTriggerGC();
3978 }
3979
Calin Juravle7c4954d2014-10-28 16:57:40 +00003980 DECLARE_INSTRUCTION(Div);
3981
3982 private:
3983 DISALLOW_COPY_AND_ASSIGN(HDiv);
3984};
3985
Calin Juravlebacfec32014-11-14 15:54:36 +00003986class HRem : public HBinaryOperation {
3987 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003988 HRem(Primitive::Type result_type,
3989 HInstruction* left,
3990 HInstruction* right,
3991 uint32_t dex_pc)
3992 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00003993
Roland Levillain9867bc72015-08-05 10:21:34 +01003994 template <typename T>
3995 T Compute(T x, T y) const {
3996 // Our graph structure ensures we never have 0 for `y` during
3997 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00003998 DCHECK_NE(y, 0);
3999 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4000 return (y == -1) ? 0 : x % y;
4001 }
4002
Roland Levillain9867bc72015-08-05 10:21:34 +01004003 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004004 return GetBlock()->GetGraph()->GetIntConstant(
4005 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004006 }
4007 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004008 return GetBlock()->GetGraph()->GetLongConstant(
4009 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004010 }
4011
Calin Juravlebacfec32014-11-14 15:54:36 +00004012
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004013 static SideEffects SideEffectsForArchRuntimeCalls() {
4014 return SideEffects::CanTriggerGC();
4015 }
4016
Calin Juravlebacfec32014-11-14 15:54:36 +00004017 DECLARE_INSTRUCTION(Rem);
4018
4019 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004020 DISALLOW_COPY_AND_ASSIGN(HRem);
4021};
4022
Calin Juravled0d48522014-11-04 16:40:20 +00004023class HDivZeroCheck : public HExpression<1> {
4024 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004025 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4026 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004027 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004028 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004029 SetRawInputAt(0, value);
4030 }
4031
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004032 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4033
Calin Juravled0d48522014-11-04 16:40:20 +00004034 bool CanBeMoved() const OVERRIDE { return true; }
4035
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004036 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004037 return true;
4038 }
4039
4040 bool NeedsEnvironment() const OVERRIDE { return true; }
4041 bool CanThrow() const OVERRIDE { return true; }
4042
Calin Juravled0d48522014-11-04 16:40:20 +00004043 DECLARE_INSTRUCTION(DivZeroCheck);
4044
4045 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004046 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4047};
4048
Calin Juravle9aec02f2014-11-18 23:06:35 +00004049class HShl : public HBinaryOperation {
4050 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004051 HShl(Primitive::Type result_type,
4052 HInstruction* left,
4053 HInstruction* right,
4054 uint32_t dex_pc = kNoDexPc)
4055 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004056
Roland Levillain9867bc72015-08-05 10:21:34 +01004057 template <typename T, typename U, typename V>
4058 T Compute(T x, U y, V max_shift_value) const {
4059 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4060 "V is not the unsigned integer type corresponding to T");
4061 return x << (y & max_shift_value);
4062 }
4063
4064 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4065 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004066 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004067 }
4068 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4069 // case is handled as `x << static_cast<int>(y)`.
4070 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4071 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004072 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004073 }
4074 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4075 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004076 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004077 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004078
4079 DECLARE_INSTRUCTION(Shl);
4080
4081 private:
4082 DISALLOW_COPY_AND_ASSIGN(HShl);
4083};
4084
4085class HShr : public HBinaryOperation {
4086 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004087 HShr(Primitive::Type result_type,
4088 HInstruction* left,
4089 HInstruction* right,
4090 uint32_t dex_pc = kNoDexPc)
4091 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004092
Roland Levillain9867bc72015-08-05 10:21:34 +01004093 template <typename T, typename U, typename V>
4094 T Compute(T x, U y, V max_shift_value) const {
4095 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4096 "V is not the unsigned integer type corresponding to T");
4097 return x >> (y & max_shift_value);
4098 }
4099
4100 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4101 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004102 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004103 }
4104 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4105 // case is handled as `x >> static_cast<int>(y)`.
4106 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4107 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004108 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004109 }
4110 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4111 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004112 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004113 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004114
4115 DECLARE_INSTRUCTION(Shr);
4116
4117 private:
4118 DISALLOW_COPY_AND_ASSIGN(HShr);
4119};
4120
4121class HUShr : public HBinaryOperation {
4122 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004123 HUShr(Primitive::Type result_type,
4124 HInstruction* left,
4125 HInstruction* right,
4126 uint32_t dex_pc = kNoDexPc)
4127 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004128
Roland Levillain9867bc72015-08-05 10:21:34 +01004129 template <typename T, typename U, typename V>
4130 T Compute(T x, U y, V max_shift_value) const {
4131 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4132 "V is not the unsigned integer type corresponding to T");
4133 V ux = static_cast<V>(x);
4134 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004135 }
4136
Roland Levillain9867bc72015-08-05 10:21:34 +01004137 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4138 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004139 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004140 }
4141 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4142 // case is handled as `x >>> static_cast<int>(y)`.
4143 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4144 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004145 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004146 }
4147 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4148 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004149 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004150 }
4151
4152 DECLARE_INSTRUCTION(UShr);
4153
4154 private:
4155 DISALLOW_COPY_AND_ASSIGN(HUShr);
4156};
4157
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004158class HAnd : public HBinaryOperation {
4159 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004160 HAnd(Primitive::Type result_type,
4161 HInstruction* left,
4162 HInstruction* right,
4163 uint32_t dex_pc = kNoDexPc)
4164 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004165
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004166 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004167
Roland Levillain9867bc72015-08-05 10:21:34 +01004168 template <typename T, typename U>
4169 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4170
4171 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004172 return GetBlock()->GetGraph()->GetIntConstant(
4173 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004174 }
4175 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004176 return GetBlock()->GetGraph()->GetLongConstant(
4177 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004178 }
4179 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004180 return GetBlock()->GetGraph()->GetLongConstant(
4181 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004182 }
4183 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004184 return GetBlock()->GetGraph()->GetLongConstant(
4185 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004186 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004187
4188 DECLARE_INSTRUCTION(And);
4189
4190 private:
4191 DISALLOW_COPY_AND_ASSIGN(HAnd);
4192};
4193
4194class HOr : public HBinaryOperation {
4195 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004196 HOr(Primitive::Type result_type,
4197 HInstruction* left,
4198 HInstruction* right,
4199 uint32_t dex_pc = kNoDexPc)
4200 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004201
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004202 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004203
Roland Levillain9867bc72015-08-05 10:21:34 +01004204 template <typename T, typename U>
4205 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4206
4207 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004208 return GetBlock()->GetGraph()->GetIntConstant(
4209 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004210 }
4211 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004212 return GetBlock()->GetGraph()->GetLongConstant(
4213 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004214 }
4215 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004216 return GetBlock()->GetGraph()->GetLongConstant(
4217 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004218 }
4219 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004220 return GetBlock()->GetGraph()->GetLongConstant(
4221 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004222 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004223
4224 DECLARE_INSTRUCTION(Or);
4225
4226 private:
4227 DISALLOW_COPY_AND_ASSIGN(HOr);
4228};
4229
4230class HXor : public HBinaryOperation {
4231 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004232 HXor(Primitive::Type result_type,
4233 HInstruction* left,
4234 HInstruction* right,
4235 uint32_t dex_pc = kNoDexPc)
4236 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004237
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004238 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004239
Roland Levillain9867bc72015-08-05 10:21:34 +01004240 template <typename T, typename U>
4241 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4242
4243 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004244 return GetBlock()->GetGraph()->GetIntConstant(
4245 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004246 }
4247 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004248 return GetBlock()->GetGraph()->GetLongConstant(
4249 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004250 }
4251 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004252 return GetBlock()->GetGraph()->GetLongConstant(
4253 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004254 }
4255 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004256 return GetBlock()->GetGraph()->GetLongConstant(
4257 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004258 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004259
4260 DECLARE_INSTRUCTION(Xor);
4261
4262 private:
4263 DISALLOW_COPY_AND_ASSIGN(HXor);
4264};
4265
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004266class HRor : public HBinaryOperation {
4267 public:
4268 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4269 : HBinaryOperation(result_type, value, distance) {}
4270
4271 template <typename T, typename U, typename V>
4272 T Compute(T x, U y, V max_shift_value) const {
4273 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4274 "V is not the unsigned integer type corresponding to T");
4275 V ux = static_cast<V>(x);
4276 if ((y & max_shift_value) == 0) {
4277 return static_cast<T>(ux);
4278 } else {
4279 const V reg_bits = sizeof(T) * 8;
4280 return static_cast<T>(ux >> (y & max_shift_value)) |
4281 (x << (reg_bits - (y & max_shift_value)));
4282 }
4283 }
4284
4285 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4286 return GetBlock()->GetGraph()->GetIntConstant(
4287 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4288 }
4289 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4290 return GetBlock()->GetGraph()->GetLongConstant(
4291 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4292 }
4293 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4294 return GetBlock()->GetGraph()->GetLongConstant(
4295 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4296 }
4297
4298 DECLARE_INSTRUCTION(Ror);
4299
4300 private:
4301 DISALLOW_COPY_AND_ASSIGN(HRor);
4302};
4303
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004304// The value of a parameter in this method. Its location depends on
4305// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004306class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004307 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004308 HParameterValue(const DexFile& dex_file,
4309 uint16_t type_index,
4310 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004311 Primitive::Type parameter_type,
4312 bool is_this = false)
4313 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004314 dex_file_(dex_file),
4315 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004316 index_(index),
4317 is_this_(is_this),
4318 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004319
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004320 const DexFile& GetDexFile() const { return dex_file_; }
4321 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004322 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004323 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004324
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004325 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4326 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004327
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004328 DECLARE_INSTRUCTION(ParameterValue);
4329
4330 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004331 const DexFile& dex_file_;
4332 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004333 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004334 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004335 const uint8_t index_;
4336
Calin Juravle10e244f2015-01-26 18:54:32 +00004337 // Whether or not the parameter value corresponds to 'this' argument.
4338 const bool is_this_;
4339
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004340 bool can_be_null_;
4341
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004342 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4343};
4344
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004345class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004346 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004347 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4348 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004349
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004350 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004351 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004352 return true;
4353 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004354
Roland Levillain9867bc72015-08-05 10:21:34 +01004355 template <typename T> T Compute(T x) const { return ~x; }
4356
4357 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004358 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004359 }
4360 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004361 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004362 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004363
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004364 DECLARE_INSTRUCTION(Not);
4365
4366 private:
4367 DISALLOW_COPY_AND_ASSIGN(HNot);
4368};
4369
David Brazdil66d126e2015-04-03 16:02:44 +01004370class HBooleanNot : public HUnaryOperation {
4371 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004372 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4373 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004374
4375 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004376 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004377 return true;
4378 }
4379
Roland Levillain9867bc72015-08-05 10:21:34 +01004380 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004381 DCHECK(IsUint<1>(x));
4382 return !x;
4383 }
4384
Roland Levillain9867bc72015-08-05 10:21:34 +01004385 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004386 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004387 }
4388 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4389 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004390 UNREACHABLE();
4391 }
4392
4393 DECLARE_INSTRUCTION(BooleanNot);
4394
4395 private:
4396 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4397};
4398
Roland Levillaindff1f282014-11-05 14:15:05 +00004399class HTypeConversion : public HExpression<1> {
4400 public:
4401 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004402 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004403 : HExpression(result_type,
4404 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4405 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004406 SetRawInputAt(0, input);
4407 DCHECK_NE(input->GetType(), result_type);
4408 }
4409
4410 HInstruction* GetInput() const { return InputAt(0); }
4411 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4412 Primitive::Type GetResultType() const { return GetType(); }
4413
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004414 // Required by the x86, ARM, MIPS and MIPS64 code generators when producing calls
Roland Levillain624279f2014-12-04 11:54:28 +00004415 // to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00004416
Roland Levillaindff1f282014-11-05 14:15:05 +00004417 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004418 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004419
Mark Mendelle82549b2015-05-06 10:55:34 -04004420 // Try to statically evaluate the conversion and return a HConstant
4421 // containing the result. If the input cannot be converted, return nullptr.
4422 HConstant* TryStaticEvaluation() const;
4423
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004424 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4425 Primitive::Type result_type) {
4426 // Some architectures may not require the 'GC' side effects, but at this point
4427 // in the compilation process we do not know what architecture we will
4428 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004429 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4430 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004431 return SideEffects::CanTriggerGC();
4432 }
4433 return SideEffects::None();
4434 }
4435
Roland Levillaindff1f282014-11-05 14:15:05 +00004436 DECLARE_INSTRUCTION(TypeConversion);
4437
4438 private:
4439 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4440};
4441
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004442static constexpr uint32_t kNoRegNumber = -1;
4443
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004444class HPhi : public HInstruction {
4445 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004446 HPhi(ArenaAllocator* arena,
4447 uint32_t reg_number,
4448 size_t number_of_inputs,
4449 Primitive::Type type,
4450 uint32_t dex_pc = kNoDexPc)
4451 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004452 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004453 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004454 type_(ToPhiType(type)),
4455 // Phis are constructed live and marked dead if conflicting or unused.
4456 // Individual steps of SsaBuilder should assume that if a phi has been
4457 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4458 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004459 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004460 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004461 }
4462
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004463 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4464 static Primitive::Type ToPhiType(Primitive::Type type) {
4465 switch (type) {
4466 case Primitive::kPrimBoolean:
4467 case Primitive::kPrimByte:
4468 case Primitive::kPrimShort:
4469 case Primitive::kPrimChar:
4470 return Primitive::kPrimInt;
4471 default:
4472 return type;
4473 }
4474 }
4475
David Brazdilffee3d32015-07-06 11:48:53 +01004476 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4477
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004478 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004479
4480 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004481 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004482
Calin Juravle10e244f2015-01-26 18:54:32 +00004483 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil4833f5a2015-12-16 10:37:39 +00004484 void SetType(Primitive::Type new_type) {
4485 // Make sure that only valid type changes occur. The following are allowed:
4486 // (1) int -> float/ref (primitive type propagation),
4487 // (2) long -> double (primitive type propagation).
4488 DCHECK(type_ == new_type ||
4489 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4490 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4491 (type_ == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4492 type_ = new_type;
4493 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004494
Calin Juravle10e244f2015-01-26 18:54:32 +00004495 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4496 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4497
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004498 uint32_t GetRegNumber() const { return reg_number_; }
4499
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004500 void SetDead() { is_live_ = false; }
4501 void SetLive() { is_live_ = true; }
4502 bool IsDead() const { return !is_live_; }
4503 bool IsLive() const { return is_live_; }
4504
David Brazdil77a48ae2015-09-15 12:34:04 +00004505 bool IsVRegEquivalentOf(HInstruction* other) const {
4506 return other != nullptr
4507 && other->IsPhi()
4508 && other->AsPhi()->GetBlock() == GetBlock()
4509 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4510 }
4511
Calin Juravlea4f88312015-04-16 12:57:19 +01004512 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4513 // An equivalent phi is a phi having the same dex register and type.
4514 // It assumes that phis with the same dex register are adjacent.
4515 HPhi* GetNextEquivalentPhiWithSameType() {
4516 HInstruction* next = GetNext();
4517 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4518 if (next->GetType() == GetType()) {
4519 return next->AsPhi();
4520 }
4521 next = next->GetNext();
4522 }
4523 return nullptr;
4524 }
4525
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004526 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004527
David Brazdil1abb4192015-02-17 18:33:36 +00004528 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004529 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004530 return inputs_[index];
4531 }
David Brazdil1abb4192015-02-17 18:33:36 +00004532
4533 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004534 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004535 }
4536
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004537 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004538 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004539 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004540 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004541 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004542 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004543
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004544 DISALLOW_COPY_AND_ASSIGN(HPhi);
4545};
4546
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004547class HNullCheck : public HExpression<1> {
4548 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004549 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4550 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004551 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004552 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004553 SetRawInputAt(0, value);
4554 }
4555
Calin Juravle10e244f2015-01-26 18:54:32 +00004556 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004557 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004558 return true;
4559 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004560
Calin Juravle10e244f2015-01-26 18:54:32 +00004561 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004562
Calin Juravle10e244f2015-01-26 18:54:32 +00004563 bool CanThrow() const OVERRIDE { return true; }
4564
4565 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004566
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004567
4568 DECLARE_INSTRUCTION(NullCheck);
4569
4570 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004571 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4572};
4573
4574class FieldInfo : public ValueObject {
4575 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004576 FieldInfo(MemberOffset field_offset,
4577 Primitive::Type field_type,
4578 bool is_volatile,
4579 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004580 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004581 const DexFile& dex_file,
4582 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004583 : field_offset_(field_offset),
4584 field_type_(field_type),
4585 is_volatile_(is_volatile),
4586 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004587 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004588 dex_file_(dex_file),
4589 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004590
4591 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004592 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004593 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004594 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004595 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004596 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004597 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004598
4599 private:
4600 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004601 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004602 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004603 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004604 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004605 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004606 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004607};
4608
4609class HInstanceFieldGet : public HExpression<1> {
4610 public:
4611 HInstanceFieldGet(HInstruction* value,
4612 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004613 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004614 bool is_volatile,
4615 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004616 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004617 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004618 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004619 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004620 : HExpression(field_type,
4621 SideEffects::FieldReadOfType(field_type, is_volatile),
4622 dex_pc),
4623 field_info_(field_offset,
4624 field_type,
4625 is_volatile,
4626 field_idx,
4627 declaring_class_def_index,
4628 dex_file,
4629 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004630 SetRawInputAt(0, value);
4631 }
4632
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004633 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004634
4635 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4636 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4637 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004638 }
4639
Calin Juravle641547a2015-04-21 22:08:51 +01004640 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4641 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004642 }
4643
4644 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004645 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4646 }
4647
Calin Juravle52c48962014-12-16 17:02:57 +00004648 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004649 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004650 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004651 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004652
4653 DECLARE_INSTRUCTION(InstanceFieldGet);
4654
4655 private:
4656 const FieldInfo field_info_;
4657
4658 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4659};
4660
4661class HInstanceFieldSet : public HTemplateInstruction<2> {
4662 public:
4663 HInstanceFieldSet(HInstruction* object,
4664 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004665 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004666 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004667 bool is_volatile,
4668 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004669 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004670 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004671 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004672 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004673 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4674 dex_pc),
4675 field_info_(field_offset,
4676 field_type,
4677 is_volatile,
4678 field_idx,
4679 declaring_class_def_index,
4680 dex_file,
4681 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004682 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004683 SetRawInputAt(0, object);
4684 SetRawInputAt(1, value);
4685 }
4686
Calin Juravle641547a2015-04-21 22:08:51 +01004687 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4688 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004689 }
4690
Calin Juravle52c48962014-12-16 17:02:57 +00004691 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004692 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004693 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004694 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004695 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004696 bool GetValueCanBeNull() const { return value_can_be_null_; }
4697 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004698
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004699 DECLARE_INSTRUCTION(InstanceFieldSet);
4700
4701 private:
4702 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004703 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004704
4705 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4706};
4707
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004708class HArrayGet : public HExpression<2> {
4709 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004710 HArrayGet(HInstruction* array,
4711 HInstruction* index,
4712 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004713 uint32_t dex_pc,
4714 SideEffects additional_side_effects = SideEffects::None())
4715 : HExpression(type,
4716 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004717 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004718 SetRawInputAt(0, array);
4719 SetRawInputAt(1, index);
4720 }
4721
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004722 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004723 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004724 return true;
4725 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004726 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004727 // TODO: We can be smarter here.
4728 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4729 // which generates the implicit null check. There are cases when these can be removed
4730 // to produce better code. If we ever add optimizations to do so we should allow an
4731 // implicit check here (as long as the address falls in the first page).
4732 return false;
4733 }
4734
David Brazdil4833f5a2015-12-16 10:37:39 +00004735 bool IsEquivalentOf(HArrayGet* other) const {
4736 bool result = (GetDexPc() == other->GetDexPc());
4737 if (kIsDebugBuild && result) {
4738 DCHECK_EQ(GetBlock(), other->GetBlock());
4739 DCHECK_EQ(GetArray(), other->GetArray());
4740 DCHECK_EQ(GetIndex(), other->GetIndex());
4741 if (Primitive::IsIntOrLongType(GetType())) {
4742 DCHECK(Primitive::IsFloatingPointType(other->GetType()));
4743 } else {
4744 DCHECK(Primitive::IsFloatingPointType(GetType()));
4745 DCHECK(Primitive::IsIntOrLongType(other->GetType()));
4746 }
4747 }
4748 return result;
4749 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004750
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004751 HInstruction* GetArray() const { return InputAt(0); }
4752 HInstruction* GetIndex() const { return InputAt(1); }
4753
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004754 DECLARE_INSTRUCTION(ArrayGet);
4755
4756 private:
4757 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4758};
4759
4760class HArraySet : public HTemplateInstruction<3> {
4761 public:
4762 HArraySet(HInstruction* array,
4763 HInstruction* index,
4764 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004765 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004766 uint32_t dex_pc,
4767 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004768 : HTemplateInstruction(
4769 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004770 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4771 additional_side_effects),
4772 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004773 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004774 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004775 value_can_be_null_(true),
4776 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004777 SetRawInputAt(0, array);
4778 SetRawInputAt(1, index);
4779 SetRawInputAt(2, value);
4780 }
4781
Calin Juravle77520bc2015-01-12 18:45:46 +00004782 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004783 // We currently always call a runtime method to catch array store
4784 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004785 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004786 }
4787
Mingyao Yang81014cb2015-06-02 03:16:27 -07004788 // Can throw ArrayStoreException.
4789 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4790
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004791 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004792 // TODO: Same as for ArrayGet.
4793 return false;
4794 }
4795
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004796 void ClearNeedsTypeCheck() {
4797 needs_type_check_ = false;
4798 }
4799
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004800 void ClearValueCanBeNull() {
4801 value_can_be_null_ = false;
4802 }
4803
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004804 void SetStaticTypeOfArrayIsObjectArray() {
4805 static_type_of_array_is_object_array_ = true;
4806 }
4807
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004808 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004809 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004810 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004811
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004812 HInstruction* GetArray() const { return InputAt(0); }
4813 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004814 HInstruction* GetValue() const { return InputAt(2); }
4815
4816 Primitive::Type GetComponentType() const {
4817 // The Dex format does not type floating point index operations. Since the
4818 // `expected_component_type_` is set during building and can therefore not
4819 // be correct, we also check what is the value type. If it is a floating
4820 // point type, we must use that type.
4821 Primitive::Type value_type = GetValue()->GetType();
4822 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4823 ? value_type
4824 : expected_component_type_;
4825 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004826
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004827 Primitive::Type GetRawExpectedComponentType() const {
4828 return expected_component_type_;
4829 }
4830
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004831 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4832 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4833 }
4834
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004835 DECLARE_INSTRUCTION(ArraySet);
4836
4837 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004838 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004839 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004840 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004841 // Cached information for the reference_type_info_ so that codegen
4842 // does not need to inspect the static type.
4843 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004844
4845 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4846};
4847
4848class HArrayLength : public HExpression<1> {
4849 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004850 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004851 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004852 // Note that arrays do not change length, so the instruction does not
4853 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004854 SetRawInputAt(0, array);
4855 }
4856
Calin Juravle77520bc2015-01-12 18:45:46 +00004857 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004858 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004859 return true;
4860 }
Calin Juravle641547a2015-04-21 22:08:51 +01004861 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4862 return obj == InputAt(0);
4863 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004864
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004865 DECLARE_INSTRUCTION(ArrayLength);
4866
4867 private:
4868 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4869};
4870
4871class HBoundsCheck : public HExpression<2> {
4872 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004873 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
4874 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004875 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004876 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004877 DCHECK(index->GetType() == Primitive::kPrimInt);
4878 SetRawInputAt(0, index);
4879 SetRawInputAt(1, length);
4880 }
4881
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004882 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004883 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004884 return true;
4885 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004886
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004887 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004888
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004889 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004890
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004891 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004892
4893 DECLARE_INSTRUCTION(BoundsCheck);
4894
4895 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004896 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4897};
4898
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004899/**
4900 * Some DEX instructions are folded into multiple HInstructions that need
4901 * to stay live until the last HInstruction. This class
4902 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00004903 * HInstruction stays live. `index` represents the stack location index of the
4904 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004905 */
4906class HTemporary : public HTemplateInstruction<0> {
4907 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004908 explicit HTemporary(size_t index, uint32_t dex_pc = kNoDexPc)
4909 : HTemplateInstruction(SideEffects::None(), dex_pc), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004910
4911 size_t GetIndex() const { return index_; }
4912
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00004913 Primitive::Type GetType() const OVERRIDE {
4914 // The previous instruction is the one that will be stored in the temporary location.
4915 DCHECK(GetPrevious() != nullptr);
4916 return GetPrevious()->GetType();
4917 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00004918
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004919 DECLARE_INSTRUCTION(Temporary);
4920
4921 private:
4922 const size_t index_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004923 DISALLOW_COPY_AND_ASSIGN(HTemporary);
4924};
4925
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004926class HSuspendCheck : public HTemplateInstruction<0> {
4927 public:
4928 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004929 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004930
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004931 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004932 return true;
4933 }
4934
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004935 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4936 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004937
4938 DECLARE_INSTRUCTION(SuspendCheck);
4939
4940 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004941 // Only used for code generation, in order to share the same slow path between back edges
4942 // of a same loop.
4943 SlowPathCode* slow_path_;
4944
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004945 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4946};
4947
David Srbecky0cf44932015-12-09 14:09:59 +00004948// Pseudo-instruction which provides the native debugger with mapping information.
4949// It ensures that we can generate line number and local variables at this point.
4950class HNativeDebugInfo : public HTemplateInstruction<0> {
4951 public:
4952 explicit HNativeDebugInfo(uint32_t dex_pc)
4953 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
4954
4955 bool NeedsEnvironment() const OVERRIDE {
4956 return true;
4957 }
4958
4959 DECLARE_INSTRUCTION(NativeDebugInfo);
4960
4961 private:
4962 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
4963};
4964
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004965/**
4966 * Instruction to load a Class object.
4967 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004968class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004969 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004970 HLoadClass(HCurrentMethod* current_method,
4971 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004972 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004973 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004974 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004975 bool needs_access_check,
4976 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004977 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004978 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004979 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004980 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00004981 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01004982 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004983 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00004984 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01004985 // Referrers class should not need access check. We never inline unverified
4986 // methods so we can't possibly end up in this situation.
4987 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004988 SetRawInputAt(0, current_method);
4989 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004990
4991 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004992
4993 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01004994 // Note that we don't need to test for generate_clinit_check_.
4995 // Whether or not we need to generate the clinit check is processed in
4996 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01004997 return other->AsLoadClass()->type_index_ == type_index_ &&
4998 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004999 }
5000
5001 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5002
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005003 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005004 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005005 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005006
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005007 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005008 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005009 }
5010
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005011 bool MustGenerateClinitCheck() const {
5012 return generate_clinit_check_;
5013 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005014
Calin Juravle0ba218d2015-05-19 18:46:01 +01005015 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005016 // The entrypoint the code generator is going to call does not do
5017 // clinit of the class.
5018 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01005019 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005020 }
5021
5022 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005023 return MustGenerateClinitCheck() ||
5024 (!is_referrers_class_ && !is_in_dex_cache_) ||
5025 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01005026 }
5027
5028 bool NeedsAccessCheck() const {
5029 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005030 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005031
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005032 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005033 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005034 }
5035
Calin Juravleacf735c2015-02-12 15:25:22 +00005036 ReferenceTypeInfo GetLoadedClassRTI() {
5037 return loaded_class_rti_;
5038 }
5039
5040 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5041 // Make sure we only set exact types (the loaded class should never be merged).
5042 DCHECK(rti.IsExact());
5043 loaded_class_rti_ = rti;
5044 }
5045
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005046 const DexFile& GetDexFile() { return dex_file_; }
5047
Vladimir Markodc151b22015-10-15 18:02:30 +01005048 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005049
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005050 static SideEffects SideEffectsForArchRuntimeCalls() {
5051 return SideEffects::CanTriggerGC();
5052 }
5053
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005054 bool IsInDexCache() const { return is_in_dex_cache_; }
5055
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005056 DECLARE_INSTRUCTION(LoadClass);
5057
5058 private:
5059 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005060 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005061 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005062 // Whether this instruction must generate the initialization check.
5063 // Used for code generation.
5064 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005065 const bool needs_access_check_;
5066 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005067
Calin Juravleacf735c2015-02-12 15:25:22 +00005068 ReferenceTypeInfo loaded_class_rti_;
5069
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005070 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5071};
5072
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005073class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005074 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005075 HLoadString(HCurrentMethod* current_method,
5076 uint32_t string_index,
5077 uint32_t dex_pc,
5078 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005079 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005080 string_index_(string_index),
5081 is_in_dex_cache_(is_in_dex_cache) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005082 SetRawInputAt(0, current_method);
5083 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005084
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005085 bool CanBeMoved() const OVERRIDE { return true; }
5086
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005087 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5088 return other->AsLoadString()->string_index_ == string_index_;
5089 }
5090
5091 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5092
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005093 uint32_t GetStringIndex() const { return string_index_; }
5094
5095 // TODO: Can we deopt or debug when we resolve a string?
5096 bool NeedsEnvironment() const OVERRIDE { return false; }
Vladimir Markodc151b22015-10-15 18:02:30 +01005097 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005098 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005099 bool IsInDexCache() const { return is_in_dex_cache_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005100
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005101 static SideEffects SideEffectsForArchRuntimeCalls() {
5102 return SideEffects::CanTriggerGC();
5103 }
5104
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005105 DECLARE_INSTRUCTION(LoadString);
5106
5107 private:
5108 const uint32_t string_index_;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005109 const bool is_in_dex_cache_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005110
5111 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5112};
5113
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005114/**
5115 * Performs an initialization check on its Class object input.
5116 */
5117class HClinitCheck : public HExpression<1> {
5118 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005119 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005120 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005121 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005122 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5123 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005124 SetRawInputAt(0, constant);
5125 }
5126
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005127 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005128 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005129 return true;
5130 }
5131
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005132 bool NeedsEnvironment() const OVERRIDE {
5133 // May call runtime to initialize the class.
5134 return true;
5135 }
5136
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005137 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005138
5139 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5140
5141 DECLARE_INSTRUCTION(ClinitCheck);
5142
5143 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005144 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5145};
5146
5147class HStaticFieldGet : public HExpression<1> {
5148 public:
5149 HStaticFieldGet(HInstruction* cls,
5150 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005151 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005152 bool is_volatile,
5153 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005154 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005155 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005156 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005157 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005158 : HExpression(field_type,
5159 SideEffects::FieldReadOfType(field_type, is_volatile),
5160 dex_pc),
5161 field_info_(field_offset,
5162 field_type,
5163 is_volatile,
5164 field_idx,
5165 declaring_class_def_index,
5166 dex_file,
5167 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005168 SetRawInputAt(0, cls);
5169 }
5170
Calin Juravle52c48962014-12-16 17:02:57 +00005171
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005172 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005173
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005174 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005175 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5176 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005177 }
5178
5179 size_t ComputeHashCode() const OVERRIDE {
5180 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5181 }
5182
Calin Juravle52c48962014-12-16 17:02:57 +00005183 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005184 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5185 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005186 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005187
5188 DECLARE_INSTRUCTION(StaticFieldGet);
5189
5190 private:
5191 const FieldInfo field_info_;
5192
5193 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5194};
5195
5196class HStaticFieldSet : public HTemplateInstruction<2> {
5197 public:
5198 HStaticFieldSet(HInstruction* cls,
5199 HInstruction* value,
5200 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005201 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005202 bool is_volatile,
5203 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005204 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005205 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005206 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005207 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005208 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5209 dex_pc),
5210 field_info_(field_offset,
5211 field_type,
5212 is_volatile,
5213 field_idx,
5214 declaring_class_def_index,
5215 dex_file,
5216 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005217 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005218 SetRawInputAt(0, cls);
5219 SetRawInputAt(1, value);
5220 }
5221
Calin Juravle52c48962014-12-16 17:02:57 +00005222 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005223 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5224 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005225 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005226
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005227 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005228 bool GetValueCanBeNull() const { return value_can_be_null_; }
5229 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005230
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005231 DECLARE_INSTRUCTION(StaticFieldSet);
5232
5233 private:
5234 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005235 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005236
5237 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5238};
5239
Calin Juravlee460d1d2015-09-29 04:52:17 +01005240class HUnresolvedInstanceFieldGet : public HExpression<1> {
5241 public:
5242 HUnresolvedInstanceFieldGet(HInstruction* obj,
5243 Primitive::Type field_type,
5244 uint32_t field_index,
5245 uint32_t dex_pc)
5246 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5247 field_index_(field_index) {
5248 SetRawInputAt(0, obj);
5249 }
5250
5251 bool NeedsEnvironment() const OVERRIDE { return true; }
5252 bool CanThrow() const OVERRIDE { return true; }
5253
5254 Primitive::Type GetFieldType() const { return GetType(); }
5255 uint32_t GetFieldIndex() const { return field_index_; }
5256
5257 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5258
5259 private:
5260 const uint32_t field_index_;
5261
5262 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5263};
5264
5265class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5266 public:
5267 HUnresolvedInstanceFieldSet(HInstruction* obj,
5268 HInstruction* value,
5269 Primitive::Type field_type,
5270 uint32_t field_index,
5271 uint32_t dex_pc)
5272 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5273 field_type_(field_type),
5274 field_index_(field_index) {
5275 DCHECK_EQ(field_type, value->GetType());
5276 SetRawInputAt(0, obj);
5277 SetRawInputAt(1, value);
5278 }
5279
5280 bool NeedsEnvironment() const OVERRIDE { return true; }
5281 bool CanThrow() const OVERRIDE { return true; }
5282
5283 Primitive::Type GetFieldType() const { return field_type_; }
5284 uint32_t GetFieldIndex() const { return field_index_; }
5285
5286 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5287
5288 private:
5289 const Primitive::Type field_type_;
5290 const uint32_t field_index_;
5291
5292 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5293};
5294
5295class HUnresolvedStaticFieldGet : public HExpression<0> {
5296 public:
5297 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5298 uint32_t field_index,
5299 uint32_t dex_pc)
5300 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5301 field_index_(field_index) {
5302 }
5303
5304 bool NeedsEnvironment() const OVERRIDE { return true; }
5305 bool CanThrow() const OVERRIDE { return true; }
5306
5307 Primitive::Type GetFieldType() const { return GetType(); }
5308 uint32_t GetFieldIndex() const { return field_index_; }
5309
5310 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5311
5312 private:
5313 const uint32_t field_index_;
5314
5315 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5316};
5317
5318class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5319 public:
5320 HUnresolvedStaticFieldSet(HInstruction* value,
5321 Primitive::Type field_type,
5322 uint32_t field_index,
5323 uint32_t dex_pc)
5324 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5325 field_type_(field_type),
5326 field_index_(field_index) {
5327 DCHECK_EQ(field_type, value->GetType());
5328 SetRawInputAt(0, value);
5329 }
5330
5331 bool NeedsEnvironment() const OVERRIDE { return true; }
5332 bool CanThrow() const OVERRIDE { return true; }
5333
5334 Primitive::Type GetFieldType() const { return field_type_; }
5335 uint32_t GetFieldIndex() const { return field_index_; }
5336
5337 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5338
5339 private:
5340 const Primitive::Type field_type_;
5341 const uint32_t field_index_;
5342
5343 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5344};
5345
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005346// Implement the move-exception DEX instruction.
5347class HLoadException : public HExpression<0> {
5348 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005349 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5350 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005351
David Brazdilbbd733e2015-08-18 17:48:17 +01005352 bool CanBeNull() const OVERRIDE { return false; }
5353
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005354 DECLARE_INSTRUCTION(LoadException);
5355
5356 private:
5357 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5358};
5359
David Brazdilcb1c0552015-08-04 16:22:25 +01005360// Implicit part of move-exception which clears thread-local exception storage.
5361// Must not be removed because the runtime expects the TLS to get cleared.
5362class HClearException : public HTemplateInstruction<0> {
5363 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005364 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5365 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005366
5367 DECLARE_INSTRUCTION(ClearException);
5368
5369 private:
5370 DISALLOW_COPY_AND_ASSIGN(HClearException);
5371};
5372
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005373class HThrow : public HTemplateInstruction<1> {
5374 public:
5375 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005376 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005377 SetRawInputAt(0, exception);
5378 }
5379
5380 bool IsControlFlow() const OVERRIDE { return true; }
5381
5382 bool NeedsEnvironment() const OVERRIDE { return true; }
5383
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005384 bool CanThrow() const OVERRIDE { return true; }
5385
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005386
5387 DECLARE_INSTRUCTION(Throw);
5388
5389 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005390 DISALLOW_COPY_AND_ASSIGN(HThrow);
5391};
5392
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005393/**
5394 * Implementation strategies for the code generator of a HInstanceOf
5395 * or `HCheckCast`.
5396 */
5397enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005398 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005399 kExactCheck, // Can do a single class compare.
5400 kClassHierarchyCheck, // Can just walk the super class chain.
5401 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5402 kInterfaceCheck, // No optimization yet when checking against an interface.
5403 kArrayObjectCheck, // Can just check if the array is not primitive.
5404 kArrayCheck // No optimization yet when checking against a generic array.
5405};
5406
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005407class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005408 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005409 HInstanceOf(HInstruction* object,
5410 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005411 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005412 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005413 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005414 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005415 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005416 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005417 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005418 SetRawInputAt(0, object);
5419 SetRawInputAt(1, constant);
5420 }
5421
5422 bool CanBeMoved() const OVERRIDE { return true; }
5423
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005424 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005425 return true;
5426 }
5427
5428 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005429 return false;
5430 }
5431
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005432 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5433
5434 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005435
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005436 // Used only in code generation.
5437 bool MustDoNullCheck() const { return must_do_null_check_; }
5438 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5439
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005440 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
5441 return (check_kind == TypeCheckKind::kExactCheck)
5442 ? SideEffects::None()
5443 // Mips currently does runtime calls for any other checks.
5444 : SideEffects::CanTriggerGC();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005445 }
5446
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005447 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005448
5449 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005450 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005451 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005452
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005453 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5454};
5455
Calin Juravleb1498f62015-02-16 13:13:29 +00005456class HBoundType : public HExpression<1> {
5457 public:
David Brazdilf5552582015-12-27 13:36:12 +00005458 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005459 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
David Brazdilf5552582015-12-27 13:36:12 +00005460 upper_bound_(ReferenceTypeInfo::CreateInvalid()),
5461 upper_can_be_null_(true),
5462 can_be_null_(true) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005463 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005464 SetRawInputAt(0, input);
5465 }
5466
David Brazdilf5552582015-12-27 13:36:12 +00005467 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005468 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5469 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
David Brazdilf5552582015-12-27 13:36:12 +00005470 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005471
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005472 void SetCanBeNull(bool can_be_null) {
5473 DCHECK(upper_can_be_null_ || !can_be_null);
5474 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005475 }
5476
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005477 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5478
Calin Juravleb1498f62015-02-16 13:13:29 +00005479 DECLARE_INSTRUCTION(BoundType);
5480
5481 private:
5482 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005483 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5484 // It is used to bound the type in cases like:
5485 // if (x instanceof ClassX) {
5486 // // uper_bound_ will be ClassX
5487 // }
David Brazdilf5552582015-12-27 13:36:12 +00005488 ReferenceTypeInfo upper_bound_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005489 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5490 // is false then can_be_null_ cannot be true).
David Brazdilf5552582015-12-27 13:36:12 +00005491 bool upper_can_be_null_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005492 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005493
5494 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5495};
5496
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005497class HCheckCast : public HTemplateInstruction<2> {
5498 public:
5499 HCheckCast(HInstruction* object,
5500 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005501 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005502 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005503 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005504 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005505 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005506 SetRawInputAt(0, object);
5507 SetRawInputAt(1, constant);
5508 }
5509
5510 bool CanBeMoved() const OVERRIDE { return true; }
5511
5512 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5513 return true;
5514 }
5515
5516 bool NeedsEnvironment() const OVERRIDE {
5517 // Instruction may throw a CheckCastError.
5518 return true;
5519 }
5520
5521 bool CanThrow() const OVERRIDE { return true; }
5522
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005523 bool MustDoNullCheck() const { return must_do_null_check_; }
5524 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005525 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005526
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005527 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005528
5529 DECLARE_INSTRUCTION(CheckCast);
5530
5531 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005532 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005533 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005534
5535 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005536};
5537
Calin Juravle27df7582015-04-17 19:12:31 +01005538class HMemoryBarrier : public HTemplateInstruction<0> {
5539 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005540 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005541 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005542 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005543 barrier_kind_(barrier_kind) {}
5544
5545 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5546
5547 DECLARE_INSTRUCTION(MemoryBarrier);
5548
5549 private:
5550 const MemBarrierKind barrier_kind_;
5551
5552 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5553};
5554
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005555class HMonitorOperation : public HTemplateInstruction<1> {
5556 public:
5557 enum OperationKind {
5558 kEnter,
5559 kExit,
5560 };
5561
5562 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005563 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005564 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5565 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005566 SetRawInputAt(0, object);
5567 }
5568
5569 // Instruction may throw a Java exception, so we need an environment.
David Brazdilbff75032015-07-08 17:26:51 +00005570 bool NeedsEnvironment() const OVERRIDE { return CanThrow(); }
5571
5572 bool CanThrow() const OVERRIDE {
5573 // Verifier guarantees that monitor-exit cannot throw.
5574 // This is important because it allows the HGraphBuilder to remove
5575 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5576 return IsEnter();
5577 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005578
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005579
5580 bool IsEnter() const { return kind_ == kEnter; }
5581
5582 DECLARE_INSTRUCTION(MonitorOperation);
5583
Calin Juravle52c48962014-12-16 17:02:57 +00005584 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005585 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005586
5587 private:
5588 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5589};
5590
Vladimir Markof9f64412015-09-02 14:05:49 +01005591class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005592 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005593 MoveOperands(Location source,
5594 Location destination,
5595 Primitive::Type type,
5596 HInstruction* instruction)
5597 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005598
5599 Location GetSource() const { return source_; }
5600 Location GetDestination() const { return destination_; }
5601
5602 void SetSource(Location value) { source_ = value; }
5603 void SetDestination(Location value) { destination_ = value; }
5604
5605 // The parallel move resolver marks moves as "in-progress" by clearing the
5606 // destination (but not the source).
5607 Location MarkPending() {
5608 DCHECK(!IsPending());
5609 Location dest = destination_;
5610 destination_ = Location::NoLocation();
5611 return dest;
5612 }
5613
5614 void ClearPending(Location dest) {
5615 DCHECK(IsPending());
5616 destination_ = dest;
5617 }
5618
5619 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00005620 DCHECK(source_.IsValid() || destination_.IsInvalid());
5621 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005622 }
5623
5624 // True if this blocks a move from the given location.
5625 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005626 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005627 }
5628
5629 // A move is redundant if it's been eliminated, if its source and
5630 // destination are the same, or if its destination is unneeded.
5631 bool IsRedundant() const {
5632 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5633 }
5634
5635 // We clear both operands to indicate move that's been eliminated.
5636 void Eliminate() {
5637 source_ = destination_ = Location::NoLocation();
5638 }
5639
5640 bool IsEliminated() const {
5641 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5642 return source_.IsInvalid();
5643 }
5644
Alexey Frunze4dda3372015-06-01 18:31:49 -07005645 Primitive::Type GetType() const { return type_; }
5646
Nicolas Geoffray90218252015-04-15 11:56:51 +01005647 bool Is64BitMove() const {
5648 return Primitive::Is64BitType(type_);
5649 }
5650
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005651 HInstruction* GetInstruction() const { return instruction_; }
5652
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005653 private:
5654 Location source_;
5655 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005656 // The type this move is for.
5657 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005658 // The instruction this move is assocatied with. Null when this move is
5659 // for moving an input in the expected locations of user (including a phi user).
5660 // This is only used in debug mode, to ensure we do not connect interval siblings
5661 // in the same parallel move.
5662 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005663};
5664
Roland Levillainc9285912015-12-18 10:38:42 +00005665std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
5666
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005667static constexpr size_t kDefaultNumberOfMoves = 4;
5668
5669class HParallelMove : public HTemplateInstruction<0> {
5670 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005671 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005672 : HTemplateInstruction(SideEffects::None(), dex_pc),
5673 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5674 moves_.reserve(kDefaultNumberOfMoves);
5675 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005676
Nicolas Geoffray90218252015-04-15 11:56:51 +01005677 void AddMove(Location source,
5678 Location destination,
5679 Primitive::Type type,
5680 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005681 DCHECK(source.IsValid());
5682 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005683 if (kIsDebugBuild) {
5684 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005685 for (const MoveOperands& move : moves_) {
5686 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005687 // Special case the situation where the move is for the spill slot
5688 // of the instruction.
5689 if ((GetPrevious() == instruction)
5690 || ((GetPrevious() == nullptr)
5691 && instruction->IsPhi()
5692 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005693 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005694 << "Doing parallel moves for the same instruction.";
5695 } else {
5696 DCHECK(false) << "Doing parallel moves for the same instruction.";
5697 }
5698 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005699 }
5700 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005701 for (const MoveOperands& move : moves_) {
5702 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005703 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005704 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005705 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005706 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005707 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005708 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005709 }
5710
Vladimir Marko225b6462015-09-28 12:17:40 +01005711 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005712 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005713 }
5714
Vladimir Marko225b6462015-09-28 12:17:40 +01005715 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005716
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005717 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005718
5719 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005720 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005721
5722 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5723};
5724
Mark Mendell0616ae02015-04-17 12:49:27 -04005725} // namespace art
5726
Vladimir Markob4536b72015-11-24 13:45:23 +00005727#ifdef ART_ENABLE_CODEGEN_arm
5728#include "nodes_arm.h"
5729#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005730#ifdef ART_ENABLE_CODEGEN_arm64
5731#include "nodes_arm64.h"
5732#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005733#ifdef ART_ENABLE_CODEGEN_x86
5734#include "nodes_x86.h"
5735#endif
5736
5737namespace art {
5738
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005739class HGraphVisitor : public ValueObject {
5740 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005741 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5742 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005743
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005744 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005745 virtual void VisitBasicBlock(HBasicBlock* block);
5746
Roland Levillain633021e2014-10-01 14:12:25 +01005747 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005748 void VisitInsertionOrder();
5749
Roland Levillain633021e2014-10-01 14:12:25 +01005750 // Visit the graph following dominator tree reverse post-order.
5751 void VisitReversePostOrder();
5752
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005753 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005754
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005755 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005756#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005757 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5758
5759 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5760
5761#undef DECLARE_VISIT_INSTRUCTION
5762
5763 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005764 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005765
5766 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5767};
5768
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005769class HGraphDelegateVisitor : public HGraphVisitor {
5770 public:
5771 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5772 virtual ~HGraphDelegateVisitor() {}
5773
5774 // Visit functions that delegate to to super class.
5775#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005776 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005777
5778 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5779
5780#undef DECLARE_VISIT_INSTRUCTION
5781
5782 private:
5783 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5784};
5785
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005786class HInsertionOrderIterator : public ValueObject {
5787 public:
5788 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5789
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005790 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005791 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005792 void Advance() { ++index_; }
5793
5794 private:
5795 const HGraph& graph_;
5796 size_t index_;
5797
5798 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5799};
5800
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005801class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005802 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005803 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5804 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005805 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005806 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005807
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005808 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5809 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005810 void Advance() { ++index_; }
5811
5812 private:
5813 const HGraph& graph_;
5814 size_t index_;
5815
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005816 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005817};
5818
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005819class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005820 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005821 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005822 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005823 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005824 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005825 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005826
5827 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005828 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005829 void Advance() { --index_; }
5830
5831 private:
5832 const HGraph& graph_;
5833 size_t index_;
5834
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005835 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005836};
5837
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005838class HLinearPostOrderIterator : public ValueObject {
5839 public:
5840 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005841 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005842
5843 bool Done() const { return index_ == 0; }
5844
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005845 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005846
5847 void Advance() {
5848 --index_;
5849 DCHECK_GE(index_, 0U);
5850 }
5851
5852 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005853 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005854 size_t index_;
5855
5856 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5857};
5858
5859class HLinearOrderIterator : public ValueObject {
5860 public:
5861 explicit HLinearOrderIterator(const HGraph& graph)
5862 : order_(graph.GetLinearOrder()), index_(0) {}
5863
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005864 bool Done() const { return index_ == order_.size(); }
5865 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005866 void Advance() { ++index_; }
5867
5868 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005869 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005870 size_t index_;
5871
5872 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5873};
5874
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005875// Iterator over the blocks that art part of the loop. Includes blocks part
5876// of an inner loop. The order in which the blocks are iterated is on their
5877// block id.
5878class HBlocksInLoopIterator : public ValueObject {
5879 public:
5880 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5881 : blocks_in_loop_(info.GetBlocks()),
5882 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5883 index_(0) {
5884 if (!blocks_in_loop_.IsBitSet(index_)) {
5885 Advance();
5886 }
5887 }
5888
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005889 bool Done() const { return index_ == blocks_.size(); }
5890 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005891 void Advance() {
5892 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005893 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005894 if (blocks_in_loop_.IsBitSet(index_)) {
5895 break;
5896 }
5897 }
5898 }
5899
5900 private:
5901 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005902 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005903 size_t index_;
5904
5905 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5906};
5907
Mingyao Yang3584bce2015-05-19 16:01:59 -07005908// Iterator over the blocks that art part of the loop. Includes blocks part
5909// of an inner loop. The order in which the blocks are iterated is reverse
5910// post order.
5911class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5912 public:
5913 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5914 : blocks_in_loop_(info.GetBlocks()),
5915 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5916 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005917 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005918 Advance();
5919 }
5920 }
5921
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005922 bool Done() const { return index_ == blocks_.size(); }
5923 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005924 void Advance() {
5925 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005926 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5927 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005928 break;
5929 }
5930 }
5931 }
5932
5933 private:
5934 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005935 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07005936 size_t index_;
5937
5938 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
5939};
5940
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00005941inline int64_t Int64FromConstant(HConstant* constant) {
5942 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
5943 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
5944 : constant->AsLongConstant()->GetValue();
5945}
5946
Vladimir Marko58155012015-08-19 12:49:41 +00005947inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
5948 // For the purposes of the compiler, the dex files must actually be the same object
5949 // if we want to safely treat them as the same. This is especially important for JIT
5950 // as custom class loaders can open the same underlying file (or memory) multiple
5951 // times and provide different class resolution but no two class loaders should ever
5952 // use the same DexFile object - doing so is an unsupported hack that can lead to
5953 // all sorts of weird failures.
5954 return &lhs == &rhs;
5955}
5956
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00005957#define INSTRUCTION_TYPE_CHECK(type, super) \
5958 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
5959 inline const H##type* HInstruction::As##type() const { \
5960 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
5961 } \
5962 inline H##type* HInstruction::As##type() { \
5963 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
5964 }
5965
5966 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
5967#undef INSTRUCTION_TYPE_CHECK
5968
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005969} // namespace art
5970
5971#endif // ART_COMPILER_OPTIMIZING_NODES_H_