blob: 75675100064a29d92f2b84bb202026f92a6bf323 [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 {
David Brazdilbadd8262016-02-02 16:28:56 +0000101 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000102 kAnalysisFailThrowCatchLoop,
103 kAnalysisFailAmbiguousArrayOp,
104 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000105};
106
Vladimir Markof9f64412015-09-02 14:05:49 +0100107class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100108 public:
109 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
110
111 void AddInstruction(HInstruction* instruction);
112 void RemoveInstruction(HInstruction* instruction);
113
David Brazdilc3d743f2015-04-22 13:40:50 +0100114 // Insert `instruction` before/after an existing instruction `cursor`.
115 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
116 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
117
Roland Levillain6b469232014-09-25 10:10:38 +0100118 // Return true if this list contains `instruction`.
119 bool Contains(HInstruction* instruction) const;
120
Roland Levillainccc07a92014-09-16 14:48:16 +0100121 // Return true if `instruction1` is found before `instruction2` in
122 // this instruction list and false otherwise. Abort if none
123 // of these instructions is found.
124 bool FoundBefore(const HInstruction* instruction1,
125 const HInstruction* instruction2) const;
126
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000127 bool IsEmpty() const { return first_instruction_ == nullptr; }
128 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
129
130 // Update the block of all instructions to be `block`.
131 void SetBlockOfInstructions(HBasicBlock* block) const;
132
133 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000134 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000135 void Add(const HInstructionList& instruction_list);
136
David Brazdil2d7352b2015-04-20 14:52:42 +0100137 // Return the number of instructions in the list. This is an expensive operation.
138 size_t CountSize() const;
139
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100140 private:
141 HInstruction* first_instruction_;
142 HInstruction* last_instruction_;
143
144 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000145 friend class HGraph;
146 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100147 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100148 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100149
150 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
151};
152
David Brazdil4833f5a2015-12-16 10:37:39 +0000153class ReferenceTypeInfo : ValueObject {
154 public:
155 typedef Handle<mirror::Class> TypeHandle;
156
157 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact) {
158 // The constructor will check that the type_handle is valid.
159 return ReferenceTypeInfo(type_handle, is_exact);
160 }
161
162 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
163
Vladimir Markof39745e2016-01-26 12:16:55 +0000164 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000165 return handle.GetReference() != nullptr;
166 }
167
168 bool IsValid() const SHARED_REQUIRES(Locks::mutator_lock_) {
169 return IsValidHandle(type_handle_);
170 }
171
172 bool IsExact() const { return is_exact_; }
173
174 bool IsObjectClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
175 DCHECK(IsValid());
176 return GetTypeHandle()->IsObjectClass();
177 }
178
179 bool IsStringClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
180 DCHECK(IsValid());
181 return GetTypeHandle()->IsStringClass();
182 }
183
184 bool IsObjectArray() const SHARED_REQUIRES(Locks::mutator_lock_) {
185 DCHECK(IsValid());
186 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
187 }
188
189 bool IsInterface() const SHARED_REQUIRES(Locks::mutator_lock_) {
190 DCHECK(IsValid());
191 return GetTypeHandle()->IsInterface();
192 }
193
194 bool IsArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
195 DCHECK(IsValid());
196 return GetTypeHandle()->IsArrayClass();
197 }
198
199 bool IsPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
200 DCHECK(IsValid());
201 return GetTypeHandle()->IsPrimitiveArray();
202 }
203
204 bool IsNonPrimitiveArrayClass() const SHARED_REQUIRES(Locks::mutator_lock_) {
205 DCHECK(IsValid());
206 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
207 }
208
209 bool CanArrayHold(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
210 DCHECK(IsValid());
211 if (!IsExact()) return false;
212 if (!IsArrayClass()) return false;
213 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
214 }
215
216 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
217 DCHECK(IsValid());
218 if (!IsExact()) return false;
219 if (!IsArrayClass()) return false;
220 if (!rti.IsArrayClass()) return false;
221 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
222 rti.GetTypeHandle()->GetComponentType());
223 }
224
225 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
226
227 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
228 DCHECK(IsValid());
229 DCHECK(rti.IsValid());
230 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
231 }
232
233 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
234 DCHECK(IsValid());
235 DCHECK(rti.IsValid());
236 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
237 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
238 }
239
240 // Returns true if the type information provide the same amount of details.
241 // Note that it does not mean that the instructions have the same actual type
242 // (because the type can be the result of a merge).
David Brazdilf5552582015-12-27 13:36:12 +0000243 bool IsEqual(ReferenceTypeInfo rti) const SHARED_REQUIRES(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000244 if (!IsValid() && !rti.IsValid()) {
245 // Invalid types are equal.
246 return true;
247 }
248 if (!IsValid() || !rti.IsValid()) {
249 // One is valid, the other not.
250 return false;
251 }
252 return IsExact() == rti.IsExact()
253 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
254 }
255
256 private:
257 ReferenceTypeInfo();
258 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact);
259
260 // The class of the object.
261 TypeHandle type_handle_;
262 // Whether or not the type is exact or a superclass of the actual type.
263 // Whether or not we have any information about this type.
264 bool is_exact_;
265};
266
267std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
268
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000269// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100270class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000271 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100272 HGraph(ArenaAllocator* arena,
273 const DexFile& dex_file,
274 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100275 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700276 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100277 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100278 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000279 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100280 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000281 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100282 blocks_(arena->Adapter(kArenaAllocBlockList)),
283 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
284 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700285 entry_block_(nullptr),
286 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100287 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100288 number_of_vregs_(0),
289 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000290 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400291 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000292 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000293 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000294 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000295 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100296 dex_file_(dex_file),
297 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100298 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100299 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100300 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700301 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000302 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100303 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
304 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
305 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
306 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000307 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000308 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
309 osr_(osr) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100310 blocks_.reserve(kDefaultNumberOfBlocks);
311 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000312
David Brazdilbadd8262016-02-02 16:28:56 +0000313 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
314 void InitializeInexactObjectRTI(StackHandleScopeCollection* handles);
315
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000316 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100317 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
318
David Brazdil69ba7b72015-06-23 18:27:30 +0100319 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000320 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100321
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000322 HBasicBlock* GetEntryBlock() const { return entry_block_; }
323 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100324 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000325
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000326 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
327 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000328
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000329 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100330
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100331 void ComputeDominanceInformation();
332 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000333 void ClearLoopInformation();
334 void FindBackEdges(ArenaBitVector* visited);
335 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100336 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100337 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000338
David Brazdil4833f5a2015-12-16 10:37:39 +0000339 // Analyze all natural loops in this graph. Returns a code specifying that it
340 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000341 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000342 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100343
David Brazdilffee3d32015-07-06 11:48:53 +0100344 // Iterate over blocks to compute try block membership. Needs reverse post
345 // order and loop information.
346 void ComputeTryBlockInformation();
347
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000348 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000349 // Returns the instruction to replace the invoke expression or null if the
350 // invoke is for a void method. Note that the caller is responsible for replacing
351 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000352 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000353
Mingyao Yang3584bce2015-05-19 16:01:59 -0700354 // Need to add a couple of blocks to test if the loop body is entered and
355 // put deoptimization instructions, etc.
356 void TransformLoopHeaderForBCE(HBasicBlock* header);
357
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000358 // Removes `block` from the graph. Assumes `block` has been disconnected from
359 // other blocks and has no instructions or phis.
360 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000361
David Brazdilfc6a86a2015-06-26 10:33:45 +0000362 // Splits the edge between `block` and `successor` while preserving the
363 // indices in the predecessor/successor lists. If there are multiple edges
364 // between the blocks, the lowest indices are used.
365 // Returns the new block which is empty and has the same dex pc as `successor`.
366 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
367
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100368 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
369 void SimplifyLoop(HBasicBlock* header);
370
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000371 int32_t GetNextInstructionId() {
372 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000373 return current_instruction_id_++;
374 }
375
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000376 int32_t GetCurrentInstructionId() const {
377 return current_instruction_id_;
378 }
379
380 void SetCurrentInstructionId(int32_t id) {
381 current_instruction_id_ = id;
382 }
383
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100384 uint16_t GetMaximumNumberOfOutVRegs() const {
385 return maximum_number_of_out_vregs_;
386 }
387
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000388 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
389 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100390 }
391
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100392 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
393 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
394 }
395
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000396 void UpdateTemporariesVRegSlots(size_t slots) {
397 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100398 }
399
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000400 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100401 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000402 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100403 }
404
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100405 void SetNumberOfVRegs(uint16_t number_of_vregs) {
406 number_of_vregs_ = number_of_vregs;
407 }
408
409 uint16_t GetNumberOfVRegs() const {
410 return number_of_vregs_;
411 }
412
413 void SetNumberOfInVRegs(uint16_t value) {
414 number_of_in_vregs_ = value;
415 }
416
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100417 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100418 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100419 return number_of_vregs_ - number_of_in_vregs_;
420 }
421
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100422 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100423 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100424 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100425
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100426 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100427 return linear_order_;
428 }
429
Mark Mendell1152c922015-04-24 17:06:35 -0400430 bool HasBoundsChecks() const {
431 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800432 }
433
Mark Mendell1152c922015-04-24 17:06:35 -0400434 void SetHasBoundsChecks(bool value) {
435 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800436 }
437
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100438 bool ShouldGenerateConstructorBarrier() const {
439 return should_generate_constructor_barrier_;
440 }
441
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000442 bool IsDebuggable() const { return debuggable_; }
443
David Brazdil8d5b8b22015-03-24 10:51:52 +0000444 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000445 // already, it is created and inserted into the graph. This method is only for
446 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600447 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000448
449 // TODO: This is problematic for the consistency of reference type propagation
450 // because it can be created anytime after the pass and thus it will be left
451 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600452 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000453
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600454 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
455 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000456 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600457 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
458 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000459 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600460 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
461 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000462 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600463 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
464 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000465 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000466
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100467 HCurrentMethod* GetCurrentMethod();
468
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100469 const DexFile& GetDexFile() const {
470 return dex_file_;
471 }
472
473 uint32_t GetMethodIdx() const {
474 return method_idx_;
475 }
476
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100477 InvokeType GetInvokeType() const {
478 return invoke_type_;
479 }
480
Mark Mendellc4701932015-04-10 13:18:51 -0400481 InstructionSet GetInstructionSet() const {
482 return instruction_set_;
483 }
484
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000485 bool IsCompilingOsr() const { return osr_; }
486
David Brazdil77a48ae2015-09-15 12:34:04 +0000487 bool HasTryCatch() const { return has_try_catch_; }
488 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100489
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000490 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
491 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
492
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100493 ArtMethod* GetArtMethod() const { return art_method_; }
494 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
495
Mark Mendellf6529172015-11-17 11:16:56 -0500496 // Returns an instruction with the opposite boolean value from 'cond'.
497 // The instruction has been inserted into the graph, either as a constant, or
498 // before cursor.
499 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
500
David Brazdil2d7352b2015-04-20 14:52:42 +0100501 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000502 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100503 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000504
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000505 template <class InstructionType, typename ValueType>
506 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600507 ArenaSafeMap<ValueType, InstructionType*>* cache,
508 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000509 // Try to find an existing constant of the given value.
510 InstructionType* constant = nullptr;
511 auto cached_constant = cache->find(value);
512 if (cached_constant != cache->end()) {
513 constant = cached_constant->second;
514 }
515
516 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100517 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000518 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600519 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000520 cache->Overwrite(value, constant);
521 InsertConstant(constant);
522 }
523 return constant;
524 }
525
David Brazdil8d5b8b22015-03-24 10:51:52 +0000526 void InsertConstant(HConstant* instruction);
527
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000528 // Cache a float constant into the graph. This method should only be
529 // called by the SsaBuilder when creating "equivalent" instructions.
530 void CacheFloatConstant(HFloatConstant* constant);
531
532 // See CacheFloatConstant comment.
533 void CacheDoubleConstant(HDoubleConstant* constant);
534
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000535 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000536
537 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100538 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000539
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100540 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100541 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000542
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100543 // List of blocks to perform a linear order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100544 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100545
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000546 HBasicBlock* entry_block_;
547 HBasicBlock* exit_block_;
548
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100549 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100550 uint16_t maximum_number_of_out_vregs_;
551
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100552 // The number of virtual registers in this method. Contains the parameters.
553 uint16_t number_of_vregs_;
554
555 // The number of virtual registers used by parameters of this method.
556 uint16_t number_of_in_vregs_;
557
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000558 // Number of vreg size slots that the temporaries use (used in baseline compiler).
559 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100560
Mark Mendell1152c922015-04-24 17:06:35 -0400561 // Has bounds checks. We can totally skip BCE if it's false.
562 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800563
David Brazdil77a48ae2015-09-15 12:34:04 +0000564 // Flag whether there are any try/catch blocks in the graph. We will skip
565 // try/catch-related passes if false.
566 bool has_try_catch_;
567
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000568 // Flag whether there are any irreducible loops in the graph.
569 bool has_irreducible_loops_;
570
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000571 // Indicates whether the graph should be compiled in a way that
572 // ensures full debuggability. If false, we can apply more
573 // aggressive optimizations that may limit the level of debugging.
574 const bool debuggable_;
575
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000576 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000577 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000578
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100579 // The dex file from which the method is from.
580 const DexFile& dex_file_;
581
582 // The method index in the dex file.
583 const uint32_t method_idx_;
584
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100585 // If inlined, this encodes how the callee is being invoked.
586 const InvokeType invoke_type_;
587
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100588 // Whether the graph has been transformed to SSA form. Only used
589 // in debug mode to ensure we are not using properties only valid
590 // for non-SSA form (like the number of temporaries).
591 bool in_ssa_form_;
592
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100593 const bool should_generate_constructor_barrier_;
594
Mathieu Chartiere401d142015-04-22 13:56:20 -0700595 const InstructionSet instruction_set_;
596
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000597 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000598 HNullConstant* cached_null_constant_;
599 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000600 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000601 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000602 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000603
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100604 HCurrentMethod* cached_current_method_;
605
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100606 // The ArtMethod this graph is for. Note that for AOT, it may be null,
607 // for example for methods whose declaring class could not be resolved
608 // (such as when the superclass could not be found).
609 ArtMethod* art_method_;
610
David Brazdil4833f5a2015-12-16 10:37:39 +0000611 // Keep the RTI of inexact Object to avoid having to pass stack handle
612 // collection pointer to passes which may create NullConstant.
613 ReferenceTypeInfo inexact_object_rti_;
614
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000615 // Whether we are compiling this graph for on stack replacement: this will
616 // make all loops seen as irreducible and emit special stack maps to mark
617 // compiled code entries which the interpreter can directly jump to.
618 const bool osr_;
619
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000620 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100621 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000622 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000623 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000624 DISALLOW_COPY_AND_ASSIGN(HGraph);
625};
626
Vladimir Markof9f64412015-09-02 14:05:49 +0100627class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000628 public:
629 HLoopInformation(HBasicBlock* header, HGraph* graph)
630 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100631 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000632 irreducible_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100633 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100634 // Make bit vector growable, as the number of blocks may change.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100635 blocks_(graph->GetArena(), graph->GetBlocks().size(), true) {
636 back_edges_.reserve(kDefaultNumberOfBackEdges);
637 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100638
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000639 bool IsIrreducible() const { return irreducible_; }
640
641 void Dump(std::ostream& os);
642
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100643 HBasicBlock* GetHeader() const {
644 return header_;
645 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000646
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000647 void SetHeader(HBasicBlock* block) {
648 header_ = block;
649 }
650
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100651 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
652 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
653 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
654
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000655 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100656 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000657 }
658
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100659 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100660 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100661 }
662
David Brazdil46e2a392015-03-16 17:31:52 +0000663 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100664 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100665 }
666
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000667 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100668 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000669 }
670
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100671 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100672
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100673 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100674 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100675 }
676
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100677 // Returns the lifetime position of the back edge that has the
678 // greatest lifetime position.
679 size_t GetLifetimeEnd() const;
680
681 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100682 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100683 }
684
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000685 // Finds blocks that are part of this loop.
686 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100687
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100688 // Returns whether this loop information contains `block`.
689 // Note that this loop information *must* be populated before entering this function.
690 bool Contains(const HBasicBlock& block) const;
691
692 // Returns whether this loop information is an inner loop of `other`.
693 // Note that `other` *must* be populated before entering this function.
694 bool IsIn(const HLoopInformation& other) const;
695
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800696 // Returns true if instruction is not defined within this loop.
697 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700698
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100699 const ArenaBitVector& GetBlocks() const { return blocks_; }
700
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000701 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000702 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000703
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000704 void ClearAllBlocks() {
705 blocks_.ClearAllBits();
706 }
707
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000708 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100709 // Internal recursive implementation of `Populate`.
710 void PopulateRecursive(HBasicBlock* block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000711 void PopulateIrreducibleRecursive(HBasicBlock* block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100712
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000713 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100714 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000715 bool irreducible_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100716 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100717 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000718
719 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
720};
721
David Brazdilec16f792015-08-19 15:04:01 +0100722// Stores try/catch information for basic blocks.
723// Note that HGraph is constructed so that catch blocks cannot simultaneously
724// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100725class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100726 public:
727 // Try block information constructor.
728 explicit TryCatchInformation(const HTryBoundary& try_entry)
729 : try_entry_(&try_entry),
730 catch_dex_file_(nullptr),
731 catch_type_index_(DexFile::kDexNoIndex16) {
732 DCHECK(try_entry_ != nullptr);
733 }
734
735 // Catch block information constructor.
736 TryCatchInformation(uint16_t catch_type_index, const DexFile& dex_file)
737 : try_entry_(nullptr),
738 catch_dex_file_(&dex_file),
739 catch_type_index_(catch_type_index) {}
740
741 bool IsTryBlock() const { return try_entry_ != nullptr; }
742
743 const HTryBoundary& GetTryEntry() const {
744 DCHECK(IsTryBlock());
745 return *try_entry_;
746 }
747
748 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
749
750 bool IsCatchAllTypeIndex() const {
751 DCHECK(IsCatchBlock());
752 return catch_type_index_ == DexFile::kDexNoIndex16;
753 }
754
755 uint16_t GetCatchTypeIndex() const {
756 DCHECK(IsCatchBlock());
757 return catch_type_index_;
758 }
759
760 const DexFile& GetCatchDexFile() const {
761 DCHECK(IsCatchBlock());
762 return *catch_dex_file_;
763 }
764
765 private:
766 // One of possibly several TryBoundary instructions entering the block's try.
767 // Only set for try blocks.
768 const HTryBoundary* try_entry_;
769
770 // Exception type information. Only set for catch blocks.
771 const DexFile* catch_dex_file_;
772 const uint16_t catch_type_index_;
773};
774
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100775static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100776static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100777
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000778// A block in a method. Contains the list of instructions represented
779// as a double linked list. Each block knows its predecessors and
780// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100781
Vladimir Markof9f64412015-09-02 14:05:49 +0100782class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000783 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600784 HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000785 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000786 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
787 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000788 loop_information_(nullptr),
789 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000790 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100791 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100792 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100793 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000794 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000795 try_catch_information_(nullptr) {
796 predecessors_.reserve(kDefaultNumberOfPredecessors);
797 successors_.reserve(kDefaultNumberOfSuccessors);
798 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
799 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000800
Vladimir Marko60584552015-09-03 13:35:12 +0000801 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100802 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000803 }
804
Vladimir Marko60584552015-09-03 13:35:12 +0000805 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100806 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000807 }
808
David Brazdild26a4112015-11-10 11:07:31 +0000809 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
810 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
811
Vladimir Marko60584552015-09-03 13:35:12 +0000812 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
813 return ContainsElement(successors_, block, start_from);
814 }
815
816 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100817 return dominated_blocks_;
818 }
819
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100820 bool IsEntryBlock() const {
821 return graph_->GetEntryBlock() == this;
822 }
823
824 bool IsExitBlock() const {
825 return graph_->GetExitBlock() == this;
826 }
827
David Brazdil46e2a392015-03-16 17:31:52 +0000828 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000829 bool IsSingleTryBoundary() const;
830
831 // Returns true if this block emits nothing but a jump.
832 bool IsSingleJump() const {
833 HLoopInformation* loop_info = GetLoopInformation();
834 return (IsSingleGoto() || IsSingleTryBoundary())
835 // Back edges generate a suspend check.
836 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
837 }
David Brazdil46e2a392015-03-16 17:31:52 +0000838
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000839 void AddBackEdge(HBasicBlock* back_edge) {
840 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000841 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000842 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100843 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000844 loop_information_->AddBackEdge(back_edge);
845 }
846
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000847 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000848 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000849
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100850 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000851 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600852 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000853
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000854 HBasicBlock* GetDominator() const { return dominator_; }
855 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000856 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
857
858 void RemoveDominatedBlock(HBasicBlock* block) {
859 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100860 }
Vladimir Marko60584552015-09-03 13:35:12 +0000861
862 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
863 ReplaceElement(dominated_blocks_, existing, new_block);
864 }
865
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100866 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000867
868 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100869 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000870 }
871
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100872 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
873 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100874 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100875 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100876 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
877 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000878
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000879 HInstruction* GetFirstInstructionDisregardMoves() const;
880
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000881 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000882 successors_.push_back(block);
883 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000884 }
885
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100886 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
887 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100888 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000889 new_block->predecessors_.push_back(this);
890 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000891 }
892
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000893 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
894 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000895 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000896 new_block->successors_.push_back(this);
897 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000898 }
899
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100900 // Insert `this` between `predecessor` and `successor. This method
901 // preserves the indicies, and will update the first edge found between
902 // `predecessor` and `successor`.
903 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
904 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100905 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000906 successor->predecessors_[predecessor_index] = this;
907 predecessor->successors_[successor_index] = this;
908 successors_.push_back(successor);
909 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100910 }
911
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100912 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000913 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100914 }
915
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000916 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000917 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000918 }
919
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100920 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000921 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100922 }
923
924 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000925 predecessors_.push_back(block);
926 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100927 }
928
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100929 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000930 DCHECK_EQ(predecessors_.size(), 2u);
931 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100932 }
933
David Brazdil769c9e52015-04-27 13:54:09 +0100934 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +0000935 DCHECK_EQ(successors_.size(), 2u);
936 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +0100937 }
938
David Brazdilfc6a86a2015-06-26 10:33:45 +0000939 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000940 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100941 }
942
David Brazdilfc6a86a2015-06-26 10:33:45 +0000943 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +0000944 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100945 }
946
David Brazdilfc6a86a2015-06-26 10:33:45 +0000947 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000948 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100949 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000950 }
951
952 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +0000953 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +0100954 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000955 }
956
957 // Returns whether the first occurrence of `predecessor` in the list of
958 // predecessors is at index `idx`.
959 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100960 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000961 return GetPredecessorIndexOf(predecessor) == idx;
962 }
963
David Brazdild7558da2015-09-22 13:04:14 +0100964 // Create a new block between this block and its predecessors. The new block
965 // is added to the graph, all predecessor edges are relinked to it and an edge
966 // is created to `this`. Returns the new empty block. Reverse post order or
967 // loop and try/catch information are not updated.
968 HBasicBlock* CreateImmediateDominator();
969
David Brazdilfc6a86a2015-06-26 10:33:45 +0000970 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +0100971 // created, latter block. Note that this method will add the block to the
972 // graph, create a Goto at the end of the former block and will create an edge
973 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +0100974 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000975 HBasicBlock* SplitBefore(HInstruction* cursor);
976
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000977 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000978 // created block. Note that this method just updates raw block information,
979 // like predecessors, successors, dominators, and instruction list. It does not
980 // update the graph, reverse post order, loop information, nor make sure the
981 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000982 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
983
984 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
985 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000986
David Brazdil9bc43612015-11-05 21:25:24 +0000987 // Split catch block into two blocks after the original move-exception bytecode
988 // instruction, or at the beginning if not present. Returns the newly created,
989 // latter block, or nullptr if such block could not be created (must be dead
990 // in that case). Note that this method just updates raw block information,
991 // like predecessors, successors, dominators, and instruction list. It does not
992 // update the graph, reverse post order, loop information, nor make sure the
993 // blocks are consistent (for example ending with a control flow instruction).
994 HBasicBlock* SplitCatchBlockAfterMoveException();
995
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000996 // Merge `other` at the end of `this`. Successors and dominated blocks of
997 // `other` are changed to be successors and dominated blocks of `this`. Note
998 // that this method does not update the graph, reverse post order, loop
999 // information, nor make sure the blocks are consistent (for example ending
1000 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001001 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001002
1003 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1004 // of `this` are moved to `other`.
1005 // Note that this method does not update the graph, reverse post order, loop
1006 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001007 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001008 void ReplaceWith(HBasicBlock* other);
1009
David Brazdil2d7352b2015-04-20 14:52:42 +01001010 // Merge `other` at the end of `this`. This method updates loops, reverse post
1011 // order, links to predecessors, successors, dominators and deletes the block
1012 // from the graph. The two blocks must be successive, i.e. `this` the only
1013 // predecessor of `other` and vice versa.
1014 void MergeWith(HBasicBlock* other);
1015
1016 // Disconnects `this` from all its predecessors, successors and dominator,
1017 // removes it from all loops it is included in and eventually from the graph.
1018 // The block must not dominate any other block. Predecessors and successors
1019 // are safely updated.
1020 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001021
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001022 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001023 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001024 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001025 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001026 // Replace instruction `initial` with `replacement` within this block.
1027 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1028 HInstruction* replacement);
David Brazdil74eb1b22015-12-14 11:44:01 +00001029 void MoveInstructionBefore(HInstruction* insn, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001030 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001031 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001032 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1033 // instruction list. With 'ensure_safety' set to true, it verifies that the
1034 // instruction is not in use and removes it from the use lists of its inputs.
1035 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1036 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001037 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001038
1039 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001040 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001041 }
1042
Roland Levillain6b879dd2014-09-22 17:13:44 +01001043 bool IsLoopPreHeaderFirstPredecessor() const {
1044 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001045 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001046 }
1047
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001048 bool IsFirstPredecessorBackEdge() const {
1049 DCHECK(IsLoopHeader());
1050 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1051 }
1052
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001053 HLoopInformation* GetLoopInformation() const {
1054 return loop_information_;
1055 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001056
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001057 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001058 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001059 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001060 void SetInLoop(HLoopInformation* info) {
1061 if (IsLoopHeader()) {
1062 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001063 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001064 loop_information_ = info;
1065 } else if (loop_information_->Contains(*info->GetHeader())) {
1066 // Block is currently part of an outer loop. Make it part of this inner loop.
1067 // Note that a non loop header having a loop information means this loop information
1068 // has already been populated
1069 loop_information_ = info;
1070 } else {
1071 // Block is part of an inner loop. Do not update the loop information.
1072 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1073 // at this point, because this method is being called while populating `info`.
1074 }
1075 }
1076
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001077 // Raw update of the loop information.
1078 void SetLoopInformation(HLoopInformation* info) {
1079 loop_information_ = info;
1080 }
1081
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001082 bool IsInLoop() const { return loop_information_ != nullptr; }
1083
David Brazdilec16f792015-08-19 15:04:01 +01001084 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1085
1086 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1087 try_catch_information_ = try_catch_information;
1088 }
1089
1090 bool IsTryBlock() const {
1091 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1092 }
1093
1094 bool IsCatchBlock() const {
1095 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1096 }
David Brazdilffee3d32015-07-06 11:48:53 +01001097
1098 // Returns the try entry that this block's successors should have. They will
1099 // be in the same try, unless the block ends in a try boundary. In that case,
1100 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001101 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001102
David Brazdild7558da2015-09-22 13:04:14 +01001103 bool HasThrowingInstructions() const;
1104
David Brazdila4b8c212015-05-07 09:59:30 +01001105 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001106 bool Dominates(HBasicBlock* block) const;
1107
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001108 size_t GetLifetimeStart() const { return lifetime_start_; }
1109 size_t GetLifetimeEnd() const { return lifetime_end_; }
1110
1111 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1112 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1113
David Brazdil8d5b8b22015-03-24 10:51:52 +00001114 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001115 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001116 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001117 bool HasSinglePhi() const;
1118
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001119 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001120 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001121 ArenaVector<HBasicBlock*> predecessors_;
1122 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001123 HInstructionList instructions_;
1124 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001125 HLoopInformation* loop_information_;
1126 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001127 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001128 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001129 // The dex program counter of the first instruction of this block.
1130 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001131 size_t lifetime_start_;
1132 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001133 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001134
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001135 friend class HGraph;
1136 friend class HInstruction;
1137
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001138 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1139};
1140
David Brazdilb2bd1c52015-03-25 11:17:37 +00001141// Iterates over the LoopInformation of all loops which contain 'block'
1142// from the innermost to the outermost.
1143class HLoopInformationOutwardIterator : public ValueObject {
1144 public:
1145 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1146 : current_(block.GetLoopInformation()) {}
1147
1148 bool Done() const { return current_ == nullptr; }
1149
1150 void Advance() {
1151 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001152 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001153 }
1154
1155 HLoopInformation* Current() const {
1156 DCHECK(!Done());
1157 return current_;
1158 }
1159
1160 private:
1161 HLoopInformation* current_;
1162
1163 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1164};
1165
Alexandre Ramesef20f712015-06-09 10:29:30 +01001166#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001167 M(Above, Condition) \
1168 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001169 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001170 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001171 M(ArrayGet, Instruction) \
1172 M(ArrayLength, Instruction) \
1173 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001174 M(Below, Condition) \
1175 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001176 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001177 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001178 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001179 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001180 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001181 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001182 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001183 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001184 M(CurrentMethod, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001185 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001186 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001187 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001188 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001189 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001190 M(Exit, Instruction) \
1191 M(FloatConstant, Constant) \
1192 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001193 M(GreaterThan, Condition) \
1194 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001195 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001196 M(InstanceFieldGet, Instruction) \
1197 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001198 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001199 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001200 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001201 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001202 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001203 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001204 M(LessThan, Condition) \
1205 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001206 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001207 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001208 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001209 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001210 M(Local, Instruction) \
1211 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001212 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001213 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001214 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001215 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001216 M(Neg, UnaryOperation) \
1217 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001218 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001219 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001220 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001221 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001222 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001223 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001224 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001225 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001226 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001227 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001228 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001229 M(Return, Instruction) \
1230 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001231 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001232 M(Shl, BinaryOperation) \
1233 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001234 M(StaticFieldGet, Instruction) \
1235 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001236 M(UnresolvedInstanceFieldGet, Instruction) \
1237 M(UnresolvedInstanceFieldSet, Instruction) \
1238 M(UnresolvedStaticFieldGet, Instruction) \
1239 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001240 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001241 M(StoreLocal, Instruction) \
1242 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001243 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001244 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001245 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001246 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001247 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001248 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001249
Vladimir Markob4536b72015-11-24 13:45:23 +00001250#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001251#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001252#else
1253#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1254 M(ArmDexCacheArraysBase, Instruction)
1255#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001256
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001257#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001258#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001259#else
1260#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001261 M(Arm64DataProcWithShifterOp, Instruction) \
Nicolas Geoffray6b5afdd2016-01-22 09:31:52 +00001262 M(Arm64IntermediateAddress, Instruction) \
1263 M(Arm64MultiplyAccumulate, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001264#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001265
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001266#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1267
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001268#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1269
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001270#ifndef ART_ENABLE_CODEGEN_x86
1271#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1272#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001273#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1274 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001275 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001276 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001277 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001278#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001279
1280#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1281
1282#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1283 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1284 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1285 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001286 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001287 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001288 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1289 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1290
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001291#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1292 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001293 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001294 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001295 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001296 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001297
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001298#define FOR_EACH_INSTRUCTION(M) \
1299 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1300 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1301
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001302#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001303FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1304#undef FORWARD_DECLARATION
1305
Roland Levillainccc07a92014-09-16 14:48:16 +01001306#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001307 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001308 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001309 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001310 return other->Is##type(); \
1311 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001312 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001313
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001314#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1315 bool Is##type() const { return As##type() != nullptr; } \
1316 const H##type* As##type() const { return this; } \
1317 H##type* As##type() { return this; }
1318
David Brazdiled596192015-01-23 10:39:45 +00001319template <typename T> class HUseList;
1320
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001321template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001322class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001323 public:
David Brazdiled596192015-01-23 10:39:45 +00001324 HUseListNode* GetPrevious() const { return prev_; }
1325 HUseListNode* GetNext() const { return next_; }
1326 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001327 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001328 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001329
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001330 private:
David Brazdiled596192015-01-23 10:39:45 +00001331 HUseListNode(T user, size_t index)
1332 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1333
1334 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001335 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001336 HUseListNode<T>* prev_;
1337 HUseListNode<T>* next_;
1338
1339 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001340
1341 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1342};
1343
David Brazdiled596192015-01-23 10:39:45 +00001344template <typename T>
1345class HUseList : public ValueObject {
1346 public:
1347 HUseList() : first_(nullptr) {}
1348
1349 void Clear() {
1350 first_ = nullptr;
1351 }
1352
1353 // Adds a new entry at the beginning of the use list and returns
1354 // the newly created node.
1355 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001356 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001357 if (IsEmpty()) {
1358 first_ = new_node;
1359 } else {
1360 first_->prev_ = new_node;
1361 new_node->next_ = first_;
1362 first_ = new_node;
1363 }
1364 return new_node;
1365 }
1366
1367 HUseListNode<T>* GetFirst() const {
1368 return first_;
1369 }
1370
1371 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001372 DCHECK(node != nullptr);
1373 DCHECK(Contains(node));
1374
David Brazdiled596192015-01-23 10:39:45 +00001375 if (node->prev_ != nullptr) {
1376 node->prev_->next_ = node->next_;
1377 }
1378 if (node->next_ != nullptr) {
1379 node->next_->prev_ = node->prev_;
1380 }
1381 if (node == first_) {
1382 first_ = node->next_;
1383 }
1384 }
1385
David Brazdil1abb4192015-02-17 18:33:36 +00001386 bool Contains(const HUseListNode<T>* node) const {
1387 if (node == nullptr) {
1388 return false;
1389 }
1390 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1391 if (current == node) {
1392 return true;
1393 }
1394 }
1395 return false;
1396 }
1397
David Brazdiled596192015-01-23 10:39:45 +00001398 bool IsEmpty() const {
1399 return first_ == nullptr;
1400 }
1401
1402 bool HasOnlyOneUse() const {
1403 return first_ != nullptr && first_->next_ == nullptr;
1404 }
1405
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001406 size_t SizeSlow() const {
1407 size_t count = 0;
1408 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1409 ++count;
1410 }
1411 return count;
1412 }
1413
David Brazdiled596192015-01-23 10:39:45 +00001414 private:
1415 HUseListNode<T>* first_;
1416};
1417
1418template<typename T>
1419class HUseIterator : public ValueObject {
1420 public:
1421 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1422
1423 bool Done() const { return current_ == nullptr; }
1424
1425 void Advance() {
1426 DCHECK(!Done());
1427 current_ = current_->GetNext();
1428 }
1429
1430 HUseListNode<T>* Current() const {
1431 DCHECK(!Done());
1432 return current_;
1433 }
1434
1435 private:
1436 HUseListNode<T>* current_;
1437
1438 friend class HValue;
1439};
1440
David Brazdil1abb4192015-02-17 18:33:36 +00001441// This class is used by HEnvironment and HInstruction classes to record the
1442// instructions they use and pointers to the corresponding HUseListNodes kept
1443// by the used instructions.
1444template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001445class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001446 public:
1447 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1448 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1449
1450 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1451 : instruction_(old_record.instruction_), use_node_(use_node) {
1452 DCHECK(instruction_ != nullptr);
1453 DCHECK(use_node_ != nullptr);
1454 DCHECK(old_record.use_node_ == nullptr);
1455 }
1456
1457 HInstruction* GetInstruction() const { return instruction_; }
1458 HUseListNode<T>* GetUseNode() const { return use_node_; }
1459
1460 private:
1461 // Instruction used by the user.
1462 HInstruction* instruction_;
1463
1464 // Corresponding entry in the use list kept by 'instruction_'.
1465 HUseListNode<T>* use_node_;
1466};
1467
Aart Bik854a02b2015-07-14 16:07:00 -07001468/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001469 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001470 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001471 * For write/read dependences on fields/arrays, the dependence analysis uses
1472 * type disambiguation (e.g. a float field write cannot modify the value of an
1473 * integer field read) and the access type (e.g. a reference array write cannot
1474 * modify the value of a reference field read [although it may modify the
1475 * reference fetch prior to reading the field, which is represented by its own
1476 * write/read dependence]). The analysis makes conservative points-to
1477 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1478 * the same, and any reference read depends on any reference read without
1479 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001480 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001481 * The internal representation uses 38-bit and is described in the table below.
1482 * The first line indicates the side effect, and for field/array accesses the
1483 * second line indicates the type of the access (in the order of the
1484 * Primitive::Type enum).
1485 * The two numbered lines below indicate the bit position in the bitfield (read
1486 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001487 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001488 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1489 * +-------------+---------+---------+--------------+---------+---------+
1490 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1491 * | 3 |333333322|222222221| 1 |111111110|000000000|
1492 * | 7 |654321098|765432109| 8 |765432109|876543210|
1493 *
1494 * Note that, to ease the implementation, 'changes' bits are least significant
1495 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001496 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001497class SideEffects : public ValueObject {
1498 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001499 SideEffects() : flags_(0) {}
1500
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001501 static SideEffects None() {
1502 return SideEffects(0);
1503 }
1504
1505 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001506 return SideEffects(kAllChangeBits | kAllDependOnBits);
1507 }
1508
1509 static SideEffects AllChanges() {
1510 return SideEffects(kAllChangeBits);
1511 }
1512
1513 static SideEffects AllDependencies() {
1514 return SideEffects(kAllDependOnBits);
1515 }
1516
1517 static SideEffects AllExceptGCDependency() {
1518 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1519 }
1520
1521 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001522 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001523 }
1524
Aart Bik34c3ba92015-07-20 14:08:59 -07001525 static SideEffects AllWrites() {
1526 return SideEffects(kAllWrites);
1527 }
1528
1529 static SideEffects AllReads() {
1530 return SideEffects(kAllReads);
1531 }
1532
1533 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1534 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001535 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001536 : SideEffects(TypeFlagWithAlias(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001537 }
1538
Aart Bik854a02b2015-07-14 16:07:00 -07001539 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1540 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001541 }
1542
Aart Bik34c3ba92015-07-20 14:08:59 -07001543 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1544 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001545 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001546 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001547 }
1548
1549 static SideEffects ArrayReadOfType(Primitive::Type type) {
1550 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1551 }
1552
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001553 static SideEffects CanTriggerGC() {
1554 return SideEffects(1ULL << kCanTriggerGCBit);
1555 }
1556
1557 static SideEffects DependsOnGC() {
1558 return SideEffects(1ULL << kDependsOnGCBit);
1559 }
1560
Aart Bik854a02b2015-07-14 16:07:00 -07001561 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001562 SideEffects Union(SideEffects other) const {
1563 return SideEffects(flags_ | other.flags_);
1564 }
1565
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001566 SideEffects Exclusion(SideEffects other) const {
1567 return SideEffects(flags_ & ~other.flags_);
1568 }
1569
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001570 void Add(SideEffects other) {
1571 flags_ |= other.flags_;
1572 }
1573
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001574 bool Includes(SideEffects other) const {
1575 return (other.flags_ & flags_) == other.flags_;
1576 }
1577
1578 bool HasSideEffects() const {
1579 return (flags_ & kAllChangeBits);
1580 }
1581
1582 bool HasDependencies() const {
1583 return (flags_ & kAllDependOnBits);
1584 }
1585
1586 // Returns true if there are no side effects or dependencies.
1587 bool DoesNothing() const {
1588 return flags_ == 0;
1589 }
1590
Aart Bik854a02b2015-07-14 16:07:00 -07001591 // Returns true if something is written.
1592 bool DoesAnyWrite() const {
1593 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001594 }
1595
Aart Bik854a02b2015-07-14 16:07:00 -07001596 // Returns true if something is read.
1597 bool DoesAnyRead() const {
1598 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001599 }
1600
Aart Bik854a02b2015-07-14 16:07:00 -07001601 // Returns true if potentially everything is written and read
1602 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001603 bool DoesAllReadWrite() const {
1604 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1605 }
1606
Aart Bik854a02b2015-07-14 16:07:00 -07001607 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001608 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001609 }
1610
Roland Levillain0d5a2812015-11-13 10:07:31 +00001611 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001612 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001613 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1614 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001615 }
1616
1617 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001618 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001619 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001620 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001621 for (int s = kLastBit; s >= 0; s--) {
1622 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1623 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1624 // This is a bit for the GC side effect.
1625 if (current_bit_is_set) {
1626 flags += "GC";
1627 }
Aart Bik854a02b2015-07-14 16:07:00 -07001628 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001629 } else {
1630 // This is a bit for the array/field analysis.
1631 // The underscore character stands for the 'can trigger GC' bit.
1632 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1633 if (current_bit_is_set) {
1634 flags += kDebug[s];
1635 }
1636 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1637 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1638 flags += "|";
1639 }
1640 }
Aart Bik854a02b2015-07-14 16:07:00 -07001641 }
1642 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001643 }
1644
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001645 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001646
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001647 private:
1648 static constexpr int kFieldArrayAnalysisBits = 9;
1649
1650 static constexpr int kFieldWriteOffset = 0;
1651 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1652 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1653 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1654
1655 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1656
1657 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1658 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1659 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1660 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1661
1662 static constexpr int kLastBit = kDependsOnGCBit;
1663 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1664
1665 // Aliases.
1666
1667 static_assert(kChangeBits == kDependOnBits,
1668 "the 'change' bits should match the 'depend on' bits.");
1669
1670 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1671 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1672 static constexpr uint64_t kAllWrites =
1673 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1674 static constexpr uint64_t kAllReads =
1675 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001676
Aart Bik854a02b2015-07-14 16:07:00 -07001677 // Work around the fact that HIR aliases I/F and J/D.
1678 // TODO: remove this interceptor once HIR types are clean
1679 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1680 switch (type) {
1681 case Primitive::kPrimInt:
1682 case Primitive::kPrimFloat:
1683 return TypeFlag(Primitive::kPrimInt, offset) |
1684 TypeFlag(Primitive::kPrimFloat, offset);
1685 case Primitive::kPrimLong:
1686 case Primitive::kPrimDouble:
1687 return TypeFlag(Primitive::kPrimLong, offset) |
1688 TypeFlag(Primitive::kPrimDouble, offset);
1689 default:
1690 return TypeFlag(type, offset);
1691 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001692 }
1693
Aart Bik854a02b2015-07-14 16:07:00 -07001694 // Translates type to bit flag.
1695 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1696 CHECK_NE(type, Primitive::kPrimVoid);
1697 const uint64_t one = 1;
1698 const int shift = type; // 0-based consecutive enum
1699 DCHECK_LE(kFieldWriteOffset, shift);
1700 DCHECK_LT(shift, kArrayWriteOffset);
1701 return one << (type + offset);
1702 }
1703
1704 // Private constructor on direct flags value.
1705 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1706
1707 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001708};
1709
David Brazdiled596192015-01-23 10:39:45 +00001710// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001711class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001712 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001713 HEnvironment(ArenaAllocator* arena,
1714 size_t number_of_vregs,
1715 const DexFile& dex_file,
1716 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001717 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001718 InvokeType invoke_type,
1719 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001720 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1721 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001722 parent_(nullptr),
1723 dex_file_(dex_file),
1724 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001725 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001726 invoke_type_(invoke_type),
1727 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001728 }
1729
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001730 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001731 : HEnvironment(arena,
1732 to_copy.Size(),
1733 to_copy.GetDexFile(),
1734 to_copy.GetMethodIdx(),
1735 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001736 to_copy.GetInvokeType(),
1737 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001738
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001739 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001740 if (parent_ != nullptr) {
1741 parent_->SetAndCopyParentChain(allocator, parent);
1742 } else {
1743 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1744 parent_->CopyFrom(parent);
1745 if (parent->GetParent() != nullptr) {
1746 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1747 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001748 }
David Brazdiled596192015-01-23 10:39:45 +00001749 }
1750
Vladimir Marko71bf8092015-09-15 15:33:14 +01001751 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001752 void CopyFrom(HEnvironment* environment);
1753
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001754 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1755 // input to the loop phi instead. This is for inserting instructions that
1756 // require an environment (like HDeoptimization) in the loop pre-header.
1757 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001758
1759 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001760 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001761 }
1762
1763 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001764 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001765 }
1766
David Brazdil1abb4192015-02-17 18:33:36 +00001767 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001768
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001769 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001770
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001771 HEnvironment* GetParent() const { return parent_; }
1772
1773 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001774 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001775 }
1776
1777 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001778 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001779 }
1780
1781 uint32_t GetDexPc() const {
1782 return dex_pc_;
1783 }
1784
1785 uint32_t GetMethodIdx() const {
1786 return method_idx_;
1787 }
1788
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001789 InvokeType GetInvokeType() const {
1790 return invoke_type_;
1791 }
1792
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001793 const DexFile& GetDexFile() const {
1794 return dex_file_;
1795 }
1796
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001797 HInstruction* GetHolder() const {
1798 return holder_;
1799 }
1800
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001801
1802 bool IsFromInlinedInvoke() const {
1803 return GetParent() != nullptr;
1804 }
1805
David Brazdiled596192015-01-23 10:39:45 +00001806 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001807 // Record instructions' use entries of this environment for constant-time removal.
1808 // It should only be called by HInstruction when a new environment use is added.
1809 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1810 DCHECK(env_use->GetUser() == this);
1811 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001812 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001813 }
David Brazdiled596192015-01-23 10:39:45 +00001814
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001815 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1816 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001817 HEnvironment* parent_;
1818 const DexFile& dex_file_;
1819 const uint32_t method_idx_;
1820 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001821 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001822
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001823 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001824 HInstruction* const holder_;
1825
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001826 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001827
1828 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1829};
1830
Vladimir Markof9f64412015-09-02 14:05:49 +01001831class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001832 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001833 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001834 : previous_(nullptr),
1835 next_(nullptr),
1836 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001837 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001838 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001839 ssa_index_(-1),
David Brazdilb3e773e2016-01-26 11:28:37 +00001840 emitted_at_use_site_(false),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001841 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001842 locations_(nullptr),
1843 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001844 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001845 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001846 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001847
Dave Allison20dfc792014-06-16 20:44:29 -07001848 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001849
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001850#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001851 enum InstructionKind {
1852 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1853 };
1854#undef DECLARE_KIND
1855
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001856 HInstruction* GetNext() const { return next_; }
1857 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001858
Calin Juravle77520bc2015-01-12 18:45:46 +00001859 HInstruction* GetNextDisregardingMoves() const;
1860 HInstruction* GetPreviousDisregardingMoves() const;
1861
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001862 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001863 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001864 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001865 bool IsInBlock() const { return block_ != nullptr; }
1866 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001867 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1868 bool IsIrreducibleLoopHeaderPhi() const {
1869 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1870 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001871
Roland Levillain6b879dd2014-09-22 17:13:44 +01001872 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001873 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001874
1875 virtual void Accept(HGraphVisitor* visitor) = 0;
1876 virtual const char* DebugName() const = 0;
1877
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001878 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001879 void SetRawInputAt(size_t index, HInstruction* input) {
1880 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1881 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001882
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001883 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001884
1885 uint32_t GetDexPc() const { return dex_pc_; }
1886
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001887 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001888
Roland Levillaine161a2a2014-10-03 12:45:18 +01001889 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001890 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001891
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001892 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001893 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001894
Calin Juravle10e244f2015-01-26 18:54:32 +00001895 // Does not apply for all instructions, but having this at top level greatly
1896 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001897 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001898 virtual bool CanBeNull() const {
1899 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1900 return true;
1901 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001902
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001903 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001904 return false;
1905 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001906
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001907 virtual bool IsActualObject() const {
1908 return GetType() == Primitive::kPrimNot;
1909 }
1910
Calin Juravle2e768302015-07-28 14:41:11 +00001911 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001912
Calin Juravle61d544b2015-02-23 16:46:57 +00001913 ReferenceTypeInfo GetReferenceTypeInfo() const {
1914 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1915 return reference_type_info_;
1916 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001917
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001918 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001919 DCHECK(user != nullptr);
1920 HUseListNode<HInstruction*>* use =
1921 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1922 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001923 }
1924
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001925 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001926 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001927 HUseListNode<HEnvironment*>* env_use =
1928 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1929 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001930 }
1931
David Brazdil1abb4192015-02-17 18:33:36 +00001932 void RemoveAsUserOfInput(size_t input) {
1933 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1934 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1935 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001936
David Brazdil1abb4192015-02-17 18:33:36 +00001937 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1938 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001939
David Brazdiled596192015-01-23 10:39:45 +00001940 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1941 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001942 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001943 bool HasOnlyOneNonEnvironmentUse() const {
1944 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1945 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001946
Roland Levillain6c82d402014-10-13 16:10:27 +01001947 // Does this instruction strictly dominate `other_instruction`?
1948 // Returns false if this instruction and `other_instruction` are the same.
1949 // Aborts if this instruction and `other_instruction` are both phis.
1950 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001951
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001952 int GetId() const { return id_; }
1953 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001954
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001955 int GetSsaIndex() const { return ssa_index_; }
1956 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1957 bool HasSsaIndex() const { return ssa_index_ != -1; }
1958
1959 bool HasEnvironment() const { return environment_ != nullptr; }
1960 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001961 // Set the `environment_` field. Raw because this method does not
1962 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001963 void SetRawEnvironment(HEnvironment* environment) {
1964 DCHECK(environment_ == nullptr);
1965 DCHECK_EQ(environment->GetHolder(), this);
1966 environment_ = environment;
1967 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001968
1969 // Set the environment of this instruction, copying it from `environment`. While
1970 // copying, the uses lists are being updated.
1971 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001972 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001973 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001974 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001975 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001976 if (environment->GetParent() != nullptr) {
1977 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1978 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001979 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001980
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001981 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1982 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001983 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001984 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001985 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001986 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001987 if (environment->GetParent() != nullptr) {
1988 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1989 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001990 }
1991
Nicolas Geoffray39468442014-09-02 15:17:15 +01001992 // Returns the number of entries in the environment. Typically, that is the
1993 // number of dex registers in a method. It could be more in case of inlining.
1994 size_t EnvironmentSize() const;
1995
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001996 LocationSummary* GetLocations() const { return locations_; }
1997 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001998
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001999 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002000 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002001
Alexandre Rames188d4312015-04-09 18:30:21 +01002002 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2003 // uses of this instruction by `other` are *not* updated.
2004 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2005 ReplaceWith(other);
2006 other->ReplaceInput(this, use_index);
2007 }
2008
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002009 // Move `this` instruction before `cursor`.
2010 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002011
Vladimir Markofb337ea2015-11-25 15:25:10 +00002012 // Move `this` before its first user and out of any loops. If there is no
2013 // out-of-loop user that dominates all other users, move the instruction
2014 // to the end of the out-of-loop common dominator of the user's blocks.
2015 //
2016 // This can be used only on non-throwing instructions with no side effects that
2017 // have at least one use but no environment uses.
2018 void MoveBeforeFirstUserAndOutOfLoops();
2019
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002020#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002021 bool Is##type() const; \
2022 const H##type* As##type() const; \
2023 H##type* As##type();
2024
2025 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2026#undef INSTRUCTION_TYPE_CHECK
2027
2028#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002029 bool Is##type() const { return (As##type() != nullptr); } \
2030 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002031 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002032 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002033#undef INSTRUCTION_TYPE_CHECK
2034
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002035 // Returns whether the instruction can be moved within the graph.
2036 virtual bool CanBeMoved() const { return false; }
2037
2038 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002039 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002040 return false;
2041 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002042
2043 // Returns whether any data encoded in the two instructions is equal.
2044 // This method does not look at the inputs. Both instructions must be
2045 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002046 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002047 return false;
2048 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002049
2050 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002051 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002052 // 2) Their inputs are identical.
2053 bool Equals(HInstruction* other) const;
2054
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002055 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2056 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2057 // the virtual function because the __attribute__((__pure__)) doesn't really
2058 // apply the strong requirement for virtual functions, preventing optimizations.
2059 InstructionKind GetKind() const PURE;
2060 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002061
2062 virtual size_t ComputeHashCode() const {
2063 size_t result = GetKind();
2064 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2065 result = (result * 31) + InputAt(i)->GetId();
2066 }
2067 return result;
2068 }
2069
2070 SideEffects GetSideEffects() const { return side_effects_; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002071 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002072
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002073 size_t GetLifetimePosition() const { return lifetime_position_; }
2074 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2075 LiveInterval* GetLiveInterval() const { return live_interval_; }
2076 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2077 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2078
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002079 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2080
2081 // Returns whether the code generation of the instruction will require to have access
2082 // to the current method. Such instructions are:
2083 // (1): Instructions that require an environment, as calling the runtime requires
2084 // to walk the stack and have the current method stored at a specific stack address.
2085 // (2): Object literals like classes and strings, that are loaded from the dex cache
2086 // fields of the current method.
2087 bool NeedsCurrentMethod() const {
2088 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2089 }
2090
Vladimir Markodc151b22015-10-15 18:02:30 +01002091 // Returns whether the code generation of the instruction will require to have access
2092 // to the dex cache of the current method's declaring class via the current method.
2093 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002094
Mark Mendellc4701932015-04-10 13:18:51 -04002095 // Does this instruction have any use in an environment before
2096 // control flow hits 'other'?
2097 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2098
2099 // Remove all references to environment uses of this instruction.
2100 // The caller must ensure that this is safe to do.
2101 void RemoveEnvironmentUsers();
2102
David Brazdilb3e773e2016-01-26 11:28:37 +00002103 bool IsEmittedAtUseSite() const { return emitted_at_use_site_; }
2104 void MarkEmittedAtUseSite() { emitted_at_use_site_ = true; }
2105
David Brazdil1abb4192015-02-17 18:33:36 +00002106 protected:
2107 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2108 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
Aart Bik5d75afe2015-12-14 11:57:01 -08002109 void SetSideEffects(SideEffects other) { side_effects_ = other; }
David Brazdil1abb4192015-02-17 18:33:36 +00002110
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002111 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002112 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2113
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002114 HInstruction* previous_;
2115 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002116 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002117 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002118
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002119 // An instruction gets an id when it is added to the graph.
2120 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002121 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002122 int id_;
2123
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002124 // When doing liveness analysis, instructions that have uses get an SSA index.
2125 int ssa_index_;
2126
David Brazdilb3e773e2016-01-26 11:28:37 +00002127 // If set, the machine code for this instruction is assumed to be generated by
2128 // its users. Used by liveness analysis to compute use positions accordingly.
2129 bool emitted_at_use_site_;
2130
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002131 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002132 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002133
2134 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002135 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002136
Nicolas Geoffray39468442014-09-02 15:17:15 +01002137 // The environment associated with this instruction. Not null if the instruction
2138 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002139 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002140
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002141 // Set by the code generator.
2142 LocationSummary* locations_;
2143
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002144 // Set by the liveness analysis.
2145 LiveInterval* live_interval_;
2146
2147 // Set by the liveness analysis, this is the position in a linear
2148 // order of blocks where this instruction's live interval start.
2149 size_t lifetime_position_;
2150
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002151 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002152
Calin Juravleacf735c2015-02-12 15:25:22 +00002153 // TODO: for primitive types this should be marked as invalid.
2154 ReferenceTypeInfo reference_type_info_;
2155
David Brazdil1abb4192015-02-17 18:33:36 +00002156 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002157 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002158 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002159 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002160 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002161
2162 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2163};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002164std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002165
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002166class HInputIterator : public ValueObject {
2167 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002168 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002169
2170 bool Done() const { return index_ == instruction_->InputCount(); }
2171 HInstruction* Current() const { return instruction_->InputAt(index_); }
2172 void Advance() { index_++; }
2173
2174 private:
2175 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002176 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002177
2178 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2179};
2180
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002181class HInstructionIterator : public ValueObject {
2182 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002183 explicit HInstructionIterator(const HInstructionList& instructions)
2184 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002185 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002186 }
2187
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002188 bool Done() const { return instruction_ == nullptr; }
2189 HInstruction* Current() const { return instruction_; }
2190 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002191 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002192 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002193 }
2194
2195 private:
2196 HInstruction* instruction_;
2197 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002198
2199 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002200};
2201
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002202class HBackwardInstructionIterator : public ValueObject {
2203 public:
2204 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2205 : instruction_(instructions.last_instruction_) {
2206 next_ = Done() ? nullptr : instruction_->GetPrevious();
2207 }
2208
2209 bool Done() const { return instruction_ == nullptr; }
2210 HInstruction* Current() const { return instruction_; }
2211 void Advance() {
2212 instruction_ = next_;
2213 next_ = Done() ? nullptr : instruction_->GetPrevious();
2214 }
2215
2216 private:
2217 HInstruction* instruction_;
2218 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002219
2220 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002221};
2222
Vladimir Markof9f64412015-09-02 14:05:49 +01002223template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002224class HTemplateInstruction: public HInstruction {
2225 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002226 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002227 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002228 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002229
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002230 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002231
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002232 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002233 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2234 DCHECK_LT(i, N);
2235 return inputs_[i];
2236 }
David Brazdil1abb4192015-02-17 18:33:36 +00002237
2238 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002239 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002240 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002241 }
2242
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002243 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002244 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002245
2246 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002247};
2248
Vladimir Markof9f64412015-09-02 14:05:49 +01002249// HTemplateInstruction specialization for N=0.
2250template<>
2251class HTemplateInstruction<0>: public HInstruction {
2252 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002253 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002254 : HInstruction(side_effects, dex_pc) {}
2255
Vladimir Markof9f64412015-09-02 14:05:49 +01002256 virtual ~HTemplateInstruction() {}
2257
2258 size_t InputCount() const OVERRIDE { return 0; }
2259
2260 protected:
2261 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2262 LOG(FATAL) << "Unreachable";
2263 UNREACHABLE();
2264 }
2265
2266 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2267 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2268 LOG(FATAL) << "Unreachable";
2269 UNREACHABLE();
2270 }
2271
2272 private:
2273 friend class SsaBuilder;
2274};
2275
Dave Allison20dfc792014-06-16 20:44:29 -07002276template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002277class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002278 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002279 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002280 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002281 virtual ~HExpression() {}
2282
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002283 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002284
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002285 protected:
2286 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002287};
2288
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002289// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2290// instruction that branches to the exit block.
2291class HReturnVoid : public HTemplateInstruction<0> {
2292 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002293 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2294 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002295
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002296 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002297
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002298 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002299
2300 private:
2301 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2302};
2303
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002304// Represents dex's RETURN opcodes. A HReturn is a control flow
2305// instruction that branches to the exit block.
2306class HReturn : public HTemplateInstruction<1> {
2307 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002308 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2309 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002310 SetRawInputAt(0, value);
2311 }
2312
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002313 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002314
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002315 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002316
2317 private:
2318 DISALLOW_COPY_AND_ASSIGN(HReturn);
2319};
2320
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002321// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002322// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002323// exit block.
2324class HExit : public HTemplateInstruction<0> {
2325 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002326 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002327
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002328 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002329
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002330 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002331
2332 private:
2333 DISALLOW_COPY_AND_ASSIGN(HExit);
2334};
2335
2336// Jumps from one block to another.
2337class HGoto : public HTemplateInstruction<0> {
2338 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002339 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002340
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002341 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002342
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002343 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002344 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002345 }
2346
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002347 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002348
2349 private:
2350 DISALLOW_COPY_AND_ASSIGN(HGoto);
2351};
2352
Roland Levillain9867bc72015-08-05 10:21:34 +01002353class HConstant : public HExpression<0> {
2354 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002355 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2356 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002357
2358 bool CanBeMoved() const OVERRIDE { return true; }
2359
2360 virtual bool IsMinusOne() const { return false; }
2361 virtual bool IsZero() const { return false; }
2362 virtual bool IsOne() const { return false; }
2363
David Brazdil77a48ae2015-09-15 12:34:04 +00002364 virtual uint64_t GetValueAsUint64() const = 0;
2365
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002366 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002367
2368 private:
2369 DISALLOW_COPY_AND_ASSIGN(HConstant);
2370};
2371
2372class HNullConstant : public HConstant {
2373 public:
2374 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2375 return true;
2376 }
2377
David Brazdil77a48ae2015-09-15 12:34:04 +00002378 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2379
Roland Levillain9867bc72015-08-05 10:21:34 +01002380 size_t ComputeHashCode() const OVERRIDE { return 0; }
2381
2382 DECLARE_INSTRUCTION(NullConstant);
2383
2384 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002385 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002386
2387 friend class HGraph;
2388 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2389};
2390
2391// Constants of the type int. Those can be from Dex instructions, or
2392// synthesized (for example with the if-eqz instruction).
2393class HIntConstant : public HConstant {
2394 public:
2395 int32_t GetValue() const { return value_; }
2396
David Brazdil9f389d42015-10-01 14:32:56 +01002397 uint64_t GetValueAsUint64() const OVERRIDE {
2398 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2399 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002400
Roland Levillain9867bc72015-08-05 10:21:34 +01002401 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2402 DCHECK(other->IsIntConstant());
2403 return other->AsIntConstant()->value_ == value_;
2404 }
2405
2406 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2407
2408 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2409 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2410 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2411
2412 DECLARE_INSTRUCTION(IntConstant);
2413
2414 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002415 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2416 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2417 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2418 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002419
2420 const int32_t value_;
2421
2422 friend class HGraph;
2423 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2424 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2425 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2426};
2427
2428class HLongConstant : public HConstant {
2429 public:
2430 int64_t GetValue() const { return value_; }
2431
David Brazdil77a48ae2015-09-15 12:34:04 +00002432 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2433
Roland Levillain9867bc72015-08-05 10:21:34 +01002434 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2435 DCHECK(other->IsLongConstant());
2436 return other->AsLongConstant()->value_ == value_;
2437 }
2438
2439 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2440
2441 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2442 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2443 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2444
2445 DECLARE_INSTRUCTION(LongConstant);
2446
2447 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002448 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2449 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002450
2451 const int64_t value_;
2452
2453 friend class HGraph;
2454 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2455};
Dave Allison20dfc792014-06-16 20:44:29 -07002456
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002457// Conditional branch. A block ending with an HIf instruction must have
2458// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002459class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002460 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002461 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2462 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002463 SetRawInputAt(0, input);
2464 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002465
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002466 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002467
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002468 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002469 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002470 }
2471
2472 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002473 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002474 }
2475
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002476 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002477
2478 private:
2479 DISALLOW_COPY_AND_ASSIGN(HIf);
2480};
2481
David Brazdilfc6a86a2015-06-26 10:33:45 +00002482
2483// Abstract instruction which marks the beginning and/or end of a try block and
2484// links it to the respective exception handlers. Behaves the same as a Goto in
2485// non-exceptional control flow.
2486// Normal-flow successor is stored at index zero, exception handlers under
2487// higher indices in no particular order.
2488class HTryBoundary : public HTemplateInstruction<0> {
2489 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002490 enum BoundaryKind {
2491 kEntry,
2492 kExit,
2493 };
2494
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002495 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2496 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002497
2498 bool IsControlFlow() const OVERRIDE { return true; }
2499
2500 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002501 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002502
David Brazdild26a4112015-11-10 11:07:31 +00002503 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2504 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2505 }
2506
David Brazdilfc6a86a2015-06-26 10:33:45 +00002507 // Returns whether `handler` is among its exception handlers (non-zero index
2508 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002509 bool HasExceptionHandler(const HBasicBlock& handler) const {
2510 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002511 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002512 }
2513
2514 // If not present already, adds `handler` to its block's list of exception
2515 // handlers.
2516 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002517 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002518 GetBlock()->AddSuccessor(handler);
2519 }
2520 }
2521
David Brazdil56e1acc2015-06-30 15:41:36 +01002522 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002523
David Brazdilffee3d32015-07-06 11:48:53 +01002524 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2525
David Brazdilfc6a86a2015-06-26 10:33:45 +00002526 DECLARE_INSTRUCTION(TryBoundary);
2527
2528 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002529 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002530
2531 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2532};
2533
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002534// Deoptimize to interpreter, upon checking a condition.
2535class HDeoptimize : public HTemplateInstruction<1> {
2536 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002537 // We set CanTriggerGC to prevent any intermediate address to be live
2538 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002539 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002540 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002541 SetRawInputAt(0, cond);
2542 }
2543
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002544 bool CanBeMoved() const OVERRIDE { return true; }
2545 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2546 return true;
2547 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002548 bool NeedsEnvironment() const OVERRIDE { return true; }
2549 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002550
2551 DECLARE_INSTRUCTION(Deoptimize);
2552
2553 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002554 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2555};
2556
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002557// Represents the ArtMethod that was passed as a first argument to
2558// the method. It is used by instructions that depend on it, like
2559// instructions that work with the dex cache.
2560class HCurrentMethod : public HExpression<0> {
2561 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002562 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2563 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002564
2565 DECLARE_INSTRUCTION(CurrentMethod);
2566
2567 private:
2568 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2569};
2570
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002571// Fetches an ArtMethod from the virtual table or the interface method table
2572// of a class.
2573class HClassTableGet : public HExpression<1> {
2574 public:
2575 enum TableKind {
2576 kVTable,
2577 kIMTable,
2578 };
2579 HClassTableGet(HInstruction* cls,
2580 Primitive::Type type,
2581 TableKind kind,
2582 size_t index,
2583 uint32_t dex_pc)
2584 : HExpression(type, SideEffects::None(), dex_pc),
2585 index_(index),
2586 table_kind_(kind) {
2587 SetRawInputAt(0, cls);
2588 }
2589
2590 bool CanBeMoved() const OVERRIDE { return true; }
2591 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2592 return other->AsClassTableGet()->GetIndex() == index_ &&
2593 other->AsClassTableGet()->GetTableKind() == table_kind_;
2594 }
2595
2596 TableKind GetTableKind() const { return table_kind_; }
2597 size_t GetIndex() const { return index_; }
2598
2599 DECLARE_INSTRUCTION(ClassTableGet);
2600
2601 private:
2602 // The index of the ArtMethod in the table.
2603 const size_t index_;
2604 const TableKind table_kind_;
2605
2606 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2607};
2608
Mark Mendellfe57faa2015-09-18 09:26:15 -04002609// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2610// have one successor for each entry in the switch table, and the final successor
2611// will be the block containing the next Dex opcode.
2612class HPackedSwitch : public HTemplateInstruction<1> {
2613 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002614 HPackedSwitch(int32_t start_value,
2615 uint32_t num_entries,
2616 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002617 uint32_t dex_pc = kNoDexPc)
2618 : HTemplateInstruction(SideEffects::None(), dex_pc),
2619 start_value_(start_value),
2620 num_entries_(num_entries) {
2621 SetRawInputAt(0, input);
2622 }
2623
2624 bool IsControlFlow() const OVERRIDE { return true; }
2625
2626 int32_t GetStartValue() const { return start_value_; }
2627
Vladimir Marko211c2112015-09-24 16:52:33 +01002628 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002629
2630 HBasicBlock* GetDefaultBlock() const {
2631 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002632 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002633 }
2634 DECLARE_INSTRUCTION(PackedSwitch);
2635
2636 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002637 const int32_t start_value_;
2638 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002639
2640 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2641};
2642
Roland Levillain88cb1752014-10-20 16:36:47 +01002643class HUnaryOperation : public HExpression<1> {
2644 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002645 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2646 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002647 SetRawInputAt(0, input);
2648 }
2649
2650 HInstruction* GetInput() const { return InputAt(0); }
2651 Primitive::Type GetResultType() const { return GetType(); }
2652
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002653 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002654 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002655 return true;
2656 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002657
Roland Levillain9240d6a2014-10-20 16:47:04 +01002658 // Try to statically evaluate `operation` and return a HConstant
2659 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002660 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002661 HConstant* TryStaticEvaluation() const;
2662
2663 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002664 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2665 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002666
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002667 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002668
2669 private:
2670 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2671};
2672
Dave Allison20dfc792014-06-16 20:44:29 -07002673class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002674 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002675 HBinaryOperation(Primitive::Type result_type,
2676 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002677 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002678 SideEffects side_effects = SideEffects::None(),
2679 uint32_t dex_pc = kNoDexPc)
2680 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002681 SetRawInputAt(0, left);
2682 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002683 }
2684
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002685 HInstruction* GetLeft() const { return InputAt(0); }
2686 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002687 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002688
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002689 virtual bool IsCommutative() const { return false; }
2690
2691 // Put constant on the right.
2692 // Returns whether order is changed.
2693 bool OrderInputsWithConstantOnTheRight() {
2694 HInstruction* left = InputAt(0);
2695 HInstruction* right = InputAt(1);
2696 if (left->IsConstant() && !right->IsConstant()) {
2697 ReplaceInput(right, 0);
2698 ReplaceInput(left, 1);
2699 return true;
2700 }
2701 return false;
2702 }
2703
2704 // Order inputs by instruction id, but favor constant on the right side.
2705 // This helps GVN for commutative ops.
2706 void OrderInputs() {
2707 DCHECK(IsCommutative());
2708 HInstruction* left = InputAt(0);
2709 HInstruction* right = InputAt(1);
2710 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2711 return;
2712 }
2713 if (OrderInputsWithConstantOnTheRight()) {
2714 return;
2715 }
2716 // Order according to instruction id.
2717 if (left->GetId() > right->GetId()) {
2718 ReplaceInput(right, 0);
2719 ReplaceInput(left, 1);
2720 }
2721 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002722
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002723 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002724 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002725 return true;
2726 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002727
Roland Levillain9240d6a2014-10-20 16:47:04 +01002728 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01002729 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002730 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002731 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002732
2733 // Apply this operation to `x` and `y`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002734 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2735 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2736 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2737 HLongConstant* y ATTRIBUTE_UNUSED) const {
2738 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2739 return nullptr;
2740 }
2741 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2742 HIntConstant* y ATTRIBUTE_UNUSED) const {
2743 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2744 return nullptr;
2745 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002746 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2747 HNullConstant* y ATTRIBUTE_UNUSED) const {
2748 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2749 return nullptr;
2750 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002751
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002752 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002753 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002754 HConstant* GetConstantRight() const;
2755
2756 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002757 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002758 HInstruction* GetLeastConstantLeft() const;
2759
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002760 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002761
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002762 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002763 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2764};
2765
Mark Mendellc4701932015-04-10 13:18:51 -04002766// The comparison bias applies for floating point operations and indicates how NaN
2767// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002768enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002769 kNoBias, // bias is not applicable (i.e. for long operation)
2770 kGtBias, // return 1 for NaN comparisons
2771 kLtBias, // return -1 for NaN comparisons
2772};
2773
Dave Allison20dfc792014-06-16 20:44:29 -07002774class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002775 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002776 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2777 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002778 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002779
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002780 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002781 // `instruction`, and disregard moves in between.
2782 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002783
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002784 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002785
2786 virtual IfCondition GetCondition() const = 0;
2787
Mark Mendellc4701932015-04-10 13:18:51 -04002788 virtual IfCondition GetOppositeCondition() const = 0;
2789
Roland Levillain4fa13f62015-07-06 18:11:54 +01002790 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Mark Mendellc4701932015-04-10 13:18:51 -04002791
2792 void SetBias(ComparisonBias bias) { bias_ = bias; }
2793
2794 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2795 return bias_ == other->AsCondition()->bias_;
2796 }
2797
Roland Levillain4fa13f62015-07-06 18:11:54 +01002798 bool IsFPConditionTrueIfNaN() const {
2799 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2800 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002801 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002802 }
2803
2804 bool IsFPConditionFalseIfNaN() const {
2805 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType()));
2806 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002807 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002808 }
2809
Dave Allison20dfc792014-06-16 20:44:29 -07002810 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002811 // Needed if we merge a HCompare into a HCondition.
2812 ComparisonBias bias_;
2813
Dave Allison20dfc792014-06-16 20:44:29 -07002814 DISALLOW_COPY_AND_ASSIGN(HCondition);
2815};
2816
2817// Instruction to check if two inputs are equal to each other.
2818class HEqual : public HCondition {
2819 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002820 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2821 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002822
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002823 bool IsCommutative() const OVERRIDE { return true; }
2824
Roland Levillain9867bc72015-08-05 10:21:34 +01002825 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002826 return GetBlock()->GetGraph()->GetIntConstant(
2827 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002828 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002829 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002830 return GetBlock()->GetGraph()->GetIntConstant(
2831 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002832 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002833 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2834 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002835 return GetBlock()->GetGraph()->GetIntConstant(1);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002836 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002837
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002838 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002839
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002840 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002841 return kCondEQ;
2842 }
2843
Mark Mendellc4701932015-04-10 13:18:51 -04002844 IfCondition GetOppositeCondition() const OVERRIDE {
2845 return kCondNE;
2846 }
2847
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002848 private:
Aart Bike9f37602015-10-09 11:15:55 -07002849 template <typename T> bool Compute(T x, T y) const { return x == y; }
2850
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002851 DISALLOW_COPY_AND_ASSIGN(HEqual);
2852};
2853
Dave Allison20dfc792014-06-16 20:44:29 -07002854class HNotEqual : public HCondition {
2855 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002856 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2857 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002858
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002859 bool IsCommutative() const OVERRIDE { return true; }
2860
Roland Levillain9867bc72015-08-05 10:21:34 +01002861 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002862 return GetBlock()->GetGraph()->GetIntConstant(
2863 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002864 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002865 HConstant* Evaluate(HLongConstant* x, HLongConstant* 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 }
Vladimir Marko9e23df52015-11-10 17:14:35 +00002869 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2870 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Vladimir Marko040db342015-11-10 19:53:01 +00002871 return GetBlock()->GetGraph()->GetIntConstant(0);
Vladimir Marko9e23df52015-11-10 17:14:35 +00002872 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002873
Dave Allison20dfc792014-06-16 20:44:29 -07002874 DECLARE_INSTRUCTION(NotEqual);
2875
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002876 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002877 return kCondNE;
2878 }
2879
Mark Mendellc4701932015-04-10 13:18:51 -04002880 IfCondition GetOppositeCondition() const OVERRIDE {
2881 return kCondEQ;
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(HNotEqual);
2888};
2889
2890class HLessThan : public HCondition {
2891 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002892 HLessThan(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(LessThan);
2905
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002906 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002907 return kCondLT;
2908 }
2909
Mark Mendellc4701932015-04-10 13:18:51 -04002910 IfCondition GetOppositeCondition() const OVERRIDE {
2911 return kCondGE;
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(HLessThan);
2918};
2919
2920class HLessThanOrEqual : public HCondition {
2921 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002922 HLessThanOrEqual(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(LessThanOrEqual);
2935
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002936 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002937 return kCondLE;
2938 }
2939
Mark Mendellc4701932015-04-10 13:18:51 -04002940 IfCondition GetOppositeCondition() const OVERRIDE {
2941 return kCondGT;
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(HLessThanOrEqual);
2948};
2949
2950class HGreaterThan : public HCondition {
2951 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002952 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2953 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002954
Roland Levillain9867bc72015-08-05 10:21:34 +01002955 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002956 return GetBlock()->GetGraph()->GetIntConstant(
2957 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002958 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002959 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002960 return GetBlock()->GetGraph()->GetIntConstant(
2961 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002962 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002963
Dave Allison20dfc792014-06-16 20:44:29 -07002964 DECLARE_INSTRUCTION(GreaterThan);
2965
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002966 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002967 return kCondGT;
2968 }
2969
Mark Mendellc4701932015-04-10 13:18:51 -04002970 IfCondition GetOppositeCondition() const OVERRIDE {
2971 return kCondLE;
2972 }
2973
Dave Allison20dfc792014-06-16 20:44:29 -07002974 private:
Aart Bike9f37602015-10-09 11:15:55 -07002975 template <typename T> bool Compute(T x, T y) const { return x > y; }
2976
Dave Allison20dfc792014-06-16 20:44:29 -07002977 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2978};
2979
2980class HGreaterThanOrEqual : public HCondition {
2981 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002982 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2983 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002984
Roland Levillain9867bc72015-08-05 10:21:34 +01002985 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002986 return GetBlock()->GetGraph()->GetIntConstant(
2987 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002988 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002989 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002990 return GetBlock()->GetGraph()->GetIntConstant(
2991 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01002992 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002993
Dave Allison20dfc792014-06-16 20:44:29 -07002994 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2995
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002996 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002997 return kCondGE;
2998 }
2999
Mark Mendellc4701932015-04-10 13:18:51 -04003000 IfCondition GetOppositeCondition() const OVERRIDE {
3001 return kCondLT;
3002 }
3003
Dave Allison20dfc792014-06-16 20:44:29 -07003004 private:
Aart Bike9f37602015-10-09 11:15:55 -07003005 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3006
Dave Allison20dfc792014-06-16 20:44:29 -07003007 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3008};
3009
Aart Bike9f37602015-10-09 11:15:55 -07003010class HBelow : public HCondition {
3011 public:
3012 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3013 : HCondition(first, second, dex_pc) {}
3014
3015 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3016 return GetBlock()->GetGraph()->GetIntConstant(
3017 Compute(static_cast<uint32_t>(x->GetValue()),
3018 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3019 }
3020 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3021 return GetBlock()->GetGraph()->GetIntConstant(
3022 Compute(static_cast<uint64_t>(x->GetValue()),
3023 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3024 }
3025
3026 DECLARE_INSTRUCTION(Below);
3027
3028 IfCondition GetCondition() const OVERRIDE {
3029 return kCondB;
3030 }
3031
3032 IfCondition GetOppositeCondition() const OVERRIDE {
3033 return kCondAE;
3034 }
3035
3036 private:
3037 template <typename T> bool Compute(T x, T y) const { return x < y; }
3038
3039 DISALLOW_COPY_AND_ASSIGN(HBelow);
3040};
3041
3042class HBelowOrEqual : public HCondition {
3043 public:
3044 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3045 : HCondition(first, second, dex_pc) {}
3046
3047 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3048 return GetBlock()->GetGraph()->GetIntConstant(
3049 Compute(static_cast<uint32_t>(x->GetValue()),
3050 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3051 }
3052 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3053 return GetBlock()->GetGraph()->GetIntConstant(
3054 Compute(static_cast<uint64_t>(x->GetValue()),
3055 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3056 }
3057
3058 DECLARE_INSTRUCTION(BelowOrEqual);
3059
3060 IfCondition GetCondition() const OVERRIDE {
3061 return kCondBE;
3062 }
3063
3064 IfCondition GetOppositeCondition() const OVERRIDE {
3065 return kCondA;
3066 }
3067
3068 private:
3069 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3070
3071 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3072};
3073
3074class HAbove : public HCondition {
3075 public:
3076 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3077 : HCondition(first, second, dex_pc) {}
3078
3079 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3080 return GetBlock()->GetGraph()->GetIntConstant(
3081 Compute(static_cast<uint32_t>(x->GetValue()),
3082 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3083 }
3084 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3085 return GetBlock()->GetGraph()->GetIntConstant(
3086 Compute(static_cast<uint64_t>(x->GetValue()),
3087 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3088 }
3089
3090 DECLARE_INSTRUCTION(Above);
3091
3092 IfCondition GetCondition() const OVERRIDE {
3093 return kCondA;
3094 }
3095
3096 IfCondition GetOppositeCondition() const OVERRIDE {
3097 return kCondBE;
3098 }
3099
3100 private:
3101 template <typename T> bool Compute(T x, T y) const { return x > y; }
3102
3103 DISALLOW_COPY_AND_ASSIGN(HAbove);
3104};
3105
3106class HAboveOrEqual : public HCondition {
3107 public:
3108 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3109 : HCondition(first, second, dex_pc) {}
3110
3111 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3112 return GetBlock()->GetGraph()->GetIntConstant(
3113 Compute(static_cast<uint32_t>(x->GetValue()),
3114 static_cast<uint32_t>(y->GetValue())), GetDexPc());
3115 }
3116 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3117 return GetBlock()->GetGraph()->GetIntConstant(
3118 Compute(static_cast<uint64_t>(x->GetValue()),
3119 static_cast<uint64_t>(y->GetValue())), GetDexPc());
3120 }
3121
3122 DECLARE_INSTRUCTION(AboveOrEqual);
3123
3124 IfCondition GetCondition() const OVERRIDE {
3125 return kCondAE;
3126 }
3127
3128 IfCondition GetOppositeCondition() const OVERRIDE {
3129 return kCondB;
3130 }
3131
3132 private:
3133 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3134
3135 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3136};
Dave Allison20dfc792014-06-16 20:44:29 -07003137
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003138// Instruction to check how two inputs compare to each other.
3139// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3140class HCompare : public HBinaryOperation {
3141 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003142 HCompare(Primitive::Type type,
3143 HInstruction* first,
3144 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003145 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003146 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003147 : HBinaryOperation(Primitive::kPrimInt,
3148 first,
3149 second,
3150 SideEffectsForArchRuntimeCalls(type),
3151 dex_pc),
3152 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003153 DCHECK_EQ(type, first->GetType());
3154 DCHECK_EQ(type, second->GetType());
3155 }
3156
Roland Levillain9867bc72015-08-05 10:21:34 +01003157 template <typename T>
3158 int32_t Compute(T x, T y) const { return x == y ? 0 : x > y ? 1 : -1; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003159
Roland Levillain9867bc72015-08-05 10:21:34 +01003160 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003161 return GetBlock()->GetGraph()->GetIntConstant(
3162 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003163 }
3164 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003165 return GetBlock()->GetGraph()->GetIntConstant(
3166 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003167 }
3168
Calin Juravleddb7df22014-11-25 20:56:51 +00003169 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3170 return bias_ == other->AsCompare()->bias_;
3171 }
3172
Mark Mendellc4701932015-04-10 13:18:51 -04003173 ComparisonBias GetBias() const { return bias_; }
3174
Roland Levillain4fa13f62015-07-06 18:11:54 +01003175 bool IsGtBias() { return bias_ == ComparisonBias::kGtBias; }
Calin Juravleddb7df22014-11-25 20:56:51 +00003176
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003177
3178 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3179 // MIPS64 uses a runtime call for FP comparisons.
3180 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3181 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003182
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003183 DECLARE_INSTRUCTION(Compare);
3184
3185 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003186 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003187
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003188 DISALLOW_COPY_AND_ASSIGN(HCompare);
3189};
3190
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003191// A local in the graph. Corresponds to a Dex register.
3192class HLocal : public HTemplateInstruction<0> {
3193 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003194 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003195 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003196
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003197 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003198
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003199 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003200
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003201 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003202 // The Dex register number.
3203 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003204
3205 DISALLOW_COPY_AND_ASSIGN(HLocal);
3206};
3207
3208// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003209class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003210 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003211 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3212 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003213 SetRawInputAt(0, local);
3214 }
3215
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003216 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3217
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003218 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003219
3220 private:
3221 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3222};
3223
3224// Store a value in a given local. This instruction has two inputs: the value
3225// and the local.
3226class HStoreLocal : public HTemplateInstruction<2> {
3227 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003228 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3229 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003230 SetRawInputAt(0, local);
3231 SetRawInputAt(1, value);
3232 }
3233
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003234 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3235
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003236 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003237
3238 private:
3239 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3240};
3241
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003242class HFloatConstant : public HConstant {
3243 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003244 float GetValue() const { return value_; }
3245
David Brazdil77a48ae2015-09-15 12:34:04 +00003246 uint64_t GetValueAsUint64() const OVERRIDE {
3247 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3248 }
3249
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003250 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003251 DCHECK(other->IsFloatConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003252 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003253 }
3254
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003255 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003256
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003257 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003258 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003259 }
3260 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003261 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003262 }
3263 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003264 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3265 }
3266 bool IsNaN() const {
3267 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003268 }
3269
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003270 DECLARE_INSTRUCTION(FloatConstant);
3271
3272 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003273 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
3274 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
3275 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
3276 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003277
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003278 const float value_;
3279
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003280 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003281 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003282 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003283 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
3284};
3285
3286class HDoubleConstant : public HConstant {
3287 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003288 double GetValue() const { return value_; }
3289
David Brazdil77a48ae2015-09-15 12:34:04 +00003290 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
3291
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003292 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01003293 DCHECK(other->IsDoubleConstant());
David Brazdil77a48ae2015-09-15 12:34:04 +00003294 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003295 }
3296
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003297 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003298
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003299 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003300 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003301 }
3302 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003303 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003304 }
3305 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01003306 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3307 }
3308 bool IsNaN() const {
3309 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003310 }
3311
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003312 DECLARE_INSTRUCTION(DoubleConstant);
3313
3314 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003315 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
3316 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
3317 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
3318 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00003319
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003320 const double value_;
3321
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003322 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00003323 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00003324 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003325 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
3326};
3327
David Brazdil6de19382016-01-08 17:37:10 +00003328class HNewInstance : public HExpression<2> {
3329 public:
3330 HNewInstance(HInstruction* cls,
3331 HCurrentMethod* current_method,
3332 uint32_t dex_pc,
3333 uint16_t type_index,
3334 const DexFile& dex_file,
3335 bool can_throw,
3336 bool finalizable,
3337 QuickEntrypointEnum entrypoint)
3338 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3339 type_index_(type_index),
3340 dex_file_(dex_file),
3341 can_throw_(can_throw),
3342 finalizable_(finalizable),
3343 entrypoint_(entrypoint) {
3344 SetRawInputAt(0, cls);
3345 SetRawInputAt(1, current_method);
3346 }
3347
3348 uint16_t GetTypeIndex() const { return type_index_; }
3349 const DexFile& GetDexFile() const { return dex_file_; }
3350
3351 // Calls runtime so needs an environment.
3352 bool NeedsEnvironment() const OVERRIDE { return true; }
3353
3354 // It may throw when called on type that's not instantiable/accessible.
3355 // It can throw OOME.
3356 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3357 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3358
3359 bool IsFinalizable() const { return finalizable_; }
3360
3361 bool CanBeNull() const OVERRIDE { return false; }
3362
3363 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3364
3365 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3366 entrypoint_ = entrypoint;
3367 }
3368
3369 bool IsStringAlloc() const;
3370
3371 DECLARE_INSTRUCTION(NewInstance);
3372
3373 private:
3374 const uint16_t type_index_;
3375 const DexFile& dex_file_;
3376 const bool can_throw_;
3377 const bool finalizable_;
3378 QuickEntrypointEnum entrypoint_;
3379
3380 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3381};
3382
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003383enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003384#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3385 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003386#include "intrinsics_list.h"
3387 kNone,
3388 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3389#undef INTRINSICS_LIST
3390#undef OPTIMIZING_INTRINSICS
3391};
3392std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3393
Agi Csaki05f20562015-08-19 14:58:14 -07003394enum IntrinsicNeedsEnvironmentOrCache {
3395 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3396 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003397};
3398
Aart Bik5d75afe2015-12-14 11:57:01 -08003399enum IntrinsicSideEffects {
3400 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3401 kReadSideEffects, // Intrinsic may read heap memory.
3402 kWriteSideEffects, // Intrinsic may write heap memory.
3403 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3404};
3405
3406enum IntrinsicExceptions {
3407 kNoThrow, // Intrinsic does not throw any exceptions.
3408 kCanThrow // Intrinsic may throw exceptions.
3409};
3410
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003411class HInvoke : public HInstruction {
3412 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003413 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003414
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003415 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003416
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003417 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003418 SetRawInputAt(index, argument);
3419 }
3420
Roland Levillain3e3d7332015-04-28 11:00:54 +01003421 // Return the number of arguments. This number can be lower than
3422 // the number of inputs returned by InputCount(), as some invoke
3423 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3424 // inputs at the end of their list of inputs.
3425 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3426
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003427 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003428
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003429 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003430 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003431
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003432 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3433
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003434 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003435 return intrinsic_;
3436 }
3437
Aart Bik5d75afe2015-12-14 11:57:01 -08003438 void SetIntrinsic(Intrinsics intrinsic,
3439 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3440 IntrinsicSideEffects side_effects,
3441 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003442
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003443 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003444 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003445 }
3446
Aart Bik5d75afe2015-12-14 11:57:01 -08003447 bool CanThrow() const OVERRIDE { return can_throw_; }
3448
3449 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3450
3451 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3452 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3453 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003454
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003455 uint32_t* GetIntrinsicOptimizations() {
3456 return &intrinsic_optimizations_;
3457 }
3458
3459 const uint32_t* GetIntrinsicOptimizations() const {
3460 return &intrinsic_optimizations_;
3461 }
3462
3463 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3464
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003465 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003466
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003467 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003468 HInvoke(ArenaAllocator* arena,
3469 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003470 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003471 Primitive::Type return_type,
3472 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003473 uint32_t dex_method_index,
3474 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003475 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003476 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003477 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003478 inputs_(number_of_arguments + number_of_other_inputs,
3479 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003480 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003481 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003482 original_invoke_type_(original_invoke_type),
Aart Bik5d75afe2015-12-14 11:57:01 -08003483 can_throw_(true),
agicsaki57b81ec2015-08-11 17:39:37 -07003484 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003485 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003486 }
3487
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003488 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003489 return inputs_[index];
3490 }
3491
David Brazdil1abb4192015-02-17 18:33:36 +00003492 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003493 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003494 }
3495
Aart Bik5d75afe2015-12-14 11:57:01 -08003496 void SetCanThrow(bool can_throw) { can_throw_ = can_throw; }
3497
Roland Levillain3e3d7332015-04-28 11:00:54 +01003498 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003499 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003500 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003501 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003502 const InvokeType original_invoke_type_;
Aart Bik5d75afe2015-12-14 11:57:01 -08003503 bool can_throw_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003504 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003505
3506 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3507 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003508
3509 private:
3510 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3511};
3512
Calin Juravle175dc732015-08-25 15:42:32 +01003513class HInvokeUnresolved : public HInvoke {
3514 public:
3515 HInvokeUnresolved(ArenaAllocator* arena,
3516 uint32_t number_of_arguments,
3517 Primitive::Type return_type,
3518 uint32_t dex_pc,
3519 uint32_t dex_method_index,
3520 InvokeType invoke_type)
3521 : HInvoke(arena,
3522 number_of_arguments,
3523 0u /* number_of_other_inputs */,
3524 return_type,
3525 dex_pc,
3526 dex_method_index,
3527 invoke_type) {
3528 }
3529
3530 DECLARE_INSTRUCTION(InvokeUnresolved);
3531
3532 private:
3533 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3534};
3535
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003536class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003537 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003538 // Requirements of this method call regarding the class
3539 // initialization (clinit) check of its declaring class.
3540 enum class ClinitCheckRequirement {
3541 kNone, // Class already initialized.
3542 kExplicit, // Static call having explicit clinit check as last input.
3543 kImplicit, // Static call implicitly requiring a clinit check.
3544 };
3545
Vladimir Marko58155012015-08-19 12:49:41 +00003546 // Determines how to load the target ArtMethod*.
3547 enum class MethodLoadKind {
3548 // Use a String init ArtMethod* loaded from Thread entrypoints.
3549 kStringInit,
3550
3551 // Use the method's own ArtMethod* loaded by the register allocator.
3552 kRecursive,
3553
3554 // Use ArtMethod* at a known address, embed the direct address in the code.
3555 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3556 kDirectAddress,
3557
3558 // Use ArtMethod* at an address that will be known at link time, embed the direct
3559 // address in the code. If the image is relocatable, emit .patch_oat entry.
3560 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3561 // the image relocatable or not.
3562 kDirectAddressWithFixup,
3563
3564 // Load from resoved methods array in the dex cache using a PC-relative load.
3565 // Used when we need to use the dex cache, for example for invoke-static that
3566 // may cause class initialization (the entry may point to a resolution method),
3567 // and we know that we can access the dex cache arrays using a PC-relative load.
3568 kDexCachePcRelative,
3569
3570 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3571 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3572 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3573 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3574 kDexCacheViaMethod,
3575 };
3576
3577 // Determines the location of the code pointer.
3578 enum class CodePtrLocation {
3579 // Recursive call, use local PC-relative call instruction.
3580 kCallSelf,
3581
3582 // Use PC-relative call instruction patched at link time.
3583 // Used for calls within an oat file, boot->boot or app->app.
3584 kCallPCRelative,
3585
3586 // Call to a known target address, embed the direct address in code.
3587 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3588 kCallDirect,
3589
3590 // Call to a target address that will be known at link time, embed the direct
3591 // address in code. If the image is relocatable, emit .patch_oat entry.
3592 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3593 // the image relocatable or not.
3594 kCallDirectWithFixup,
3595
3596 // Use code pointer from the ArtMethod*.
3597 // Used when we don't know the target code. This is also the last-resort-kind used when
3598 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3599 kCallArtMethod,
3600 };
3601
3602 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003603 MethodLoadKind method_load_kind;
3604 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003605 // The method load data holds
3606 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3607 // Note that there are multiple string init methods, each having its own offset.
3608 // - the method address for kDirectAddress
3609 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003610 uint64_t method_load_data;
3611 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003612 };
3613
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003614 HInvokeStaticOrDirect(ArenaAllocator* arena,
3615 uint32_t number_of_arguments,
3616 Primitive::Type return_type,
3617 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003618 uint32_t method_index,
3619 MethodReference target_method,
3620 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003621 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003622 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003623 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003624 : HInvoke(arena,
3625 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003626 // There is potentially one extra argument for the HCurrentMethod node, and
3627 // potentially one other if the clinit check is explicit, and potentially
3628 // one other if the method is a string factory.
3629 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3630 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3631 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003632 return_type,
3633 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003634 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003635 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003636 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003637 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003638 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003639 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003640
Vladimir Markodc151b22015-10-15 18:02:30 +01003641 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003642 bool had_current_method_input = HasCurrentMethodInput();
3643 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3644
3645 // Using the current method is the default and once we find a better
3646 // method load kind, we should not go back to using the current method.
3647 DCHECK(had_current_method_input || !needs_current_method_input);
3648
3649 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003650 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3651 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003652 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003653 dispatch_info_ = dispatch_info;
3654 }
3655
Vladimir Markoc53c0792015-11-19 15:48:33 +00003656 void AddSpecialInput(HInstruction* input) {
3657 // We allow only one special input.
3658 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3659 DCHECK(InputCount() == GetSpecialInputIndex() ||
3660 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3661 InsertInputAt(GetSpecialInputIndex(), input);
3662 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003663
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003664 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003665 // We access the method via the dex cache so we can't do an implicit null check.
3666 // TODO: for intrinsics we can generate implicit null checks.
3667 return false;
3668 }
3669
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003670 bool CanBeNull() const OVERRIDE {
3671 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3672 }
3673
Vladimir Markoc53c0792015-11-19 15:48:33 +00003674 // Get the index of the special input, if any.
3675 //
David Brazdil6de19382016-01-08 17:37:10 +00003676 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3677 // method pointer; otherwise there may be one platform-specific special input,
3678 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003679 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003680 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003681
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003682 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3683 void SetOptimizedInvokeType(InvokeType invoke_type) {
3684 optimized_invoke_type_ = invoke_type;
3685 }
3686
Vladimir Marko58155012015-08-19 12:49:41 +00003687 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3688 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3689 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003690 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003691 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003692 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003693 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003694 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3695 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003696 bool HasCurrentMethodInput() const {
3697 // This function can be called only after the invoke has been fully initialized by the builder.
3698 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003699 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003700 return true;
3701 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003702 DCHECK(InputCount() == GetSpecialInputIndex() ||
3703 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003704 return false;
3705 }
3706 }
Vladimir Marko58155012015-08-19 12:49:41 +00003707 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3708 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003709 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003710
3711 int32_t GetStringInitOffset() const {
3712 DCHECK(IsStringInit());
3713 return dispatch_info_.method_load_data;
3714 }
3715
3716 uint64_t GetMethodAddress() const {
3717 DCHECK(HasMethodAddress());
3718 return dispatch_info_.method_load_data;
3719 }
3720
3721 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003722 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003723 return dispatch_info_.method_load_data;
3724 }
3725
3726 uint64_t GetDirectCodePtr() const {
3727 DCHECK(HasDirectCodePtr());
3728 return dispatch_info_.direct_code_ptr;
3729 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003730
Calin Juravle68ad6492015-08-18 17:08:12 +01003731 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3732
Roland Levillain4c0eb422015-04-24 16:43:49 +01003733 // Is this instruction a call to a static method?
3734 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003735 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003736 }
3737
Vladimir Markofbb184a2015-11-13 14:47:00 +00003738 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3739 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3740 // instruction; only relevant for static calls with explicit clinit check.
3741 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003742 DCHECK(IsStaticWithExplicitClinitCheck());
3743 size_t last_input_index = InputCount() - 1;
3744 HInstruction* last_input = InputAt(last_input_index);
3745 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003746 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003747 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003748 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003749 clinit_check_requirement_ = new_requirement;
3750 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003751 }
3752
David Brazdilbc9ab162016-01-20 14:50:19 +00003753 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00003754 DCHECK(IsStringInit());
3755 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00003756 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00003757 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003758 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00003759 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003760 }
3761
Roland Levillain4c0eb422015-04-24 16:43:49 +01003762 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003763 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003764 bool IsStaticWithExplicitClinitCheck() const {
3765 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3766 }
3767
3768 // Is this a call to a static method whose declaring class has an
3769 // implicit intialization check requirement?
3770 bool IsStaticWithImplicitClinitCheck() const {
3771 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3772 }
3773
Vladimir Markob554b5a2015-11-06 12:57:55 +00003774 // Does this method load kind need the current method as an input?
3775 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3776 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3777 }
3778
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003779 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003780
Roland Levillain4c0eb422015-04-24 16:43:49 +01003781 protected:
3782 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3783 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3784 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3785 HInstruction* input = input_record.GetInstruction();
3786 // `input` is the last input of a static invoke marked as having
3787 // an explicit clinit check. It must either be:
3788 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3789 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3790 DCHECK(input != nullptr);
3791 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3792 }
3793 return input_record;
3794 }
3795
Vladimir Markoc53c0792015-11-19 15:48:33 +00003796 void InsertInputAt(size_t index, HInstruction* input);
3797 void RemoveInputAt(size_t index);
3798
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003799 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003800 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003801 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003802 // The target method may refer to different dex file or method index than the original
3803 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3804 // in derived class to increase visibility.
3805 MethodReference target_method_;
3806 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003807
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003808 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003809};
Vladimir Markof64242a2015-12-01 14:58:23 +00003810std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003811std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003812
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003813class HInvokeVirtual : public HInvoke {
3814 public:
3815 HInvokeVirtual(ArenaAllocator* arena,
3816 uint32_t number_of_arguments,
3817 Primitive::Type return_type,
3818 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003819 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003820 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003821 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003822 vtable_index_(vtable_index) {}
3823
Calin Juravle641547a2015-04-21 22:08:51 +01003824 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003825 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003826 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003827 }
3828
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003829 uint32_t GetVTableIndex() const { return vtable_index_; }
3830
3831 DECLARE_INSTRUCTION(InvokeVirtual);
3832
3833 private:
3834 const uint32_t vtable_index_;
3835
3836 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3837};
3838
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003839class HInvokeInterface : public HInvoke {
3840 public:
3841 HInvokeInterface(ArenaAllocator* arena,
3842 uint32_t number_of_arguments,
3843 Primitive::Type return_type,
3844 uint32_t dex_pc,
3845 uint32_t dex_method_index,
3846 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003847 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003848 imt_index_(imt_index) {}
3849
Calin Juravle641547a2015-04-21 22:08:51 +01003850 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003851 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003852 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003853 }
3854
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003855 uint32_t GetImtIndex() const { return imt_index_; }
3856 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3857
3858 DECLARE_INSTRUCTION(InvokeInterface);
3859
3860 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003861 const uint32_t imt_index_;
3862
3863 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3864};
3865
Roland Levillain88cb1752014-10-20 16:36:47 +01003866class HNeg : public HUnaryOperation {
3867 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003868 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3869 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003870
Roland Levillain9867bc72015-08-05 10:21:34 +01003871 template <typename T> T Compute(T x) const { return -x; }
3872
3873 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003874 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003875 }
3876 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003877 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003878 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01003879
Roland Levillain88cb1752014-10-20 16:36:47 +01003880 DECLARE_INSTRUCTION(Neg);
3881
3882 private:
3883 DISALLOW_COPY_AND_ASSIGN(HNeg);
3884};
3885
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003886class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003887 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003888 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003889 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003890 uint32_t dex_pc,
3891 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003892 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003893 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003894 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003895 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003896 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003897 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003898 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003899 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003900 }
3901
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003902 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003903 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003904
3905 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00003906 bool NeedsEnvironment() const OVERRIDE { return true; }
3907
Mingyao Yang0c365e62015-03-31 15:09:29 -07003908 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
3909 bool CanThrow() const OVERRIDE { return true; }
3910
Calin Juravle10e244f2015-01-26 18:54:32 +00003911 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003912
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003913 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3914
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003915 DECLARE_INSTRUCTION(NewArray);
3916
3917 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003918 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01003919 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00003920 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003921
3922 DISALLOW_COPY_AND_ASSIGN(HNewArray);
3923};
3924
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003925class HAdd : public HBinaryOperation {
3926 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003927 HAdd(Primitive::Type result_type,
3928 HInstruction* left,
3929 HInstruction* right,
3930 uint32_t dex_pc = kNoDexPc)
3931 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003932
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003933 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003934
Roland Levillain9867bc72015-08-05 10:21:34 +01003935 template <typename T> T Compute(T x, T y) const { return x + y; }
3936
3937 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003938 return GetBlock()->GetGraph()->GetIntConstant(
3939 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003940 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003941 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003942 return GetBlock()->GetGraph()->GetLongConstant(
3943 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003944 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003945
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003946 DECLARE_INSTRUCTION(Add);
3947
3948 private:
3949 DISALLOW_COPY_AND_ASSIGN(HAdd);
3950};
3951
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003952class HSub : public HBinaryOperation {
3953 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003954 HSub(Primitive::Type result_type,
3955 HInstruction* left,
3956 HInstruction* right,
3957 uint32_t dex_pc = kNoDexPc)
3958 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003959
Roland Levillain9867bc72015-08-05 10:21:34 +01003960 template <typename T> T Compute(T x, T y) const { return x - y; }
3961
3962 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003963 return GetBlock()->GetGraph()->GetIntConstant(
3964 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003965 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003966 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003967 return GetBlock()->GetGraph()->GetLongConstant(
3968 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003969 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003970
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003971 DECLARE_INSTRUCTION(Sub);
3972
3973 private:
3974 DISALLOW_COPY_AND_ASSIGN(HSub);
3975};
3976
Calin Juravle34bacdf2014-10-07 20:23:36 +01003977class HMul : public HBinaryOperation {
3978 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003979 HMul(Primitive::Type result_type,
3980 HInstruction* left,
3981 HInstruction* right,
3982 uint32_t dex_pc = kNoDexPc)
3983 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01003984
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003985 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003986
Roland Levillain9867bc72015-08-05 10:21:34 +01003987 template <typename T> T Compute(T x, T y) const { return x * y; }
3988
3989 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003990 return GetBlock()->GetGraph()->GetIntConstant(
3991 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003992 }
3993 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003994 return GetBlock()->GetGraph()->GetLongConstant(
3995 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003996 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003997
3998 DECLARE_INSTRUCTION(Mul);
3999
4000 private:
4001 DISALLOW_COPY_AND_ASSIGN(HMul);
4002};
4003
Calin Juravle7c4954d2014-10-28 16:57:40 +00004004class HDiv : public HBinaryOperation {
4005 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004006 HDiv(Primitive::Type result_type,
4007 HInstruction* left,
4008 HInstruction* right,
4009 uint32_t dex_pc)
4010 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004011
Roland Levillain9867bc72015-08-05 10:21:34 +01004012 template <typename T>
4013 T Compute(T x, T y) const {
4014 // Our graph structure ensures we never have 0 for `y` during
4015 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004016 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004017 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004018 return (y == -1) ? -x : x / y;
4019 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004020
Roland Levillain9867bc72015-08-05 10:21:34 +01004021 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004022 return GetBlock()->GetGraph()->GetIntConstant(
4023 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004024 }
4025 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004026 return GetBlock()->GetGraph()->GetLongConstant(
4027 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004028 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004029
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004030 static SideEffects SideEffectsForArchRuntimeCalls() {
4031 // The generated code can use a runtime call.
4032 return SideEffects::CanTriggerGC();
4033 }
4034
Calin Juravle7c4954d2014-10-28 16:57:40 +00004035 DECLARE_INSTRUCTION(Div);
4036
4037 private:
4038 DISALLOW_COPY_AND_ASSIGN(HDiv);
4039};
4040
Calin Juravlebacfec32014-11-14 15:54:36 +00004041class HRem : public HBinaryOperation {
4042 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004043 HRem(Primitive::Type result_type,
4044 HInstruction* left,
4045 HInstruction* right,
4046 uint32_t dex_pc)
4047 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004048
Roland Levillain9867bc72015-08-05 10:21:34 +01004049 template <typename T>
4050 T Compute(T x, T y) const {
4051 // Our graph structure ensures we never have 0 for `y` during
4052 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004053 DCHECK_NE(y, 0);
4054 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4055 return (y == -1) ? 0 : x % y;
4056 }
4057
Roland Levillain9867bc72015-08-05 10:21:34 +01004058 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004059 return GetBlock()->GetGraph()->GetIntConstant(
4060 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004061 }
4062 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004063 return GetBlock()->GetGraph()->GetLongConstant(
4064 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004065 }
4066
Calin Juravlebacfec32014-11-14 15:54:36 +00004067
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004068 static SideEffects SideEffectsForArchRuntimeCalls() {
4069 return SideEffects::CanTriggerGC();
4070 }
4071
Calin Juravlebacfec32014-11-14 15:54:36 +00004072 DECLARE_INSTRUCTION(Rem);
4073
4074 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004075 DISALLOW_COPY_AND_ASSIGN(HRem);
4076};
4077
Calin Juravled0d48522014-11-04 16:40:20 +00004078class HDivZeroCheck : public HExpression<1> {
4079 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004080 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4081 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004082 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004083 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004084 SetRawInputAt(0, value);
4085 }
4086
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004087 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4088
Calin Juravled0d48522014-11-04 16:40:20 +00004089 bool CanBeMoved() const OVERRIDE { return true; }
4090
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004091 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004092 return true;
4093 }
4094
4095 bool NeedsEnvironment() const OVERRIDE { return true; }
4096 bool CanThrow() const OVERRIDE { return true; }
4097
Calin Juravled0d48522014-11-04 16:40:20 +00004098 DECLARE_INSTRUCTION(DivZeroCheck);
4099
4100 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004101 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4102};
4103
Calin Juravle9aec02f2014-11-18 23:06:35 +00004104class HShl : public HBinaryOperation {
4105 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004106 HShl(Primitive::Type result_type,
4107 HInstruction* left,
4108 HInstruction* right,
4109 uint32_t dex_pc = kNoDexPc)
4110 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004111
Roland Levillain9867bc72015-08-05 10:21:34 +01004112 template <typename T, typename U, typename V>
4113 T Compute(T x, U y, V max_shift_value) const {
4114 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4115 "V is not the unsigned integer type corresponding to T");
4116 return x << (y & max_shift_value);
4117 }
4118
4119 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4120 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004121 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004122 }
4123 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4124 // case is handled as `x << static_cast<int>(y)`.
4125 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4126 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004127 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004128 }
4129 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4130 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004131 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004132 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004133
4134 DECLARE_INSTRUCTION(Shl);
4135
4136 private:
4137 DISALLOW_COPY_AND_ASSIGN(HShl);
4138};
4139
4140class HShr : public HBinaryOperation {
4141 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004142 HShr(Primitive::Type result_type,
4143 HInstruction* left,
4144 HInstruction* right,
4145 uint32_t dex_pc = kNoDexPc)
4146 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004147
Roland Levillain9867bc72015-08-05 10:21:34 +01004148 template <typename T, typename U, typename V>
4149 T Compute(T x, U y, V max_shift_value) const {
4150 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4151 "V is not the unsigned integer type corresponding to T");
4152 return x >> (y & max_shift_value);
4153 }
4154
4155 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4156 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004157 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004158 }
4159 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4160 // case is handled as `x >> static_cast<int>(y)`.
4161 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4162 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004163 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004164 }
4165 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4166 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004167 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004168 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004169
4170 DECLARE_INSTRUCTION(Shr);
4171
4172 private:
4173 DISALLOW_COPY_AND_ASSIGN(HShr);
4174};
4175
4176class HUShr : public HBinaryOperation {
4177 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004178 HUShr(Primitive::Type result_type,
4179 HInstruction* left,
4180 HInstruction* right,
4181 uint32_t dex_pc = kNoDexPc)
4182 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004183
Roland Levillain9867bc72015-08-05 10:21:34 +01004184 template <typename T, typename U, typename V>
4185 T Compute(T x, U y, V max_shift_value) const {
4186 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4187 "V is not the unsigned integer type corresponding to T");
4188 V ux = static_cast<V>(x);
4189 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004190 }
4191
Roland Levillain9867bc72015-08-05 10:21:34 +01004192 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4193 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004194 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004195 }
4196 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4197 // case is handled as `x >>> static_cast<int>(y)`.
4198 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4199 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004200 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004201 }
4202 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4203 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004204 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004205 }
4206
4207 DECLARE_INSTRUCTION(UShr);
4208
4209 private:
4210 DISALLOW_COPY_AND_ASSIGN(HUShr);
4211};
4212
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004213class HAnd : public HBinaryOperation {
4214 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004215 HAnd(Primitive::Type result_type,
4216 HInstruction* left,
4217 HInstruction* right,
4218 uint32_t dex_pc = kNoDexPc)
4219 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004220
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004221 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004222
Roland Levillain9867bc72015-08-05 10:21:34 +01004223 template <typename T, typename U>
4224 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4225
4226 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004227 return GetBlock()->GetGraph()->GetIntConstant(
4228 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004229 }
4230 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004231 return GetBlock()->GetGraph()->GetLongConstant(
4232 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004233 }
4234 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004235 return GetBlock()->GetGraph()->GetLongConstant(
4236 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004237 }
4238 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004239 return GetBlock()->GetGraph()->GetLongConstant(
4240 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004241 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004242
4243 DECLARE_INSTRUCTION(And);
4244
4245 private:
4246 DISALLOW_COPY_AND_ASSIGN(HAnd);
4247};
4248
4249class HOr : public HBinaryOperation {
4250 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004251 HOr(Primitive::Type result_type,
4252 HInstruction* left,
4253 HInstruction* right,
4254 uint32_t dex_pc = kNoDexPc)
4255 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004256
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004257 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004258
Roland Levillain9867bc72015-08-05 10:21:34 +01004259 template <typename T, typename U>
4260 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4261
4262 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004263 return GetBlock()->GetGraph()->GetIntConstant(
4264 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004265 }
4266 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004267 return GetBlock()->GetGraph()->GetLongConstant(
4268 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004269 }
4270 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004271 return GetBlock()->GetGraph()->GetLongConstant(
4272 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004273 }
4274 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004275 return GetBlock()->GetGraph()->GetLongConstant(
4276 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004277 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004278
4279 DECLARE_INSTRUCTION(Or);
4280
4281 private:
4282 DISALLOW_COPY_AND_ASSIGN(HOr);
4283};
4284
4285class HXor : public HBinaryOperation {
4286 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004287 HXor(Primitive::Type result_type,
4288 HInstruction* left,
4289 HInstruction* right,
4290 uint32_t dex_pc = kNoDexPc)
4291 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004292
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004293 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004294
Roland Levillain9867bc72015-08-05 10:21:34 +01004295 template <typename T, typename U>
4296 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4297
4298 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004299 return GetBlock()->GetGraph()->GetIntConstant(
4300 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004301 }
4302 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004303 return GetBlock()->GetGraph()->GetLongConstant(
4304 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004305 }
4306 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004307 return GetBlock()->GetGraph()->GetLongConstant(
4308 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004309 }
4310 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004311 return GetBlock()->GetGraph()->GetLongConstant(
4312 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004313 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004314
4315 DECLARE_INSTRUCTION(Xor);
4316
4317 private:
4318 DISALLOW_COPY_AND_ASSIGN(HXor);
4319};
4320
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004321class HRor : public HBinaryOperation {
4322 public:
4323 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4324 : HBinaryOperation(result_type, value, distance) {}
4325
4326 template <typename T, typename U, typename V>
4327 T Compute(T x, U y, V max_shift_value) const {
4328 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4329 "V is not the unsigned integer type corresponding to T");
4330 V ux = static_cast<V>(x);
4331 if ((y & max_shift_value) == 0) {
4332 return static_cast<T>(ux);
4333 } else {
4334 const V reg_bits = sizeof(T) * 8;
4335 return static_cast<T>(ux >> (y & max_shift_value)) |
4336 (x << (reg_bits - (y & max_shift_value)));
4337 }
4338 }
4339
4340 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4341 return GetBlock()->GetGraph()->GetIntConstant(
4342 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4343 }
4344 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4345 return GetBlock()->GetGraph()->GetLongConstant(
4346 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4347 }
4348 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4349 return GetBlock()->GetGraph()->GetLongConstant(
4350 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4351 }
4352
4353 DECLARE_INSTRUCTION(Ror);
4354
4355 private:
4356 DISALLOW_COPY_AND_ASSIGN(HRor);
4357};
4358
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004359// The value of a parameter in this method. Its location depends on
4360// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004361class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004362 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004363 HParameterValue(const DexFile& dex_file,
4364 uint16_t type_index,
4365 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004366 Primitive::Type parameter_type,
4367 bool is_this = false)
4368 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004369 dex_file_(dex_file),
4370 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004371 index_(index),
4372 is_this_(is_this),
4373 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004374
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004375 const DexFile& GetDexFile() const { return dex_file_; }
4376 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004377 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004378 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004379
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004380 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4381 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004382
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004383 DECLARE_INSTRUCTION(ParameterValue);
4384
4385 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004386 const DexFile& dex_file_;
4387 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004388 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004389 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004390 const uint8_t index_;
4391
Calin Juravle10e244f2015-01-26 18:54:32 +00004392 // Whether or not the parameter value corresponds to 'this' argument.
4393 const bool is_this_;
4394
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004395 bool can_be_null_;
4396
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004397 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4398};
4399
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004400class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004401 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004402 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4403 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004404
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004405 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004406 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004407 return true;
4408 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004409
Roland Levillain9867bc72015-08-05 10:21:34 +01004410 template <typename T> T Compute(T x) const { return ~x; }
4411
4412 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004413 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004414 }
4415 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004416 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004417 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004418
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004419 DECLARE_INSTRUCTION(Not);
4420
4421 private:
4422 DISALLOW_COPY_AND_ASSIGN(HNot);
4423};
4424
David Brazdil66d126e2015-04-03 16:02:44 +01004425class HBooleanNot : public HUnaryOperation {
4426 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004427 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4428 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004429
4430 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004431 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004432 return true;
4433 }
4434
Roland Levillain9867bc72015-08-05 10:21:34 +01004435 template <typename T> bool Compute(T x) const {
David Brazdil66d126e2015-04-03 16:02:44 +01004436 DCHECK(IsUint<1>(x));
4437 return !x;
4438 }
4439
Roland Levillain9867bc72015-08-05 10:21:34 +01004440 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004441 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004442 }
4443 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4444 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004445 UNREACHABLE();
4446 }
4447
4448 DECLARE_INSTRUCTION(BooleanNot);
4449
4450 private:
4451 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4452};
4453
Roland Levillaindff1f282014-11-05 14:15:05 +00004454class HTypeConversion : public HExpression<1> {
4455 public:
4456 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004457 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004458 : HExpression(result_type,
4459 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4460 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004461 SetRawInputAt(0, input);
4462 DCHECK_NE(input->GetType(), result_type);
4463 }
4464
4465 HInstruction* GetInput() const { return InputAt(0); }
4466 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4467 Primitive::Type GetResultType() const { return GetType(); }
4468
4469 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004470 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004471
Mark Mendelle82549b2015-05-06 10:55:34 -04004472 // Try to statically evaluate the conversion and return a HConstant
4473 // containing the result. If the input cannot be converted, return nullptr.
4474 HConstant* TryStaticEvaluation() const;
4475
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004476 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4477 Primitive::Type result_type) {
4478 // Some architectures may not require the 'GC' side effects, but at this point
4479 // in the compilation process we do not know what architecture we will
4480 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004481 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4482 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004483 return SideEffects::CanTriggerGC();
4484 }
4485 return SideEffects::None();
4486 }
4487
Roland Levillaindff1f282014-11-05 14:15:05 +00004488 DECLARE_INSTRUCTION(TypeConversion);
4489
4490 private:
4491 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4492};
4493
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004494static constexpr uint32_t kNoRegNumber = -1;
4495
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004496class HPhi : public HInstruction {
4497 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004498 HPhi(ArenaAllocator* arena,
4499 uint32_t reg_number,
4500 size_t number_of_inputs,
4501 Primitive::Type type,
4502 uint32_t dex_pc = kNoDexPc)
4503 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004504 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004505 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004506 type_(ToPhiType(type)),
4507 // Phis are constructed live and marked dead if conflicting or unused.
4508 // Individual steps of SsaBuilder should assume that if a phi has been
4509 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4510 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004511 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004512 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004513 }
4514
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004515 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4516 static Primitive::Type ToPhiType(Primitive::Type type) {
4517 switch (type) {
4518 case Primitive::kPrimBoolean:
4519 case Primitive::kPrimByte:
4520 case Primitive::kPrimShort:
4521 case Primitive::kPrimChar:
4522 return Primitive::kPrimInt;
4523 default:
4524 return type;
4525 }
4526 }
4527
David Brazdilffee3d32015-07-06 11:48:53 +01004528 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4529
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004530 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004531
4532 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004533 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004534
Calin Juravle10e244f2015-01-26 18:54:32 +00004535 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil4833f5a2015-12-16 10:37:39 +00004536 void SetType(Primitive::Type new_type) {
4537 // Make sure that only valid type changes occur. The following are allowed:
4538 // (1) int -> float/ref (primitive type propagation),
4539 // (2) long -> double (primitive type propagation).
4540 DCHECK(type_ == new_type ||
4541 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4542 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4543 (type_ == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4544 type_ = new_type;
4545 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004546
Calin Juravle10e244f2015-01-26 18:54:32 +00004547 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4548 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4549
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004550 uint32_t GetRegNumber() const { return reg_number_; }
4551
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004552 void SetDead() { is_live_ = false; }
4553 void SetLive() { is_live_ = true; }
4554 bool IsDead() const { return !is_live_; }
4555 bool IsLive() const { return is_live_; }
4556
David Brazdil77a48ae2015-09-15 12:34:04 +00004557 bool IsVRegEquivalentOf(HInstruction* other) const {
4558 return other != nullptr
4559 && other->IsPhi()
4560 && other->AsPhi()->GetBlock() == GetBlock()
4561 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4562 }
4563
Calin Juravlea4f88312015-04-16 12:57:19 +01004564 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4565 // An equivalent phi is a phi having the same dex register and type.
4566 // It assumes that phis with the same dex register are adjacent.
4567 HPhi* GetNextEquivalentPhiWithSameType() {
4568 HInstruction* next = GetNext();
4569 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4570 if (next->GetType() == GetType()) {
4571 return next->AsPhi();
4572 }
4573 next = next->GetNext();
4574 }
4575 return nullptr;
4576 }
4577
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004578 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004579
David Brazdil1abb4192015-02-17 18:33:36 +00004580 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004581 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004582 return inputs_[index];
4583 }
David Brazdil1abb4192015-02-17 18:33:36 +00004584
4585 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004586 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004587 }
4588
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004589 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004590 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004591 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004592 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004593 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004594 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004595
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004596 DISALLOW_COPY_AND_ASSIGN(HPhi);
4597};
4598
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004599class HNullCheck : public HExpression<1> {
4600 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004601 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4602 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004603 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004604 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004605 SetRawInputAt(0, value);
4606 }
4607
Calin Juravle10e244f2015-01-26 18:54:32 +00004608 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004609 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004610 return true;
4611 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004612
Calin Juravle10e244f2015-01-26 18:54:32 +00004613 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004614
Calin Juravle10e244f2015-01-26 18:54:32 +00004615 bool CanThrow() const OVERRIDE { return true; }
4616
4617 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004618
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004619
4620 DECLARE_INSTRUCTION(NullCheck);
4621
4622 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004623 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4624};
4625
4626class FieldInfo : public ValueObject {
4627 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004628 FieldInfo(MemberOffset field_offset,
4629 Primitive::Type field_type,
4630 bool is_volatile,
4631 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004632 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004633 const DexFile& dex_file,
4634 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004635 : field_offset_(field_offset),
4636 field_type_(field_type),
4637 is_volatile_(is_volatile),
4638 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004639 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004640 dex_file_(dex_file),
4641 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004642
4643 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004644 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004645 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004646 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004647 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004648 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004649 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004650
4651 private:
4652 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004653 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004654 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004655 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004656 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004657 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004658 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004659};
4660
4661class HInstanceFieldGet : public HExpression<1> {
4662 public:
4663 HInstanceFieldGet(HInstruction* value,
4664 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004665 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004666 bool is_volatile,
4667 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004668 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004669 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004670 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004671 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004672 : HExpression(field_type,
4673 SideEffects::FieldReadOfType(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 Geoffraye5038322014-07-04 09:41:32 +01004682 SetRawInputAt(0, value);
4683 }
4684
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004685 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004686
4687 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4688 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4689 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004690 }
4691
Calin Juravle641547a2015-04-21 22:08:51 +01004692 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4693 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004694 }
4695
4696 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004697 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4698 }
4699
Calin Juravle52c48962014-12-16 17:02:57 +00004700 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004701 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004702 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004703 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004704
4705 DECLARE_INSTRUCTION(InstanceFieldGet);
4706
4707 private:
4708 const FieldInfo field_info_;
4709
4710 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4711};
4712
4713class HInstanceFieldSet : public HTemplateInstruction<2> {
4714 public:
4715 HInstanceFieldSet(HInstruction* object,
4716 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004717 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004718 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004719 bool is_volatile,
4720 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004721 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004722 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004723 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004724 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004725 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4726 dex_pc),
4727 field_info_(field_offset,
4728 field_type,
4729 is_volatile,
4730 field_idx,
4731 declaring_class_def_index,
4732 dex_file,
4733 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004734 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004735 SetRawInputAt(0, object);
4736 SetRawInputAt(1, value);
4737 }
4738
Calin Juravle641547a2015-04-21 22:08:51 +01004739 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4740 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004741 }
4742
Calin Juravle52c48962014-12-16 17:02:57 +00004743 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004744 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004745 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004746 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004747 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004748 bool GetValueCanBeNull() const { return value_can_be_null_; }
4749 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004750
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004751 DECLARE_INSTRUCTION(InstanceFieldSet);
4752
4753 private:
4754 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004755 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004756
4757 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
4758};
4759
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004760class HArrayGet : public HExpression<2> {
4761 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004762 HArrayGet(HInstruction* array,
4763 HInstruction* index,
4764 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004765 uint32_t dex_pc,
4766 SideEffects additional_side_effects = SideEffects::None())
4767 : HExpression(type,
4768 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00004769 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004770 SetRawInputAt(0, array);
4771 SetRawInputAt(1, index);
4772 }
4773
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004774 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004775 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004776 return true;
4777 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004778 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004779 // TODO: We can be smarter here.
4780 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
4781 // which generates the implicit null check. There are cases when these can be removed
4782 // to produce better code. If we ever add optimizations to do so we should allow an
4783 // implicit check here (as long as the address falls in the first page).
4784 return false;
4785 }
4786
David Brazdil4833f5a2015-12-16 10:37:39 +00004787 bool IsEquivalentOf(HArrayGet* other) const {
4788 bool result = (GetDexPc() == other->GetDexPc());
4789 if (kIsDebugBuild && result) {
4790 DCHECK_EQ(GetBlock(), other->GetBlock());
4791 DCHECK_EQ(GetArray(), other->GetArray());
4792 DCHECK_EQ(GetIndex(), other->GetIndex());
4793 if (Primitive::IsIntOrLongType(GetType())) {
4794 DCHECK(Primitive::IsFloatingPointType(other->GetType()));
4795 } else {
4796 DCHECK(Primitive::IsFloatingPointType(GetType()));
4797 DCHECK(Primitive::IsIntOrLongType(other->GetType()));
4798 }
4799 }
4800 return result;
4801 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004802
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004803 HInstruction* GetArray() const { return InputAt(0); }
4804 HInstruction* GetIndex() const { return InputAt(1); }
4805
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004806 DECLARE_INSTRUCTION(ArrayGet);
4807
4808 private:
4809 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
4810};
4811
4812class HArraySet : public HTemplateInstruction<3> {
4813 public:
4814 HArraySet(HInstruction* array,
4815 HInstruction* index,
4816 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004817 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004818 uint32_t dex_pc,
4819 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004820 : HTemplateInstruction(
4821 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004822 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
4823 additional_side_effects),
4824 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004825 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004826 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004827 value_can_be_null_(true),
4828 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004829 SetRawInputAt(0, array);
4830 SetRawInputAt(1, index);
4831 SetRawInputAt(2, value);
4832 }
4833
Calin Juravle77520bc2015-01-12 18:45:46 +00004834 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00004835 // We call a runtime method to throw ArrayStoreException.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004836 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004837 }
4838
Mingyao Yang81014cb2015-06-02 03:16:27 -07004839 // Can throw ArrayStoreException.
4840 bool CanThrow() const OVERRIDE { return needs_type_check_; }
4841
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004842 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004843 // TODO: Same as for ArrayGet.
4844 return false;
4845 }
4846
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004847 void ClearNeedsTypeCheck() {
4848 needs_type_check_ = false;
4849 }
4850
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004851 void ClearValueCanBeNull() {
4852 value_can_be_null_ = false;
4853 }
4854
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004855 void SetStaticTypeOfArrayIsObjectArray() {
4856 static_type_of_array_is_object_array_ = true;
4857 }
4858
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004859 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004860 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004861 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004862
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004863 HInstruction* GetArray() const { return InputAt(0); }
4864 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004865 HInstruction* GetValue() const { return InputAt(2); }
4866
4867 Primitive::Type GetComponentType() const {
4868 // The Dex format does not type floating point index operations. Since the
4869 // `expected_component_type_` is set during building and can therefore not
4870 // be correct, we also check what is the value type. If it is a floating
4871 // point type, we must use that type.
4872 Primitive::Type value_type = GetValue()->GetType();
4873 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
4874 ? value_type
4875 : expected_component_type_;
4876 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004877
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004878 Primitive::Type GetRawExpectedComponentType() const {
4879 return expected_component_type_;
4880 }
4881
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004882 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
4883 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
4884 }
4885
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004886 DECLARE_INSTRUCTION(ArraySet);
4887
4888 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004889 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004890 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004891 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004892 // Cached information for the reference_type_info_ so that codegen
4893 // does not need to inspect the static type.
4894 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004895
4896 DISALLOW_COPY_AND_ASSIGN(HArraySet);
4897};
4898
4899class HArrayLength : public HExpression<1> {
4900 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01004901 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004902 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004903 // Note that arrays do not change length, so the instruction does not
4904 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004905 SetRawInputAt(0, array);
4906 }
4907
Calin Juravle77520bc2015-01-12 18:45:46 +00004908 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004909 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004910 return true;
4911 }
Calin Juravle641547a2015-04-21 22:08:51 +01004912 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4913 return obj == InputAt(0);
4914 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004915
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004916 DECLARE_INSTRUCTION(ArrayLength);
4917
4918 private:
4919 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
4920};
4921
4922class HBoundsCheck : public HExpression<2> {
4923 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004924 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
4925 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004926 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004927 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004928 DCHECK(index->GetType() == Primitive::kPrimInt);
4929 SetRawInputAt(0, index);
4930 SetRawInputAt(1, length);
4931 }
4932
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004933 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004934 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004935 return true;
4936 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004937
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004938 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004939
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004940 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004941
Alexandre Ramese6dbf482015-10-19 10:10:41 +01004942 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004943
4944 DECLARE_INSTRUCTION(BoundsCheck);
4945
4946 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004947 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
4948};
4949
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004950class HSuspendCheck : public HTemplateInstruction<0> {
4951 public:
4952 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004953 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004954
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004955 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004956 return true;
4957 }
4958
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004959 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
4960 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004961
4962 DECLARE_INSTRUCTION(SuspendCheck);
4963
4964 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004965 // Only used for code generation, in order to share the same slow path between back edges
4966 // of a same loop.
4967 SlowPathCode* slow_path_;
4968
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004969 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
4970};
4971
David Srbecky0cf44932015-12-09 14:09:59 +00004972// Pseudo-instruction which provides the native debugger with mapping information.
4973// It ensures that we can generate line number and local variables at this point.
4974class HNativeDebugInfo : public HTemplateInstruction<0> {
4975 public:
4976 explicit HNativeDebugInfo(uint32_t dex_pc)
4977 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
4978
4979 bool NeedsEnvironment() const OVERRIDE {
4980 return true;
4981 }
4982
4983 DECLARE_INSTRUCTION(NativeDebugInfo);
4984
4985 private:
4986 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
4987};
4988
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004989/**
4990 * Instruction to load a Class object.
4991 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004992class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004993 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004994 HLoadClass(HCurrentMethod* current_method,
4995 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01004996 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004997 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01004998 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004999 bool needs_access_check,
5000 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005001 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005002 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005003 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005004 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00005005 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01005006 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005007 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00005008 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005009 // Referrers class should not need access check. We never inline unverified
5010 // methods so we can't possibly end up in this situation.
5011 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005012 SetRawInputAt(0, current_method);
5013 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005014
5015 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005016
5017 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005018 // Note that we don't need to test for generate_clinit_check_.
5019 // Whether or not we need to generate the clinit check is processed in
5020 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005021 return other->AsLoadClass()->type_index_ == type_index_ &&
5022 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005023 }
5024
5025 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5026
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005027 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005028 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005029 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005030
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005031 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005032 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005033 }
5034
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005035 bool MustGenerateClinitCheck() const {
5036 return generate_clinit_check_;
5037 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005038
Calin Juravle0ba218d2015-05-19 18:46:01 +01005039 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005040 // The entrypoint the code generator is going to call does not do
5041 // clinit of the class.
5042 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01005043 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005044 }
5045
5046 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005047 return MustGenerateClinitCheck() ||
5048 (!is_referrers_class_ && !is_in_dex_cache_) ||
5049 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01005050 }
5051
5052 bool NeedsAccessCheck() const {
5053 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005054 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005055
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005056 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005057 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005058 }
5059
Calin Juravleacf735c2015-02-12 15:25:22 +00005060 ReferenceTypeInfo GetLoadedClassRTI() {
5061 return loaded_class_rti_;
5062 }
5063
5064 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5065 // Make sure we only set exact types (the loaded class should never be merged).
5066 DCHECK(rti.IsExact());
5067 loaded_class_rti_ = rti;
5068 }
5069
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005070 const DexFile& GetDexFile() { return dex_file_; }
5071
Vladimir Markodc151b22015-10-15 18:02:30 +01005072 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005073
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005074 static SideEffects SideEffectsForArchRuntimeCalls() {
5075 return SideEffects::CanTriggerGC();
5076 }
5077
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005078 bool IsInDexCache() const { return is_in_dex_cache_; }
5079
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005080 DECLARE_INSTRUCTION(LoadClass);
5081
5082 private:
5083 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005084 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005085 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005086 // Whether this instruction must generate the initialization check.
5087 // Used for code generation.
5088 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005089 const bool needs_access_check_;
5090 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005091
Calin Juravleacf735c2015-02-12 15:25:22 +00005092 ReferenceTypeInfo loaded_class_rti_;
5093
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005094 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5095};
5096
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005097class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005098 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005099 HLoadString(HCurrentMethod* current_method,
5100 uint32_t string_index,
5101 uint32_t dex_pc,
5102 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005103 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005104 string_index_(string_index),
5105 is_in_dex_cache_(is_in_dex_cache) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005106 SetRawInputAt(0, current_method);
5107 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005108
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005109 bool CanBeMoved() const OVERRIDE { return true; }
5110
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005111 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5112 return other->AsLoadString()->string_index_ == string_index_;
5113 }
5114
5115 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5116
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005117 uint32_t GetStringIndex() const { return string_index_; }
5118
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005119 // Will call the runtime if the string is not already in the dex cache.
5120 bool NeedsEnvironment() const OVERRIDE { return !IsInDexCache(); }
5121
Vladimir Markodc151b22015-10-15 18:02:30 +01005122 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005123 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005124 bool IsInDexCache() const { return is_in_dex_cache_; }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005125 bool CanThrow() const OVERRIDE { return !IsInDexCache(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005126
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005127 static SideEffects SideEffectsForArchRuntimeCalls() {
5128 return SideEffects::CanTriggerGC();
5129 }
5130
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005131 DECLARE_INSTRUCTION(LoadString);
5132
5133 private:
5134 const uint32_t string_index_;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005135 const bool is_in_dex_cache_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005136
5137 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5138};
5139
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005140/**
5141 * Performs an initialization check on its Class object input.
5142 */
5143class HClinitCheck : public HExpression<1> {
5144 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005145 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005146 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005147 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005148 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5149 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005150 SetRawInputAt(0, constant);
5151 }
5152
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005153 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005154 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005155 return true;
5156 }
5157
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005158 bool NeedsEnvironment() const OVERRIDE {
5159 // May call runtime to initialize the class.
5160 return true;
5161 }
5162
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005163 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005164
5165 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5166
5167 DECLARE_INSTRUCTION(ClinitCheck);
5168
5169 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005170 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5171};
5172
5173class HStaticFieldGet : public HExpression<1> {
5174 public:
5175 HStaticFieldGet(HInstruction* cls,
5176 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005177 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005178 bool is_volatile,
5179 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005180 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005181 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005182 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005183 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005184 : HExpression(field_type,
5185 SideEffects::FieldReadOfType(field_type, is_volatile),
5186 dex_pc),
5187 field_info_(field_offset,
5188 field_type,
5189 is_volatile,
5190 field_idx,
5191 declaring_class_def_index,
5192 dex_file,
5193 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005194 SetRawInputAt(0, cls);
5195 }
5196
Calin Juravle52c48962014-12-16 17:02:57 +00005197
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005198 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005199
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005200 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005201 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5202 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005203 }
5204
5205 size_t ComputeHashCode() const OVERRIDE {
5206 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5207 }
5208
Calin Juravle52c48962014-12-16 17:02:57 +00005209 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005210 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5211 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005212 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005213
5214 DECLARE_INSTRUCTION(StaticFieldGet);
5215
5216 private:
5217 const FieldInfo field_info_;
5218
5219 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5220};
5221
5222class HStaticFieldSet : public HTemplateInstruction<2> {
5223 public:
5224 HStaticFieldSet(HInstruction* cls,
5225 HInstruction* value,
5226 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005227 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005228 bool is_volatile,
5229 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005230 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005231 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005232 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005233 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005234 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5235 dex_pc),
5236 field_info_(field_offset,
5237 field_type,
5238 is_volatile,
5239 field_idx,
5240 declaring_class_def_index,
5241 dex_file,
5242 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005243 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005244 SetRawInputAt(0, cls);
5245 SetRawInputAt(1, value);
5246 }
5247
Calin Juravle52c48962014-12-16 17:02:57 +00005248 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005249 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5250 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005251 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005252
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005253 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005254 bool GetValueCanBeNull() const { return value_can_be_null_; }
5255 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005256
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005257 DECLARE_INSTRUCTION(StaticFieldSet);
5258
5259 private:
5260 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005261 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005262
5263 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5264};
5265
Calin Juravlee460d1d2015-09-29 04:52:17 +01005266class HUnresolvedInstanceFieldGet : public HExpression<1> {
5267 public:
5268 HUnresolvedInstanceFieldGet(HInstruction* obj,
5269 Primitive::Type field_type,
5270 uint32_t field_index,
5271 uint32_t dex_pc)
5272 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5273 field_index_(field_index) {
5274 SetRawInputAt(0, obj);
5275 }
5276
5277 bool NeedsEnvironment() const OVERRIDE { return true; }
5278 bool CanThrow() const OVERRIDE { return true; }
5279
5280 Primitive::Type GetFieldType() const { return GetType(); }
5281 uint32_t GetFieldIndex() const { return field_index_; }
5282
5283 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5284
5285 private:
5286 const uint32_t field_index_;
5287
5288 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5289};
5290
5291class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5292 public:
5293 HUnresolvedInstanceFieldSet(HInstruction* obj,
5294 HInstruction* value,
5295 Primitive::Type field_type,
5296 uint32_t field_index,
5297 uint32_t dex_pc)
5298 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5299 field_type_(field_type),
5300 field_index_(field_index) {
5301 DCHECK_EQ(field_type, value->GetType());
5302 SetRawInputAt(0, obj);
5303 SetRawInputAt(1, value);
5304 }
5305
5306 bool NeedsEnvironment() const OVERRIDE { return true; }
5307 bool CanThrow() const OVERRIDE { return true; }
5308
5309 Primitive::Type GetFieldType() const { return field_type_; }
5310 uint32_t GetFieldIndex() const { return field_index_; }
5311
5312 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5313
5314 private:
5315 const Primitive::Type field_type_;
5316 const uint32_t field_index_;
5317
5318 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5319};
5320
5321class HUnresolvedStaticFieldGet : public HExpression<0> {
5322 public:
5323 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5324 uint32_t field_index,
5325 uint32_t dex_pc)
5326 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5327 field_index_(field_index) {
5328 }
5329
5330 bool NeedsEnvironment() const OVERRIDE { return true; }
5331 bool CanThrow() const OVERRIDE { return true; }
5332
5333 Primitive::Type GetFieldType() const { return GetType(); }
5334 uint32_t GetFieldIndex() const { return field_index_; }
5335
5336 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5337
5338 private:
5339 const uint32_t field_index_;
5340
5341 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5342};
5343
5344class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5345 public:
5346 HUnresolvedStaticFieldSet(HInstruction* value,
5347 Primitive::Type field_type,
5348 uint32_t field_index,
5349 uint32_t dex_pc)
5350 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5351 field_type_(field_type),
5352 field_index_(field_index) {
5353 DCHECK_EQ(field_type, value->GetType());
5354 SetRawInputAt(0, value);
5355 }
5356
5357 bool NeedsEnvironment() const OVERRIDE { return true; }
5358 bool CanThrow() const OVERRIDE { return true; }
5359
5360 Primitive::Type GetFieldType() const { return field_type_; }
5361 uint32_t GetFieldIndex() const { return field_index_; }
5362
5363 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5364
5365 private:
5366 const Primitive::Type field_type_;
5367 const uint32_t field_index_;
5368
5369 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5370};
5371
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005372// Implement the move-exception DEX instruction.
5373class HLoadException : public HExpression<0> {
5374 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005375 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5376 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005377
David Brazdilbbd733e2015-08-18 17:48:17 +01005378 bool CanBeNull() const OVERRIDE { return false; }
5379
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005380 DECLARE_INSTRUCTION(LoadException);
5381
5382 private:
5383 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5384};
5385
David Brazdilcb1c0552015-08-04 16:22:25 +01005386// Implicit part of move-exception which clears thread-local exception storage.
5387// Must not be removed because the runtime expects the TLS to get cleared.
5388class HClearException : public HTemplateInstruction<0> {
5389 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005390 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5391 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005392
5393 DECLARE_INSTRUCTION(ClearException);
5394
5395 private:
5396 DISALLOW_COPY_AND_ASSIGN(HClearException);
5397};
5398
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005399class HThrow : public HTemplateInstruction<1> {
5400 public:
5401 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005402 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005403 SetRawInputAt(0, exception);
5404 }
5405
5406 bool IsControlFlow() const OVERRIDE { return true; }
5407
5408 bool NeedsEnvironment() const OVERRIDE { return true; }
5409
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005410 bool CanThrow() const OVERRIDE { return true; }
5411
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005412
5413 DECLARE_INSTRUCTION(Throw);
5414
5415 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005416 DISALLOW_COPY_AND_ASSIGN(HThrow);
5417};
5418
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005419/**
5420 * Implementation strategies for the code generator of a HInstanceOf
5421 * or `HCheckCast`.
5422 */
5423enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005424 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005425 kExactCheck, // Can do a single class compare.
5426 kClassHierarchyCheck, // Can just walk the super class chain.
5427 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5428 kInterfaceCheck, // No optimization yet when checking against an interface.
5429 kArrayObjectCheck, // Can just check if the array is not primitive.
5430 kArrayCheck // No optimization yet when checking against a generic array.
5431};
5432
Roland Levillain86503782016-02-11 19:07:30 +00005433std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5434
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005435class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005436 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005437 HInstanceOf(HInstruction* object,
5438 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005439 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005440 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005441 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005442 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005443 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005444 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005445 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005446 SetRawInputAt(0, object);
5447 SetRawInputAt(1, constant);
5448 }
5449
5450 bool CanBeMoved() const OVERRIDE { return true; }
5451
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005452 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005453 return true;
5454 }
5455
5456 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005457 return CanCallRuntime(check_kind_);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005458 }
5459
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005460 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5461
5462 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005463
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005464 // Used only in code generation.
5465 bool MustDoNullCheck() const { return must_do_null_check_; }
5466 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5467
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005468 static bool CanCallRuntime(TypeCheckKind check_kind) {
5469 // Mips currently does runtime calls for any other checks.
5470 return check_kind != TypeCheckKind::kExactCheck;
5471 }
5472
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005473 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005474 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005475 }
5476
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005477 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005478
5479 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005480 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005481 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005482
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005483 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5484};
5485
Calin Juravleb1498f62015-02-16 13:13:29 +00005486class HBoundType : public HExpression<1> {
5487 public:
David Brazdilf5552582015-12-27 13:36:12 +00005488 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005489 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
David Brazdilf5552582015-12-27 13:36:12 +00005490 upper_bound_(ReferenceTypeInfo::CreateInvalid()),
5491 upper_can_be_null_(true),
5492 can_be_null_(true) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005493 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005494 SetRawInputAt(0, input);
5495 }
5496
David Brazdilf5552582015-12-27 13:36:12 +00005497 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005498 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5499 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
David Brazdilf5552582015-12-27 13:36:12 +00005500 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005501
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005502 void SetCanBeNull(bool can_be_null) {
5503 DCHECK(upper_can_be_null_ || !can_be_null);
5504 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005505 }
5506
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005507 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5508
Calin Juravleb1498f62015-02-16 13:13:29 +00005509 DECLARE_INSTRUCTION(BoundType);
5510
5511 private:
5512 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005513 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5514 // It is used to bound the type in cases like:
5515 // if (x instanceof ClassX) {
5516 // // uper_bound_ will be ClassX
5517 // }
David Brazdilf5552582015-12-27 13:36:12 +00005518 ReferenceTypeInfo upper_bound_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005519 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5520 // is false then can_be_null_ cannot be true).
David Brazdilf5552582015-12-27 13:36:12 +00005521 bool upper_can_be_null_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005522 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005523
5524 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5525};
5526
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005527class HCheckCast : public HTemplateInstruction<2> {
5528 public:
5529 HCheckCast(HInstruction* object,
5530 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005531 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005532 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005533 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005534 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005535 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005536 SetRawInputAt(0, object);
5537 SetRawInputAt(1, constant);
5538 }
5539
5540 bool CanBeMoved() const OVERRIDE { return true; }
5541
5542 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5543 return true;
5544 }
5545
5546 bool NeedsEnvironment() const OVERRIDE {
5547 // Instruction may throw a CheckCastError.
5548 return true;
5549 }
5550
5551 bool CanThrow() const OVERRIDE { return true; }
5552
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005553 bool MustDoNullCheck() const { return must_do_null_check_; }
5554 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005555 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005556
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005557 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005558
5559 DECLARE_INSTRUCTION(CheckCast);
5560
5561 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005562 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005563 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005564
5565 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005566};
5567
Calin Juravle27df7582015-04-17 19:12:31 +01005568class HMemoryBarrier : public HTemplateInstruction<0> {
5569 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005570 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005571 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005572 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005573 barrier_kind_(barrier_kind) {}
5574
5575 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5576
5577 DECLARE_INSTRUCTION(MemoryBarrier);
5578
5579 private:
5580 const MemBarrierKind barrier_kind_;
5581
5582 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5583};
5584
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005585class HMonitorOperation : public HTemplateInstruction<1> {
5586 public:
5587 enum OperationKind {
5588 kEnter,
5589 kExit,
5590 };
5591
5592 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005593 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005594 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5595 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005596 SetRawInputAt(0, object);
5597 }
5598
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005599 // Instruction may go into runtime, so we need an environment.
5600 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00005601
5602 bool CanThrow() const OVERRIDE {
5603 // Verifier guarantees that monitor-exit cannot throw.
5604 // This is important because it allows the HGraphBuilder to remove
5605 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5606 return IsEnter();
5607 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005608
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005609
5610 bool IsEnter() const { return kind_ == kEnter; }
5611
5612 DECLARE_INSTRUCTION(MonitorOperation);
5613
Calin Juravle52c48962014-12-16 17:02:57 +00005614 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005615 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005616
5617 private:
5618 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5619};
5620
David Brazdil74eb1b22015-12-14 11:44:01 +00005621class HSelect : public HExpression<3> {
5622 public:
5623 HSelect(HInstruction* condition,
5624 HInstruction* true_value,
5625 HInstruction* false_value,
5626 uint32_t dex_pc)
5627 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
5628 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
5629
5630 // First input must be `true_value` or `false_value` to allow codegens to
5631 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
5632 // that architectures which implement HSelect as a conditional move also
5633 // will not need to invert the condition.
5634 SetRawInputAt(0, false_value);
5635 SetRawInputAt(1, true_value);
5636 SetRawInputAt(2, condition);
5637 }
5638
5639 HInstruction* GetFalseValue() const { return InputAt(0); }
5640 HInstruction* GetTrueValue() const { return InputAt(1); }
5641 HInstruction* GetCondition() const { return InputAt(2); }
5642
5643 bool CanBeMoved() const OVERRIDE { return true; }
5644 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
5645
5646 bool CanBeNull() const OVERRIDE {
5647 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
5648 }
5649
5650 DECLARE_INSTRUCTION(Select);
5651
5652 private:
5653 DISALLOW_COPY_AND_ASSIGN(HSelect);
5654};
5655
Vladimir Markof9f64412015-09-02 14:05:49 +01005656class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005657 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005658 MoveOperands(Location source,
5659 Location destination,
5660 Primitive::Type type,
5661 HInstruction* instruction)
5662 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005663
5664 Location GetSource() const { return source_; }
5665 Location GetDestination() const { return destination_; }
5666
5667 void SetSource(Location value) { source_ = value; }
5668 void SetDestination(Location value) { destination_ = value; }
5669
5670 // The parallel move resolver marks moves as "in-progress" by clearing the
5671 // destination (but not the source).
5672 Location MarkPending() {
5673 DCHECK(!IsPending());
5674 Location dest = destination_;
5675 destination_ = Location::NoLocation();
5676 return dest;
5677 }
5678
5679 void ClearPending(Location dest) {
5680 DCHECK(IsPending());
5681 destination_ = dest;
5682 }
5683
5684 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00005685 DCHECK(source_.IsValid() || destination_.IsInvalid());
5686 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005687 }
5688
5689 // True if this blocks a move from the given location.
5690 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005691 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005692 }
5693
5694 // A move is redundant if it's been eliminated, if its source and
5695 // destination are the same, or if its destination is unneeded.
5696 bool IsRedundant() const {
5697 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5698 }
5699
5700 // We clear both operands to indicate move that's been eliminated.
5701 void Eliminate() {
5702 source_ = destination_ = Location::NoLocation();
5703 }
5704
5705 bool IsEliminated() const {
5706 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5707 return source_.IsInvalid();
5708 }
5709
Alexey Frunze4dda3372015-06-01 18:31:49 -07005710 Primitive::Type GetType() const { return type_; }
5711
Nicolas Geoffray90218252015-04-15 11:56:51 +01005712 bool Is64BitMove() const {
5713 return Primitive::Is64BitType(type_);
5714 }
5715
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005716 HInstruction* GetInstruction() const { return instruction_; }
5717
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005718 private:
5719 Location source_;
5720 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005721 // The type this move is for.
5722 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005723 // The instruction this move is assocatied with. Null when this move is
5724 // for moving an input in the expected locations of user (including a phi user).
5725 // This is only used in debug mode, to ensure we do not connect interval siblings
5726 // in the same parallel move.
5727 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005728};
5729
Roland Levillainc9285912015-12-18 10:38:42 +00005730std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
5731
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005732static constexpr size_t kDefaultNumberOfMoves = 4;
5733
5734class HParallelMove : public HTemplateInstruction<0> {
5735 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005736 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01005737 : HTemplateInstruction(SideEffects::None(), dex_pc),
5738 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
5739 moves_.reserve(kDefaultNumberOfMoves);
5740 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005741
Nicolas Geoffray90218252015-04-15 11:56:51 +01005742 void AddMove(Location source,
5743 Location destination,
5744 Primitive::Type type,
5745 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005746 DCHECK(source.IsValid());
5747 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005748 if (kIsDebugBuild) {
5749 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005750 for (const MoveOperands& move : moves_) {
5751 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005752 // Special case the situation where the move is for the spill slot
5753 // of the instruction.
5754 if ((GetPrevious() == instruction)
5755 || ((GetPrevious() == nullptr)
5756 && instruction->IsPhi()
5757 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005758 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005759 << "Doing parallel moves for the same instruction.";
5760 } else {
5761 DCHECK(false) << "Doing parallel moves for the same instruction.";
5762 }
5763 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00005764 }
5765 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005766 for (const MoveOperands& move : moves_) {
5767 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005768 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01005769 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01005770 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005771 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005772 }
Vladimir Marko225b6462015-09-28 12:17:40 +01005773 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005774 }
5775
Vladimir Marko225b6462015-09-28 12:17:40 +01005776 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005777 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005778 }
5779
Vladimir Marko225b6462015-09-28 12:17:40 +01005780 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005781
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01005782 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005783
5784 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01005785 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005786
5787 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
5788};
5789
Mark Mendell0616ae02015-04-17 12:49:27 -04005790} // namespace art
5791
Vladimir Markob4536b72015-11-24 13:45:23 +00005792#ifdef ART_ENABLE_CODEGEN_arm
5793#include "nodes_arm.h"
5794#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005795#ifdef ART_ENABLE_CODEGEN_arm64
5796#include "nodes_arm64.h"
5797#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04005798#ifdef ART_ENABLE_CODEGEN_x86
5799#include "nodes_x86.h"
5800#endif
5801
5802namespace art {
5803
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005804class HGraphVisitor : public ValueObject {
5805 public:
Dave Allison20dfc792014-06-16 20:44:29 -07005806 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
5807 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005808
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005809 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005810 virtual void VisitBasicBlock(HBasicBlock* block);
5811
Roland Levillain633021e2014-10-01 14:12:25 +01005812 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005813 void VisitInsertionOrder();
5814
Roland Levillain633021e2014-10-01 14:12:25 +01005815 // Visit the graph following dominator tree reverse post-order.
5816 void VisitReversePostOrder();
5817
Nicolas Geoffray787c3072014-03-17 10:20:19 +00005818 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005819
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005820 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005821#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005822 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
5823
5824 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5825
5826#undef DECLARE_VISIT_INSTRUCTION
5827
5828 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07005829 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00005830
5831 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
5832};
5833
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005834class HGraphDelegateVisitor : public HGraphVisitor {
5835 public:
5836 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
5837 virtual ~HGraphDelegateVisitor() {}
5838
5839 // Visit functions that delegate to to super class.
5840#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005841 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01005842
5843 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
5844
5845#undef DECLARE_VISIT_INSTRUCTION
5846
5847 private:
5848 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
5849};
5850
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005851class HInsertionOrderIterator : public ValueObject {
5852 public:
5853 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
5854
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005855 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005856 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005857 void Advance() { ++index_; }
5858
5859 private:
5860 const HGraph& graph_;
5861 size_t index_;
5862
5863 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
5864};
5865
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005866class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005867 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00005868 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
5869 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005870 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005871 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005872
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005873 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
5874 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005875 void Advance() { ++index_; }
5876
5877 private:
5878 const HGraph& graph_;
5879 size_t index_;
5880
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005881 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005882};
5883
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005884class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005885 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005886 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005887 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00005888 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005889 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00005890 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005891
5892 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005893 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005894 void Advance() { --index_; }
5895
5896 private:
5897 const HGraph& graph_;
5898 size_t index_;
5899
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01005900 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01005901};
5902
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005903class HLinearPostOrderIterator : public ValueObject {
5904 public:
5905 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005906 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005907
5908 bool Done() const { return index_ == 0; }
5909
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005910 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005911
5912 void Advance() {
5913 --index_;
5914 DCHECK_GE(index_, 0U);
5915 }
5916
5917 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005918 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005919 size_t index_;
5920
5921 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
5922};
5923
5924class HLinearOrderIterator : public ValueObject {
5925 public:
5926 explicit HLinearOrderIterator(const HGraph& graph)
5927 : order_(graph.GetLinearOrder()), index_(0) {}
5928
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005929 bool Done() const { return index_ == order_.size(); }
5930 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005931 void Advance() { ++index_; }
5932
5933 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005934 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01005935 size_t index_;
5936
5937 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
5938};
5939
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005940// Iterator over the blocks that art part of the loop. Includes blocks part
5941// of an inner loop. The order in which the blocks are iterated is on their
5942// block id.
5943class HBlocksInLoopIterator : public ValueObject {
5944 public:
5945 explicit HBlocksInLoopIterator(const HLoopInformation& info)
5946 : blocks_in_loop_(info.GetBlocks()),
5947 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
5948 index_(0) {
5949 if (!blocks_in_loop_.IsBitSet(index_)) {
5950 Advance();
5951 }
5952 }
5953
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005954 bool Done() const { return index_ == blocks_.size(); }
5955 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005956 void Advance() {
5957 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005958 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005959 if (blocks_in_loop_.IsBitSet(index_)) {
5960 break;
5961 }
5962 }
5963 }
5964
5965 private:
5966 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005967 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005968 size_t index_;
5969
5970 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
5971};
5972
Mingyao Yang3584bce2015-05-19 16:01:59 -07005973// Iterator over the blocks that art part of the loop. Includes blocks part
5974// of an inner loop. The order in which the blocks are iterated is reverse
5975// post order.
5976class HBlocksInLoopReversePostOrderIterator : public ValueObject {
5977 public:
5978 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
5979 : blocks_in_loop_(info.GetBlocks()),
5980 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
5981 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005982 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005983 Advance();
5984 }
5985 }
5986
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005987 bool Done() const { return index_ == blocks_.size(); }
5988 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07005989 void Advance() {
5990 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005991 for (size_t e = blocks_.size(); index_ < e; ++index_) {
5992 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07005993 break;
5994 }
5995 }
5996 }
5997
5998 private:
5999 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006000 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006001 size_t index_;
6002
6003 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6004};
6005
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006006inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006007 if (constant->IsIntConstant()) {
6008 return constant->AsIntConstant()->GetValue();
6009 } else if (constant->IsLongConstant()) {
6010 return constant->AsLongConstant()->GetValue();
6011 } else {
6012 DCHECK(constant->IsNullConstant());
6013 return 0;
6014 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006015}
6016
Vladimir Marko58155012015-08-19 12:49:41 +00006017inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6018 // For the purposes of the compiler, the dex files must actually be the same object
6019 // if we want to safely treat them as the same. This is especially important for JIT
6020 // as custom class loaders can open the same underlying file (or memory) multiple
6021 // times and provide different class resolution but no two class loaders should ever
6022 // use the same DexFile object - doing so is an unsupported hack that can lead to
6023 // all sorts of weird failures.
6024 return &lhs == &rhs;
6025}
6026
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006027#define INSTRUCTION_TYPE_CHECK(type, super) \
6028 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6029 inline const H##type* HInstruction::As##type() const { \
6030 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6031 } \
6032 inline H##type* HInstruction::As##type() { \
6033 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6034 }
6035
6036 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6037#undef INSTRUCTION_TYPE_CHECK
6038
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00006039class SwitchTable : public ValueObject {
6040 public:
6041 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
6042 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
6043 int32_t table_offset = instruction.VRegB_31t();
6044 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
6045 if (sparse) {
6046 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
6047 } else {
6048 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
6049 }
6050 num_entries_ = table[1];
6051 values_ = reinterpret_cast<const int32_t*>(&table[2]);
6052 }
6053
6054 uint16_t GetNumEntries() const {
6055 return num_entries_;
6056 }
6057
6058 void CheckIndex(size_t index) const {
6059 if (sparse_) {
6060 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6061 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
6062 } else {
6063 // In a packed table, we have the starting key and num_entries_ values.
6064 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
6065 }
6066 }
6067
6068 int32_t GetEntryAt(size_t index) const {
6069 CheckIndex(index);
6070 return values_[index];
6071 }
6072
6073 uint32_t GetDexPcForIndex(size_t index) const {
6074 CheckIndex(index);
6075 return dex_pc_ +
6076 (reinterpret_cast<const int16_t*>(values_ + index) -
6077 reinterpret_cast<const int16_t*>(&instruction_));
6078 }
6079
6080 // Index of the first value in the table.
6081 size_t GetFirstValueIndex() const {
6082 if (sparse_) {
6083 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6084 return num_entries_;
6085 } else {
6086 // In a packed table, we have the starting key and num_entries_ values.
6087 return 1;
6088 }
6089 }
6090
6091 private:
6092 const Instruction& instruction_;
6093 const uint32_t dex_pc_;
6094
6095 // Whether this is a sparse-switch table (or a packed-switch one).
6096 const bool sparse_;
6097
6098 // This can't be const as it needs to be computed off of the given instruction, and complicated
6099 // expressions in the initializer list seemed very ugly.
6100 uint16_t num_entries_;
6101
6102 const int32_t* values_;
6103
6104 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
6105};
6106
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006107// Create space in `blocks` for adding `number_of_new_blocks` entries
6108// starting at location `at`. Blocks after `at` are moved accordingly.
6109inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6110 size_t number_of_new_blocks,
6111 size_t after) {
6112 DCHECK_LT(after, blocks->size());
6113 size_t old_size = blocks->size();
6114 size_t new_size = old_size + number_of_new_blocks;
6115 blocks->resize(new_size);
6116 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6117}
6118
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006119} // namespace art
6120
6121#endif // ART_COMPILER_OPTIMIZING_NODES_H_