blob: 399afabea6f13b8433899e27857b3c021422446c [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_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002071 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002072 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002073
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002074 size_t GetLifetimePosition() const { return lifetime_position_; }
2075 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2076 LiveInterval* GetLiveInterval() const { return live_interval_; }
2077 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2078 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2079
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002080 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2081
2082 // Returns whether the code generation of the instruction will require to have access
2083 // to the current method. Such instructions are:
2084 // (1): Instructions that require an environment, as calling the runtime requires
2085 // to walk the stack and have the current method stored at a specific stack address.
2086 // (2): Object literals like classes and strings, that are loaded from the dex cache
2087 // fields of the current method.
2088 bool NeedsCurrentMethod() const {
2089 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2090 }
2091
Vladimir Markodc151b22015-10-15 18:02:30 +01002092 // Returns whether the code generation of the instruction will require to have access
2093 // to the dex cache of the current method's declaring class via the current method.
2094 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002095
Mark Mendellc4701932015-04-10 13:18:51 -04002096 // Does this instruction have any use in an environment before
2097 // control flow hits 'other'?
2098 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2099
2100 // Remove all references to environment uses of this instruction.
2101 // The caller must ensure that this is safe to do.
2102 void RemoveEnvironmentUsers();
2103
David Brazdilb3e773e2016-01-26 11:28:37 +00002104 bool IsEmittedAtUseSite() const { return emitted_at_use_site_; }
2105 void MarkEmittedAtUseSite() { emitted_at_use_site_ = true; }
2106
David Brazdil1abb4192015-02-17 18:33:36 +00002107 protected:
2108 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2109 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2110
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 {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002402 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002403 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 {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002435 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002436 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
Roland Levillain31dd3d62016-02-16 12:21:02 +00002457class HFloatConstant : public HConstant {
2458 public:
2459 float GetValue() const { return value_; }
2460
2461 uint64_t GetValueAsUint64() const OVERRIDE {
2462 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2463 }
2464
2465 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2466 DCHECK(other->IsFloatConstant()) << other->DebugName();
2467 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2468 }
2469
2470 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2471
2472 bool IsMinusOne() const OVERRIDE {
2473 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2474 }
2475 bool IsZero() const OVERRIDE {
2476 return value_ == 0.0f;
2477 }
2478 bool IsOne() const OVERRIDE {
2479 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2480 }
2481 bool IsNaN() const {
2482 return std::isnan(value_);
2483 }
2484
2485 DECLARE_INSTRUCTION(FloatConstant);
2486
2487 private:
2488 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2489 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2490 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2491 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2492
2493 const float value_;
2494
2495 // Only the SsaBuilder and HGraph can create floating-point constants.
2496 friend class SsaBuilder;
2497 friend class HGraph;
2498 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2499};
2500
2501class HDoubleConstant : public HConstant {
2502 public:
2503 double GetValue() const { return value_; }
2504
2505 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2506
2507 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2508 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2509 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2510 }
2511
2512 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2513
2514 bool IsMinusOne() const OVERRIDE {
2515 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2516 }
2517 bool IsZero() const OVERRIDE {
2518 return value_ == 0.0;
2519 }
2520 bool IsOne() const OVERRIDE {
2521 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2522 }
2523 bool IsNaN() const {
2524 return std::isnan(value_);
2525 }
2526
2527 DECLARE_INSTRUCTION(DoubleConstant);
2528
2529 private:
2530 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2531 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2532 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2533 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2534
2535 const double value_;
2536
2537 // Only the SsaBuilder and HGraph can create floating-point constants.
2538 friend class SsaBuilder;
2539 friend class HGraph;
2540 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2541};
2542
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002543// Conditional branch. A block ending with an HIf instruction must have
2544// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002545class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002546 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002547 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2548 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002549 SetRawInputAt(0, input);
2550 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002551
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002552 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002553
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002554 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002555 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002556 }
2557
2558 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002559 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002560 }
2561
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002562 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002563
2564 private:
2565 DISALLOW_COPY_AND_ASSIGN(HIf);
2566};
2567
David Brazdilfc6a86a2015-06-26 10:33:45 +00002568
2569// Abstract instruction which marks the beginning and/or end of a try block and
2570// links it to the respective exception handlers. Behaves the same as a Goto in
2571// non-exceptional control flow.
2572// Normal-flow successor is stored at index zero, exception handlers under
2573// higher indices in no particular order.
2574class HTryBoundary : public HTemplateInstruction<0> {
2575 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002576 enum BoundaryKind {
2577 kEntry,
2578 kExit,
2579 };
2580
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002581 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2582 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002583
2584 bool IsControlFlow() const OVERRIDE { return true; }
2585
2586 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002587 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002588
David Brazdild26a4112015-11-10 11:07:31 +00002589 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2590 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2591 }
2592
David Brazdilfc6a86a2015-06-26 10:33:45 +00002593 // Returns whether `handler` is among its exception handlers (non-zero index
2594 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002595 bool HasExceptionHandler(const HBasicBlock& handler) const {
2596 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002597 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002598 }
2599
2600 // If not present already, adds `handler` to its block's list of exception
2601 // handlers.
2602 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002603 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002604 GetBlock()->AddSuccessor(handler);
2605 }
2606 }
2607
David Brazdil56e1acc2015-06-30 15:41:36 +01002608 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002609
David Brazdilffee3d32015-07-06 11:48:53 +01002610 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2611
David Brazdilfc6a86a2015-06-26 10:33:45 +00002612 DECLARE_INSTRUCTION(TryBoundary);
2613
2614 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002615 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002616
2617 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2618};
2619
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002620// Deoptimize to interpreter, upon checking a condition.
2621class HDeoptimize : public HTemplateInstruction<1> {
2622 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002623 // We set CanTriggerGC to prevent any intermediate address to be live
2624 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002625 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002626 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002627 SetRawInputAt(0, cond);
2628 }
2629
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002630 bool CanBeMoved() const OVERRIDE { return true; }
2631 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2632 return true;
2633 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002634 bool NeedsEnvironment() const OVERRIDE { return true; }
2635 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002636
2637 DECLARE_INSTRUCTION(Deoptimize);
2638
2639 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002640 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2641};
2642
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002643// Represents the ArtMethod that was passed as a first argument to
2644// the method. It is used by instructions that depend on it, like
2645// instructions that work with the dex cache.
2646class HCurrentMethod : public HExpression<0> {
2647 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002648 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2649 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002650
2651 DECLARE_INSTRUCTION(CurrentMethod);
2652
2653 private:
2654 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2655};
2656
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002657// Fetches an ArtMethod from the virtual table or the interface method table
2658// of a class.
2659class HClassTableGet : public HExpression<1> {
2660 public:
2661 enum TableKind {
2662 kVTable,
2663 kIMTable,
2664 };
2665 HClassTableGet(HInstruction* cls,
2666 Primitive::Type type,
2667 TableKind kind,
2668 size_t index,
2669 uint32_t dex_pc)
2670 : HExpression(type, SideEffects::None(), dex_pc),
2671 index_(index),
2672 table_kind_(kind) {
2673 SetRawInputAt(0, cls);
2674 }
2675
2676 bool CanBeMoved() const OVERRIDE { return true; }
2677 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2678 return other->AsClassTableGet()->GetIndex() == index_ &&
2679 other->AsClassTableGet()->GetTableKind() == table_kind_;
2680 }
2681
2682 TableKind GetTableKind() const { return table_kind_; }
2683 size_t GetIndex() const { return index_; }
2684
2685 DECLARE_INSTRUCTION(ClassTableGet);
2686
2687 private:
2688 // The index of the ArtMethod in the table.
2689 const size_t index_;
2690 const TableKind table_kind_;
2691
2692 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2693};
2694
Mark Mendellfe57faa2015-09-18 09:26:15 -04002695// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2696// have one successor for each entry in the switch table, and the final successor
2697// will be the block containing the next Dex opcode.
2698class HPackedSwitch : public HTemplateInstruction<1> {
2699 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002700 HPackedSwitch(int32_t start_value,
2701 uint32_t num_entries,
2702 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002703 uint32_t dex_pc = kNoDexPc)
2704 : HTemplateInstruction(SideEffects::None(), dex_pc),
2705 start_value_(start_value),
2706 num_entries_(num_entries) {
2707 SetRawInputAt(0, input);
2708 }
2709
2710 bool IsControlFlow() const OVERRIDE { return true; }
2711
2712 int32_t GetStartValue() const { return start_value_; }
2713
Vladimir Marko211c2112015-09-24 16:52:33 +01002714 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002715
2716 HBasicBlock* GetDefaultBlock() const {
2717 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002718 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002719 }
2720 DECLARE_INSTRUCTION(PackedSwitch);
2721
2722 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002723 const int32_t start_value_;
2724 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002725
2726 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2727};
2728
Roland Levillain88cb1752014-10-20 16:36:47 +01002729class HUnaryOperation : public HExpression<1> {
2730 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002731 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2732 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002733 SetRawInputAt(0, input);
2734 }
2735
2736 HInstruction* GetInput() const { return InputAt(0); }
2737 Primitive::Type GetResultType() const { return GetType(); }
2738
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002739 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002740 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002741 return true;
2742 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002743
Roland Levillain31dd3d62016-02-16 12:21:02 +00002744 // Try to statically evaluate `this` and return a HConstant
2745 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002746 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002747 HConstant* TryStaticEvaluation() const;
2748
2749 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002750 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2751 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002752 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2753 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002754
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002755 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002756
2757 private:
2758 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2759};
2760
Dave Allison20dfc792014-06-16 20:44:29 -07002761class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002762 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002763 HBinaryOperation(Primitive::Type result_type,
2764 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002765 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002766 SideEffects side_effects = SideEffects::None(),
2767 uint32_t dex_pc = kNoDexPc)
2768 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002769 SetRawInputAt(0, left);
2770 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002771 }
2772
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002773 HInstruction* GetLeft() const { return InputAt(0); }
2774 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002775 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002776
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002777 virtual bool IsCommutative() const { return false; }
2778
2779 // Put constant on the right.
2780 // Returns whether order is changed.
2781 bool OrderInputsWithConstantOnTheRight() {
2782 HInstruction* left = InputAt(0);
2783 HInstruction* right = InputAt(1);
2784 if (left->IsConstant() && !right->IsConstant()) {
2785 ReplaceInput(right, 0);
2786 ReplaceInput(left, 1);
2787 return true;
2788 }
2789 return false;
2790 }
2791
2792 // Order inputs by instruction id, but favor constant on the right side.
2793 // This helps GVN for commutative ops.
2794 void OrderInputs() {
2795 DCHECK(IsCommutative());
2796 HInstruction* left = InputAt(0);
2797 HInstruction* right = InputAt(1);
2798 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2799 return;
2800 }
2801 if (OrderInputsWithConstantOnTheRight()) {
2802 return;
2803 }
2804 // Order according to instruction id.
2805 if (left->GetId() > right->GetId()) {
2806 ReplaceInput(right, 0);
2807 ReplaceInput(left, 1);
2808 }
2809 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002810
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002811 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002812 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002813 return true;
2814 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002815
Roland Levillain31dd3d62016-02-16 12:21:02 +00002816 // Try to statically evaluate `this` and return a HConstant
2817 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002818 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002819 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002820
2821 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002822 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2823 HNullConstant* y ATTRIBUTE_UNUSED) const {
2824 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2825 return nullptr;
2826 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002827 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2828 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2829 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2830 HLongConstant* y ATTRIBUTE_UNUSED) const {
2831 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2832 return nullptr;
2833 }
2834 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2835 HIntConstant* y ATTRIBUTE_UNUSED) const {
2836 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2837 return nullptr;
2838 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002839 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2840 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002841
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002842 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002843 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002844 HConstant* GetConstantRight() const;
2845
2846 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002847 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002848 HInstruction* GetLeastConstantLeft() const;
2849
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002850 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002851
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002852 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002853 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2854};
2855
Mark Mendellc4701932015-04-10 13:18:51 -04002856// The comparison bias applies for floating point operations and indicates how NaN
2857// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002858enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002859 kNoBias, // bias is not applicable (i.e. for long operation)
2860 kGtBias, // return 1 for NaN comparisons
2861 kLtBias, // return -1 for NaN comparisons
2862};
2863
Roland Levillain31dd3d62016-02-16 12:21:02 +00002864std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
2865
Dave Allison20dfc792014-06-16 20:44:29 -07002866class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002867 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002868 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2869 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002870 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002871
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002872 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002873 // `instruction`, and disregard moves in between.
2874 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002875
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002876 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002877
2878 virtual IfCondition GetCondition() const = 0;
2879
Mark Mendellc4701932015-04-10 13:18:51 -04002880 virtual IfCondition GetOppositeCondition() const = 0;
2881
Roland Levillain4fa13f62015-07-06 18:11:54 +01002882 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002883 ComparisonBias GetBias() const { return bias_; }
Mark Mendellc4701932015-04-10 13:18:51 -04002884 void SetBias(ComparisonBias bias) { bias_ = bias; }
2885
2886 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2887 return bias_ == other->AsCondition()->bias_;
2888 }
2889
Roland Levillain4fa13f62015-07-06 18:11:54 +01002890 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002891 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002892 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002893 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002894 }
2895
2896 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002897 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002898 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002899 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002900 }
2901
Roland Levillain31dd3d62016-02-16 12:21:02 +00002902 protected:
2903 template <typename T>
2904 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
2905
2906 template <typename T>
2907 int32_t CompareFP(T x, T y) const {
2908 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
2909 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
2910 // Handle the bias.
2911 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
2912 }
2913
2914 // Return an integer constant containing the result of a condition evaluated at compile time.
2915 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
2916 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
2917 }
2918
Dave Allison20dfc792014-06-16 20:44:29 -07002919 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002920 // Needed if we merge a HCompare into a HCondition.
2921 ComparisonBias bias_;
2922
Dave Allison20dfc792014-06-16 20:44:29 -07002923 DISALLOW_COPY_AND_ASSIGN(HCondition);
2924};
2925
2926// Instruction to check if two inputs are equal to each other.
2927class HEqual : public HCondition {
2928 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002929 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2930 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002931
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002932 bool IsCommutative() const OVERRIDE { return true; }
2933
Vladimir Marko9e23df52015-11-10 17:14:35 +00002934 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2935 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002936 return MakeConstantCondition(true, GetDexPc());
2937 }
2938 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2939 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
2940 }
2941 // In the following Evaluate methods, a HCompare instruction has
2942 // been merged into this HEqual instruction; evaluate it as
2943 // `Compare(x, y) == 0`.
2944 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2945 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
2946 GetDexPc());
2947 }
2948 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
2949 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
2950 }
2951 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
2952 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00002953 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002954
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002955 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002956
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002957 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002958 return kCondEQ;
2959 }
2960
Mark Mendellc4701932015-04-10 13:18:51 -04002961 IfCondition GetOppositeCondition() const OVERRIDE {
2962 return kCondNE;
2963 }
2964
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002965 private:
Aart Bike9f37602015-10-09 11:15:55 -07002966 template <typename T> bool Compute(T x, T y) const { return x == y; }
2967
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002968 DISALLOW_COPY_AND_ASSIGN(HEqual);
2969};
2970
Dave Allison20dfc792014-06-16 20:44:29 -07002971class HNotEqual : public HCondition {
2972 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002973 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2974 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002975
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002976 bool IsCommutative() const OVERRIDE { return true; }
2977
Vladimir Marko9e23df52015-11-10 17:14:35 +00002978 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2979 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002980 return MakeConstantCondition(false, GetDexPc());
2981 }
2982 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2983 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
2984 }
2985 // In the following Evaluate methods, a HCompare instruction has
2986 // been merged into this HNotEqual instruction; evaluate it as
2987 // `Compare(x, y) != 0`.
2988 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2989 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
2990 }
2991 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
2992 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
2993 }
2994 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
2995 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00002996 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002997
Dave Allison20dfc792014-06-16 20:44:29 -07002998 DECLARE_INSTRUCTION(NotEqual);
2999
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003000 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003001 return kCondNE;
3002 }
3003
Mark Mendellc4701932015-04-10 13:18:51 -04003004 IfCondition GetOppositeCondition() const OVERRIDE {
3005 return kCondEQ;
3006 }
3007
Dave Allison20dfc792014-06-16 20:44:29 -07003008 private:
Aart Bike9f37602015-10-09 11:15:55 -07003009 template <typename T> bool Compute(T x, T y) const { return x != y; }
3010
Dave Allison20dfc792014-06-16 20:44:29 -07003011 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3012};
3013
3014class HLessThan : public HCondition {
3015 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003016 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3017 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003018
Roland Levillain9867bc72015-08-05 10:21:34 +01003019 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003020 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003021 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003022 // In the following Evaluate methods, a HCompare instruction has
3023 // been merged into this HLessThan instruction; evaluate it as
3024 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003025 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003026 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3027 }
3028 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3029 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3030 }
3031 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3032 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003033 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003034
Dave Allison20dfc792014-06-16 20:44:29 -07003035 DECLARE_INSTRUCTION(LessThan);
3036
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003037 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003038 return kCondLT;
3039 }
3040
Mark Mendellc4701932015-04-10 13:18:51 -04003041 IfCondition GetOppositeCondition() const OVERRIDE {
3042 return kCondGE;
3043 }
3044
Dave Allison20dfc792014-06-16 20:44:29 -07003045 private:
Aart Bike9f37602015-10-09 11:15:55 -07003046 template <typename T> bool Compute(T x, T y) const { return x < y; }
3047
Dave Allison20dfc792014-06-16 20:44:29 -07003048 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3049};
3050
3051class HLessThanOrEqual : public HCondition {
3052 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003053 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3054 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003055
Roland Levillain9867bc72015-08-05 10:21:34 +01003056 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003057 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003058 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003059 // In the following Evaluate methods, a HCompare instruction has
3060 // been merged into this HLessThanOrEqual instruction; evaluate it as
3061 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003062 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003063 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3064 }
3065 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3066 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3067 }
3068 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3069 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003070 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003071
Dave Allison20dfc792014-06-16 20:44:29 -07003072 DECLARE_INSTRUCTION(LessThanOrEqual);
3073
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003074 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003075 return kCondLE;
3076 }
3077
Mark Mendellc4701932015-04-10 13:18:51 -04003078 IfCondition GetOppositeCondition() const OVERRIDE {
3079 return kCondGT;
3080 }
3081
Dave Allison20dfc792014-06-16 20:44:29 -07003082 private:
Aart Bike9f37602015-10-09 11:15:55 -07003083 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3084
Dave Allison20dfc792014-06-16 20:44:29 -07003085 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3086};
3087
3088class HGreaterThan : public HCondition {
3089 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003090 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3091 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003092
Roland Levillain9867bc72015-08-05 10:21:34 +01003093 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003094 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003095 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003096 // In the following Evaluate methods, a HCompare instruction has
3097 // been merged into this HGreaterThan instruction; evaluate it as
3098 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003099 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003100 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3101 }
3102 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3103 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3104 }
3105 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3106 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003107 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003108
Dave Allison20dfc792014-06-16 20:44:29 -07003109 DECLARE_INSTRUCTION(GreaterThan);
3110
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003111 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003112 return kCondGT;
3113 }
3114
Mark Mendellc4701932015-04-10 13:18:51 -04003115 IfCondition GetOppositeCondition() const OVERRIDE {
3116 return kCondLE;
3117 }
3118
Dave Allison20dfc792014-06-16 20:44:29 -07003119 private:
Aart Bike9f37602015-10-09 11:15:55 -07003120 template <typename T> bool Compute(T x, T y) const { return x > y; }
3121
Dave Allison20dfc792014-06-16 20:44:29 -07003122 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3123};
3124
3125class HGreaterThanOrEqual : public HCondition {
3126 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003127 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3128 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003129
Roland Levillain9867bc72015-08-05 10:21:34 +01003130 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003131 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003132 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003133 // In the following Evaluate methods, a HCompare instruction has
3134 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3135 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003136 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003137 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3138 }
3139 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3140 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3141 }
3142 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3143 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003144 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003145
Dave Allison20dfc792014-06-16 20:44:29 -07003146 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3147
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003148 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003149 return kCondGE;
3150 }
3151
Mark Mendellc4701932015-04-10 13:18:51 -04003152 IfCondition GetOppositeCondition() const OVERRIDE {
3153 return kCondLT;
3154 }
3155
Dave Allison20dfc792014-06-16 20:44:29 -07003156 private:
Aart Bike9f37602015-10-09 11:15:55 -07003157 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3158
Dave Allison20dfc792014-06-16 20:44:29 -07003159 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3160};
3161
Aart Bike9f37602015-10-09 11:15:55 -07003162class HBelow : public HCondition {
3163 public:
3164 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3165 : HCondition(first, second, dex_pc) {}
3166
3167 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003168 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003169 }
3170 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003171 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3172 }
3173 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3174 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3175 LOG(FATAL) << DebugName() << " is not defined for float values";
3176 UNREACHABLE();
3177 }
3178 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3179 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3180 LOG(FATAL) << DebugName() << " is not defined for double values";
3181 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003182 }
3183
3184 DECLARE_INSTRUCTION(Below);
3185
3186 IfCondition GetCondition() const OVERRIDE {
3187 return kCondB;
3188 }
3189
3190 IfCondition GetOppositeCondition() const OVERRIDE {
3191 return kCondAE;
3192 }
3193
3194 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003195 template <typename T> bool Compute(T x, T y) const {
3196 return MakeUnsigned(x) < MakeUnsigned(y);
3197 }
Aart Bike9f37602015-10-09 11:15:55 -07003198
3199 DISALLOW_COPY_AND_ASSIGN(HBelow);
3200};
3201
3202class HBelowOrEqual : public HCondition {
3203 public:
3204 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3205 : HCondition(first, second, dex_pc) {}
3206
3207 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003208 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003209 }
3210 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003211 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3212 }
3213 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3214 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3215 LOG(FATAL) << DebugName() << " is not defined for float values";
3216 UNREACHABLE();
3217 }
3218 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3219 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3220 LOG(FATAL) << DebugName() << " is not defined for double values";
3221 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003222 }
3223
3224 DECLARE_INSTRUCTION(BelowOrEqual);
3225
3226 IfCondition GetCondition() const OVERRIDE {
3227 return kCondBE;
3228 }
3229
3230 IfCondition GetOppositeCondition() const OVERRIDE {
3231 return kCondA;
3232 }
3233
3234 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003235 template <typename T> bool Compute(T x, T y) const {
3236 return MakeUnsigned(x) <= MakeUnsigned(y);
3237 }
Aart Bike9f37602015-10-09 11:15:55 -07003238
3239 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3240};
3241
3242class HAbove : public HCondition {
3243 public:
3244 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3245 : HCondition(first, second, dex_pc) {}
3246
3247 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003248 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003249 }
3250 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003251 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3252 }
3253 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3254 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3255 LOG(FATAL) << DebugName() << " is not defined for float values";
3256 UNREACHABLE();
3257 }
3258 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3259 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3260 LOG(FATAL) << DebugName() << " is not defined for double values";
3261 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003262 }
3263
3264 DECLARE_INSTRUCTION(Above);
3265
3266 IfCondition GetCondition() const OVERRIDE {
3267 return kCondA;
3268 }
3269
3270 IfCondition GetOppositeCondition() const OVERRIDE {
3271 return kCondBE;
3272 }
3273
3274 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003275 template <typename T> bool Compute(T x, T y) const {
3276 return MakeUnsigned(x) > MakeUnsigned(y);
3277 }
Aart Bike9f37602015-10-09 11:15:55 -07003278
3279 DISALLOW_COPY_AND_ASSIGN(HAbove);
3280};
3281
3282class HAboveOrEqual : public HCondition {
3283 public:
3284 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3285 : HCondition(first, second, dex_pc) {}
3286
3287 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003288 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003289 }
3290 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003291 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3292 }
3293 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3294 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3295 LOG(FATAL) << DebugName() << " is not defined for float values";
3296 UNREACHABLE();
3297 }
3298 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3299 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3300 LOG(FATAL) << DebugName() << " is not defined for double values";
3301 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003302 }
3303
3304 DECLARE_INSTRUCTION(AboveOrEqual);
3305
3306 IfCondition GetCondition() const OVERRIDE {
3307 return kCondAE;
3308 }
3309
3310 IfCondition GetOppositeCondition() const OVERRIDE {
3311 return kCondB;
3312 }
3313
3314 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003315 template <typename T> bool Compute(T x, T y) const {
3316 return MakeUnsigned(x) >= MakeUnsigned(y);
3317 }
Aart Bike9f37602015-10-09 11:15:55 -07003318
3319 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3320};
Dave Allison20dfc792014-06-16 20:44:29 -07003321
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003322// Instruction to check how two inputs compare to each other.
3323// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3324class HCompare : public HBinaryOperation {
3325 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003326 HCompare(Primitive::Type type,
3327 HInstruction* first,
3328 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003329 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003330 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003331 : HBinaryOperation(Primitive::kPrimInt,
3332 first,
3333 second,
3334 SideEffectsForArchRuntimeCalls(type),
3335 dex_pc),
3336 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003337 DCHECK_EQ(type, first->GetType());
3338 DCHECK_EQ(type, second->GetType());
3339 }
3340
Roland Levillain9867bc72015-08-05 10:21:34 +01003341 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003342 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3343
3344 template <typename T>
3345 int32_t ComputeFP(T x, T y) const {
3346 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3347 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3348 // Handle the bias.
3349 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3350 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003351
Roland Levillain9867bc72015-08-05 10:21:34 +01003352 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003353 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3354 // reach this code path when processing a freshly built HIR
3355 // graph. However HCompare integer instructions can be synthesized
3356 // by the instruction simplifier to implement IntegerCompare and
3357 // IntegerSignum intrinsics, so we have to handle this case.
3358 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003359 }
3360 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003361 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3362 }
3363 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3364 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3365 }
3366 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3367 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003368 }
3369
Calin Juravleddb7df22014-11-25 20:56:51 +00003370 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3371 return bias_ == other->AsCompare()->bias_;
3372 }
3373
Mark Mendellc4701932015-04-10 13:18:51 -04003374 ComparisonBias GetBias() const { return bias_; }
3375
Roland Levillain31dd3d62016-02-16 12:21:02 +00003376 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
3377 // Only meaninfgul for floating-point comparisons.
3378 bool IsGtBias() const {
3379 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3380 return bias_ == ComparisonBias::kGtBias;
3381 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003382
3383 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3384 // MIPS64 uses a runtime call for FP comparisons.
3385 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3386 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003387
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003388 DECLARE_INSTRUCTION(Compare);
3389
Roland Levillain31dd3d62016-02-16 12:21:02 +00003390 protected:
3391 // Return an integer constant containing the result of a comparison evaluated at compile time.
3392 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3393 DCHECK(value == -1 || value == 0 || value == 1) << value;
3394 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3395 }
3396
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003397 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003398 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003399
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003400 DISALLOW_COPY_AND_ASSIGN(HCompare);
3401};
3402
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003403// A local in the graph. Corresponds to a Dex register.
3404class HLocal : public HTemplateInstruction<0> {
3405 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003406 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003407 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003408
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003409 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003410
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003411 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003412
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003413 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003414 // The Dex register number.
3415 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003416
3417 DISALLOW_COPY_AND_ASSIGN(HLocal);
3418};
3419
3420// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003421class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003422 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003423 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3424 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003425 SetRawInputAt(0, local);
3426 }
3427
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003428 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3429
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003430 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003431
3432 private:
3433 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3434};
3435
3436// Store a value in a given local. This instruction has two inputs: the value
3437// and the local.
3438class HStoreLocal : public HTemplateInstruction<2> {
3439 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003440 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3441 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003442 SetRawInputAt(0, local);
3443 SetRawInputAt(1, value);
3444 }
3445
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003446 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3447
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003448 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003449
3450 private:
3451 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3452};
3453
David Brazdil6de19382016-01-08 17:37:10 +00003454class HNewInstance : public HExpression<2> {
3455 public:
3456 HNewInstance(HInstruction* cls,
3457 HCurrentMethod* current_method,
3458 uint32_t dex_pc,
3459 uint16_t type_index,
3460 const DexFile& dex_file,
3461 bool can_throw,
3462 bool finalizable,
3463 QuickEntrypointEnum entrypoint)
3464 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3465 type_index_(type_index),
3466 dex_file_(dex_file),
3467 can_throw_(can_throw),
3468 finalizable_(finalizable),
3469 entrypoint_(entrypoint) {
3470 SetRawInputAt(0, cls);
3471 SetRawInputAt(1, current_method);
3472 }
3473
3474 uint16_t GetTypeIndex() const { return type_index_; }
3475 const DexFile& GetDexFile() const { return dex_file_; }
3476
3477 // Calls runtime so needs an environment.
3478 bool NeedsEnvironment() const OVERRIDE { return true; }
3479
3480 // It may throw when called on type that's not instantiable/accessible.
3481 // It can throw OOME.
3482 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3483 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3484
3485 bool IsFinalizable() const { return finalizable_; }
3486
3487 bool CanBeNull() const OVERRIDE { return false; }
3488
3489 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3490
3491 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3492 entrypoint_ = entrypoint;
3493 }
3494
3495 bool IsStringAlloc() const;
3496
3497 DECLARE_INSTRUCTION(NewInstance);
3498
3499 private:
3500 const uint16_t type_index_;
3501 const DexFile& dex_file_;
3502 const bool can_throw_;
3503 const bool finalizable_;
3504 QuickEntrypointEnum entrypoint_;
3505
3506 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3507};
3508
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003509enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003510#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3511 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003512#include "intrinsics_list.h"
3513 kNone,
3514 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3515#undef INTRINSICS_LIST
3516#undef OPTIMIZING_INTRINSICS
3517};
3518std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3519
Agi Csaki05f20562015-08-19 14:58:14 -07003520enum IntrinsicNeedsEnvironmentOrCache {
3521 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3522 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003523};
3524
Aart Bik5d75afe2015-12-14 11:57:01 -08003525enum IntrinsicSideEffects {
3526 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3527 kReadSideEffects, // Intrinsic may read heap memory.
3528 kWriteSideEffects, // Intrinsic may write heap memory.
3529 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3530};
3531
3532enum IntrinsicExceptions {
3533 kNoThrow, // Intrinsic does not throw any exceptions.
3534 kCanThrow // Intrinsic may throw exceptions.
3535};
3536
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003537class HInvoke : public HInstruction {
3538 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003539 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003540
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003541 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003542
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003543 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003544 SetRawInputAt(index, argument);
3545 }
3546
Roland Levillain3e3d7332015-04-28 11:00:54 +01003547 // Return the number of arguments. This number can be lower than
3548 // the number of inputs returned by InputCount(), as some invoke
3549 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3550 // inputs at the end of their list of inputs.
3551 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3552
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003553 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003554
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003555 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003556 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003557
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003558 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3559
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003560 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003561 return intrinsic_;
3562 }
3563
Aart Bik5d75afe2015-12-14 11:57:01 -08003564 void SetIntrinsic(Intrinsics intrinsic,
3565 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3566 IntrinsicSideEffects side_effects,
3567 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003568
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003569 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003570 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003571 }
3572
Aart Bik5d75afe2015-12-14 11:57:01 -08003573 bool CanThrow() const OVERRIDE { return can_throw_; }
3574
3575 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3576
3577 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3578 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3579 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003580
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003581 uint32_t* GetIntrinsicOptimizations() {
3582 return &intrinsic_optimizations_;
3583 }
3584
3585 const uint32_t* GetIntrinsicOptimizations() const {
3586 return &intrinsic_optimizations_;
3587 }
3588
3589 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3590
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003591 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003592
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003593 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003594 HInvoke(ArenaAllocator* arena,
3595 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003596 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003597 Primitive::Type return_type,
3598 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003599 uint32_t dex_method_index,
3600 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003601 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003602 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003603 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003604 inputs_(number_of_arguments + number_of_other_inputs,
3605 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003606 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003607 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003608 original_invoke_type_(original_invoke_type),
Aart Bik5d75afe2015-12-14 11:57:01 -08003609 can_throw_(true),
agicsaki57b81ec2015-08-11 17:39:37 -07003610 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003611 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003612 }
3613
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003614 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003615 return inputs_[index];
3616 }
3617
David Brazdil1abb4192015-02-17 18:33:36 +00003618 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003619 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003620 }
3621
Aart Bik5d75afe2015-12-14 11:57:01 -08003622 void SetCanThrow(bool can_throw) { can_throw_ = can_throw; }
3623
Roland Levillain3e3d7332015-04-28 11:00:54 +01003624 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003625 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003626 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003627 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003628 const InvokeType original_invoke_type_;
Aart Bik5d75afe2015-12-14 11:57:01 -08003629 bool can_throw_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003630 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003631
3632 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3633 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003634
3635 private:
3636 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3637};
3638
Calin Juravle175dc732015-08-25 15:42:32 +01003639class HInvokeUnresolved : public HInvoke {
3640 public:
3641 HInvokeUnresolved(ArenaAllocator* arena,
3642 uint32_t number_of_arguments,
3643 Primitive::Type return_type,
3644 uint32_t dex_pc,
3645 uint32_t dex_method_index,
3646 InvokeType invoke_type)
3647 : HInvoke(arena,
3648 number_of_arguments,
3649 0u /* number_of_other_inputs */,
3650 return_type,
3651 dex_pc,
3652 dex_method_index,
3653 invoke_type) {
3654 }
3655
3656 DECLARE_INSTRUCTION(InvokeUnresolved);
3657
3658 private:
3659 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3660};
3661
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003662class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003663 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003664 // Requirements of this method call regarding the class
3665 // initialization (clinit) check of its declaring class.
3666 enum class ClinitCheckRequirement {
3667 kNone, // Class already initialized.
3668 kExplicit, // Static call having explicit clinit check as last input.
3669 kImplicit, // Static call implicitly requiring a clinit check.
3670 };
3671
Vladimir Marko58155012015-08-19 12:49:41 +00003672 // Determines how to load the target ArtMethod*.
3673 enum class MethodLoadKind {
3674 // Use a String init ArtMethod* loaded from Thread entrypoints.
3675 kStringInit,
3676
3677 // Use the method's own ArtMethod* loaded by the register allocator.
3678 kRecursive,
3679
3680 // Use ArtMethod* at a known address, embed the direct address in the code.
3681 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3682 kDirectAddress,
3683
3684 // Use ArtMethod* at an address that will be known at link time, embed the direct
3685 // address in the code. If the image is relocatable, emit .patch_oat entry.
3686 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3687 // the image relocatable or not.
3688 kDirectAddressWithFixup,
3689
3690 // Load from resoved methods array in the dex cache using a PC-relative load.
3691 // Used when we need to use the dex cache, for example for invoke-static that
3692 // may cause class initialization (the entry may point to a resolution method),
3693 // and we know that we can access the dex cache arrays using a PC-relative load.
3694 kDexCachePcRelative,
3695
3696 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3697 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3698 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3699 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3700 kDexCacheViaMethod,
3701 };
3702
3703 // Determines the location of the code pointer.
3704 enum class CodePtrLocation {
3705 // Recursive call, use local PC-relative call instruction.
3706 kCallSelf,
3707
3708 // Use PC-relative call instruction patched at link time.
3709 // Used for calls within an oat file, boot->boot or app->app.
3710 kCallPCRelative,
3711
3712 // Call to a known target address, embed the direct address in code.
3713 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3714 kCallDirect,
3715
3716 // Call to a target address that will be known at link time, embed the direct
3717 // address in code. If the image is relocatable, emit .patch_oat entry.
3718 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3719 // the image relocatable or not.
3720 kCallDirectWithFixup,
3721
3722 // Use code pointer from the ArtMethod*.
3723 // Used when we don't know the target code. This is also the last-resort-kind used when
3724 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3725 kCallArtMethod,
3726 };
3727
3728 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003729 MethodLoadKind method_load_kind;
3730 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003731 // The method load data holds
3732 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3733 // Note that there are multiple string init methods, each having its own offset.
3734 // - the method address for kDirectAddress
3735 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003736 uint64_t method_load_data;
3737 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003738 };
3739
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003740 HInvokeStaticOrDirect(ArenaAllocator* arena,
3741 uint32_t number_of_arguments,
3742 Primitive::Type return_type,
3743 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003744 uint32_t method_index,
3745 MethodReference target_method,
3746 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003747 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003748 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003749 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003750 : HInvoke(arena,
3751 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003752 // There is potentially one extra argument for the HCurrentMethod node, and
3753 // potentially one other if the clinit check is explicit, and potentially
3754 // one other if the method is a string factory.
3755 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3756 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3757 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003758 return_type,
3759 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003760 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003761 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003762 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003763 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003764 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003765 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003766
Vladimir Markodc151b22015-10-15 18:02:30 +01003767 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003768 bool had_current_method_input = HasCurrentMethodInput();
3769 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3770
3771 // Using the current method is the default and once we find a better
3772 // method load kind, we should not go back to using the current method.
3773 DCHECK(had_current_method_input || !needs_current_method_input);
3774
3775 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003776 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3777 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003778 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003779 dispatch_info_ = dispatch_info;
3780 }
3781
Vladimir Markoc53c0792015-11-19 15:48:33 +00003782 void AddSpecialInput(HInstruction* input) {
3783 // We allow only one special input.
3784 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3785 DCHECK(InputCount() == GetSpecialInputIndex() ||
3786 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3787 InsertInputAt(GetSpecialInputIndex(), input);
3788 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003789
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003790 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003791 // We access the method via the dex cache so we can't do an implicit null check.
3792 // TODO: for intrinsics we can generate implicit null checks.
3793 return false;
3794 }
3795
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003796 bool CanBeNull() const OVERRIDE {
3797 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3798 }
3799
Vladimir Markoc53c0792015-11-19 15:48:33 +00003800 // Get the index of the special input, if any.
3801 //
David Brazdil6de19382016-01-08 17:37:10 +00003802 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3803 // method pointer; otherwise there may be one platform-specific special input,
3804 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003805 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003806 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003807
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003808 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3809 void SetOptimizedInvokeType(InvokeType invoke_type) {
3810 optimized_invoke_type_ = invoke_type;
3811 }
3812
Vladimir Marko58155012015-08-19 12:49:41 +00003813 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3814 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3815 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003816 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003817 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003818 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003819 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003820 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3821 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003822 bool HasCurrentMethodInput() const {
3823 // This function can be called only after the invoke has been fully initialized by the builder.
3824 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003825 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003826 return true;
3827 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003828 DCHECK(InputCount() == GetSpecialInputIndex() ||
3829 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003830 return false;
3831 }
3832 }
Vladimir Marko58155012015-08-19 12:49:41 +00003833 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3834 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003835 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003836
3837 int32_t GetStringInitOffset() const {
3838 DCHECK(IsStringInit());
3839 return dispatch_info_.method_load_data;
3840 }
3841
3842 uint64_t GetMethodAddress() const {
3843 DCHECK(HasMethodAddress());
3844 return dispatch_info_.method_load_data;
3845 }
3846
3847 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003848 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003849 return dispatch_info_.method_load_data;
3850 }
3851
3852 uint64_t GetDirectCodePtr() const {
3853 DCHECK(HasDirectCodePtr());
3854 return dispatch_info_.direct_code_ptr;
3855 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003856
Calin Juravle68ad6492015-08-18 17:08:12 +01003857 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3858
Roland Levillain4c0eb422015-04-24 16:43:49 +01003859 // Is this instruction a call to a static method?
3860 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003861 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003862 }
3863
Vladimir Markofbb184a2015-11-13 14:47:00 +00003864 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3865 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3866 // instruction; only relevant for static calls with explicit clinit check.
3867 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003868 DCHECK(IsStaticWithExplicitClinitCheck());
3869 size_t last_input_index = InputCount() - 1;
3870 HInstruction* last_input = InputAt(last_input_index);
3871 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003872 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003873 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003874 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003875 clinit_check_requirement_ = new_requirement;
3876 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003877 }
3878
David Brazdilbc9ab162016-01-20 14:50:19 +00003879 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00003880 DCHECK(IsStringInit());
3881 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00003882 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00003883 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003884 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00003885 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003886 }
3887
Roland Levillain4c0eb422015-04-24 16:43:49 +01003888 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003889 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003890 bool IsStaticWithExplicitClinitCheck() const {
3891 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3892 }
3893
3894 // Is this a call to a static method whose declaring class has an
3895 // implicit intialization check requirement?
3896 bool IsStaticWithImplicitClinitCheck() const {
3897 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3898 }
3899
Vladimir Markob554b5a2015-11-06 12:57:55 +00003900 // Does this method load kind need the current method as an input?
3901 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3902 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3903 }
3904
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003905 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003906
Roland Levillain4c0eb422015-04-24 16:43:49 +01003907 protected:
3908 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3909 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3910 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3911 HInstruction* input = input_record.GetInstruction();
3912 // `input` is the last input of a static invoke marked as having
3913 // an explicit clinit check. It must either be:
3914 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3915 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3916 DCHECK(input != nullptr);
3917 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3918 }
3919 return input_record;
3920 }
3921
Vladimir Markoc53c0792015-11-19 15:48:33 +00003922 void InsertInputAt(size_t index, HInstruction* input);
3923 void RemoveInputAt(size_t index);
3924
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003925 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003926 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003927 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003928 // The target method may refer to different dex file or method index than the original
3929 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3930 // in derived class to increase visibility.
3931 MethodReference target_method_;
3932 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003933
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003934 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003935};
Vladimir Markof64242a2015-12-01 14:58:23 +00003936std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003937std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003938
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003939class HInvokeVirtual : public HInvoke {
3940 public:
3941 HInvokeVirtual(ArenaAllocator* arena,
3942 uint32_t number_of_arguments,
3943 Primitive::Type return_type,
3944 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003945 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003946 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003947 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003948 vtable_index_(vtable_index) {}
3949
Calin Juravle641547a2015-04-21 22:08:51 +01003950 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003951 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003952 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003953 }
3954
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003955 uint32_t GetVTableIndex() const { return vtable_index_; }
3956
3957 DECLARE_INSTRUCTION(InvokeVirtual);
3958
3959 private:
3960 const uint32_t vtable_index_;
3961
3962 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3963};
3964
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003965class HInvokeInterface : public HInvoke {
3966 public:
3967 HInvokeInterface(ArenaAllocator* arena,
3968 uint32_t number_of_arguments,
3969 Primitive::Type return_type,
3970 uint32_t dex_pc,
3971 uint32_t dex_method_index,
3972 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003973 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003974 imt_index_(imt_index) {}
3975
Calin Juravle641547a2015-04-21 22:08:51 +01003976 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003977 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003978 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003979 }
3980
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003981 uint32_t GetImtIndex() const { return imt_index_; }
3982 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3983
3984 DECLARE_INSTRUCTION(InvokeInterface);
3985
3986 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003987 const uint32_t imt_index_;
3988
3989 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
3990};
3991
Roland Levillain88cb1752014-10-20 16:36:47 +01003992class HNeg : public HUnaryOperation {
3993 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003994 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3995 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01003996
Roland Levillain9867bc72015-08-05 10:21:34 +01003997 template <typename T> T Compute(T x) const { return -x; }
3998
3999 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004000 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004001 }
4002 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004003 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004004 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004005 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4006 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4007 }
4008 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4009 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4010 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004011
Roland Levillain88cb1752014-10-20 16:36:47 +01004012 DECLARE_INSTRUCTION(Neg);
4013
4014 private:
4015 DISALLOW_COPY_AND_ASSIGN(HNeg);
4016};
4017
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004018class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004019 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004020 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004021 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004022 uint32_t dex_pc,
4023 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004024 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004025 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004026 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004027 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004028 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004029 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004030 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004031 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004032 }
4033
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004034 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004035 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004036
4037 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004038 bool NeedsEnvironment() const OVERRIDE { return true; }
4039
Mingyao Yang0c365e62015-03-31 15:09:29 -07004040 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4041 bool CanThrow() const OVERRIDE { return true; }
4042
Calin Juravle10e244f2015-01-26 18:54:32 +00004043 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004044
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004045 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4046
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004047 DECLARE_INSTRUCTION(NewArray);
4048
4049 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004050 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004051 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004052 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004053
4054 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4055};
4056
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004057class HAdd : public HBinaryOperation {
4058 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004059 HAdd(Primitive::Type result_type,
4060 HInstruction* left,
4061 HInstruction* right,
4062 uint32_t dex_pc = kNoDexPc)
4063 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004064
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004065 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004066
Roland Levillain9867bc72015-08-05 10:21:34 +01004067 template <typename T> T Compute(T x, T y) const { return x + y; }
4068
4069 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004070 return GetBlock()->GetGraph()->GetIntConstant(
4071 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004072 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004073 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004074 return GetBlock()->GetGraph()->GetLongConstant(
4075 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004076 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004077 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4078 return GetBlock()->GetGraph()->GetFloatConstant(
4079 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4080 }
4081 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4082 return GetBlock()->GetGraph()->GetDoubleConstant(
4083 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4084 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004085
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004086 DECLARE_INSTRUCTION(Add);
4087
4088 private:
4089 DISALLOW_COPY_AND_ASSIGN(HAdd);
4090};
4091
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004092class HSub : public HBinaryOperation {
4093 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004094 HSub(Primitive::Type result_type,
4095 HInstruction* left,
4096 HInstruction* right,
4097 uint32_t dex_pc = kNoDexPc)
4098 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004099
Roland Levillain9867bc72015-08-05 10:21:34 +01004100 template <typename T> T Compute(T x, T y) const { return x - y; }
4101
4102 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004103 return GetBlock()->GetGraph()->GetIntConstant(
4104 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004105 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004106 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004107 return GetBlock()->GetGraph()->GetLongConstant(
4108 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004109 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004110 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4111 return GetBlock()->GetGraph()->GetFloatConstant(
4112 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4113 }
4114 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4115 return GetBlock()->GetGraph()->GetDoubleConstant(
4116 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4117 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004118
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004119 DECLARE_INSTRUCTION(Sub);
4120
4121 private:
4122 DISALLOW_COPY_AND_ASSIGN(HSub);
4123};
4124
Calin Juravle34bacdf2014-10-07 20:23:36 +01004125class HMul : public HBinaryOperation {
4126 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004127 HMul(Primitive::Type result_type,
4128 HInstruction* left,
4129 HInstruction* right,
4130 uint32_t dex_pc = kNoDexPc)
4131 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004132
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004133 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004134
Roland Levillain9867bc72015-08-05 10:21:34 +01004135 template <typename T> T Compute(T x, T y) const { return x * y; }
4136
4137 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004138 return GetBlock()->GetGraph()->GetIntConstant(
4139 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004140 }
4141 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004142 return GetBlock()->GetGraph()->GetLongConstant(
4143 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004144 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004145 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4146 return GetBlock()->GetGraph()->GetFloatConstant(
4147 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4148 }
4149 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4150 return GetBlock()->GetGraph()->GetDoubleConstant(
4151 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4152 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004153
4154 DECLARE_INSTRUCTION(Mul);
4155
4156 private:
4157 DISALLOW_COPY_AND_ASSIGN(HMul);
4158};
4159
Calin Juravle7c4954d2014-10-28 16:57:40 +00004160class HDiv : public HBinaryOperation {
4161 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004162 HDiv(Primitive::Type result_type,
4163 HInstruction* left,
4164 HInstruction* right,
4165 uint32_t dex_pc)
4166 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004167
Roland Levillain9867bc72015-08-05 10:21:34 +01004168 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004169 T ComputeIntegral(T x, T y) const {
4170 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004171 // Our graph structure ensures we never have 0 for `y` during
4172 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004173 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004174 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004175 return (y == -1) ? -x : x / y;
4176 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004177
Roland Levillain31dd3d62016-02-16 12:21:02 +00004178 template <typename T>
4179 T ComputeFP(T x, T y) const {
4180 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4181 return x / y;
4182 }
4183
Roland Levillain9867bc72015-08-05 10:21:34 +01004184 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004185 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004186 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004187 }
4188 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004189 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004190 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4191 }
4192 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4193 return GetBlock()->GetGraph()->GetFloatConstant(
4194 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4195 }
4196 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4197 return GetBlock()->GetGraph()->GetDoubleConstant(
4198 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004199 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004200
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004201 static SideEffects SideEffectsForArchRuntimeCalls() {
4202 // The generated code can use a runtime call.
4203 return SideEffects::CanTriggerGC();
4204 }
4205
Calin Juravle7c4954d2014-10-28 16:57:40 +00004206 DECLARE_INSTRUCTION(Div);
4207
4208 private:
4209 DISALLOW_COPY_AND_ASSIGN(HDiv);
4210};
4211
Calin Juravlebacfec32014-11-14 15:54:36 +00004212class HRem : public HBinaryOperation {
4213 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004214 HRem(Primitive::Type result_type,
4215 HInstruction* left,
4216 HInstruction* right,
4217 uint32_t dex_pc)
4218 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004219
Roland Levillain9867bc72015-08-05 10:21:34 +01004220 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004221 T ComputeIntegral(T x, T y) const {
4222 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004223 // Our graph structure ensures we never have 0 for `y` during
4224 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004225 DCHECK_NE(y, 0);
4226 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4227 return (y == -1) ? 0 : x % y;
4228 }
4229
Roland Levillain31dd3d62016-02-16 12:21:02 +00004230 template <typename T>
4231 T ComputeFP(T x, T y) const {
4232 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4233 return std::fmod(x, y);
4234 }
4235
Roland Levillain9867bc72015-08-05 10:21:34 +01004236 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004237 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004238 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004239 }
4240 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004241 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004242 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004243 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004244 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4245 return GetBlock()->GetGraph()->GetFloatConstant(
4246 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4247 }
4248 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4249 return GetBlock()->GetGraph()->GetDoubleConstant(
4250 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4251 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004252
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004253 static SideEffects SideEffectsForArchRuntimeCalls() {
4254 return SideEffects::CanTriggerGC();
4255 }
4256
Calin Juravlebacfec32014-11-14 15:54:36 +00004257 DECLARE_INSTRUCTION(Rem);
4258
4259 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004260 DISALLOW_COPY_AND_ASSIGN(HRem);
4261};
4262
Calin Juravled0d48522014-11-04 16:40:20 +00004263class HDivZeroCheck : public HExpression<1> {
4264 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004265 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4266 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004267 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004268 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004269 SetRawInputAt(0, value);
4270 }
4271
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004272 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4273
Calin Juravled0d48522014-11-04 16:40:20 +00004274 bool CanBeMoved() const OVERRIDE { return true; }
4275
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004276 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004277 return true;
4278 }
4279
4280 bool NeedsEnvironment() const OVERRIDE { return true; }
4281 bool CanThrow() const OVERRIDE { return true; }
4282
Calin Juravled0d48522014-11-04 16:40:20 +00004283 DECLARE_INSTRUCTION(DivZeroCheck);
4284
4285 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004286 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4287};
4288
Calin Juravle9aec02f2014-11-18 23:06:35 +00004289class HShl : public HBinaryOperation {
4290 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004291 HShl(Primitive::Type result_type,
4292 HInstruction* left,
4293 HInstruction* right,
4294 uint32_t dex_pc = kNoDexPc)
4295 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004296
Roland Levillain9867bc72015-08-05 10:21:34 +01004297 template <typename T, typename U, typename V>
4298 T Compute(T x, U y, V max_shift_value) const {
4299 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4300 "V is not the unsigned integer type corresponding to T");
4301 return x << (y & max_shift_value);
4302 }
4303
4304 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4305 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004306 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004307 }
4308 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4309 // case is handled as `x << static_cast<int>(y)`.
4310 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4311 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004312 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004313 }
4314 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4315 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004316 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004317 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004318 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4319 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4320 LOG(FATAL) << DebugName() << " is not defined for float values";
4321 UNREACHABLE();
4322 }
4323 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4324 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4325 LOG(FATAL) << DebugName() << " is not defined for double values";
4326 UNREACHABLE();
4327 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004328
4329 DECLARE_INSTRUCTION(Shl);
4330
4331 private:
4332 DISALLOW_COPY_AND_ASSIGN(HShl);
4333};
4334
4335class HShr : public HBinaryOperation {
4336 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004337 HShr(Primitive::Type result_type,
4338 HInstruction* left,
4339 HInstruction* right,
4340 uint32_t dex_pc = kNoDexPc)
4341 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004342
Roland Levillain9867bc72015-08-05 10:21:34 +01004343 template <typename T, typename U, typename V>
4344 T Compute(T x, U y, V max_shift_value) const {
4345 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4346 "V is not the unsigned integer type corresponding to T");
4347 return x >> (y & max_shift_value);
4348 }
4349
4350 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4351 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004352 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004353 }
4354 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4355 // case is handled as `x >> static_cast<int>(y)`.
4356 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4357 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004358 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004359 }
4360 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4361 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004362 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004363 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004364 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4365 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4366 LOG(FATAL) << DebugName() << " is not defined for float values";
4367 UNREACHABLE();
4368 }
4369 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4370 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4371 LOG(FATAL) << DebugName() << " is not defined for double values";
4372 UNREACHABLE();
4373 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004374
4375 DECLARE_INSTRUCTION(Shr);
4376
4377 private:
4378 DISALLOW_COPY_AND_ASSIGN(HShr);
4379};
4380
4381class HUShr : public HBinaryOperation {
4382 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004383 HUShr(Primitive::Type result_type,
4384 HInstruction* left,
4385 HInstruction* right,
4386 uint32_t dex_pc = kNoDexPc)
4387 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004388
Roland Levillain9867bc72015-08-05 10:21:34 +01004389 template <typename T, typename U, typename V>
4390 T Compute(T x, U y, V max_shift_value) const {
4391 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4392 "V is not the unsigned integer type corresponding to T");
4393 V ux = static_cast<V>(x);
4394 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004395 }
4396
Roland Levillain9867bc72015-08-05 10:21:34 +01004397 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4398 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004399 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004400 }
4401 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4402 // case is handled as `x >>> static_cast<int>(y)`.
4403 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4404 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004405 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004406 }
4407 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4408 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004409 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004410 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004411 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4412 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4413 LOG(FATAL) << DebugName() << " is not defined for float values";
4414 UNREACHABLE();
4415 }
4416 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4417 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4418 LOG(FATAL) << DebugName() << " is not defined for double values";
4419 UNREACHABLE();
4420 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004421
4422 DECLARE_INSTRUCTION(UShr);
4423
4424 private:
4425 DISALLOW_COPY_AND_ASSIGN(HUShr);
4426};
4427
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004428class HAnd : public HBinaryOperation {
4429 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004430 HAnd(Primitive::Type result_type,
4431 HInstruction* left,
4432 HInstruction* right,
4433 uint32_t dex_pc = kNoDexPc)
4434 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004435
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004436 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004437
Roland Levillain9867bc72015-08-05 10:21:34 +01004438 template <typename T, typename U>
4439 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4440
4441 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004442 return GetBlock()->GetGraph()->GetIntConstant(
4443 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004444 }
4445 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004446 return GetBlock()->GetGraph()->GetLongConstant(
4447 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004448 }
4449 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004450 return GetBlock()->GetGraph()->GetLongConstant(
4451 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004452 }
4453 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004454 return GetBlock()->GetGraph()->GetLongConstant(
4455 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004456 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004457 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4458 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4459 LOG(FATAL) << DebugName() << " is not defined for float values";
4460 UNREACHABLE();
4461 }
4462 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4463 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4464 LOG(FATAL) << DebugName() << " is not defined for double values";
4465 UNREACHABLE();
4466 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004467
4468 DECLARE_INSTRUCTION(And);
4469
4470 private:
4471 DISALLOW_COPY_AND_ASSIGN(HAnd);
4472};
4473
4474class HOr : public HBinaryOperation {
4475 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004476 HOr(Primitive::Type result_type,
4477 HInstruction* left,
4478 HInstruction* right,
4479 uint32_t dex_pc = kNoDexPc)
4480 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004481
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004482 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004483
Roland Levillain9867bc72015-08-05 10:21:34 +01004484 template <typename T, typename U>
4485 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4486
4487 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004488 return GetBlock()->GetGraph()->GetIntConstant(
4489 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004490 }
4491 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004492 return GetBlock()->GetGraph()->GetLongConstant(
4493 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004494 }
4495 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004496 return GetBlock()->GetGraph()->GetLongConstant(
4497 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004498 }
4499 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004500 return GetBlock()->GetGraph()->GetLongConstant(
4501 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004502 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004503 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4504 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4505 LOG(FATAL) << DebugName() << " is not defined for float values";
4506 UNREACHABLE();
4507 }
4508 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4509 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4510 LOG(FATAL) << DebugName() << " is not defined for double values";
4511 UNREACHABLE();
4512 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004513
4514 DECLARE_INSTRUCTION(Or);
4515
4516 private:
4517 DISALLOW_COPY_AND_ASSIGN(HOr);
4518};
4519
4520class HXor : public HBinaryOperation {
4521 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004522 HXor(Primitive::Type result_type,
4523 HInstruction* left,
4524 HInstruction* right,
4525 uint32_t dex_pc = kNoDexPc)
4526 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004527
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004528 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004529
Roland Levillain9867bc72015-08-05 10:21:34 +01004530 template <typename T, typename U>
4531 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4532
4533 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004534 return GetBlock()->GetGraph()->GetIntConstant(
4535 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004536 }
4537 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004538 return GetBlock()->GetGraph()->GetLongConstant(
4539 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004540 }
4541 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004542 return GetBlock()->GetGraph()->GetLongConstant(
4543 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004544 }
4545 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004546 return GetBlock()->GetGraph()->GetLongConstant(
4547 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004548 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004549 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4550 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4551 LOG(FATAL) << DebugName() << " is not defined for float values";
4552 UNREACHABLE();
4553 }
4554 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4555 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4556 LOG(FATAL) << DebugName() << " is not defined for double values";
4557 UNREACHABLE();
4558 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004559
4560 DECLARE_INSTRUCTION(Xor);
4561
4562 private:
4563 DISALLOW_COPY_AND_ASSIGN(HXor);
4564};
4565
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004566class HRor : public HBinaryOperation {
4567 public:
4568 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4569 : HBinaryOperation(result_type, value, distance) {}
4570
4571 template <typename T, typename U, typename V>
4572 T Compute(T x, U y, V max_shift_value) const {
4573 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4574 "V is not the unsigned integer type corresponding to T");
4575 V ux = static_cast<V>(x);
4576 if ((y & max_shift_value) == 0) {
4577 return static_cast<T>(ux);
4578 } else {
4579 const V reg_bits = sizeof(T) * 8;
4580 return static_cast<T>(ux >> (y & max_shift_value)) |
4581 (x << (reg_bits - (y & max_shift_value)));
4582 }
4583 }
4584
4585 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4586 return GetBlock()->GetGraph()->GetIntConstant(
4587 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4588 }
4589 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4590 return GetBlock()->GetGraph()->GetLongConstant(
4591 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4592 }
4593 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4594 return GetBlock()->GetGraph()->GetLongConstant(
4595 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4596 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004597 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4598 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4599 LOG(FATAL) << DebugName() << " is not defined for float values";
4600 UNREACHABLE();
4601 }
4602 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4603 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4604 LOG(FATAL) << DebugName() << " is not defined for double values";
4605 UNREACHABLE();
4606 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004607
4608 DECLARE_INSTRUCTION(Ror);
4609
4610 private:
4611 DISALLOW_COPY_AND_ASSIGN(HRor);
4612};
4613
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004614// The value of a parameter in this method. Its location depends on
4615// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004616class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004617 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004618 HParameterValue(const DexFile& dex_file,
4619 uint16_t type_index,
4620 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004621 Primitive::Type parameter_type,
4622 bool is_this = false)
4623 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004624 dex_file_(dex_file),
4625 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004626 index_(index),
4627 is_this_(is_this),
4628 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004629
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004630 const DexFile& GetDexFile() const { return dex_file_; }
4631 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004632 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004633 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004634
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004635 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4636 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004637
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004638 DECLARE_INSTRUCTION(ParameterValue);
4639
4640 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004641 const DexFile& dex_file_;
4642 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004643 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004644 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004645 const uint8_t index_;
4646
Calin Juravle10e244f2015-01-26 18:54:32 +00004647 // Whether or not the parameter value corresponds to 'this' argument.
4648 const bool is_this_;
4649
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004650 bool can_be_null_;
4651
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004652 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4653};
4654
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004655class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004656 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004657 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4658 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004659
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004660 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004661 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004662 return true;
4663 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004664
Roland Levillain9867bc72015-08-05 10:21:34 +01004665 template <typename T> T Compute(T x) const { return ~x; }
4666
4667 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004668 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004669 }
4670 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004671 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004672 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004673 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4674 LOG(FATAL) << DebugName() << " is not defined for float values";
4675 UNREACHABLE();
4676 }
4677 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4678 LOG(FATAL) << DebugName() << " is not defined for double values";
4679 UNREACHABLE();
4680 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004681
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004682 DECLARE_INSTRUCTION(Not);
4683
4684 private:
4685 DISALLOW_COPY_AND_ASSIGN(HNot);
4686};
4687
David Brazdil66d126e2015-04-03 16:02:44 +01004688class HBooleanNot : public HUnaryOperation {
4689 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004690 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4691 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004692
4693 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004694 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004695 return true;
4696 }
4697
Roland Levillain9867bc72015-08-05 10:21:34 +01004698 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004699 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004700 return !x;
4701 }
4702
Roland Levillain9867bc72015-08-05 10:21:34 +01004703 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004704 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004705 }
4706 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4707 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004708 UNREACHABLE();
4709 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004710 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4711 LOG(FATAL) << DebugName() << " is not defined for float values";
4712 UNREACHABLE();
4713 }
4714 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4715 LOG(FATAL) << DebugName() << " is not defined for double values";
4716 UNREACHABLE();
4717 }
David Brazdil66d126e2015-04-03 16:02:44 +01004718
4719 DECLARE_INSTRUCTION(BooleanNot);
4720
4721 private:
4722 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4723};
4724
Roland Levillaindff1f282014-11-05 14:15:05 +00004725class HTypeConversion : public HExpression<1> {
4726 public:
4727 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004728 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004729 : HExpression(result_type,
4730 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4731 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004732 SetRawInputAt(0, input);
4733 DCHECK_NE(input->GetType(), result_type);
4734 }
4735
4736 HInstruction* GetInput() const { return InputAt(0); }
4737 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4738 Primitive::Type GetResultType() const { return GetType(); }
4739
4740 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004741 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004742
Mark Mendelle82549b2015-05-06 10:55:34 -04004743 // Try to statically evaluate the conversion and return a HConstant
4744 // containing the result. If the input cannot be converted, return nullptr.
4745 HConstant* TryStaticEvaluation() const;
4746
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004747 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4748 Primitive::Type result_type) {
4749 // Some architectures may not require the 'GC' side effects, but at this point
4750 // in the compilation process we do not know what architecture we will
4751 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004752 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4753 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004754 return SideEffects::CanTriggerGC();
4755 }
4756 return SideEffects::None();
4757 }
4758
Roland Levillaindff1f282014-11-05 14:15:05 +00004759 DECLARE_INSTRUCTION(TypeConversion);
4760
4761 private:
4762 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4763};
4764
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004765static constexpr uint32_t kNoRegNumber = -1;
4766
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004767class HPhi : public HInstruction {
4768 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004769 HPhi(ArenaAllocator* arena,
4770 uint32_t reg_number,
4771 size_t number_of_inputs,
4772 Primitive::Type type,
4773 uint32_t dex_pc = kNoDexPc)
4774 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004775 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004776 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004777 type_(ToPhiType(type)),
4778 // Phis are constructed live and marked dead if conflicting or unused.
4779 // Individual steps of SsaBuilder should assume that if a phi has been
4780 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4781 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004782 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004783 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004784 }
4785
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004786 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4787 static Primitive::Type ToPhiType(Primitive::Type type) {
4788 switch (type) {
4789 case Primitive::kPrimBoolean:
4790 case Primitive::kPrimByte:
4791 case Primitive::kPrimShort:
4792 case Primitive::kPrimChar:
4793 return Primitive::kPrimInt;
4794 default:
4795 return type;
4796 }
4797 }
4798
David Brazdilffee3d32015-07-06 11:48:53 +01004799 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4800
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004801 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004802
4803 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004804 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004805
Calin Juravle10e244f2015-01-26 18:54:32 +00004806 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil4833f5a2015-12-16 10:37:39 +00004807 void SetType(Primitive::Type new_type) {
4808 // Make sure that only valid type changes occur. The following are allowed:
4809 // (1) int -> float/ref (primitive type propagation),
4810 // (2) long -> double (primitive type propagation).
4811 DCHECK(type_ == new_type ||
4812 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4813 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4814 (type_ == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4815 type_ = new_type;
4816 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004817
Calin Juravle10e244f2015-01-26 18:54:32 +00004818 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4819 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4820
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004821 uint32_t GetRegNumber() const { return reg_number_; }
4822
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004823 void SetDead() { is_live_ = false; }
4824 void SetLive() { is_live_ = true; }
4825 bool IsDead() const { return !is_live_; }
4826 bool IsLive() const { return is_live_; }
4827
David Brazdil77a48ae2015-09-15 12:34:04 +00004828 bool IsVRegEquivalentOf(HInstruction* other) const {
4829 return other != nullptr
4830 && other->IsPhi()
4831 && other->AsPhi()->GetBlock() == GetBlock()
4832 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4833 }
4834
Calin Juravlea4f88312015-04-16 12:57:19 +01004835 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4836 // An equivalent phi is a phi having the same dex register and type.
4837 // It assumes that phis with the same dex register are adjacent.
4838 HPhi* GetNextEquivalentPhiWithSameType() {
4839 HInstruction* next = GetNext();
4840 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4841 if (next->GetType() == GetType()) {
4842 return next->AsPhi();
4843 }
4844 next = next->GetNext();
4845 }
4846 return nullptr;
4847 }
4848
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004849 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004850
David Brazdil1abb4192015-02-17 18:33:36 +00004851 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004852 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004853 return inputs_[index];
4854 }
David Brazdil1abb4192015-02-17 18:33:36 +00004855
4856 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004857 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004858 }
4859
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004860 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004861 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004862 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004863 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004864 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004865 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004866
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004867 DISALLOW_COPY_AND_ASSIGN(HPhi);
4868};
4869
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004870class HNullCheck : public HExpression<1> {
4871 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004872 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4873 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004874 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004875 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004876 SetRawInputAt(0, value);
4877 }
4878
Calin Juravle10e244f2015-01-26 18:54:32 +00004879 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004880 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004881 return true;
4882 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004883
Calin Juravle10e244f2015-01-26 18:54:32 +00004884 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004885
Calin Juravle10e244f2015-01-26 18:54:32 +00004886 bool CanThrow() const OVERRIDE { return true; }
4887
4888 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004889
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004890
4891 DECLARE_INSTRUCTION(NullCheck);
4892
4893 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004894 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4895};
4896
4897class FieldInfo : public ValueObject {
4898 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004899 FieldInfo(MemberOffset field_offset,
4900 Primitive::Type field_type,
4901 bool is_volatile,
4902 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004903 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004904 const DexFile& dex_file,
4905 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004906 : field_offset_(field_offset),
4907 field_type_(field_type),
4908 is_volatile_(is_volatile),
4909 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004910 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004911 dex_file_(dex_file),
4912 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004913
4914 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004915 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004916 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004917 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004918 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004919 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004920 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004921
4922 private:
4923 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004924 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004925 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004926 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004927 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004928 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004929 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004930};
4931
4932class HInstanceFieldGet : public HExpression<1> {
4933 public:
4934 HInstanceFieldGet(HInstruction* value,
4935 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004936 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004937 bool is_volatile,
4938 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004939 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004940 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004941 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004942 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004943 : HExpression(field_type,
4944 SideEffects::FieldReadOfType(field_type, is_volatile),
4945 dex_pc),
4946 field_info_(field_offset,
4947 field_type,
4948 is_volatile,
4949 field_idx,
4950 declaring_class_def_index,
4951 dex_file,
4952 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004953 SetRawInputAt(0, value);
4954 }
4955
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004956 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004957
4958 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4959 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4960 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004961 }
4962
Calin Juravle641547a2015-04-21 22:08:51 +01004963 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4964 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004965 }
4966
4967 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004968 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4969 }
4970
Calin Juravle52c48962014-12-16 17:02:57 +00004971 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004972 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004973 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004974 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004975
4976 DECLARE_INSTRUCTION(InstanceFieldGet);
4977
4978 private:
4979 const FieldInfo field_info_;
4980
4981 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4982};
4983
4984class HInstanceFieldSet : public HTemplateInstruction<2> {
4985 public:
4986 HInstanceFieldSet(HInstruction* object,
4987 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004988 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004989 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004990 bool is_volatile,
4991 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004992 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004993 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004994 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004995 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004996 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
4997 dex_pc),
4998 field_info_(field_offset,
4999 field_type,
5000 is_volatile,
5001 field_idx,
5002 declaring_class_def_index,
5003 dex_file,
5004 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005005 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005006 SetRawInputAt(0, object);
5007 SetRawInputAt(1, value);
5008 }
5009
Calin Juravle641547a2015-04-21 22:08:51 +01005010 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5011 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005012 }
5013
Calin Juravle52c48962014-12-16 17:02:57 +00005014 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005015 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005016 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005017 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005018 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005019 bool GetValueCanBeNull() const { return value_can_be_null_; }
5020 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005021
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005022 DECLARE_INSTRUCTION(InstanceFieldSet);
5023
5024 private:
5025 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005026 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005027
5028 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5029};
5030
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005031class HArrayGet : public HExpression<2> {
5032 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005033 HArrayGet(HInstruction* array,
5034 HInstruction* index,
5035 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005036 uint32_t dex_pc,
5037 SideEffects additional_side_effects = SideEffects::None())
5038 : HExpression(type,
5039 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00005040 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005041 SetRawInputAt(0, array);
5042 SetRawInputAt(1, index);
5043 }
5044
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005045 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005046 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005047 return true;
5048 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005049 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005050 // TODO: We can be smarter here.
5051 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5052 // which generates the implicit null check. There are cases when these can be removed
5053 // to produce better code. If we ever add optimizations to do so we should allow an
5054 // implicit check here (as long as the address falls in the first page).
5055 return false;
5056 }
5057
David Brazdil4833f5a2015-12-16 10:37:39 +00005058 bool IsEquivalentOf(HArrayGet* other) const {
5059 bool result = (GetDexPc() == other->GetDexPc());
5060 if (kIsDebugBuild && result) {
5061 DCHECK_EQ(GetBlock(), other->GetBlock());
5062 DCHECK_EQ(GetArray(), other->GetArray());
5063 DCHECK_EQ(GetIndex(), other->GetIndex());
5064 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005065 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005066 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005067 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5068 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005069 }
5070 }
5071 return result;
5072 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005073
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005074 HInstruction* GetArray() const { return InputAt(0); }
5075 HInstruction* GetIndex() const { return InputAt(1); }
5076
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005077 DECLARE_INSTRUCTION(ArrayGet);
5078
5079 private:
5080 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5081};
5082
5083class HArraySet : public HTemplateInstruction<3> {
5084 public:
5085 HArraySet(HInstruction* array,
5086 HInstruction* index,
5087 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005088 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005089 uint32_t dex_pc,
5090 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005091 : HTemplateInstruction(
5092 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005093 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
5094 additional_side_effects),
5095 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005096 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005097 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005098 value_can_be_null_(true),
5099 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005100 SetRawInputAt(0, array);
5101 SetRawInputAt(1, index);
5102 SetRawInputAt(2, value);
5103 }
5104
Calin Juravle77520bc2015-01-12 18:45:46 +00005105 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005106 // We call a runtime method to throw ArrayStoreException.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005107 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005108 }
5109
Mingyao Yang81014cb2015-06-02 03:16:27 -07005110 // Can throw ArrayStoreException.
5111 bool CanThrow() const OVERRIDE { return needs_type_check_; }
5112
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005113 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005114 // TODO: Same as for ArrayGet.
5115 return false;
5116 }
5117
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005118 void ClearNeedsTypeCheck() {
5119 needs_type_check_ = false;
5120 }
5121
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005122 void ClearValueCanBeNull() {
5123 value_can_be_null_ = false;
5124 }
5125
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005126 void SetStaticTypeOfArrayIsObjectArray() {
5127 static_type_of_array_is_object_array_ = true;
5128 }
5129
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005130 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005131 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005132 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005133
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005134 HInstruction* GetArray() const { return InputAt(0); }
5135 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005136 HInstruction* GetValue() const { return InputAt(2); }
5137
5138 Primitive::Type GetComponentType() const {
5139 // The Dex format does not type floating point index operations. Since the
5140 // `expected_component_type_` is set during building and can therefore not
5141 // be correct, we also check what is the value type. If it is a floating
5142 // point type, we must use that type.
5143 Primitive::Type value_type = GetValue()->GetType();
5144 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5145 ? value_type
5146 : expected_component_type_;
5147 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005148
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005149 Primitive::Type GetRawExpectedComponentType() const {
5150 return expected_component_type_;
5151 }
5152
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005153 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5154 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5155 }
5156
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005157 DECLARE_INSTRUCTION(ArraySet);
5158
5159 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005160 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005161 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005162 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005163 // Cached information for the reference_type_info_ so that codegen
5164 // does not need to inspect the static type.
5165 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005166
5167 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5168};
5169
5170class HArrayLength : public HExpression<1> {
5171 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005172 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005173 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005174 // Note that arrays do not change length, so the instruction does not
5175 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005176 SetRawInputAt(0, array);
5177 }
5178
Calin Juravle77520bc2015-01-12 18:45:46 +00005179 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005180 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005181 return true;
5182 }
Calin Juravle641547a2015-04-21 22:08:51 +01005183 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5184 return obj == InputAt(0);
5185 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005186
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005187 DECLARE_INSTRUCTION(ArrayLength);
5188
5189 private:
5190 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5191};
5192
5193class HBoundsCheck : public HExpression<2> {
5194 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005195 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5196 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005197 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005198 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005199 DCHECK(index->GetType() == Primitive::kPrimInt);
5200 SetRawInputAt(0, index);
5201 SetRawInputAt(1, length);
5202 }
5203
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005204 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005205 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005206 return true;
5207 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005208
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005209 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005210
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005211 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005212
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005213 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005214
5215 DECLARE_INSTRUCTION(BoundsCheck);
5216
5217 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005218 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5219};
5220
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005221class HSuspendCheck : public HTemplateInstruction<0> {
5222 public:
5223 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005224 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005225
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005226 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005227 return true;
5228 }
5229
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005230 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5231 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005232
5233 DECLARE_INSTRUCTION(SuspendCheck);
5234
5235 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005236 // Only used for code generation, in order to share the same slow path between back edges
5237 // of a same loop.
5238 SlowPathCode* slow_path_;
5239
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005240 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5241};
5242
David Srbecky0cf44932015-12-09 14:09:59 +00005243// Pseudo-instruction which provides the native debugger with mapping information.
5244// It ensures that we can generate line number and local variables at this point.
5245class HNativeDebugInfo : public HTemplateInstruction<0> {
5246 public:
5247 explicit HNativeDebugInfo(uint32_t dex_pc)
5248 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5249
5250 bool NeedsEnvironment() const OVERRIDE {
5251 return true;
5252 }
5253
5254 DECLARE_INSTRUCTION(NativeDebugInfo);
5255
5256 private:
5257 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5258};
5259
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005260/**
5261 * Instruction to load a Class object.
5262 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005263class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005264 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005265 HLoadClass(HCurrentMethod* current_method,
5266 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005267 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005268 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005269 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005270 bool needs_access_check,
5271 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005272 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005273 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005274 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005275 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00005276 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01005277 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005278 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00005279 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005280 // Referrers class should not need access check. We never inline unverified
5281 // methods so we can't possibly end up in this situation.
5282 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005283 SetRawInputAt(0, current_method);
5284 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005285
5286 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005287
5288 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005289 // Note that we don't need to test for generate_clinit_check_.
5290 // Whether or not we need to generate the clinit check is processed in
5291 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005292 return other->AsLoadClass()->type_index_ == type_index_ &&
5293 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005294 }
5295
5296 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5297
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005298 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005299 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005300 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005301
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005302 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005303 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005304 }
5305
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005306 bool MustGenerateClinitCheck() const {
5307 return generate_clinit_check_;
5308 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005309
Calin Juravle0ba218d2015-05-19 18:46:01 +01005310 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005311 // The entrypoint the code generator is going to call does not do
5312 // clinit of the class.
5313 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01005314 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005315 }
5316
5317 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005318 return MustGenerateClinitCheck() ||
5319 (!is_referrers_class_ && !is_in_dex_cache_) ||
5320 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01005321 }
5322
5323 bool NeedsAccessCheck() const {
5324 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005325 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005326
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005327 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005328 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005329 }
5330
Calin Juravleacf735c2015-02-12 15:25:22 +00005331 ReferenceTypeInfo GetLoadedClassRTI() {
5332 return loaded_class_rti_;
5333 }
5334
5335 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5336 // Make sure we only set exact types (the loaded class should never be merged).
5337 DCHECK(rti.IsExact());
5338 loaded_class_rti_ = rti;
5339 }
5340
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005341 const DexFile& GetDexFile() { return dex_file_; }
5342
Vladimir Markodc151b22015-10-15 18:02:30 +01005343 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005344
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005345 static SideEffects SideEffectsForArchRuntimeCalls() {
5346 return SideEffects::CanTriggerGC();
5347 }
5348
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005349 bool IsInDexCache() const { return is_in_dex_cache_; }
5350
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005351 DECLARE_INSTRUCTION(LoadClass);
5352
5353 private:
5354 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005355 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005356 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005357 // Whether this instruction must generate the initialization check.
5358 // Used for code generation.
5359 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005360 const bool needs_access_check_;
5361 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005362
Calin Juravleacf735c2015-02-12 15:25:22 +00005363 ReferenceTypeInfo loaded_class_rti_;
5364
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005365 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5366};
5367
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005368class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005369 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005370 HLoadString(HCurrentMethod* current_method,
5371 uint32_t string_index,
5372 uint32_t dex_pc,
5373 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005374 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005375 string_index_(string_index),
5376 is_in_dex_cache_(is_in_dex_cache) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005377 SetRawInputAt(0, current_method);
5378 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005379
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005380 bool CanBeMoved() const OVERRIDE { return true; }
5381
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005382 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5383 return other->AsLoadString()->string_index_ == string_index_;
5384 }
5385
5386 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5387
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005388 uint32_t GetStringIndex() const { return string_index_; }
5389
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005390 // Will call the runtime if the string is not already in the dex cache.
5391 bool NeedsEnvironment() const OVERRIDE { return !IsInDexCache(); }
5392
Vladimir Markodc151b22015-10-15 18:02:30 +01005393 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005394 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005395 bool IsInDexCache() const { return is_in_dex_cache_; }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005396 bool CanThrow() const OVERRIDE { return !IsInDexCache(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005397
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005398 static SideEffects SideEffectsForArchRuntimeCalls() {
5399 return SideEffects::CanTriggerGC();
5400 }
5401
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005402 DECLARE_INSTRUCTION(LoadString);
5403
5404 private:
5405 const uint32_t string_index_;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005406 const bool is_in_dex_cache_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005407
5408 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5409};
5410
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005411/**
5412 * Performs an initialization check on its Class object input.
5413 */
5414class HClinitCheck : public HExpression<1> {
5415 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005416 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005417 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005418 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005419 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5420 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005421 SetRawInputAt(0, constant);
5422 }
5423
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005424 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005425 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005426 return true;
5427 }
5428
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005429 bool NeedsEnvironment() const OVERRIDE {
5430 // May call runtime to initialize the class.
5431 return true;
5432 }
5433
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005434 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005435
5436 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5437
5438 DECLARE_INSTRUCTION(ClinitCheck);
5439
5440 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005441 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5442};
5443
5444class HStaticFieldGet : public HExpression<1> {
5445 public:
5446 HStaticFieldGet(HInstruction* cls,
5447 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005448 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005449 bool is_volatile,
5450 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005451 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005452 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005453 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005454 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005455 : HExpression(field_type,
5456 SideEffects::FieldReadOfType(field_type, is_volatile),
5457 dex_pc),
5458 field_info_(field_offset,
5459 field_type,
5460 is_volatile,
5461 field_idx,
5462 declaring_class_def_index,
5463 dex_file,
5464 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005465 SetRawInputAt(0, cls);
5466 }
5467
Calin Juravle52c48962014-12-16 17:02:57 +00005468
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005469 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005470
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005471 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005472 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5473 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005474 }
5475
5476 size_t ComputeHashCode() const OVERRIDE {
5477 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5478 }
5479
Calin Juravle52c48962014-12-16 17:02:57 +00005480 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005481 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5482 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005483 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005484
5485 DECLARE_INSTRUCTION(StaticFieldGet);
5486
5487 private:
5488 const FieldInfo field_info_;
5489
5490 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5491};
5492
5493class HStaticFieldSet : public HTemplateInstruction<2> {
5494 public:
5495 HStaticFieldSet(HInstruction* cls,
5496 HInstruction* value,
5497 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005498 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005499 bool is_volatile,
5500 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005501 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005502 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005503 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005504 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005505 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5506 dex_pc),
5507 field_info_(field_offset,
5508 field_type,
5509 is_volatile,
5510 field_idx,
5511 declaring_class_def_index,
5512 dex_file,
5513 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005514 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005515 SetRawInputAt(0, cls);
5516 SetRawInputAt(1, value);
5517 }
5518
Calin Juravle52c48962014-12-16 17:02:57 +00005519 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005520 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5521 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005522 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005523
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005524 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005525 bool GetValueCanBeNull() const { return value_can_be_null_; }
5526 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005527
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005528 DECLARE_INSTRUCTION(StaticFieldSet);
5529
5530 private:
5531 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005532 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005533
5534 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5535};
5536
Calin Juravlee460d1d2015-09-29 04:52:17 +01005537class HUnresolvedInstanceFieldGet : public HExpression<1> {
5538 public:
5539 HUnresolvedInstanceFieldGet(HInstruction* obj,
5540 Primitive::Type field_type,
5541 uint32_t field_index,
5542 uint32_t dex_pc)
5543 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5544 field_index_(field_index) {
5545 SetRawInputAt(0, obj);
5546 }
5547
5548 bool NeedsEnvironment() const OVERRIDE { return true; }
5549 bool CanThrow() const OVERRIDE { return true; }
5550
5551 Primitive::Type GetFieldType() const { return GetType(); }
5552 uint32_t GetFieldIndex() const { return field_index_; }
5553
5554 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5555
5556 private:
5557 const uint32_t field_index_;
5558
5559 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5560};
5561
5562class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5563 public:
5564 HUnresolvedInstanceFieldSet(HInstruction* obj,
5565 HInstruction* value,
5566 Primitive::Type field_type,
5567 uint32_t field_index,
5568 uint32_t dex_pc)
5569 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5570 field_type_(field_type),
5571 field_index_(field_index) {
5572 DCHECK_EQ(field_type, value->GetType());
5573 SetRawInputAt(0, obj);
5574 SetRawInputAt(1, value);
5575 }
5576
5577 bool NeedsEnvironment() const OVERRIDE { return true; }
5578 bool CanThrow() const OVERRIDE { return true; }
5579
5580 Primitive::Type GetFieldType() const { return field_type_; }
5581 uint32_t GetFieldIndex() const { return field_index_; }
5582
5583 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5584
5585 private:
5586 const Primitive::Type field_type_;
5587 const uint32_t field_index_;
5588
5589 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5590};
5591
5592class HUnresolvedStaticFieldGet : public HExpression<0> {
5593 public:
5594 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5595 uint32_t field_index,
5596 uint32_t dex_pc)
5597 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5598 field_index_(field_index) {
5599 }
5600
5601 bool NeedsEnvironment() const OVERRIDE { return true; }
5602 bool CanThrow() const OVERRIDE { return true; }
5603
5604 Primitive::Type GetFieldType() const { return GetType(); }
5605 uint32_t GetFieldIndex() const { return field_index_; }
5606
5607 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5608
5609 private:
5610 const uint32_t field_index_;
5611
5612 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5613};
5614
5615class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5616 public:
5617 HUnresolvedStaticFieldSet(HInstruction* value,
5618 Primitive::Type field_type,
5619 uint32_t field_index,
5620 uint32_t dex_pc)
5621 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5622 field_type_(field_type),
5623 field_index_(field_index) {
5624 DCHECK_EQ(field_type, value->GetType());
5625 SetRawInputAt(0, value);
5626 }
5627
5628 bool NeedsEnvironment() const OVERRIDE { return true; }
5629 bool CanThrow() const OVERRIDE { return true; }
5630
5631 Primitive::Type GetFieldType() const { return field_type_; }
5632 uint32_t GetFieldIndex() const { return field_index_; }
5633
5634 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5635
5636 private:
5637 const Primitive::Type field_type_;
5638 const uint32_t field_index_;
5639
5640 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5641};
5642
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005643// Implement the move-exception DEX instruction.
5644class HLoadException : public HExpression<0> {
5645 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005646 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5647 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005648
David Brazdilbbd733e2015-08-18 17:48:17 +01005649 bool CanBeNull() const OVERRIDE { return false; }
5650
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005651 DECLARE_INSTRUCTION(LoadException);
5652
5653 private:
5654 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5655};
5656
David Brazdilcb1c0552015-08-04 16:22:25 +01005657// Implicit part of move-exception which clears thread-local exception storage.
5658// Must not be removed because the runtime expects the TLS to get cleared.
5659class HClearException : public HTemplateInstruction<0> {
5660 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005661 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5662 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005663
5664 DECLARE_INSTRUCTION(ClearException);
5665
5666 private:
5667 DISALLOW_COPY_AND_ASSIGN(HClearException);
5668};
5669
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005670class HThrow : public HTemplateInstruction<1> {
5671 public:
5672 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005673 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005674 SetRawInputAt(0, exception);
5675 }
5676
5677 bool IsControlFlow() const OVERRIDE { return true; }
5678
5679 bool NeedsEnvironment() const OVERRIDE { return true; }
5680
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005681 bool CanThrow() const OVERRIDE { return true; }
5682
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005683
5684 DECLARE_INSTRUCTION(Throw);
5685
5686 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005687 DISALLOW_COPY_AND_ASSIGN(HThrow);
5688};
5689
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005690/**
5691 * Implementation strategies for the code generator of a HInstanceOf
5692 * or `HCheckCast`.
5693 */
5694enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005695 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005696 kExactCheck, // Can do a single class compare.
5697 kClassHierarchyCheck, // Can just walk the super class chain.
5698 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5699 kInterfaceCheck, // No optimization yet when checking against an interface.
5700 kArrayObjectCheck, // Can just check if the array is not primitive.
5701 kArrayCheck // No optimization yet when checking against a generic array.
5702};
5703
Roland Levillain86503782016-02-11 19:07:30 +00005704std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5705
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005706class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005707 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005708 HInstanceOf(HInstruction* object,
5709 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005710 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005711 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005712 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005713 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005714 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005715 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005716 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005717 SetRawInputAt(0, object);
5718 SetRawInputAt(1, constant);
5719 }
5720
5721 bool CanBeMoved() const OVERRIDE { return true; }
5722
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005723 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005724 return true;
5725 }
5726
5727 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005728 return CanCallRuntime(check_kind_);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005729 }
5730
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005731 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5732
5733 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005734
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005735 // Used only in code generation.
5736 bool MustDoNullCheck() const { return must_do_null_check_; }
5737 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5738
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005739 static bool CanCallRuntime(TypeCheckKind check_kind) {
5740 // Mips currently does runtime calls for any other checks.
5741 return check_kind != TypeCheckKind::kExactCheck;
5742 }
5743
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005744 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005745 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005746 }
5747
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005748 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005749
5750 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005751 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005752 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005753
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005754 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5755};
5756
Calin Juravleb1498f62015-02-16 13:13:29 +00005757class HBoundType : public HExpression<1> {
5758 public:
David Brazdilf5552582015-12-27 13:36:12 +00005759 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005760 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
David Brazdilf5552582015-12-27 13:36:12 +00005761 upper_bound_(ReferenceTypeInfo::CreateInvalid()),
5762 upper_can_be_null_(true),
5763 can_be_null_(true) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005764 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005765 SetRawInputAt(0, input);
5766 }
5767
David Brazdilf5552582015-12-27 13:36:12 +00005768 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005769 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5770 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
David Brazdilf5552582015-12-27 13:36:12 +00005771 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005772
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005773 void SetCanBeNull(bool can_be_null) {
5774 DCHECK(upper_can_be_null_ || !can_be_null);
5775 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005776 }
5777
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005778 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5779
Calin Juravleb1498f62015-02-16 13:13:29 +00005780 DECLARE_INSTRUCTION(BoundType);
5781
5782 private:
5783 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005784 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5785 // It is used to bound the type in cases like:
5786 // if (x instanceof ClassX) {
5787 // // uper_bound_ will be ClassX
5788 // }
David Brazdilf5552582015-12-27 13:36:12 +00005789 ReferenceTypeInfo upper_bound_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005790 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5791 // is false then can_be_null_ cannot be true).
David Brazdilf5552582015-12-27 13:36:12 +00005792 bool upper_can_be_null_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005793 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005794
5795 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5796};
5797
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005798class HCheckCast : public HTemplateInstruction<2> {
5799 public:
5800 HCheckCast(HInstruction* object,
5801 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005802 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005803 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005804 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005805 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005806 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005807 SetRawInputAt(0, object);
5808 SetRawInputAt(1, constant);
5809 }
5810
5811 bool CanBeMoved() const OVERRIDE { return true; }
5812
5813 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5814 return true;
5815 }
5816
5817 bool NeedsEnvironment() const OVERRIDE {
5818 // Instruction may throw a CheckCastError.
5819 return true;
5820 }
5821
5822 bool CanThrow() const OVERRIDE { return true; }
5823
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005824 bool MustDoNullCheck() const { return must_do_null_check_; }
5825 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005826 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005827
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005828 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005829
5830 DECLARE_INSTRUCTION(CheckCast);
5831
5832 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005833 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005834 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005835
5836 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005837};
5838
Calin Juravle27df7582015-04-17 19:12:31 +01005839class HMemoryBarrier : public HTemplateInstruction<0> {
5840 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005841 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005842 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005843 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005844 barrier_kind_(barrier_kind) {}
5845
5846 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5847
5848 DECLARE_INSTRUCTION(MemoryBarrier);
5849
5850 private:
5851 const MemBarrierKind barrier_kind_;
5852
5853 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5854};
5855
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005856class HMonitorOperation : public HTemplateInstruction<1> {
5857 public:
5858 enum OperationKind {
5859 kEnter,
5860 kExit,
5861 };
5862
5863 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005864 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005865 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5866 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005867 SetRawInputAt(0, object);
5868 }
5869
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005870 // Instruction may go into runtime, so we need an environment.
5871 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00005872
5873 bool CanThrow() const OVERRIDE {
5874 // Verifier guarantees that monitor-exit cannot throw.
5875 // This is important because it allows the HGraphBuilder to remove
5876 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5877 return IsEnter();
5878 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005879
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005880
5881 bool IsEnter() const { return kind_ == kEnter; }
5882
5883 DECLARE_INSTRUCTION(MonitorOperation);
5884
Calin Juravle52c48962014-12-16 17:02:57 +00005885 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005886 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005887
5888 private:
5889 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5890};
5891
David Brazdil74eb1b22015-12-14 11:44:01 +00005892class HSelect : public HExpression<3> {
5893 public:
5894 HSelect(HInstruction* condition,
5895 HInstruction* true_value,
5896 HInstruction* false_value,
5897 uint32_t dex_pc)
5898 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
5899 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
5900
5901 // First input must be `true_value` or `false_value` to allow codegens to
5902 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
5903 // that architectures which implement HSelect as a conditional move also
5904 // will not need to invert the condition.
5905 SetRawInputAt(0, false_value);
5906 SetRawInputAt(1, true_value);
5907 SetRawInputAt(2, condition);
5908 }
5909
5910 HInstruction* GetFalseValue() const { return InputAt(0); }
5911 HInstruction* GetTrueValue() const { return InputAt(1); }
5912 HInstruction* GetCondition() const { return InputAt(2); }
5913
5914 bool CanBeMoved() const OVERRIDE { return true; }
5915 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
5916
5917 bool CanBeNull() const OVERRIDE {
5918 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
5919 }
5920
5921 DECLARE_INSTRUCTION(Select);
5922
5923 private:
5924 DISALLOW_COPY_AND_ASSIGN(HSelect);
5925};
5926
Vladimir Markof9f64412015-09-02 14:05:49 +01005927class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005928 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005929 MoveOperands(Location source,
5930 Location destination,
5931 Primitive::Type type,
5932 HInstruction* instruction)
5933 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005934
5935 Location GetSource() const { return source_; }
5936 Location GetDestination() const { return destination_; }
5937
5938 void SetSource(Location value) { source_ = value; }
5939 void SetDestination(Location value) { destination_ = value; }
5940
5941 // The parallel move resolver marks moves as "in-progress" by clearing the
5942 // destination (but not the source).
5943 Location MarkPending() {
5944 DCHECK(!IsPending());
5945 Location dest = destination_;
5946 destination_ = Location::NoLocation();
5947 return dest;
5948 }
5949
5950 void ClearPending(Location dest) {
5951 DCHECK(IsPending());
5952 destination_ = dest;
5953 }
5954
5955 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00005956 DCHECK(source_.IsValid() || destination_.IsInvalid());
5957 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005958 }
5959
5960 // True if this blocks a move from the given location.
5961 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005962 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005963 }
5964
5965 // A move is redundant if it's been eliminated, if its source and
5966 // destination are the same, or if its destination is unneeded.
5967 bool IsRedundant() const {
5968 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5969 }
5970
5971 // We clear both operands to indicate move that's been eliminated.
5972 void Eliminate() {
5973 source_ = destination_ = Location::NoLocation();
5974 }
5975
5976 bool IsEliminated() const {
5977 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5978 return source_.IsInvalid();
5979 }
5980
Alexey Frunze4dda3372015-06-01 18:31:49 -07005981 Primitive::Type GetType() const { return type_; }
5982
Nicolas Geoffray90218252015-04-15 11:56:51 +01005983 bool Is64BitMove() const {
5984 return Primitive::Is64BitType(type_);
5985 }
5986
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005987 HInstruction* GetInstruction() const { return instruction_; }
5988
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005989 private:
5990 Location source_;
5991 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01005992 // The type this move is for.
5993 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005994 // The instruction this move is assocatied with. Null when this move is
5995 // for moving an input in the expected locations of user (including a phi user).
5996 // This is only used in debug mode, to ensure we do not connect interval siblings
5997 // in the same parallel move.
5998 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005999};
6000
Roland Levillainc9285912015-12-18 10:38:42 +00006001std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6002
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006003static constexpr size_t kDefaultNumberOfMoves = 4;
6004
6005class HParallelMove : public HTemplateInstruction<0> {
6006 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006007 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006008 : HTemplateInstruction(SideEffects::None(), dex_pc),
6009 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6010 moves_.reserve(kDefaultNumberOfMoves);
6011 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006012
Nicolas Geoffray90218252015-04-15 11:56:51 +01006013 void AddMove(Location source,
6014 Location destination,
6015 Primitive::Type type,
6016 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006017 DCHECK(source.IsValid());
6018 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006019 if (kIsDebugBuild) {
6020 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006021 for (const MoveOperands& move : moves_) {
6022 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006023 // Special case the situation where the move is for the spill slot
6024 // of the instruction.
6025 if ((GetPrevious() == instruction)
6026 || ((GetPrevious() == nullptr)
6027 && instruction->IsPhi()
6028 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006029 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006030 << "Doing parallel moves for the same instruction.";
6031 } else {
6032 DCHECK(false) << "Doing parallel moves for the same instruction.";
6033 }
6034 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006035 }
6036 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006037 for (const MoveOperands& move : moves_) {
6038 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006039 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006040 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006041 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006042 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006043 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006044 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006045 }
6046
Vladimir Marko225b6462015-09-28 12:17:40 +01006047 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006048 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006049 }
6050
Vladimir Marko225b6462015-09-28 12:17:40 +01006051 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006052
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006053 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006054
6055 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006056 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006057
6058 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6059};
6060
Mark Mendell0616ae02015-04-17 12:49:27 -04006061} // namespace art
6062
Vladimir Markob4536b72015-11-24 13:45:23 +00006063#ifdef ART_ENABLE_CODEGEN_arm
6064#include "nodes_arm.h"
6065#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006066#ifdef ART_ENABLE_CODEGEN_arm64
6067#include "nodes_arm64.h"
6068#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006069#ifdef ART_ENABLE_CODEGEN_x86
6070#include "nodes_x86.h"
6071#endif
6072
6073namespace art {
6074
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006075class HGraphVisitor : public ValueObject {
6076 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006077 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6078 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006079
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006080 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006081 virtual void VisitBasicBlock(HBasicBlock* block);
6082
Roland Levillain633021e2014-10-01 14:12:25 +01006083 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006084 void VisitInsertionOrder();
6085
Roland Levillain633021e2014-10-01 14:12:25 +01006086 // Visit the graph following dominator tree reverse post-order.
6087 void VisitReversePostOrder();
6088
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006089 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006090
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006091 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006092#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006093 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6094
6095 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6096
6097#undef DECLARE_VISIT_INSTRUCTION
6098
6099 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006100 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006101
6102 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6103};
6104
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006105class HGraphDelegateVisitor : public HGraphVisitor {
6106 public:
6107 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6108 virtual ~HGraphDelegateVisitor() {}
6109
6110 // Visit functions that delegate to to super class.
6111#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006112 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006113
6114 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6115
6116#undef DECLARE_VISIT_INSTRUCTION
6117
6118 private:
6119 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6120};
6121
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006122class HInsertionOrderIterator : public ValueObject {
6123 public:
6124 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6125
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006126 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006127 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006128 void Advance() { ++index_; }
6129
6130 private:
6131 const HGraph& graph_;
6132 size_t index_;
6133
6134 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6135};
6136
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006137class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006138 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006139 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6140 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006141 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006142 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006143
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006144 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6145 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006146 void Advance() { ++index_; }
6147
6148 private:
6149 const HGraph& graph_;
6150 size_t index_;
6151
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006152 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006153};
6154
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006155class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006156 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006157 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006158 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006159 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006160 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006161 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006162
6163 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006164 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006165 void Advance() { --index_; }
6166
6167 private:
6168 const HGraph& graph_;
6169 size_t index_;
6170
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006171 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006172};
6173
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006174class HLinearPostOrderIterator : public ValueObject {
6175 public:
6176 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006177 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006178
6179 bool Done() const { return index_ == 0; }
6180
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006181 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006182
6183 void Advance() {
6184 --index_;
6185 DCHECK_GE(index_, 0U);
6186 }
6187
6188 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006189 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006190 size_t index_;
6191
6192 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6193};
6194
6195class HLinearOrderIterator : public ValueObject {
6196 public:
6197 explicit HLinearOrderIterator(const HGraph& graph)
6198 : order_(graph.GetLinearOrder()), index_(0) {}
6199
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006200 bool Done() const { return index_ == order_.size(); }
6201 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006202 void Advance() { ++index_; }
6203
6204 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006205 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006206 size_t index_;
6207
6208 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6209};
6210
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006211// Iterator over the blocks that art part of the loop. Includes blocks part
6212// of an inner loop. The order in which the blocks are iterated is on their
6213// block id.
6214class HBlocksInLoopIterator : public ValueObject {
6215 public:
6216 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6217 : blocks_in_loop_(info.GetBlocks()),
6218 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6219 index_(0) {
6220 if (!blocks_in_loop_.IsBitSet(index_)) {
6221 Advance();
6222 }
6223 }
6224
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006225 bool Done() const { return index_ == blocks_.size(); }
6226 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006227 void Advance() {
6228 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006229 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006230 if (blocks_in_loop_.IsBitSet(index_)) {
6231 break;
6232 }
6233 }
6234 }
6235
6236 private:
6237 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006238 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006239 size_t index_;
6240
6241 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6242};
6243
Mingyao Yang3584bce2015-05-19 16:01:59 -07006244// Iterator over the blocks that art part of the loop. Includes blocks part
6245// of an inner loop. The order in which the blocks are iterated is reverse
6246// post order.
6247class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6248 public:
6249 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6250 : blocks_in_loop_(info.GetBlocks()),
6251 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6252 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006253 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006254 Advance();
6255 }
6256 }
6257
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006258 bool Done() const { return index_ == blocks_.size(); }
6259 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006260 void Advance() {
6261 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006262 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6263 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006264 break;
6265 }
6266 }
6267 }
6268
6269 private:
6270 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006271 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006272 size_t index_;
6273
6274 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6275};
6276
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006277inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006278 if (constant->IsIntConstant()) {
6279 return constant->AsIntConstant()->GetValue();
6280 } else if (constant->IsLongConstant()) {
6281 return constant->AsLongConstant()->GetValue();
6282 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006283 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006284 return 0;
6285 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006286}
6287
Vladimir Marko58155012015-08-19 12:49:41 +00006288inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6289 // For the purposes of the compiler, the dex files must actually be the same object
6290 // if we want to safely treat them as the same. This is especially important for JIT
6291 // as custom class loaders can open the same underlying file (or memory) multiple
6292 // times and provide different class resolution but no two class loaders should ever
6293 // use the same DexFile object - doing so is an unsupported hack that can lead to
6294 // all sorts of weird failures.
6295 return &lhs == &rhs;
6296}
6297
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006298#define INSTRUCTION_TYPE_CHECK(type, super) \
6299 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6300 inline const H##type* HInstruction::As##type() const { \
6301 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6302 } \
6303 inline H##type* HInstruction::As##type() { \
6304 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6305 }
6306
6307 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6308#undef INSTRUCTION_TYPE_CHECK
6309
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00006310class SwitchTable : public ValueObject {
6311 public:
6312 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
6313 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
6314 int32_t table_offset = instruction.VRegB_31t();
6315 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
6316 if (sparse) {
6317 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
6318 } else {
6319 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
6320 }
6321 num_entries_ = table[1];
6322 values_ = reinterpret_cast<const int32_t*>(&table[2]);
6323 }
6324
6325 uint16_t GetNumEntries() const {
6326 return num_entries_;
6327 }
6328
6329 void CheckIndex(size_t index) const {
6330 if (sparse_) {
6331 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6332 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
6333 } else {
6334 // In a packed table, we have the starting key and num_entries_ values.
6335 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
6336 }
6337 }
6338
6339 int32_t GetEntryAt(size_t index) const {
6340 CheckIndex(index);
6341 return values_[index];
6342 }
6343
6344 uint32_t GetDexPcForIndex(size_t index) const {
6345 CheckIndex(index);
6346 return dex_pc_ +
6347 (reinterpret_cast<const int16_t*>(values_ + index) -
6348 reinterpret_cast<const int16_t*>(&instruction_));
6349 }
6350
6351 // Index of the first value in the table.
6352 size_t GetFirstValueIndex() const {
6353 if (sparse_) {
6354 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6355 return num_entries_;
6356 } else {
6357 // In a packed table, we have the starting key and num_entries_ values.
6358 return 1;
6359 }
6360 }
6361
6362 private:
6363 const Instruction& instruction_;
6364 const uint32_t dex_pc_;
6365
6366 // Whether this is a sparse-switch table (or a packed-switch one).
6367 const bool sparse_;
6368
6369 // This can't be const as it needs to be computed off of the given instruction, and complicated
6370 // expressions in the initializer list seemed very ugly.
6371 uint16_t num_entries_;
6372
6373 const int32_t* values_;
6374
6375 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
6376};
6377
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006378// Create space in `blocks` for adding `number_of_new_blocks` entries
6379// starting at location `at`. Blocks after `at` are moved accordingly.
6380inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6381 size_t number_of_new_blocks,
6382 size_t after) {
6383 DCHECK_LT(after, blocks->size());
6384 size_t old_size = blocks->size();
6385 size_t new_size = old_size + number_of_new_blocks;
6386 blocks->resize(new_size);
6387 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6388}
6389
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006390} // namespace art
6391
6392#endif // ART_COMPILER_OPTIMIZING_NODES_H_