blob: 2b7d2dec8a885f1f339505008994a46838701b54 [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
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001250/*
1251 * Instructions, shared across several (not all) architectures.
1252 */
1253#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1254#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1255#else
1256#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
1257 M(MultiplyAccumulate, Instruction)
1258#endif
1259
Vladimir Markob4536b72015-11-24 13:45:23 +00001260#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001261#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001262#else
1263#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1264 M(ArmDexCacheArraysBase, Instruction)
1265#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001266
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001267#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001268#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001269#else
1270#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Alexandre Rames8626b742015-11-25 16:28:08 +00001271 M(Arm64DataProcWithShifterOp, Instruction) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001272 M(Arm64IntermediateAddress, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001273#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001274
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001275#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
1276
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001277#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1278
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001279#ifndef ART_ENABLE_CODEGEN_x86
1280#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1281#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001282#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1283 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001284 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001285 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001286 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001287#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001288
1289#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1290
1291#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1292 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001293 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001294 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1295 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001296 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001297 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001298 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1299 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1300
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001301#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1302 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001303 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001304 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001305 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001306 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001307
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001308#define FOR_EACH_INSTRUCTION(M) \
1309 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1310 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1311
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001312#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001313FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1314#undef FORWARD_DECLARATION
1315
Roland Levillainccc07a92014-09-16 14:48:16 +01001316#define DECLARE_INSTRUCTION(type) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001317 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001318 const char* DebugName() const OVERRIDE { return #type; } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001319 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +01001320 return other->Is##type(); \
1321 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001322 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001323
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001324#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1325 bool Is##type() const { return As##type() != nullptr; } \
1326 const H##type* As##type() const { return this; } \
1327 H##type* As##type() { return this; }
1328
David Brazdiled596192015-01-23 10:39:45 +00001329template <typename T> class HUseList;
1330
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001331template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001332class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001333 public:
David Brazdiled596192015-01-23 10:39:45 +00001334 HUseListNode* GetPrevious() const { return prev_; }
1335 HUseListNode* GetNext() const { return next_; }
1336 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001337 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001338 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001339
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001340 private:
David Brazdiled596192015-01-23 10:39:45 +00001341 HUseListNode(T user, size_t index)
1342 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
1343
1344 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001345 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001346 HUseListNode<T>* prev_;
1347 HUseListNode<T>* next_;
1348
1349 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001350
1351 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1352};
1353
David Brazdiled596192015-01-23 10:39:45 +00001354template <typename T>
1355class HUseList : public ValueObject {
1356 public:
1357 HUseList() : first_(nullptr) {}
1358
1359 void Clear() {
1360 first_ = nullptr;
1361 }
1362
1363 // Adds a new entry at the beginning of the use list and returns
1364 // the newly created node.
1365 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +00001366 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +00001367 if (IsEmpty()) {
1368 first_ = new_node;
1369 } else {
1370 first_->prev_ = new_node;
1371 new_node->next_ = first_;
1372 first_ = new_node;
1373 }
1374 return new_node;
1375 }
1376
1377 HUseListNode<T>* GetFirst() const {
1378 return first_;
1379 }
1380
1381 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001382 DCHECK(node != nullptr);
1383 DCHECK(Contains(node));
1384
David Brazdiled596192015-01-23 10:39:45 +00001385 if (node->prev_ != nullptr) {
1386 node->prev_->next_ = node->next_;
1387 }
1388 if (node->next_ != nullptr) {
1389 node->next_->prev_ = node->prev_;
1390 }
1391 if (node == first_) {
1392 first_ = node->next_;
1393 }
1394 }
1395
David Brazdil1abb4192015-02-17 18:33:36 +00001396 bool Contains(const HUseListNode<T>* node) const {
1397 if (node == nullptr) {
1398 return false;
1399 }
1400 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1401 if (current == node) {
1402 return true;
1403 }
1404 }
1405 return false;
1406 }
1407
David Brazdiled596192015-01-23 10:39:45 +00001408 bool IsEmpty() const {
1409 return first_ == nullptr;
1410 }
1411
1412 bool HasOnlyOneUse() const {
1413 return first_ != nullptr && first_->next_ == nullptr;
1414 }
1415
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001416 size_t SizeSlow() const {
1417 size_t count = 0;
1418 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
1419 ++count;
1420 }
1421 return count;
1422 }
1423
David Brazdiled596192015-01-23 10:39:45 +00001424 private:
1425 HUseListNode<T>* first_;
1426};
1427
1428template<typename T>
1429class HUseIterator : public ValueObject {
1430 public:
1431 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
1432
1433 bool Done() const { return current_ == nullptr; }
1434
1435 void Advance() {
1436 DCHECK(!Done());
1437 current_ = current_->GetNext();
1438 }
1439
1440 HUseListNode<T>* Current() const {
1441 DCHECK(!Done());
1442 return current_;
1443 }
1444
1445 private:
1446 HUseListNode<T>* current_;
1447
1448 friend class HValue;
1449};
1450
David Brazdil1abb4192015-02-17 18:33:36 +00001451// This class is used by HEnvironment and HInstruction classes to record the
1452// instructions they use and pointers to the corresponding HUseListNodes kept
1453// by the used instructions.
1454template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001455class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001456 public:
1457 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1458 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1459
1460 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1461 : instruction_(old_record.instruction_), use_node_(use_node) {
1462 DCHECK(instruction_ != nullptr);
1463 DCHECK(use_node_ != nullptr);
1464 DCHECK(old_record.use_node_ == nullptr);
1465 }
1466
1467 HInstruction* GetInstruction() const { return instruction_; }
1468 HUseListNode<T>* GetUseNode() const { return use_node_; }
1469
1470 private:
1471 // Instruction used by the user.
1472 HInstruction* instruction_;
1473
1474 // Corresponding entry in the use list kept by 'instruction_'.
1475 HUseListNode<T>* use_node_;
1476};
1477
Aart Bik854a02b2015-07-14 16:07:00 -07001478/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001479 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001480 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001481 * For write/read dependences on fields/arrays, the dependence analysis uses
1482 * type disambiguation (e.g. a float field write cannot modify the value of an
1483 * integer field read) and the access type (e.g. a reference array write cannot
1484 * modify the value of a reference field read [although it may modify the
1485 * reference fetch prior to reading the field, which is represented by its own
1486 * write/read dependence]). The analysis makes conservative points-to
1487 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1488 * the same, and any reference read depends on any reference read without
1489 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001490 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001491 * The internal representation uses 38-bit and is described in the table below.
1492 * The first line indicates the side effect, and for field/array accesses the
1493 * second line indicates the type of the access (in the order of the
1494 * Primitive::Type enum).
1495 * The two numbered lines below indicate the bit position in the bitfield (read
1496 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001497 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001498 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1499 * +-------------+---------+---------+--------------+---------+---------+
1500 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1501 * | 3 |333333322|222222221| 1 |111111110|000000000|
1502 * | 7 |654321098|765432109| 8 |765432109|876543210|
1503 *
1504 * Note that, to ease the implementation, 'changes' bits are least significant
1505 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001506 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001507class SideEffects : public ValueObject {
1508 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001509 SideEffects() : flags_(0) {}
1510
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001511 static SideEffects None() {
1512 return SideEffects(0);
1513 }
1514
1515 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001516 return SideEffects(kAllChangeBits | kAllDependOnBits);
1517 }
1518
1519 static SideEffects AllChanges() {
1520 return SideEffects(kAllChangeBits);
1521 }
1522
1523 static SideEffects AllDependencies() {
1524 return SideEffects(kAllDependOnBits);
1525 }
1526
1527 static SideEffects AllExceptGCDependency() {
1528 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1529 }
1530
1531 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001532 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001533 }
1534
Aart Bik34c3ba92015-07-20 14:08:59 -07001535 static SideEffects AllWrites() {
1536 return SideEffects(kAllWrites);
1537 }
1538
1539 static SideEffects AllReads() {
1540 return SideEffects(kAllReads);
1541 }
1542
1543 static SideEffects FieldWriteOfType(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, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001547 }
1548
Aart Bik854a02b2015-07-14 16:07:00 -07001549 static SideEffects ArrayWriteOfType(Primitive::Type type) {
1550 return SideEffects(TypeFlagWithAlias(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001551 }
1552
Aart Bik34c3ba92015-07-20 14:08:59 -07001553 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1554 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001555 ? AllWritesAndReads()
Aart Bik34c3ba92015-07-20 14:08:59 -07001556 : SideEffects(TypeFlagWithAlias(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001557 }
1558
1559 static SideEffects ArrayReadOfType(Primitive::Type type) {
1560 return SideEffects(TypeFlagWithAlias(type, kArrayReadOffset));
1561 }
1562
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001563 static SideEffects CanTriggerGC() {
1564 return SideEffects(1ULL << kCanTriggerGCBit);
1565 }
1566
1567 static SideEffects DependsOnGC() {
1568 return SideEffects(1ULL << kDependsOnGCBit);
1569 }
1570
Aart Bik854a02b2015-07-14 16:07:00 -07001571 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001572 SideEffects Union(SideEffects other) const {
1573 return SideEffects(flags_ | other.flags_);
1574 }
1575
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001576 SideEffects Exclusion(SideEffects other) const {
1577 return SideEffects(flags_ & ~other.flags_);
1578 }
1579
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001580 void Add(SideEffects other) {
1581 flags_ |= other.flags_;
1582 }
1583
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001584 bool Includes(SideEffects other) const {
1585 return (other.flags_ & flags_) == other.flags_;
1586 }
1587
1588 bool HasSideEffects() const {
1589 return (flags_ & kAllChangeBits);
1590 }
1591
1592 bool HasDependencies() const {
1593 return (flags_ & kAllDependOnBits);
1594 }
1595
1596 // Returns true if there are no side effects or dependencies.
1597 bool DoesNothing() const {
1598 return flags_ == 0;
1599 }
1600
Aart Bik854a02b2015-07-14 16:07:00 -07001601 // Returns true if something is written.
1602 bool DoesAnyWrite() const {
1603 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001604 }
1605
Aart Bik854a02b2015-07-14 16:07:00 -07001606 // Returns true if something is read.
1607 bool DoesAnyRead() const {
1608 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001609 }
1610
Aart Bik854a02b2015-07-14 16:07:00 -07001611 // Returns true if potentially everything is written and read
1612 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001613 bool DoesAllReadWrite() const {
1614 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1615 }
1616
Aart Bik854a02b2015-07-14 16:07:00 -07001617 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001618 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001619 }
1620
Roland Levillain0d5a2812015-11-13 10:07:31 +00001621 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001622 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001623 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1624 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001625 }
1626
1627 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001628 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001629 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001630 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001631 for (int s = kLastBit; s >= 0; s--) {
1632 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1633 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1634 // This is a bit for the GC side effect.
1635 if (current_bit_is_set) {
1636 flags += "GC";
1637 }
Aart Bik854a02b2015-07-14 16:07:00 -07001638 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001639 } else {
1640 // This is a bit for the array/field analysis.
1641 // The underscore character stands for the 'can trigger GC' bit.
1642 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1643 if (current_bit_is_set) {
1644 flags += kDebug[s];
1645 }
1646 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1647 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1648 flags += "|";
1649 }
1650 }
Aart Bik854a02b2015-07-14 16:07:00 -07001651 }
1652 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001653 }
1654
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001655 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001656
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001657 private:
1658 static constexpr int kFieldArrayAnalysisBits = 9;
1659
1660 static constexpr int kFieldWriteOffset = 0;
1661 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1662 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1663 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1664
1665 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1666
1667 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1668 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1669 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1670 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1671
1672 static constexpr int kLastBit = kDependsOnGCBit;
1673 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1674
1675 // Aliases.
1676
1677 static_assert(kChangeBits == kDependOnBits,
1678 "the 'change' bits should match the 'depend on' bits.");
1679
1680 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1681 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1682 static constexpr uint64_t kAllWrites =
1683 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1684 static constexpr uint64_t kAllReads =
1685 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001686
Aart Bik854a02b2015-07-14 16:07:00 -07001687 // Work around the fact that HIR aliases I/F and J/D.
1688 // TODO: remove this interceptor once HIR types are clean
1689 static uint64_t TypeFlagWithAlias(Primitive::Type type, int offset) {
1690 switch (type) {
1691 case Primitive::kPrimInt:
1692 case Primitive::kPrimFloat:
1693 return TypeFlag(Primitive::kPrimInt, offset) |
1694 TypeFlag(Primitive::kPrimFloat, offset);
1695 case Primitive::kPrimLong:
1696 case Primitive::kPrimDouble:
1697 return TypeFlag(Primitive::kPrimLong, offset) |
1698 TypeFlag(Primitive::kPrimDouble, offset);
1699 default:
1700 return TypeFlag(type, offset);
1701 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001702 }
1703
Aart Bik854a02b2015-07-14 16:07:00 -07001704 // Translates type to bit flag.
1705 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1706 CHECK_NE(type, Primitive::kPrimVoid);
1707 const uint64_t one = 1;
1708 const int shift = type; // 0-based consecutive enum
1709 DCHECK_LE(kFieldWriteOffset, shift);
1710 DCHECK_LT(shift, kArrayWriteOffset);
1711 return one << (type + offset);
1712 }
1713
1714 // Private constructor on direct flags value.
1715 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1716
1717 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001718};
1719
David Brazdiled596192015-01-23 10:39:45 +00001720// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001721class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001722 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001723 HEnvironment(ArenaAllocator* arena,
1724 size_t number_of_vregs,
1725 const DexFile& dex_file,
1726 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001727 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001728 InvokeType invoke_type,
1729 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001730 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1731 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001732 parent_(nullptr),
1733 dex_file_(dex_file),
1734 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001735 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001736 invoke_type_(invoke_type),
1737 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001738 }
1739
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001740 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001741 : HEnvironment(arena,
1742 to_copy.Size(),
1743 to_copy.GetDexFile(),
1744 to_copy.GetMethodIdx(),
1745 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001746 to_copy.GetInvokeType(),
1747 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001748
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001749 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001750 if (parent_ != nullptr) {
1751 parent_->SetAndCopyParentChain(allocator, parent);
1752 } else {
1753 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1754 parent_->CopyFrom(parent);
1755 if (parent->GetParent() != nullptr) {
1756 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1757 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001758 }
David Brazdiled596192015-01-23 10:39:45 +00001759 }
1760
Vladimir Marko71bf8092015-09-15 15:33:14 +01001761 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001762 void CopyFrom(HEnvironment* environment);
1763
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001764 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1765 // input to the loop phi instead. This is for inserting instructions that
1766 // require an environment (like HDeoptimization) in the loop pre-header.
1767 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001768
1769 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001770 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001771 }
1772
1773 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001774 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001775 }
1776
David Brazdil1abb4192015-02-17 18:33:36 +00001777 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001778
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001779 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001780
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001781 HEnvironment* GetParent() const { return parent_; }
1782
1783 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001784 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001785 }
1786
1787 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001788 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001789 }
1790
1791 uint32_t GetDexPc() const {
1792 return dex_pc_;
1793 }
1794
1795 uint32_t GetMethodIdx() const {
1796 return method_idx_;
1797 }
1798
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001799 InvokeType GetInvokeType() const {
1800 return invoke_type_;
1801 }
1802
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001803 const DexFile& GetDexFile() const {
1804 return dex_file_;
1805 }
1806
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001807 HInstruction* GetHolder() const {
1808 return holder_;
1809 }
1810
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001811
1812 bool IsFromInlinedInvoke() const {
1813 return GetParent() != nullptr;
1814 }
1815
David Brazdiled596192015-01-23 10:39:45 +00001816 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001817 // Record instructions' use entries of this environment for constant-time removal.
1818 // It should only be called by HInstruction when a new environment use is added.
1819 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1820 DCHECK(env_use->GetUser() == this);
1821 size_t index = env_use->GetIndex();
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001822 vregs_[index] = HUserRecord<HEnvironment*>(vregs_[index], env_use);
David Brazdil1abb4192015-02-17 18:33:36 +00001823 }
David Brazdiled596192015-01-23 10:39:45 +00001824
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001825 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1826 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001827 HEnvironment* parent_;
1828 const DexFile& dex_file_;
1829 const uint32_t method_idx_;
1830 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001831 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001832
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001833 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001834 HInstruction* const holder_;
1835
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001836 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001837
1838 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1839};
1840
Vladimir Markof9f64412015-09-02 14:05:49 +01001841class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001842 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001843 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001844 : previous_(nullptr),
1845 next_(nullptr),
1846 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001847 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001848 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001849 ssa_index_(-1),
David Brazdilb3e773e2016-01-26 11:28:37 +00001850 emitted_at_use_site_(false),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001851 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001852 locations_(nullptr),
1853 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001854 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001855 side_effects_(side_effects),
Calin Juravle2e768302015-07-28 14:41:11 +00001856 reference_type_info_(ReferenceTypeInfo::CreateInvalid()) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001857
Dave Allison20dfc792014-06-16 20:44:29 -07001858 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001859
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001860#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001861 enum InstructionKind {
1862 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1863 };
1864#undef DECLARE_KIND
1865
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001866 HInstruction* GetNext() const { return next_; }
1867 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001868
Calin Juravle77520bc2015-01-12 18:45:46 +00001869 HInstruction* GetNextDisregardingMoves() const;
1870 HInstruction* GetPreviousDisregardingMoves() const;
1871
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001872 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001873 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001874 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001875 bool IsInBlock() const { return block_ != nullptr; }
1876 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001877 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1878 bool IsIrreducibleLoopHeaderPhi() const {
1879 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1880 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001881
Roland Levillain6b879dd2014-09-22 17:13:44 +01001882 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001883 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001884
1885 virtual void Accept(HGraphVisitor* visitor) = 0;
1886 virtual const char* DebugName() const = 0;
1887
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001888 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001889 void SetRawInputAt(size_t index, HInstruction* input) {
1890 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1891 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001892
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001893 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001894
1895 uint32_t GetDexPc() const { return dex_pc_; }
1896
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001897 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001898
Roland Levillaine161a2a2014-10-03 12:45:18 +01001899 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001900 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001901
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001902 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001903 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001904
Calin Juravle10e244f2015-01-26 18:54:32 +00001905 // Does not apply for all instructions, but having this at top level greatly
1906 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001907 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001908 virtual bool CanBeNull() const {
1909 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1910 return true;
1911 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001912
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001913 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001914 return false;
1915 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001916
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001917 virtual bool IsActualObject() const {
1918 return GetType() == Primitive::kPrimNot;
1919 }
1920
Calin Juravle2e768302015-07-28 14:41:11 +00001921 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001922
Calin Juravle61d544b2015-02-23 16:46:57 +00001923 ReferenceTypeInfo GetReferenceTypeInfo() const {
1924 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1925 return reference_type_info_;
1926 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001927
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001928 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001929 DCHECK(user != nullptr);
1930 HUseListNode<HInstruction*>* use =
1931 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1932 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001933 }
1934
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001935 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001936 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001937 HUseListNode<HEnvironment*>* env_use =
1938 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1939 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001940 }
1941
David Brazdil1abb4192015-02-17 18:33:36 +00001942 void RemoveAsUserOfInput(size_t input) {
1943 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1944 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1945 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001946
David Brazdil1abb4192015-02-17 18:33:36 +00001947 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1948 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001949
David Brazdiled596192015-01-23 10:39:45 +00001950 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1951 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001952 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001953 bool HasOnlyOneNonEnvironmentUse() const {
1954 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1955 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001956
Roland Levillain6c82d402014-10-13 16:10:27 +01001957 // Does this instruction strictly dominate `other_instruction`?
1958 // Returns false if this instruction and `other_instruction` are the same.
1959 // Aborts if this instruction and `other_instruction` are both phis.
1960 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001961
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001962 int GetId() const { return id_; }
1963 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001964
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001965 int GetSsaIndex() const { return ssa_index_; }
1966 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1967 bool HasSsaIndex() const { return ssa_index_ != -1; }
1968
1969 bool HasEnvironment() const { return environment_ != nullptr; }
1970 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001971 // Set the `environment_` field. Raw because this method does not
1972 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001973 void SetRawEnvironment(HEnvironment* environment) {
1974 DCHECK(environment_ == nullptr);
1975 DCHECK_EQ(environment->GetHolder(), this);
1976 environment_ = environment;
1977 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001978
1979 // Set the environment of this instruction, copying it from `environment`. While
1980 // copying, the uses lists are being updated.
1981 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001982 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001983 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001984 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001985 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001986 if (environment->GetParent() != nullptr) {
1987 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1988 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001989 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001990
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001991 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1992 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001993 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001994 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001995 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001996 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001997 if (environment->GetParent() != nullptr) {
1998 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1999 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002000 }
2001
Nicolas Geoffray39468442014-09-02 15:17:15 +01002002 // Returns the number of entries in the environment. Typically, that is the
2003 // number of dex registers in a method. It could be more in case of inlining.
2004 size_t EnvironmentSize() const;
2005
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002006 LocationSummary* GetLocations() const { return locations_; }
2007 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002008
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002009 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002010 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002011
Alexandre Rames188d4312015-04-09 18:30:21 +01002012 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2013 // uses of this instruction by `other` are *not* updated.
2014 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2015 ReplaceWith(other);
2016 other->ReplaceInput(this, use_index);
2017 }
2018
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002019 // Move `this` instruction before `cursor`.
2020 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002021
Vladimir Markofb337ea2015-11-25 15:25:10 +00002022 // Move `this` before its first user and out of any loops. If there is no
2023 // out-of-loop user that dominates all other users, move the instruction
2024 // to the end of the out-of-loop common dominator of the user's blocks.
2025 //
2026 // This can be used only on non-throwing instructions with no side effects that
2027 // have at least one use but no environment uses.
2028 void MoveBeforeFirstUserAndOutOfLoops();
2029
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002030#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002031 bool Is##type() const; \
2032 const H##type* As##type() const; \
2033 H##type* As##type();
2034
2035 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2036#undef INSTRUCTION_TYPE_CHECK
2037
2038#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002039 bool Is##type() const { return (As##type() != nullptr); } \
2040 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002041 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002042 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002043#undef INSTRUCTION_TYPE_CHECK
2044
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002045 // Returns whether the instruction can be moved within the graph.
2046 virtual bool CanBeMoved() const { return false; }
2047
2048 // Returns whether the two instructions are of the same kind.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002049 virtual bool InstructionTypeEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002050 return false;
2051 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002052
2053 // Returns whether any data encoded in the two instructions is equal.
2054 // This method does not look at the inputs. Both instructions must be
2055 // of the same type, otherwise the method has undefined behavior.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002056 virtual bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002057 return false;
2058 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002059
2060 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002061 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002062 // 2) Their inputs are identical.
2063 bool Equals(HInstruction* other) const;
2064
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002065 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2066 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2067 // the virtual function because the __attribute__((__pure__)) doesn't really
2068 // apply the strong requirement for virtual functions, preventing optimizations.
2069 InstructionKind GetKind() const PURE;
2070 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002071
2072 virtual size_t ComputeHashCode() const {
2073 size_t result = GetKind();
2074 for (size_t i = 0, e = InputCount(); i < e; ++i) {
2075 result = (result * 31) + InputAt(i)->GetId();
2076 }
2077 return result;
2078 }
2079
2080 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002081 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002082 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002083
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002084 size_t GetLifetimePosition() const { return lifetime_position_; }
2085 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2086 LiveInterval* GetLiveInterval() const { return live_interval_; }
2087 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2088 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2089
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002090 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2091
2092 // Returns whether the code generation of the instruction will require to have access
2093 // to the current method. Such instructions are:
2094 // (1): Instructions that require an environment, as calling the runtime requires
2095 // to walk the stack and have the current method stored at a specific stack address.
2096 // (2): Object literals like classes and strings, that are loaded from the dex cache
2097 // fields of the current method.
2098 bool NeedsCurrentMethod() const {
2099 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
2100 }
2101
Vladimir Markodc151b22015-10-15 18:02:30 +01002102 // Returns whether the code generation of the instruction will require to have access
2103 // to the dex cache of the current method's declaring class via the current method.
2104 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002105
Mark Mendellc4701932015-04-10 13:18:51 -04002106 // Does this instruction have any use in an environment before
2107 // control flow hits 'other'?
2108 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2109
2110 // Remove all references to environment uses of this instruction.
2111 // The caller must ensure that this is safe to do.
2112 void RemoveEnvironmentUsers();
2113
David Brazdilb3e773e2016-01-26 11:28:37 +00002114 bool IsEmittedAtUseSite() const { return emitted_at_use_site_; }
2115 void MarkEmittedAtUseSite() { emitted_at_use_site_ = true; }
2116
David Brazdil1abb4192015-02-17 18:33:36 +00002117 protected:
2118 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
2119 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
2120
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002121 private:
David Brazdil1abb4192015-02-17 18:33:36 +00002122 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
2123
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002124 HInstruction* previous_;
2125 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002126 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002127 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002128
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002129 // An instruction gets an id when it is added to the graph.
2130 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002131 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002132 int id_;
2133
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002134 // When doing liveness analysis, instructions that have uses get an SSA index.
2135 int ssa_index_;
2136
David Brazdilb3e773e2016-01-26 11:28:37 +00002137 // If set, the machine code for this instruction is assumed to be generated by
2138 // its users. Used by liveness analysis to compute use positions accordingly.
2139 bool emitted_at_use_site_;
2140
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002141 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002142 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002143
2144 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002145 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002146
Nicolas Geoffray39468442014-09-02 15:17:15 +01002147 // The environment associated with this instruction. Not null if the instruction
2148 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002149 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002150
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002151 // Set by the code generator.
2152 LocationSummary* locations_;
2153
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002154 // Set by the liveness analysis.
2155 LiveInterval* live_interval_;
2156
2157 // Set by the liveness analysis, this is the position in a linear
2158 // order of blocks where this instruction's live interval start.
2159 size_t lifetime_position_;
2160
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002161 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002162
Calin Juravleacf735c2015-02-12 15:25:22 +00002163 // TODO: for primitive types this should be marked as invalid.
2164 ReferenceTypeInfo reference_type_info_;
2165
David Brazdil1abb4192015-02-17 18:33:36 +00002166 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002167 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002168 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002169 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002170 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002171
2172 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2173};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002174std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002175
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002176class HInputIterator : public ValueObject {
2177 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002178 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002179
2180 bool Done() const { return index_ == instruction_->InputCount(); }
2181 HInstruction* Current() const { return instruction_->InputAt(index_); }
2182 void Advance() { index_++; }
2183
2184 private:
2185 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002186 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002187
2188 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
2189};
2190
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002191class HInstructionIterator : public ValueObject {
2192 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002193 explicit HInstructionIterator(const HInstructionList& instructions)
2194 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002195 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002196 }
2197
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002198 bool Done() const { return instruction_ == nullptr; }
2199 HInstruction* Current() const { return instruction_; }
2200 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002201 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002202 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002203 }
2204
2205 private:
2206 HInstruction* instruction_;
2207 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002208
2209 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002210};
2211
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002212class HBackwardInstructionIterator : public ValueObject {
2213 public:
2214 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2215 : instruction_(instructions.last_instruction_) {
2216 next_ = Done() ? nullptr : instruction_->GetPrevious();
2217 }
2218
2219 bool Done() const { return instruction_ == nullptr; }
2220 HInstruction* Current() const { return instruction_; }
2221 void Advance() {
2222 instruction_ = next_;
2223 next_ = Done() ? nullptr : instruction_->GetPrevious();
2224 }
2225
2226 private:
2227 HInstruction* instruction_;
2228 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002229
2230 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002231};
2232
Vladimir Markof9f64412015-09-02 14:05:49 +01002233template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002234class HTemplateInstruction: public HInstruction {
2235 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002236 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002237 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002238 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002239
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002240 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002241
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002242 protected:
Vladimir Markof9f64412015-09-02 14:05:49 +01002243 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2244 DCHECK_LT(i, N);
2245 return inputs_[i];
2246 }
David Brazdil1abb4192015-02-17 18:33:36 +00002247
2248 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markof9f64412015-09-02 14:05:49 +01002249 DCHECK_LT(i, N);
David Brazdil1abb4192015-02-17 18:33:36 +00002250 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002251 }
2252
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002253 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002254 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002255
2256 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002257};
2258
Vladimir Markof9f64412015-09-02 14:05:49 +01002259// HTemplateInstruction specialization for N=0.
2260template<>
2261class HTemplateInstruction<0>: public HInstruction {
2262 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002263 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002264 : HInstruction(side_effects, dex_pc) {}
2265
Vladimir Markof9f64412015-09-02 14:05:49 +01002266 virtual ~HTemplateInstruction() {}
2267
2268 size_t InputCount() const OVERRIDE { return 0; }
2269
2270 protected:
2271 const HUserRecord<HInstruction*> InputRecordAt(size_t i ATTRIBUTE_UNUSED) const OVERRIDE {
2272 LOG(FATAL) << "Unreachable";
2273 UNREACHABLE();
2274 }
2275
2276 void SetRawInputRecordAt(size_t i ATTRIBUTE_UNUSED,
2277 const HUserRecord<HInstruction*>& input ATTRIBUTE_UNUSED) OVERRIDE {
2278 LOG(FATAL) << "Unreachable";
2279 UNREACHABLE();
2280 }
2281
2282 private:
2283 friend class SsaBuilder;
2284};
2285
Dave Allison20dfc792014-06-16 20:44:29 -07002286template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002287class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002288 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002289 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002290 : HTemplateInstruction<N>(side_effects, dex_pc), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002291 virtual ~HExpression() {}
2292
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002293 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07002294
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002295 protected:
2296 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07002297};
2298
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002299// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2300// instruction that branches to the exit block.
2301class HReturnVoid : public HTemplateInstruction<0> {
2302 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002303 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2304 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002305
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002306 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002307
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002308 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002309
2310 private:
2311 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2312};
2313
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002314// Represents dex's RETURN opcodes. A HReturn is a control flow
2315// instruction that branches to the exit block.
2316class HReturn : public HTemplateInstruction<1> {
2317 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002318 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2319 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002320 SetRawInputAt(0, value);
2321 }
2322
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002323 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002324
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002325 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002326
2327 private:
2328 DISALLOW_COPY_AND_ASSIGN(HReturn);
2329};
2330
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002331// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002332// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002333// exit block.
2334class HExit : public HTemplateInstruction<0> {
2335 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002336 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002337
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002338 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002339
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002340 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002341
2342 private:
2343 DISALLOW_COPY_AND_ASSIGN(HExit);
2344};
2345
2346// Jumps from one block to another.
2347class HGoto : public HTemplateInstruction<0> {
2348 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002349 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002350
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002351 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002352
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002353 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002354 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002355 }
2356
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002357 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002358
2359 private:
2360 DISALLOW_COPY_AND_ASSIGN(HGoto);
2361};
2362
Roland Levillain9867bc72015-08-05 10:21:34 +01002363class HConstant : public HExpression<0> {
2364 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002365 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2366 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002367
2368 bool CanBeMoved() const OVERRIDE { return true; }
2369
2370 virtual bool IsMinusOne() const { return false; }
2371 virtual bool IsZero() const { return false; }
2372 virtual bool IsOne() const { return false; }
2373
David Brazdil77a48ae2015-09-15 12:34:04 +00002374 virtual uint64_t GetValueAsUint64() const = 0;
2375
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002376 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002377
2378 private:
2379 DISALLOW_COPY_AND_ASSIGN(HConstant);
2380};
2381
2382class HNullConstant : public HConstant {
2383 public:
2384 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2385 return true;
2386 }
2387
David Brazdil77a48ae2015-09-15 12:34:04 +00002388 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2389
Roland Levillain9867bc72015-08-05 10:21:34 +01002390 size_t ComputeHashCode() const OVERRIDE { return 0; }
2391
2392 DECLARE_INSTRUCTION(NullConstant);
2393
2394 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002395 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002396
2397 friend class HGraph;
2398 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2399};
2400
2401// Constants of the type int. Those can be from Dex instructions, or
2402// synthesized (for example with the if-eqz instruction).
2403class HIntConstant : public HConstant {
2404 public:
2405 int32_t GetValue() const { return value_; }
2406
David Brazdil9f389d42015-10-01 14:32:56 +01002407 uint64_t GetValueAsUint64() const OVERRIDE {
2408 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2409 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002410
Roland Levillain9867bc72015-08-05 10:21:34 +01002411 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002412 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002413 return other->AsIntConstant()->value_ == value_;
2414 }
2415
2416 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2417
2418 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2419 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2420 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2421
2422 DECLARE_INSTRUCTION(IntConstant);
2423
2424 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002425 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2426 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2427 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2428 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002429
2430 const int32_t value_;
2431
2432 friend class HGraph;
2433 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2434 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2435 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2436};
2437
2438class HLongConstant : public HConstant {
2439 public:
2440 int64_t GetValue() const { return value_; }
2441
David Brazdil77a48ae2015-09-15 12:34:04 +00002442 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2443
Roland Levillain9867bc72015-08-05 10:21:34 +01002444 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002445 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002446 return other->AsLongConstant()->value_ == value_;
2447 }
2448
2449 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2450
2451 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2452 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2453 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2454
2455 DECLARE_INSTRUCTION(LongConstant);
2456
2457 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002458 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2459 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002460
2461 const int64_t value_;
2462
2463 friend class HGraph;
2464 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2465};
Dave Allison20dfc792014-06-16 20:44:29 -07002466
Roland Levillain31dd3d62016-02-16 12:21:02 +00002467class HFloatConstant : public HConstant {
2468 public:
2469 float GetValue() const { return value_; }
2470
2471 uint64_t GetValueAsUint64() const OVERRIDE {
2472 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2473 }
2474
2475 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2476 DCHECK(other->IsFloatConstant()) << other->DebugName();
2477 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2478 }
2479
2480 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2481
2482 bool IsMinusOne() const OVERRIDE {
2483 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2484 }
2485 bool IsZero() const OVERRIDE {
2486 return value_ == 0.0f;
2487 }
2488 bool IsOne() const OVERRIDE {
2489 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2490 }
2491 bool IsNaN() const {
2492 return std::isnan(value_);
2493 }
2494
2495 DECLARE_INSTRUCTION(FloatConstant);
2496
2497 private:
2498 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2499 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2500 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2501 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2502
2503 const float value_;
2504
2505 // Only the SsaBuilder and HGraph can create floating-point constants.
2506 friend class SsaBuilder;
2507 friend class HGraph;
2508 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2509};
2510
2511class HDoubleConstant : public HConstant {
2512 public:
2513 double GetValue() const { return value_; }
2514
2515 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2516
2517 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2518 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2519 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2520 }
2521
2522 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2523
2524 bool IsMinusOne() const OVERRIDE {
2525 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2526 }
2527 bool IsZero() const OVERRIDE {
2528 return value_ == 0.0;
2529 }
2530 bool IsOne() const OVERRIDE {
2531 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2532 }
2533 bool IsNaN() const {
2534 return std::isnan(value_);
2535 }
2536
2537 DECLARE_INSTRUCTION(DoubleConstant);
2538
2539 private:
2540 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2541 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2542 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2543 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2544
2545 const double value_;
2546
2547 // Only the SsaBuilder and HGraph can create floating-point constants.
2548 friend class SsaBuilder;
2549 friend class HGraph;
2550 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2551};
2552
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002553// Conditional branch. A block ending with an HIf instruction must have
2554// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002555class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002556 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002557 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2558 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002559 SetRawInputAt(0, input);
2560 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002561
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002562 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002563
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002564 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002565 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002566 }
2567
2568 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002569 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002570 }
2571
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002572 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002573
2574 private:
2575 DISALLOW_COPY_AND_ASSIGN(HIf);
2576};
2577
David Brazdilfc6a86a2015-06-26 10:33:45 +00002578
2579// Abstract instruction which marks the beginning and/or end of a try block and
2580// links it to the respective exception handlers. Behaves the same as a Goto in
2581// non-exceptional control flow.
2582// Normal-flow successor is stored at index zero, exception handlers under
2583// higher indices in no particular order.
2584class HTryBoundary : public HTemplateInstruction<0> {
2585 public:
David Brazdil56e1acc2015-06-30 15:41:36 +01002586 enum BoundaryKind {
2587 kEntry,
2588 kExit,
2589 };
2590
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002591 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
2592 : HTemplateInstruction(SideEffects::None(), dex_pc), kind_(kind) {}
David Brazdilfc6a86a2015-06-26 10:33:45 +00002593
2594 bool IsControlFlow() const OVERRIDE { return true; }
2595
2596 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002597 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002598
David Brazdild26a4112015-11-10 11:07:31 +00002599 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2600 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2601 }
2602
David Brazdilfc6a86a2015-06-26 10:33:45 +00002603 // Returns whether `handler` is among its exception handlers (non-zero index
2604 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002605 bool HasExceptionHandler(const HBasicBlock& handler) const {
2606 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002607 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002608 }
2609
2610 // If not present already, adds `handler` to its block's list of exception
2611 // handlers.
2612 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002613 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002614 GetBlock()->AddSuccessor(handler);
2615 }
2616 }
2617
David Brazdil56e1acc2015-06-30 15:41:36 +01002618 bool IsEntry() const { return kind_ == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002619
David Brazdilffee3d32015-07-06 11:48:53 +01002620 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2621
David Brazdilfc6a86a2015-06-26 10:33:45 +00002622 DECLARE_INSTRUCTION(TryBoundary);
2623
2624 private:
David Brazdil56e1acc2015-06-30 15:41:36 +01002625 const BoundaryKind kind_;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002626
2627 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2628};
2629
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002630// Deoptimize to interpreter, upon checking a condition.
2631class HDeoptimize : public HTemplateInstruction<1> {
2632 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002633 // We set CanTriggerGC to prevent any intermediate address to be live
2634 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002635 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002636 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002637 SetRawInputAt(0, cond);
2638 }
2639
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002640 bool CanBeMoved() const OVERRIDE { return true; }
2641 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2642 return true;
2643 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002644 bool NeedsEnvironment() const OVERRIDE { return true; }
2645 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002646
2647 DECLARE_INSTRUCTION(Deoptimize);
2648
2649 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002650 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2651};
2652
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002653// Represents the ArtMethod that was passed as a first argument to
2654// the method. It is used by instructions that depend on it, like
2655// instructions that work with the dex cache.
2656class HCurrentMethod : public HExpression<0> {
2657 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002658 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2659 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002660
2661 DECLARE_INSTRUCTION(CurrentMethod);
2662
2663 private:
2664 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2665};
2666
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002667// Fetches an ArtMethod from the virtual table or the interface method table
2668// of a class.
2669class HClassTableGet : public HExpression<1> {
2670 public:
2671 enum TableKind {
2672 kVTable,
2673 kIMTable,
2674 };
2675 HClassTableGet(HInstruction* cls,
2676 Primitive::Type type,
2677 TableKind kind,
2678 size_t index,
2679 uint32_t dex_pc)
2680 : HExpression(type, SideEffects::None(), dex_pc),
2681 index_(index),
2682 table_kind_(kind) {
2683 SetRawInputAt(0, cls);
2684 }
2685
2686 bool CanBeMoved() const OVERRIDE { return true; }
2687 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2688 return other->AsClassTableGet()->GetIndex() == index_ &&
2689 other->AsClassTableGet()->GetTableKind() == table_kind_;
2690 }
2691
2692 TableKind GetTableKind() const { return table_kind_; }
2693 size_t GetIndex() const { return index_; }
2694
2695 DECLARE_INSTRUCTION(ClassTableGet);
2696
2697 private:
2698 // The index of the ArtMethod in the table.
2699 const size_t index_;
2700 const TableKind table_kind_;
2701
2702 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2703};
2704
Mark Mendellfe57faa2015-09-18 09:26:15 -04002705// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2706// have one successor for each entry in the switch table, and the final successor
2707// will be the block containing the next Dex opcode.
2708class HPackedSwitch : public HTemplateInstruction<1> {
2709 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002710 HPackedSwitch(int32_t start_value,
2711 uint32_t num_entries,
2712 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002713 uint32_t dex_pc = kNoDexPc)
2714 : HTemplateInstruction(SideEffects::None(), dex_pc),
2715 start_value_(start_value),
2716 num_entries_(num_entries) {
2717 SetRawInputAt(0, input);
2718 }
2719
2720 bool IsControlFlow() const OVERRIDE { return true; }
2721
2722 int32_t GetStartValue() const { return start_value_; }
2723
Vladimir Marko211c2112015-09-24 16:52:33 +01002724 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04002725
2726 HBasicBlock* GetDefaultBlock() const {
2727 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01002728 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04002729 }
2730 DECLARE_INSTRUCTION(PackedSwitch);
2731
2732 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002733 const int32_t start_value_;
2734 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04002735
2736 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
2737};
2738
Roland Levillain88cb1752014-10-20 16:36:47 +01002739class HUnaryOperation : public HExpression<1> {
2740 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002741 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
2742 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01002743 SetRawInputAt(0, input);
2744 }
2745
2746 HInstruction* GetInput() const { return InputAt(0); }
2747 Primitive::Type GetResultType() const { return GetType(); }
2748
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002749 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002750 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002751 return true;
2752 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002753
Roland Levillain31dd3d62016-02-16 12:21:02 +00002754 // Try to statically evaluate `this` and return a HConstant
2755 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002756 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002757 HConstant* TryStaticEvaluation() const;
2758
2759 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01002760 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
2761 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00002762 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
2763 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01002764
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002765 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01002766
2767 private:
2768 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
2769};
2770
Dave Allison20dfc792014-06-16 20:44:29 -07002771class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002772 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002773 HBinaryOperation(Primitive::Type result_type,
2774 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002775 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002776 SideEffects side_effects = SideEffects::None(),
2777 uint32_t dex_pc = kNoDexPc)
2778 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002779 SetRawInputAt(0, left);
2780 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002781 }
2782
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002783 HInstruction* GetLeft() const { return InputAt(0); }
2784 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07002785 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002786
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002787 virtual bool IsCommutative() const { return false; }
2788
2789 // Put constant on the right.
2790 // Returns whether order is changed.
2791 bool OrderInputsWithConstantOnTheRight() {
2792 HInstruction* left = InputAt(0);
2793 HInstruction* right = InputAt(1);
2794 if (left->IsConstant() && !right->IsConstant()) {
2795 ReplaceInput(right, 0);
2796 ReplaceInput(left, 1);
2797 return true;
2798 }
2799 return false;
2800 }
2801
2802 // Order inputs by instruction id, but favor constant on the right side.
2803 // This helps GVN for commutative ops.
2804 void OrderInputs() {
2805 DCHECK(IsCommutative());
2806 HInstruction* left = InputAt(0);
2807 HInstruction* right = InputAt(1);
2808 if (left == right || (!left->IsConstant() && right->IsConstant())) {
2809 return;
2810 }
2811 if (OrderInputsWithConstantOnTheRight()) {
2812 return;
2813 }
2814 // Order according to instruction id.
2815 if (left->GetId() > right->GetId()) {
2816 ReplaceInput(right, 0);
2817 ReplaceInput(left, 1);
2818 }
2819 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002820
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002821 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002822 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002823 return true;
2824 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002825
Roland Levillain31dd3d62016-02-16 12:21:02 +00002826 // Try to statically evaluate `this` and return a HConstant
2827 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002828 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01002829 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01002830
2831 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00002832 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2833 HNullConstant* y ATTRIBUTE_UNUSED) const {
2834 VLOG(compiler) << DebugName() << " is not defined for the (null, null) case.";
2835 return nullptr;
2836 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002837 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
2838 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
2839 virtual HConstant* Evaluate(HIntConstant* x ATTRIBUTE_UNUSED,
2840 HLongConstant* y ATTRIBUTE_UNUSED) const {
2841 VLOG(compiler) << DebugName() << " is not defined for the (int, long) case.";
2842 return nullptr;
2843 }
2844 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
2845 HIntConstant* y ATTRIBUTE_UNUSED) const {
2846 VLOG(compiler) << DebugName() << " is not defined for the (long, int) case.";
2847 return nullptr;
2848 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002849 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
2850 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01002851
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002852 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002853 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002854 HConstant* GetConstantRight() const;
2855
2856 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07002857 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002858 HInstruction* GetLeastConstantLeft() const;
2859
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002860 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01002861
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002862 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002863 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
2864};
2865
Mark Mendellc4701932015-04-10 13:18:51 -04002866// The comparison bias applies for floating point operations and indicates how NaN
2867// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002868enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04002869 kNoBias, // bias is not applicable (i.e. for long operation)
2870 kGtBias, // return 1 for NaN comparisons
2871 kLtBias, // return -1 for NaN comparisons
2872};
2873
Roland Levillain31dd3d62016-02-16 12:21:02 +00002874std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
2875
Dave Allison20dfc792014-06-16 20:44:29 -07002876class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002877 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002878 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2879 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc),
Roland Levillain4fa13f62015-07-06 18:11:54 +01002880 bias_(ComparisonBias::kNoBias) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002881
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002882 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002883 // `instruction`, and disregard moves in between.
2884 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002885
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002886 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07002887
2888 virtual IfCondition GetCondition() const = 0;
2889
Mark Mendellc4701932015-04-10 13:18:51 -04002890 virtual IfCondition GetOppositeCondition() const = 0;
2891
Roland Levillain4fa13f62015-07-06 18:11:54 +01002892 bool IsGtBias() const { return bias_ == ComparisonBias::kGtBias; }
Roland Levillain31dd3d62016-02-16 12:21:02 +00002893 ComparisonBias GetBias() const { return bias_; }
Mark Mendellc4701932015-04-10 13:18:51 -04002894 void SetBias(ComparisonBias bias) { bias_ = bias; }
2895
2896 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2897 return bias_ == other->AsCondition()->bias_;
2898 }
2899
Roland Levillain4fa13f62015-07-06 18:11:54 +01002900 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002901 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002902 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002903 return IsGtBias() ? ((if_cond == kCondGT) || (if_cond == kCondGE)) : (if_cond == kCondNE);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002904 }
2905
2906 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002907 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002908 IfCondition if_cond = GetCondition();
Nicolas Geoffrayd4aee942016-01-22 12:35:26 +00002909 return IsGtBias() ? ((if_cond == kCondLT) || (if_cond == kCondLE)) : (if_cond == kCondEQ);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002910 }
2911
Roland Levillain31dd3d62016-02-16 12:21:02 +00002912 protected:
2913 template <typename T>
2914 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
2915
2916 template <typename T>
2917 int32_t CompareFP(T x, T y) const {
2918 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
2919 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
2920 // Handle the bias.
2921 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
2922 }
2923
2924 // Return an integer constant containing the result of a condition evaluated at compile time.
2925 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
2926 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
2927 }
2928
Dave Allison20dfc792014-06-16 20:44:29 -07002929 private:
Mark Mendellc4701932015-04-10 13:18:51 -04002930 // Needed if we merge a HCompare into a HCondition.
2931 ComparisonBias bias_;
2932
Dave Allison20dfc792014-06-16 20:44:29 -07002933 DISALLOW_COPY_AND_ASSIGN(HCondition);
2934};
2935
2936// Instruction to check if two inputs are equal to each other.
2937class HEqual : public HCondition {
2938 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002939 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2940 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002941
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002942 bool IsCommutative() const OVERRIDE { return true; }
2943
Vladimir Marko9e23df52015-11-10 17:14:35 +00002944 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2945 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002946 return MakeConstantCondition(true, GetDexPc());
2947 }
2948 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2949 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
2950 }
2951 // In the following Evaluate methods, a HCompare instruction has
2952 // been merged into this HEqual instruction; evaluate it as
2953 // `Compare(x, y) == 0`.
2954 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2955 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
2956 GetDexPc());
2957 }
2958 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
2959 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
2960 }
2961 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
2962 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00002963 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002964
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002965 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002966
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002967 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002968 return kCondEQ;
2969 }
2970
Mark Mendellc4701932015-04-10 13:18:51 -04002971 IfCondition GetOppositeCondition() const OVERRIDE {
2972 return kCondNE;
2973 }
2974
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002975 private:
Aart Bike9f37602015-10-09 11:15:55 -07002976 template <typename T> bool Compute(T x, T y) const { return x == y; }
2977
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002978 DISALLOW_COPY_AND_ASSIGN(HEqual);
2979};
2980
Dave Allison20dfc792014-06-16 20:44:29 -07002981class HNotEqual : public HCondition {
2982 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002983 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
2984 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07002985
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002986 bool IsCommutative() const OVERRIDE { return true; }
2987
Vladimir Marko9e23df52015-11-10 17:14:35 +00002988 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
2989 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002990 return MakeConstantCondition(false, GetDexPc());
2991 }
2992 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
2993 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
2994 }
2995 // In the following Evaluate methods, a HCompare instruction has
2996 // been merged into this HNotEqual instruction; evaluate it as
2997 // `Compare(x, y) != 0`.
2998 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
2999 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3000 }
3001 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3002 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3003 }
3004 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3005 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003006 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003007
Dave Allison20dfc792014-06-16 20:44:29 -07003008 DECLARE_INSTRUCTION(NotEqual);
3009
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003010 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003011 return kCondNE;
3012 }
3013
Mark Mendellc4701932015-04-10 13:18:51 -04003014 IfCondition GetOppositeCondition() const OVERRIDE {
3015 return kCondEQ;
3016 }
3017
Dave Allison20dfc792014-06-16 20:44:29 -07003018 private:
Aart Bike9f37602015-10-09 11:15:55 -07003019 template <typename T> bool Compute(T x, T y) const { return x != y; }
3020
Dave Allison20dfc792014-06-16 20:44:29 -07003021 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3022};
3023
3024class HLessThan : public HCondition {
3025 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003026 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3027 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003028
Roland Levillain9867bc72015-08-05 10:21:34 +01003029 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003030 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003031 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003032 // In the following Evaluate methods, a HCompare instruction has
3033 // been merged into this HLessThan instruction; evaluate it as
3034 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003035 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003036 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3037 }
3038 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3039 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3040 }
3041 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3042 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003043 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003044
Dave Allison20dfc792014-06-16 20:44:29 -07003045 DECLARE_INSTRUCTION(LessThan);
3046
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003047 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003048 return kCondLT;
3049 }
3050
Mark Mendellc4701932015-04-10 13:18:51 -04003051 IfCondition GetOppositeCondition() const OVERRIDE {
3052 return kCondGE;
3053 }
3054
Dave Allison20dfc792014-06-16 20:44:29 -07003055 private:
Aart Bike9f37602015-10-09 11:15:55 -07003056 template <typename T> bool Compute(T x, T y) const { return x < y; }
3057
Dave Allison20dfc792014-06-16 20:44:29 -07003058 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3059};
3060
3061class HLessThanOrEqual : public HCondition {
3062 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003063 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3064 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003065
Roland Levillain9867bc72015-08-05 10:21:34 +01003066 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003067 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003068 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003069 // In the following Evaluate methods, a HCompare instruction has
3070 // been merged into this HLessThanOrEqual instruction; evaluate it as
3071 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003072 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003073 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3074 }
3075 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3076 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3077 }
3078 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3079 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003080 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003081
Dave Allison20dfc792014-06-16 20:44:29 -07003082 DECLARE_INSTRUCTION(LessThanOrEqual);
3083
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003084 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003085 return kCondLE;
3086 }
3087
Mark Mendellc4701932015-04-10 13:18:51 -04003088 IfCondition GetOppositeCondition() const OVERRIDE {
3089 return kCondGT;
3090 }
3091
Dave Allison20dfc792014-06-16 20:44:29 -07003092 private:
Aart Bike9f37602015-10-09 11:15:55 -07003093 template <typename T> bool Compute(T x, T y) const { return x <= y; }
3094
Dave Allison20dfc792014-06-16 20:44:29 -07003095 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3096};
3097
3098class HGreaterThan : public HCondition {
3099 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003100 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3101 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003102
Roland Levillain9867bc72015-08-05 10:21:34 +01003103 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003104 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003105 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003106 // In the following Evaluate methods, a HCompare instruction has
3107 // been merged into this HGreaterThan instruction; evaluate it as
3108 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003109 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003110 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3111 }
3112 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3113 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3114 }
3115 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3116 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003117 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003118
Dave Allison20dfc792014-06-16 20:44:29 -07003119 DECLARE_INSTRUCTION(GreaterThan);
3120
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003121 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003122 return kCondGT;
3123 }
3124
Mark Mendellc4701932015-04-10 13:18:51 -04003125 IfCondition GetOppositeCondition() const OVERRIDE {
3126 return kCondLE;
3127 }
3128
Dave Allison20dfc792014-06-16 20:44:29 -07003129 private:
Aart Bike9f37602015-10-09 11:15:55 -07003130 template <typename T> bool Compute(T x, T y) const { return x > y; }
3131
Dave Allison20dfc792014-06-16 20:44:29 -07003132 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3133};
3134
3135class HGreaterThanOrEqual : public HCondition {
3136 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003137 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3138 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003139
Roland Levillain9867bc72015-08-05 10:21:34 +01003140 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003141 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003142 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003143 // In the following Evaluate methods, a HCompare instruction has
3144 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3145 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003146 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003147 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3148 }
3149 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3150 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3151 }
3152 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3153 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003154 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003155
Dave Allison20dfc792014-06-16 20:44:29 -07003156 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3157
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003158 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003159 return kCondGE;
3160 }
3161
Mark Mendellc4701932015-04-10 13:18:51 -04003162 IfCondition GetOppositeCondition() const OVERRIDE {
3163 return kCondLT;
3164 }
3165
Dave Allison20dfc792014-06-16 20:44:29 -07003166 private:
Aart Bike9f37602015-10-09 11:15:55 -07003167 template <typename T> bool Compute(T x, T y) const { return x >= y; }
3168
Dave Allison20dfc792014-06-16 20:44:29 -07003169 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3170};
3171
Aart Bike9f37602015-10-09 11:15:55 -07003172class HBelow : public HCondition {
3173 public:
3174 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3175 : HCondition(first, second, dex_pc) {}
3176
3177 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003178 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003179 }
3180 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003181 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3182 }
3183 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3184 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3185 LOG(FATAL) << DebugName() << " is not defined for float values";
3186 UNREACHABLE();
3187 }
3188 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3189 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3190 LOG(FATAL) << DebugName() << " is not defined for double values";
3191 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003192 }
3193
3194 DECLARE_INSTRUCTION(Below);
3195
3196 IfCondition GetCondition() const OVERRIDE {
3197 return kCondB;
3198 }
3199
3200 IfCondition GetOppositeCondition() const OVERRIDE {
3201 return kCondAE;
3202 }
3203
3204 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003205 template <typename T> bool Compute(T x, T y) const {
3206 return MakeUnsigned(x) < MakeUnsigned(y);
3207 }
Aart Bike9f37602015-10-09 11:15:55 -07003208
3209 DISALLOW_COPY_AND_ASSIGN(HBelow);
3210};
3211
3212class HBelowOrEqual : public HCondition {
3213 public:
3214 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3215 : HCondition(first, second, dex_pc) {}
3216
3217 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003218 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003219 }
3220 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003221 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3222 }
3223 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3224 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3225 LOG(FATAL) << DebugName() << " is not defined for float values";
3226 UNREACHABLE();
3227 }
3228 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3229 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3230 LOG(FATAL) << DebugName() << " is not defined for double values";
3231 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003232 }
3233
3234 DECLARE_INSTRUCTION(BelowOrEqual);
3235
3236 IfCondition GetCondition() const OVERRIDE {
3237 return kCondBE;
3238 }
3239
3240 IfCondition GetOppositeCondition() const OVERRIDE {
3241 return kCondA;
3242 }
3243
3244 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003245 template <typename T> bool Compute(T x, T y) const {
3246 return MakeUnsigned(x) <= MakeUnsigned(y);
3247 }
Aart Bike9f37602015-10-09 11:15:55 -07003248
3249 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3250};
3251
3252class HAbove : public HCondition {
3253 public:
3254 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3255 : HCondition(first, second, dex_pc) {}
3256
3257 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003258 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003259 }
3260 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003261 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3262 }
3263 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3264 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3265 LOG(FATAL) << DebugName() << " is not defined for float values";
3266 UNREACHABLE();
3267 }
3268 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3269 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3270 LOG(FATAL) << DebugName() << " is not defined for double values";
3271 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003272 }
3273
3274 DECLARE_INSTRUCTION(Above);
3275
3276 IfCondition GetCondition() const OVERRIDE {
3277 return kCondA;
3278 }
3279
3280 IfCondition GetOppositeCondition() const OVERRIDE {
3281 return kCondBE;
3282 }
3283
3284 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003285 template <typename T> bool Compute(T x, T y) const {
3286 return MakeUnsigned(x) > MakeUnsigned(y);
3287 }
Aart Bike9f37602015-10-09 11:15:55 -07003288
3289 DISALLOW_COPY_AND_ASSIGN(HAbove);
3290};
3291
3292class HAboveOrEqual : public HCondition {
3293 public:
3294 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3295 : HCondition(first, second, dex_pc) {}
3296
3297 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003298 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003299 }
3300 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003301 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3302 }
3303 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3304 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3305 LOG(FATAL) << DebugName() << " is not defined for float values";
3306 UNREACHABLE();
3307 }
3308 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3309 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3310 LOG(FATAL) << DebugName() << " is not defined for double values";
3311 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003312 }
3313
3314 DECLARE_INSTRUCTION(AboveOrEqual);
3315
3316 IfCondition GetCondition() const OVERRIDE {
3317 return kCondAE;
3318 }
3319
3320 IfCondition GetOppositeCondition() const OVERRIDE {
3321 return kCondB;
3322 }
3323
3324 private:
Roland Levillain31dd3d62016-02-16 12:21:02 +00003325 template <typename T> bool Compute(T x, T y) const {
3326 return MakeUnsigned(x) >= MakeUnsigned(y);
3327 }
Aart Bike9f37602015-10-09 11:15:55 -07003328
3329 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3330};
Dave Allison20dfc792014-06-16 20:44:29 -07003331
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003332// Instruction to check how two inputs compare to each other.
3333// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
3334class HCompare : public HBinaryOperation {
3335 public:
Alexey Frunze4dda3372015-06-01 18:31:49 -07003336 HCompare(Primitive::Type type,
3337 HInstruction* first,
3338 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003339 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003340 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003341 : HBinaryOperation(Primitive::kPrimInt,
3342 first,
3343 second,
3344 SideEffectsForArchRuntimeCalls(type),
3345 dex_pc),
3346 bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003347 DCHECK_EQ(type, first->GetType());
3348 DCHECK_EQ(type, second->GetType());
3349 }
3350
Roland Levillain9867bc72015-08-05 10:21:34 +01003351 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003352 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3353
3354 template <typename T>
3355 int32_t ComputeFP(T x, T y) const {
3356 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3357 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3358 // Handle the bias.
3359 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3360 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003361
Roland Levillain9867bc72015-08-05 10:21:34 +01003362 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003363 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3364 // reach this code path when processing a freshly built HIR
3365 // graph. However HCompare integer instructions can be synthesized
3366 // by the instruction simplifier to implement IntegerCompare and
3367 // IntegerSignum intrinsics, so we have to handle this case.
3368 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003369 }
3370 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003371 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3372 }
3373 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3374 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3375 }
3376 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3377 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003378 }
3379
Calin Juravleddb7df22014-11-25 20:56:51 +00003380 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3381 return bias_ == other->AsCompare()->bias_;
3382 }
3383
Mark Mendellc4701932015-04-10 13:18:51 -04003384 ComparisonBias GetBias() const { return bias_; }
3385
Roland Levillain31dd3d62016-02-16 12:21:02 +00003386 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
3387 // Only meaninfgul for floating-point comparisons.
3388 bool IsGtBias() const {
3389 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3390 return bias_ == ComparisonBias::kGtBias;
3391 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003392
3393 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type) {
3394 // MIPS64 uses a runtime call for FP comparisons.
3395 return Primitive::IsFloatingPointType(type) ? SideEffects::CanTriggerGC() : SideEffects::None();
3396 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003397
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003398 DECLARE_INSTRUCTION(Compare);
3399
Roland Levillain31dd3d62016-02-16 12:21:02 +00003400 protected:
3401 // Return an integer constant containing the result of a comparison evaluated at compile time.
3402 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3403 DCHECK(value == -1 || value == 0 || value == 1) << value;
3404 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3405 }
3406
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003407 private:
Mark Mendellc4701932015-04-10 13:18:51 -04003408 const ComparisonBias bias_;
Calin Juravleddb7df22014-11-25 20:56:51 +00003409
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003410 DISALLOW_COPY_AND_ASSIGN(HCompare);
3411};
3412
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003413// A local in the graph. Corresponds to a Dex register.
3414class HLocal : public HTemplateInstruction<0> {
3415 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003416 explicit HLocal(uint16_t reg_number)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003417 : HTemplateInstruction(SideEffects::None(), kNoDexPc), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003418
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003419 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003420
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003421 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003422
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003423 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003424 // The Dex register number.
3425 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003426
3427 DISALLOW_COPY_AND_ASSIGN(HLocal);
3428};
3429
3430// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07003431class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003432 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003433 HLoadLocal(HLocal* local, Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3434 : HExpression(type, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003435 SetRawInputAt(0, local);
3436 }
3437
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003438 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3439
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003440 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003441
3442 private:
3443 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
3444};
3445
3446// Store a value in a given local. This instruction has two inputs: the value
3447// and the local.
3448class HStoreLocal : public HTemplateInstruction<2> {
3449 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003450 HStoreLocal(HLocal* local, HInstruction* value, uint32_t dex_pc = kNoDexPc)
3451 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003452 SetRawInputAt(0, local);
3453 SetRawInputAt(1, value);
3454 }
3455
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003456 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
3457
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003458 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003459
3460 private:
3461 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
3462};
3463
David Brazdil6de19382016-01-08 17:37:10 +00003464class HNewInstance : public HExpression<2> {
3465 public:
3466 HNewInstance(HInstruction* cls,
3467 HCurrentMethod* current_method,
3468 uint32_t dex_pc,
3469 uint16_t type_index,
3470 const DexFile& dex_file,
3471 bool can_throw,
3472 bool finalizable,
3473 QuickEntrypointEnum entrypoint)
3474 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3475 type_index_(type_index),
3476 dex_file_(dex_file),
3477 can_throw_(can_throw),
3478 finalizable_(finalizable),
3479 entrypoint_(entrypoint) {
3480 SetRawInputAt(0, cls);
3481 SetRawInputAt(1, current_method);
3482 }
3483
3484 uint16_t GetTypeIndex() const { return type_index_; }
3485 const DexFile& GetDexFile() const { return dex_file_; }
3486
3487 // Calls runtime so needs an environment.
3488 bool NeedsEnvironment() const OVERRIDE { return true; }
3489
3490 // It may throw when called on type that's not instantiable/accessible.
3491 // It can throw OOME.
3492 // TODO: distinguish between the two cases so we can for example allow allocation elimination.
3493 bool CanThrow() const OVERRIDE { return can_throw_ || true; }
3494
3495 bool IsFinalizable() const { return finalizable_; }
3496
3497 bool CanBeNull() const OVERRIDE { return false; }
3498
3499 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3500
3501 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3502 entrypoint_ = entrypoint;
3503 }
3504
3505 bool IsStringAlloc() const;
3506
3507 DECLARE_INSTRUCTION(NewInstance);
3508
3509 private:
3510 const uint16_t type_index_;
3511 const DexFile& dex_file_;
3512 const bool can_throw_;
3513 const bool finalizable_;
3514 QuickEntrypointEnum entrypoint_;
3515
3516 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3517};
3518
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003519enum class Intrinsics {
Aart Bik5d75afe2015-12-14 11:57:01 -08003520#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
3521 k ## Name,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003522#include "intrinsics_list.h"
3523 kNone,
3524 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
3525#undef INTRINSICS_LIST
3526#undef OPTIMIZING_INTRINSICS
3527};
3528std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
3529
Agi Csaki05f20562015-08-19 14:58:14 -07003530enum IntrinsicNeedsEnvironmentOrCache {
3531 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3532 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003533};
3534
Aart Bik5d75afe2015-12-14 11:57:01 -08003535enum IntrinsicSideEffects {
3536 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3537 kReadSideEffects, // Intrinsic may read heap memory.
3538 kWriteSideEffects, // Intrinsic may write heap memory.
3539 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3540};
3541
3542enum IntrinsicExceptions {
3543 kNoThrow, // Intrinsic does not throw any exceptions.
3544 kCanThrow // Intrinsic may throw exceptions.
3545};
3546
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003547class HInvoke : public HInstruction {
3548 public:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003549 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003550
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003551 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003552
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003553 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003554 SetRawInputAt(index, argument);
3555 }
3556
Roland Levillain3e3d7332015-04-28 11:00:54 +01003557 // Return the number of arguments. This number can be lower than
3558 // the number of inputs returned by InputCount(), as some invoke
3559 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3560 // inputs at the end of their list of inputs.
3561 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3562
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003563 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003564
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003565 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003566 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003567
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003568 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
3569
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003570 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003571 return intrinsic_;
3572 }
3573
Aart Bik5d75afe2015-12-14 11:57:01 -08003574 void SetIntrinsic(Intrinsics intrinsic,
3575 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3576 IntrinsicSideEffects side_effects,
3577 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003578
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003579 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003580 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003581 }
3582
Aart Bik5d75afe2015-12-14 11:57:01 -08003583 bool CanThrow() const OVERRIDE { return can_throw_; }
3584
3585 bool CanBeMoved() const OVERRIDE { return IsIntrinsic(); }
3586
3587 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3588 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3589 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003590
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003591 uint32_t* GetIntrinsicOptimizations() {
3592 return &intrinsic_optimizations_;
3593 }
3594
3595 const uint32_t* GetIntrinsicOptimizations() const {
3596 return &intrinsic_optimizations_;
3597 }
3598
3599 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3600
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003601 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003602
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003603 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003604 HInvoke(ArenaAllocator* arena,
3605 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003606 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003607 Primitive::Type return_type,
3608 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003609 uint32_t dex_method_index,
3610 InvokeType original_invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003611 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003612 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003613 number_of_arguments_(number_of_arguments),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003614 inputs_(number_of_arguments + number_of_other_inputs,
3615 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003616 return_type_(return_type),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003617 dex_method_index_(dex_method_index),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003618 original_invoke_type_(original_invoke_type),
Aart Bik5d75afe2015-12-14 11:57:01 -08003619 can_throw_(true),
agicsaki57b81ec2015-08-11 17:39:37 -07003620 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003621 intrinsic_optimizations_(0) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003622 }
3623
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003624 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003625 return inputs_[index];
3626 }
3627
David Brazdil1abb4192015-02-17 18:33:36 +00003628 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003629 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00003630 }
3631
Aart Bik5d75afe2015-12-14 11:57:01 -08003632 void SetCanThrow(bool can_throw) { can_throw_ = can_throw; }
3633
Roland Levillain3e3d7332015-04-28 11:00:54 +01003634 uint32_t number_of_arguments_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003635 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003636 const Primitive::Type return_type_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003637 const uint32_t dex_method_index_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003638 const InvokeType original_invoke_type_;
Aart Bik5d75afe2015-12-14 11:57:01 -08003639 bool can_throw_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003640 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003641
3642 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3643 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003644
3645 private:
3646 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3647};
3648
Calin Juravle175dc732015-08-25 15:42:32 +01003649class HInvokeUnresolved : public HInvoke {
3650 public:
3651 HInvokeUnresolved(ArenaAllocator* arena,
3652 uint32_t number_of_arguments,
3653 Primitive::Type return_type,
3654 uint32_t dex_pc,
3655 uint32_t dex_method_index,
3656 InvokeType invoke_type)
3657 : HInvoke(arena,
3658 number_of_arguments,
3659 0u /* number_of_other_inputs */,
3660 return_type,
3661 dex_pc,
3662 dex_method_index,
3663 invoke_type) {
3664 }
3665
3666 DECLARE_INSTRUCTION(InvokeUnresolved);
3667
3668 private:
3669 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3670};
3671
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003672class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003673 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003674 // Requirements of this method call regarding the class
3675 // initialization (clinit) check of its declaring class.
3676 enum class ClinitCheckRequirement {
3677 kNone, // Class already initialized.
3678 kExplicit, // Static call having explicit clinit check as last input.
3679 kImplicit, // Static call implicitly requiring a clinit check.
3680 };
3681
Vladimir Marko58155012015-08-19 12:49:41 +00003682 // Determines how to load the target ArtMethod*.
3683 enum class MethodLoadKind {
3684 // Use a String init ArtMethod* loaded from Thread entrypoints.
3685 kStringInit,
3686
3687 // Use the method's own ArtMethod* loaded by the register allocator.
3688 kRecursive,
3689
3690 // Use ArtMethod* at a known address, embed the direct address in the code.
3691 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3692 kDirectAddress,
3693
3694 // Use ArtMethod* at an address that will be known at link time, embed the direct
3695 // address in the code. If the image is relocatable, emit .patch_oat entry.
3696 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3697 // the image relocatable or not.
3698 kDirectAddressWithFixup,
3699
3700 // Load from resoved methods array in the dex cache using a PC-relative load.
3701 // Used when we need to use the dex cache, for example for invoke-static that
3702 // may cause class initialization (the entry may point to a resolution method),
3703 // and we know that we can access the dex cache arrays using a PC-relative load.
3704 kDexCachePcRelative,
3705
3706 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3707 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3708 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3709 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3710 kDexCacheViaMethod,
3711 };
3712
3713 // Determines the location of the code pointer.
3714 enum class CodePtrLocation {
3715 // Recursive call, use local PC-relative call instruction.
3716 kCallSelf,
3717
3718 // Use PC-relative call instruction patched at link time.
3719 // Used for calls within an oat file, boot->boot or app->app.
3720 kCallPCRelative,
3721
3722 // Call to a known target address, embed the direct address in code.
3723 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3724 kCallDirect,
3725
3726 // Call to a target address that will be known at link time, embed the direct
3727 // address in code. If the image is relocatable, emit .patch_oat entry.
3728 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3729 // the image relocatable or not.
3730 kCallDirectWithFixup,
3731
3732 // Use code pointer from the ArtMethod*.
3733 // Used when we don't know the target code. This is also the last-resort-kind used when
3734 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3735 kCallArtMethod,
3736 };
3737
3738 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01003739 MethodLoadKind method_load_kind;
3740 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00003741 // The method load data holds
3742 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
3743 // Note that there are multiple string init methods, each having its own offset.
3744 // - the method address for kDirectAddress
3745 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01003746 uint64_t method_load_data;
3747 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00003748 };
3749
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003750 HInvokeStaticOrDirect(ArenaAllocator* arena,
3751 uint32_t number_of_arguments,
3752 Primitive::Type return_type,
3753 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003754 uint32_t method_index,
3755 MethodReference target_method,
3756 DispatchInfo dispatch_info,
Nicolas Geoffray79041292015-03-26 10:05:54 +00003757 InvokeType original_invoke_type,
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003758 InvokeType optimized_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01003759 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01003760 : HInvoke(arena,
3761 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00003762 // There is potentially one extra argument for the HCurrentMethod node, and
3763 // potentially one other if the clinit check is explicit, and potentially
3764 // one other if the method is a string factory.
3765 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
3766 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u) +
3767 (dispatch_info.method_load_kind == MethodLoadKind::kStringInit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003768 return_type,
3769 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00003770 method_index,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003771 original_invoke_type),
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003772 optimized_invoke_type_(optimized_invoke_type),
Jeff Hao848f70a2014-01-15 13:49:50 -08003773 clinit_check_requirement_(clinit_check_requirement),
Vladimir Marko58155012015-08-19 12:49:41 +00003774 target_method_(target_method),
Vladimir Markob554b5a2015-11-06 12:57:55 +00003775 dispatch_info_(dispatch_info) { }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003776
Vladimir Markodc151b22015-10-15 18:02:30 +01003777 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00003778 bool had_current_method_input = HasCurrentMethodInput();
3779 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
3780
3781 // Using the current method is the default and once we find a better
3782 // method load kind, we should not go back to using the current method.
3783 DCHECK(had_current_method_input || !needs_current_method_input);
3784
3785 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003786 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
3787 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003788 }
Vladimir Markodc151b22015-10-15 18:02:30 +01003789 dispatch_info_ = dispatch_info;
3790 }
3791
Vladimir Markoc53c0792015-11-19 15:48:33 +00003792 void AddSpecialInput(HInstruction* input) {
3793 // We allow only one special input.
3794 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
3795 DCHECK(InputCount() == GetSpecialInputIndex() ||
3796 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
3797 InsertInputAt(GetSpecialInputIndex(), input);
3798 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003799
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003800 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003801 // We access the method via the dex cache so we can't do an implicit null check.
3802 // TODO: for intrinsics we can generate implicit null checks.
3803 return false;
3804 }
3805
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07003806 bool CanBeNull() const OVERRIDE {
3807 return return_type_ == Primitive::kPrimNot && !IsStringInit();
3808 }
3809
Vladimir Markoc53c0792015-11-19 15:48:33 +00003810 // Get the index of the special input, if any.
3811 //
David Brazdil6de19382016-01-08 17:37:10 +00003812 // If the invoke HasCurrentMethodInput(), the "special input" is the current
3813 // method pointer; otherwise there may be one platform-specific special input,
3814 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00003815 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00003816 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00003817
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003818 InvokeType GetOptimizedInvokeType() const { return optimized_invoke_type_; }
3819 void SetOptimizedInvokeType(InvokeType invoke_type) {
3820 optimized_invoke_type_ = invoke_type;
3821 }
3822
Vladimir Marko58155012015-08-19 12:49:41 +00003823 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
3824 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
3825 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01003826 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00003827 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00003828 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003829 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01003830 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
3831 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00003832 bool HasCurrentMethodInput() const {
3833 // This function can be called only after the invoke has been fully initialized by the builder.
3834 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003835 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003836 return true;
3837 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003838 DCHECK(InputCount() == GetSpecialInputIndex() ||
3839 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00003840 return false;
3841 }
3842 }
Vladimir Marko58155012015-08-19 12:49:41 +00003843 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
3844 MethodReference GetTargetMethod() const { return target_method_; }
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003845 void SetTargetMethod(MethodReference method) { target_method_ = method; }
Vladimir Marko58155012015-08-19 12:49:41 +00003846
3847 int32_t GetStringInitOffset() const {
3848 DCHECK(IsStringInit());
3849 return dispatch_info_.method_load_data;
3850 }
3851
3852 uint64_t GetMethodAddress() const {
3853 DCHECK(HasMethodAddress());
3854 return dispatch_info_.method_load_data;
3855 }
3856
3857 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003858 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00003859 return dispatch_info_.method_load_data;
3860 }
3861
3862 uint64_t GetDirectCodePtr() const {
3863 DCHECK(HasDirectCodePtr());
3864 return dispatch_info_.direct_code_ptr;
3865 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003866
Calin Juravle68ad6492015-08-18 17:08:12 +01003867 ClinitCheckRequirement GetClinitCheckRequirement() const { return clinit_check_requirement_; }
3868
Roland Levillain4c0eb422015-04-24 16:43:49 +01003869 // Is this instruction a call to a static method?
3870 bool IsStatic() const {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003871 return GetOriginalInvokeType() == kStatic;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003872 }
3873
Vladimir Markofbb184a2015-11-13 14:47:00 +00003874 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
3875 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
3876 // instruction; only relevant for static calls with explicit clinit check.
3877 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01003878 DCHECK(IsStaticWithExplicitClinitCheck());
3879 size_t last_input_index = InputCount() - 1;
3880 HInstruction* last_input = InputAt(last_input_index);
3881 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003882 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01003883 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003884 inputs_.pop_back();
Vladimir Markofbb184a2015-11-13 14:47:00 +00003885 clinit_check_requirement_ = new_requirement;
3886 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003887 }
3888
David Brazdilbc9ab162016-01-20 14:50:19 +00003889 HInstruction* GetAndRemoveThisArgumentOfStringInit() {
David Brazdil6de19382016-01-08 17:37:10 +00003890 DCHECK(IsStringInit());
3891 size_t index = InputCount() - 1;
David Brazdilbc9ab162016-01-20 14:50:19 +00003892 HInstruction* input = InputAt(index);
David Brazdil6de19382016-01-08 17:37:10 +00003893 RemoveAsUserOfInput(index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003894 inputs_.pop_back();
David Brazdilbc9ab162016-01-20 14:50:19 +00003895 return input;
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003896 }
3897
Roland Levillain4c0eb422015-04-24 16:43:49 +01003898 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00003899 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01003900 bool IsStaticWithExplicitClinitCheck() const {
3901 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
3902 }
3903
3904 // Is this a call to a static method whose declaring class has an
3905 // implicit intialization check requirement?
3906 bool IsStaticWithImplicitClinitCheck() const {
3907 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
3908 }
3909
Vladimir Markob554b5a2015-11-06 12:57:55 +00003910 // Does this method load kind need the current method as an input?
3911 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
3912 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
3913 }
3914
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003915 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003916
Roland Levillain4c0eb422015-04-24 16:43:49 +01003917 protected:
3918 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
3919 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
3920 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
3921 HInstruction* input = input_record.GetInstruction();
3922 // `input` is the last input of a static invoke marked as having
3923 // an explicit clinit check. It must either be:
3924 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
3925 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
3926 DCHECK(input != nullptr);
3927 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
3928 }
3929 return input_record;
3930 }
3931
Vladimir Markoc53c0792015-11-19 15:48:33 +00003932 void InsertInputAt(size_t index, HInstruction* input);
3933 void RemoveInputAt(size_t index);
3934
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003935 private:
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003936 InvokeType optimized_invoke_type_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01003937 ClinitCheckRequirement clinit_check_requirement_;
Vladimir Marko58155012015-08-19 12:49:41 +00003938 // The target method may refer to different dex file or method index than the original
3939 // invoke. This happens for sharpened calls and for calls where a method was redeclared
3940 // in derived class to increase visibility.
3941 MethodReference target_method_;
3942 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003943
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003944 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003945};
Vladimir Markof64242a2015-12-01 14:58:23 +00003946std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00003947std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003948
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003949class HInvokeVirtual : public HInvoke {
3950 public:
3951 HInvokeVirtual(ArenaAllocator* arena,
3952 uint32_t number_of_arguments,
3953 Primitive::Type return_type,
3954 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003955 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003956 uint32_t vtable_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003957 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003958 vtable_index_(vtable_index) {}
3959
Calin Juravle641547a2015-04-21 22:08:51 +01003960 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003961 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003962 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003963 }
3964
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003965 uint32_t GetVTableIndex() const { return vtable_index_; }
3966
3967 DECLARE_INSTRUCTION(InvokeVirtual);
3968
3969 private:
3970 const uint32_t vtable_index_;
3971
3972 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
3973};
3974
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003975class HInvokeInterface : public HInvoke {
3976 public:
3977 HInvokeInterface(ArenaAllocator* arena,
3978 uint32_t number_of_arguments,
3979 Primitive::Type return_type,
3980 uint32_t dex_pc,
3981 uint32_t dex_method_index,
3982 uint32_t imt_index)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003983 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index, kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003984 imt_index_(imt_index) {}
3985
Calin Juravle641547a2015-04-21 22:08:51 +01003986 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00003987 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01003988 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00003989 }
3990
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003991 uint32_t GetImtIndex() const { return imt_index_; }
3992 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3993
3994 DECLARE_INSTRUCTION(InvokeInterface);
3995
3996 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003997 const uint32_t imt_index_;
3998
3999 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4000};
4001
Roland Levillain88cb1752014-10-20 16:36:47 +01004002class HNeg : public HUnaryOperation {
4003 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004004 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4005 : HUnaryOperation(result_type, input, dex_pc) {}
Roland Levillain88cb1752014-10-20 16:36:47 +01004006
Roland Levillain9867bc72015-08-05 10:21:34 +01004007 template <typename T> T Compute(T x) const { return -x; }
4008
4009 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004010 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004011 }
4012 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004013 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004014 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004015 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4016 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4017 }
4018 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4019 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4020 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004021
Roland Levillain88cb1752014-10-20 16:36:47 +01004022 DECLARE_INSTRUCTION(Neg);
4023
4024 private:
4025 DISALLOW_COPY_AND_ASSIGN(HNeg);
4026};
4027
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004028class HNewArray : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004029 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004030 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004031 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004032 uint32_t dex_pc,
4033 uint16_t type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004034 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004035 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004036 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004037 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004038 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004039 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004040 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004041 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004042 }
4043
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004044 uint16_t GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004045 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004046
4047 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004048 bool NeedsEnvironment() const OVERRIDE { return true; }
4049
Mingyao Yang0c365e62015-03-31 15:09:29 -07004050 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4051 bool CanThrow() const OVERRIDE { return true; }
4052
Calin Juravle10e244f2015-01-26 18:54:32 +00004053 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004054
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004055 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4056
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004057 DECLARE_INSTRUCTION(NewArray);
4058
4059 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004060 const uint16_t type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004061 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004062 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004063
4064 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4065};
4066
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004067class HAdd : public HBinaryOperation {
4068 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004069 HAdd(Primitive::Type result_type,
4070 HInstruction* left,
4071 HInstruction* right,
4072 uint32_t dex_pc = kNoDexPc)
4073 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004074
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004075 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004076
Roland Levillain9867bc72015-08-05 10:21:34 +01004077 template <typename T> T Compute(T x, T y) const { return x + y; }
4078
4079 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004080 return GetBlock()->GetGraph()->GetIntConstant(
4081 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004082 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004083 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004084 return GetBlock()->GetGraph()->GetLongConstant(
4085 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004086 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004087 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4088 return GetBlock()->GetGraph()->GetFloatConstant(
4089 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4090 }
4091 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4092 return GetBlock()->GetGraph()->GetDoubleConstant(
4093 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4094 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004095
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004096 DECLARE_INSTRUCTION(Add);
4097
4098 private:
4099 DISALLOW_COPY_AND_ASSIGN(HAdd);
4100};
4101
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004102class HSub : public HBinaryOperation {
4103 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004104 HSub(Primitive::Type result_type,
4105 HInstruction* left,
4106 HInstruction* right,
4107 uint32_t dex_pc = kNoDexPc)
4108 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004109
Roland Levillain9867bc72015-08-05 10:21:34 +01004110 template <typename T> T Compute(T x, T y) const { return x - y; }
4111
4112 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004113 return GetBlock()->GetGraph()->GetIntConstant(
4114 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004115 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004116 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004117 return GetBlock()->GetGraph()->GetLongConstant(
4118 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004119 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004120 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4121 return GetBlock()->GetGraph()->GetFloatConstant(
4122 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4123 }
4124 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4125 return GetBlock()->GetGraph()->GetDoubleConstant(
4126 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4127 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004128
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004129 DECLARE_INSTRUCTION(Sub);
4130
4131 private:
4132 DISALLOW_COPY_AND_ASSIGN(HSub);
4133};
4134
Calin Juravle34bacdf2014-10-07 20:23:36 +01004135class HMul : public HBinaryOperation {
4136 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004137 HMul(Primitive::Type result_type,
4138 HInstruction* left,
4139 HInstruction* right,
4140 uint32_t dex_pc = kNoDexPc)
4141 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004142
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004143 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004144
Roland Levillain9867bc72015-08-05 10:21:34 +01004145 template <typename T> T Compute(T x, T y) const { return x * y; }
4146
4147 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004148 return GetBlock()->GetGraph()->GetIntConstant(
4149 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004150 }
4151 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004152 return GetBlock()->GetGraph()->GetLongConstant(
4153 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004154 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004155 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4156 return GetBlock()->GetGraph()->GetFloatConstant(
4157 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4158 }
4159 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4160 return GetBlock()->GetGraph()->GetDoubleConstant(
4161 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4162 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004163
4164 DECLARE_INSTRUCTION(Mul);
4165
4166 private:
4167 DISALLOW_COPY_AND_ASSIGN(HMul);
4168};
4169
Calin Juravle7c4954d2014-10-28 16:57:40 +00004170class HDiv : public HBinaryOperation {
4171 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004172 HDiv(Primitive::Type result_type,
4173 HInstruction* left,
4174 HInstruction* right,
4175 uint32_t dex_pc)
4176 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004177
Roland Levillain9867bc72015-08-05 10:21:34 +01004178 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004179 T ComputeIntegral(T x, T y) const {
4180 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004181 // Our graph structure ensures we never have 0 for `y` during
4182 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004183 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004184 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004185 return (y == -1) ? -x : x / y;
4186 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004187
Roland Levillain31dd3d62016-02-16 12:21:02 +00004188 template <typename T>
4189 T ComputeFP(T x, T y) const {
4190 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4191 return x / y;
4192 }
4193
Roland Levillain9867bc72015-08-05 10:21:34 +01004194 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004195 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004196 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004197 }
4198 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004199 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004200 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4201 }
4202 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4203 return GetBlock()->GetGraph()->GetFloatConstant(
4204 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4205 }
4206 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4207 return GetBlock()->GetGraph()->GetDoubleConstant(
4208 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004209 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004210
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004211 static SideEffects SideEffectsForArchRuntimeCalls() {
4212 // The generated code can use a runtime call.
4213 return SideEffects::CanTriggerGC();
4214 }
4215
Calin Juravle7c4954d2014-10-28 16:57:40 +00004216 DECLARE_INSTRUCTION(Div);
4217
4218 private:
4219 DISALLOW_COPY_AND_ASSIGN(HDiv);
4220};
4221
Calin Juravlebacfec32014-11-14 15:54:36 +00004222class HRem : public HBinaryOperation {
4223 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004224 HRem(Primitive::Type result_type,
4225 HInstruction* left,
4226 HInstruction* right,
4227 uint32_t dex_pc)
4228 : HBinaryOperation(result_type, left, right, SideEffectsForArchRuntimeCalls(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004229
Roland Levillain9867bc72015-08-05 10:21:34 +01004230 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004231 T ComputeIntegral(T x, T y) const {
4232 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004233 // Our graph structure ensures we never have 0 for `y` during
4234 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004235 DCHECK_NE(y, 0);
4236 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4237 return (y == -1) ? 0 : x % y;
4238 }
4239
Roland Levillain31dd3d62016-02-16 12:21:02 +00004240 template <typename T>
4241 T ComputeFP(T x, T y) const {
4242 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4243 return std::fmod(x, y);
4244 }
4245
Roland Levillain9867bc72015-08-05 10:21:34 +01004246 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004247 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004248 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004249 }
4250 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004251 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004252 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004253 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004254 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4255 return GetBlock()->GetGraph()->GetFloatConstant(
4256 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4257 }
4258 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4259 return GetBlock()->GetGraph()->GetDoubleConstant(
4260 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4261 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004262
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004263 static SideEffects SideEffectsForArchRuntimeCalls() {
4264 return SideEffects::CanTriggerGC();
4265 }
4266
Calin Juravlebacfec32014-11-14 15:54:36 +00004267 DECLARE_INSTRUCTION(Rem);
4268
4269 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004270 DISALLOW_COPY_AND_ASSIGN(HRem);
4271};
4272
Calin Juravled0d48522014-11-04 16:40:20 +00004273class HDivZeroCheck : public HExpression<1> {
4274 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004275 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4276 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004277 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004278 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004279 SetRawInputAt(0, value);
4280 }
4281
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004282 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4283
Calin Juravled0d48522014-11-04 16:40:20 +00004284 bool CanBeMoved() const OVERRIDE { return true; }
4285
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004286 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004287 return true;
4288 }
4289
4290 bool NeedsEnvironment() const OVERRIDE { return true; }
4291 bool CanThrow() const OVERRIDE { return true; }
4292
Calin Juravled0d48522014-11-04 16:40:20 +00004293 DECLARE_INSTRUCTION(DivZeroCheck);
4294
4295 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004296 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4297};
4298
Calin Juravle9aec02f2014-11-18 23:06:35 +00004299class HShl : public HBinaryOperation {
4300 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004301 HShl(Primitive::Type result_type,
4302 HInstruction* left,
4303 HInstruction* right,
4304 uint32_t dex_pc = kNoDexPc)
4305 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004306
Roland Levillain9867bc72015-08-05 10:21:34 +01004307 template <typename T, typename U, typename V>
4308 T Compute(T x, U y, V max_shift_value) const {
4309 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4310 "V is not the unsigned integer type corresponding to T");
4311 return x << (y & max_shift_value);
4312 }
4313
4314 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4315 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004316 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004317 }
4318 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4319 // case is handled as `x << static_cast<int>(y)`.
4320 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4321 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004322 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004323 }
4324 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4325 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004326 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004327 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004328 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4329 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4330 LOG(FATAL) << DebugName() << " is not defined for float values";
4331 UNREACHABLE();
4332 }
4333 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4334 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4335 LOG(FATAL) << DebugName() << " is not defined for double values";
4336 UNREACHABLE();
4337 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004338
4339 DECLARE_INSTRUCTION(Shl);
4340
4341 private:
4342 DISALLOW_COPY_AND_ASSIGN(HShl);
4343};
4344
4345class HShr : public HBinaryOperation {
4346 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004347 HShr(Primitive::Type result_type,
4348 HInstruction* left,
4349 HInstruction* right,
4350 uint32_t dex_pc = kNoDexPc)
4351 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004352
Roland Levillain9867bc72015-08-05 10:21:34 +01004353 template <typename T, typename U, typename V>
4354 T Compute(T x, U y, V max_shift_value) const {
4355 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4356 "V is not the unsigned integer type corresponding to T");
4357 return x >> (y & max_shift_value);
4358 }
4359
4360 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4361 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004362 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004363 }
4364 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4365 // case is handled as `x >> static_cast<int>(y)`.
4366 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4367 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004368 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004369 }
4370 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4371 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004372 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004373 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004374 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4375 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4376 LOG(FATAL) << DebugName() << " is not defined for float values";
4377 UNREACHABLE();
4378 }
4379 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4380 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4381 LOG(FATAL) << DebugName() << " is not defined for double values";
4382 UNREACHABLE();
4383 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004384
4385 DECLARE_INSTRUCTION(Shr);
4386
4387 private:
4388 DISALLOW_COPY_AND_ASSIGN(HShr);
4389};
4390
4391class HUShr : public HBinaryOperation {
4392 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004393 HUShr(Primitive::Type result_type,
4394 HInstruction* left,
4395 HInstruction* right,
4396 uint32_t dex_pc = kNoDexPc)
4397 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle9aec02f2014-11-18 23:06:35 +00004398
Roland Levillain9867bc72015-08-05 10:21:34 +01004399 template <typename T, typename U, typename V>
4400 T Compute(T x, U y, V max_shift_value) const {
4401 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4402 "V is not the unsigned integer type corresponding to T");
4403 V ux = static_cast<V>(x);
4404 return static_cast<T>(ux >> (y & max_shift_value));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004405 }
4406
Roland Levillain9867bc72015-08-05 10:21:34 +01004407 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4408 return GetBlock()->GetGraph()->GetIntConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004409 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004410 }
4411 // There is no `Evaluate(HIntConstant* x, HLongConstant* y)`, as this
4412 // case is handled as `x >>> static_cast<int>(y)`.
4413 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4414 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004415 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004416 }
4417 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4418 return GetBlock()->GetGraph()->GetLongConstant(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004419 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
Calin Juravle9aec02f2014-11-18 23:06:35 +00004420 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004421 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4422 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4423 LOG(FATAL) << DebugName() << " is not defined for float values";
4424 UNREACHABLE();
4425 }
4426 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4427 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4428 LOG(FATAL) << DebugName() << " is not defined for double values";
4429 UNREACHABLE();
4430 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004431
4432 DECLARE_INSTRUCTION(UShr);
4433
4434 private:
4435 DISALLOW_COPY_AND_ASSIGN(HUShr);
4436};
4437
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004438class HAnd : public HBinaryOperation {
4439 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004440 HAnd(Primitive::Type result_type,
4441 HInstruction* left,
4442 HInstruction* right,
4443 uint32_t dex_pc = kNoDexPc)
4444 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004445
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004446 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004447
Roland Levillain9867bc72015-08-05 10:21:34 +01004448 template <typename T, typename U>
4449 auto Compute(T x, U y) const -> decltype(x & y) { return x & y; }
4450
4451 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004452 return GetBlock()->GetGraph()->GetIntConstant(
4453 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004454 }
4455 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004456 return GetBlock()->GetGraph()->GetLongConstant(
4457 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004458 }
4459 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004460 return GetBlock()->GetGraph()->GetLongConstant(
4461 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004462 }
4463 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004464 return GetBlock()->GetGraph()->GetLongConstant(
4465 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004466 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004467 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4468 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4469 LOG(FATAL) << DebugName() << " is not defined for float values";
4470 UNREACHABLE();
4471 }
4472 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4473 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4474 LOG(FATAL) << DebugName() << " is not defined for double values";
4475 UNREACHABLE();
4476 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004477
4478 DECLARE_INSTRUCTION(And);
4479
4480 private:
4481 DISALLOW_COPY_AND_ASSIGN(HAnd);
4482};
4483
4484class HOr : public HBinaryOperation {
4485 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004486 HOr(Primitive::Type result_type,
4487 HInstruction* left,
4488 HInstruction* right,
4489 uint32_t dex_pc = kNoDexPc)
4490 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004491
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004492 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004493
Roland Levillain9867bc72015-08-05 10:21:34 +01004494 template <typename T, typename U>
4495 auto Compute(T x, U y) const -> decltype(x | y) { return x | y; }
4496
4497 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004498 return GetBlock()->GetGraph()->GetIntConstant(
4499 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004500 }
4501 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004502 return GetBlock()->GetGraph()->GetLongConstant(
4503 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004504 }
4505 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004506 return GetBlock()->GetGraph()->GetLongConstant(
4507 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004508 }
4509 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004510 return GetBlock()->GetGraph()->GetLongConstant(
4511 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004512 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004513 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4514 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4515 LOG(FATAL) << DebugName() << " is not defined for float values";
4516 UNREACHABLE();
4517 }
4518 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4519 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4520 LOG(FATAL) << DebugName() << " is not defined for double values";
4521 UNREACHABLE();
4522 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004523
4524 DECLARE_INSTRUCTION(Or);
4525
4526 private:
4527 DISALLOW_COPY_AND_ASSIGN(HOr);
4528};
4529
4530class HXor : public HBinaryOperation {
4531 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004532 HXor(Primitive::Type result_type,
4533 HInstruction* left,
4534 HInstruction* right,
4535 uint32_t dex_pc = kNoDexPc)
4536 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004537
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004538 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004539
Roland Levillain9867bc72015-08-05 10:21:34 +01004540 template <typename T, typename U>
4541 auto Compute(T x, U y) const -> decltype(x ^ y) { return x ^ y; }
4542
4543 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004544 return GetBlock()->GetGraph()->GetIntConstant(
4545 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004546 }
4547 HConstant* Evaluate(HIntConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004548 return GetBlock()->GetGraph()->GetLongConstant(
4549 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004550 }
4551 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004552 return GetBlock()->GetGraph()->GetLongConstant(
4553 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004554 }
4555 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004556 return GetBlock()->GetGraph()->GetLongConstant(
4557 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004558 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004559 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4560 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4561 LOG(FATAL) << DebugName() << " is not defined for float values";
4562 UNREACHABLE();
4563 }
4564 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4565 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4566 LOG(FATAL) << DebugName() << " is not defined for double values";
4567 UNREACHABLE();
4568 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004569
4570 DECLARE_INSTRUCTION(Xor);
4571
4572 private:
4573 DISALLOW_COPY_AND_ASSIGN(HXor);
4574};
4575
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004576class HRor : public HBinaryOperation {
4577 public:
4578 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
4579 : HBinaryOperation(result_type, value, distance) {}
4580
4581 template <typename T, typename U, typename V>
4582 T Compute(T x, U y, V max_shift_value) const {
4583 static_assert(std::is_same<V, typename std::make_unsigned<T>::type>::value,
4584 "V is not the unsigned integer type corresponding to T");
4585 V ux = static_cast<V>(x);
4586 if ((y & max_shift_value) == 0) {
4587 return static_cast<T>(ux);
4588 } else {
4589 const V reg_bits = sizeof(T) * 8;
4590 return static_cast<T>(ux >> (y & max_shift_value)) |
4591 (x << (reg_bits - (y & max_shift_value)));
4592 }
4593 }
4594
4595 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
4596 return GetBlock()->GetGraph()->GetIntConstant(
4597 Compute(x->GetValue(), y->GetValue(), kMaxIntShiftValue), GetDexPc());
4598 }
4599 HConstant* Evaluate(HLongConstant* x, HIntConstant* y) const OVERRIDE {
4600 return GetBlock()->GetGraph()->GetLongConstant(
4601 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4602 }
4603 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
4604 return GetBlock()->GetGraph()->GetLongConstant(
4605 Compute(x->GetValue(), y->GetValue(), kMaxLongShiftValue), GetDexPc());
4606 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004607 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4608 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4609 LOG(FATAL) << DebugName() << " is not defined for float values";
4610 UNREACHABLE();
4611 }
4612 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4613 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4614 LOG(FATAL) << DebugName() << " is not defined for double values";
4615 UNREACHABLE();
4616 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004617
4618 DECLARE_INSTRUCTION(Ror);
4619
4620 private:
4621 DISALLOW_COPY_AND_ASSIGN(HRor);
4622};
4623
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004624// The value of a parameter in this method. Its location depends on
4625// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07004626class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004627 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004628 HParameterValue(const DexFile& dex_file,
4629 uint16_t type_index,
4630 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004631 Primitive::Type parameter_type,
4632 bool is_this = false)
4633 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004634 dex_file_(dex_file),
4635 type_index_(type_index),
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004636 index_(index),
4637 is_this_(is_this),
4638 can_be_null_(!is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004639
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004640 const DexFile& GetDexFile() const { return dex_file_; }
4641 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004642 uint8_t GetIndex() const { return index_; }
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004643 bool IsThis() const { return is_this_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004644
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004645 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4646 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004647
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004648 DECLARE_INSTRUCTION(ParameterValue);
4649
4650 private:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004651 const DexFile& dex_file_;
4652 const uint16_t type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004653 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004654 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004655 const uint8_t index_;
4656
Calin Juravle10e244f2015-01-26 18:54:32 +00004657 // Whether or not the parameter value corresponds to 'this' argument.
4658 const bool is_this_;
4659
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07004660 bool can_be_null_;
4661
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004662 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4663};
4664
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004665class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004666 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004667 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4668 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004669
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004670 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004671 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004672 return true;
4673 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004674
Roland Levillain9867bc72015-08-05 10:21:34 +01004675 template <typename T> T Compute(T x) const { return ~x; }
4676
4677 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004678 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004679 }
4680 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004681 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004682 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004683 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4684 LOG(FATAL) << DebugName() << " is not defined for float values";
4685 UNREACHABLE();
4686 }
4687 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4688 LOG(FATAL) << DebugName() << " is not defined for double values";
4689 UNREACHABLE();
4690 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004691
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004692 DECLARE_INSTRUCTION(Not);
4693
4694 private:
4695 DISALLOW_COPY_AND_ASSIGN(HNot);
4696};
4697
David Brazdil66d126e2015-04-03 16:02:44 +01004698class HBooleanNot : public HUnaryOperation {
4699 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004700 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4701 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004702
4703 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004704 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004705 return true;
4706 }
4707
Roland Levillain9867bc72015-08-05 10:21:34 +01004708 template <typename T> bool Compute(T x) const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004709 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004710 return !x;
4711 }
4712
Roland Levillain9867bc72015-08-05 10:21:34 +01004713 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004714 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004715 }
4716 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4717 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004718 UNREACHABLE();
4719 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004720 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4721 LOG(FATAL) << DebugName() << " is not defined for float values";
4722 UNREACHABLE();
4723 }
4724 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4725 LOG(FATAL) << DebugName() << " is not defined for double values";
4726 UNREACHABLE();
4727 }
David Brazdil66d126e2015-04-03 16:02:44 +01004728
4729 DECLARE_INSTRUCTION(BooleanNot);
4730
4731 private:
4732 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4733};
4734
Roland Levillaindff1f282014-11-05 14:15:05 +00004735class HTypeConversion : public HExpression<1> {
4736 public:
4737 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00004738 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004739 : HExpression(result_type,
4740 SideEffectsForArchRuntimeCalls(input->GetType(), result_type),
4741 dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00004742 SetRawInputAt(0, input);
4743 DCHECK_NE(input->GetType(), result_type);
4744 }
4745
4746 HInstruction* GetInput() const { return InputAt(0); }
4747 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
4748 Primitive::Type GetResultType() const { return GetType(); }
4749
4750 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00004751 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00004752
Mark Mendelle82549b2015-05-06 10:55:34 -04004753 // Try to statically evaluate the conversion and return a HConstant
4754 // containing the result. If the input cannot be converted, return nullptr.
4755 HConstant* TryStaticEvaluation() const;
4756
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004757 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type input_type,
4758 Primitive::Type result_type) {
4759 // Some architectures may not require the 'GC' side effects, but at this point
4760 // in the compilation process we do not know what architecture we will
4761 // generate code for, so we must be conservative.
Roland Levillaindf3f8222015-08-13 12:31:44 +01004762 if ((Primitive::IsFloatingPointType(input_type) && Primitive::IsIntegralType(result_type))
4763 || (input_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(result_type))) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004764 return SideEffects::CanTriggerGC();
4765 }
4766 return SideEffects::None();
4767 }
4768
Roland Levillaindff1f282014-11-05 14:15:05 +00004769 DECLARE_INSTRUCTION(TypeConversion);
4770
4771 private:
4772 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
4773};
4774
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00004775static constexpr uint32_t kNoRegNumber = -1;
4776
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004777class HPhi : public HInstruction {
4778 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004779 HPhi(ArenaAllocator* arena,
4780 uint32_t reg_number,
4781 size_t number_of_inputs,
4782 Primitive::Type type,
4783 uint32_t dex_pc = kNoDexPc)
4784 : HInstruction(SideEffects::None(), dex_pc),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004785 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004786 reg_number_(reg_number),
David Brazdil809d70f2015-11-19 10:29:39 +00004787 type_(ToPhiType(type)),
4788 // Phis are constructed live and marked dead if conflicting or unused.
4789 // Individual steps of SsaBuilder should assume that if a phi has been
4790 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
4791 is_live_(true),
Calin Juravle10e244f2015-01-26 18:54:32 +00004792 can_be_null_(true) {
David Brazdil809d70f2015-11-19 10:29:39 +00004793 DCHECK_NE(type_, Primitive::kPrimVoid);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004794 }
4795
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00004796 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
4797 static Primitive::Type ToPhiType(Primitive::Type type) {
4798 switch (type) {
4799 case Primitive::kPrimBoolean:
4800 case Primitive::kPrimByte:
4801 case Primitive::kPrimShort:
4802 case Primitive::kPrimChar:
4803 return Primitive::kPrimInt;
4804 default:
4805 return type;
4806 }
4807 }
4808
David Brazdilffee3d32015-07-06 11:48:53 +01004809 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
4810
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004811 size_t InputCount() const OVERRIDE { return inputs_.size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004812
4813 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01004814 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004815
Calin Juravle10e244f2015-01-26 18:54:32 +00004816 Primitive::Type GetType() const OVERRIDE { return type_; }
David Brazdil4833f5a2015-12-16 10:37:39 +00004817 void SetType(Primitive::Type new_type) {
4818 // Make sure that only valid type changes occur. The following are allowed:
4819 // (1) int -> float/ref (primitive type propagation),
4820 // (2) long -> double (primitive type propagation).
4821 DCHECK(type_ == new_type ||
4822 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
4823 (type_ == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
4824 (type_ == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
4825 type_ = new_type;
4826 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004827
Calin Juravle10e244f2015-01-26 18:54:32 +00004828 bool CanBeNull() const OVERRIDE { return can_be_null_; }
4829 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
4830
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004831 uint32_t GetRegNumber() const { return reg_number_; }
4832
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004833 void SetDead() { is_live_ = false; }
4834 void SetLive() { is_live_ = true; }
4835 bool IsDead() const { return !is_live_; }
4836 bool IsLive() const { return is_live_; }
4837
David Brazdil77a48ae2015-09-15 12:34:04 +00004838 bool IsVRegEquivalentOf(HInstruction* other) const {
4839 return other != nullptr
4840 && other->IsPhi()
4841 && other->AsPhi()->GetBlock() == GetBlock()
4842 && other->AsPhi()->GetRegNumber() == GetRegNumber();
4843 }
4844
Calin Juravlea4f88312015-04-16 12:57:19 +01004845 // Returns the next equivalent phi (starting from the current one) or null if there is none.
4846 // An equivalent phi is a phi having the same dex register and type.
4847 // It assumes that phis with the same dex register are adjacent.
4848 HPhi* GetNextEquivalentPhiWithSameType() {
4849 HInstruction* next = GetNext();
4850 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
4851 if (next->GetType() == GetType()) {
4852 return next->AsPhi();
4853 }
4854 next = next->GetNext();
4855 }
4856 return nullptr;
4857 }
4858
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004859 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004860
David Brazdil1abb4192015-02-17 18:33:36 +00004861 protected:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004862 const HUserRecord<HInstruction*> InputRecordAt(size_t index) const OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004863 return inputs_[index];
4864 }
David Brazdil1abb4192015-02-17 18:33:36 +00004865
4866 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004867 inputs_[index] = input;
David Brazdil1abb4192015-02-17 18:33:36 +00004868 }
4869
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004870 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004871 ArenaVector<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004872 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004873 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01004874 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00004875 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004876
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004877 DISALLOW_COPY_AND_ASSIGN(HPhi);
4878};
4879
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004880class HNullCheck : public HExpression<1> {
4881 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004882 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
4883 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004884 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00004885 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004886 SetRawInputAt(0, value);
4887 }
4888
Calin Juravle10e244f2015-01-26 18:54:32 +00004889 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004890 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004891 return true;
4892 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004893
Calin Juravle10e244f2015-01-26 18:54:32 +00004894 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004895
Calin Juravle10e244f2015-01-26 18:54:32 +00004896 bool CanThrow() const OVERRIDE { return true; }
4897
4898 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01004899
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004900
4901 DECLARE_INSTRUCTION(NullCheck);
4902
4903 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004904 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
4905};
4906
4907class FieldInfo : public ValueObject {
4908 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004909 FieldInfo(MemberOffset field_offset,
4910 Primitive::Type field_type,
4911 bool is_volatile,
4912 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004913 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004914 const DexFile& dex_file,
4915 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004916 : field_offset_(field_offset),
4917 field_type_(field_type),
4918 is_volatile_(is_volatile),
4919 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07004920 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07004921 dex_file_(dex_file),
4922 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004923
4924 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004925 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004926 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07004927 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004928 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00004929 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07004930 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004931
4932 private:
4933 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01004934 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00004935 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004936 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07004937 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004938 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07004939 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004940};
4941
4942class HInstanceFieldGet : public HExpression<1> {
4943 public:
4944 HInstanceFieldGet(HInstruction* value,
4945 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004946 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01004947 bool is_volatile,
4948 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07004949 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07004950 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004951 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01004952 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07004953 : HExpression(field_type,
4954 SideEffects::FieldReadOfType(field_type, is_volatile),
4955 dex_pc),
4956 field_info_(field_offset,
4957 field_type,
4958 is_volatile,
4959 field_idx,
4960 declaring_class_def_index,
4961 dex_file,
4962 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004963 SetRawInputAt(0, value);
4964 }
4965
Calin Juravle10c9cbe2014-12-19 10:50:19 +00004966 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004967
4968 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
4969 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
4970 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004971 }
4972
Calin Juravle641547a2015-04-21 22:08:51 +01004973 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
4974 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00004975 }
4976
4977 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01004978 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
4979 }
4980
Calin Juravle52c48962014-12-16 17:02:57 +00004981 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004982 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01004983 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00004984 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004985
4986 DECLARE_INSTRUCTION(InstanceFieldGet);
4987
4988 private:
4989 const FieldInfo field_info_;
4990
4991 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
4992};
4993
4994class HInstanceFieldSet : public HTemplateInstruction<2> {
4995 public:
4996 HInstanceFieldSet(HInstruction* object,
4997 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01004998 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00004999 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005000 bool is_volatile,
5001 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005002 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005003 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005004 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005005 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005006 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5007 dex_pc),
5008 field_info_(field_offset,
5009 field_type,
5010 is_volatile,
5011 field_idx,
5012 declaring_class_def_index,
5013 dex_file,
5014 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005015 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005016 SetRawInputAt(0, object);
5017 SetRawInputAt(1, value);
5018 }
5019
Calin Juravle641547a2015-04-21 22:08:51 +01005020 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5021 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00005022 }
5023
Calin Juravle52c48962014-12-16 17:02:57 +00005024 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005025 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005026 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005027 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005028 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005029 bool GetValueCanBeNull() const { return value_can_be_null_; }
5030 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005031
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005032 DECLARE_INSTRUCTION(InstanceFieldSet);
5033
5034 private:
5035 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005036 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005037
5038 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5039};
5040
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005041class HArrayGet : public HExpression<2> {
5042 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005043 HArrayGet(HInstruction* array,
5044 HInstruction* index,
5045 Primitive::Type type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005046 uint32_t dex_pc,
5047 SideEffects additional_side_effects = SideEffects::None())
5048 : HExpression(type,
5049 SideEffects::ArrayReadOfType(type).Union(additional_side_effects),
David Brazdil2bd4c5c2015-11-04 22:48:45 +00005050 dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005051 SetRawInputAt(0, array);
5052 SetRawInputAt(1, index);
5053 }
5054
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005055 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005056 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005057 return true;
5058 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005059 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005060 // TODO: We can be smarter here.
5061 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5062 // which generates the implicit null check. There are cases when these can be removed
5063 // to produce better code. If we ever add optimizations to do so we should allow an
5064 // implicit check here (as long as the address falls in the first page).
5065 return false;
5066 }
5067
David Brazdil4833f5a2015-12-16 10:37:39 +00005068 bool IsEquivalentOf(HArrayGet* other) const {
5069 bool result = (GetDexPc() == other->GetDexPc());
5070 if (kIsDebugBuild && result) {
5071 DCHECK_EQ(GetBlock(), other->GetBlock());
5072 DCHECK_EQ(GetArray(), other->GetArray());
5073 DCHECK_EQ(GetIndex(), other->GetIndex());
5074 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005075 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005076 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005077 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5078 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005079 }
5080 }
5081 return result;
5082 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005083
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005084 HInstruction* GetArray() const { return InputAt(0); }
5085 HInstruction* GetIndex() const { return InputAt(1); }
5086
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005087 DECLARE_INSTRUCTION(ArrayGet);
5088
5089 private:
5090 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5091};
5092
5093class HArraySet : public HTemplateInstruction<3> {
5094 public:
5095 HArraySet(HInstruction* array,
5096 HInstruction* index,
5097 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005098 Primitive::Type expected_component_type,
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005099 uint32_t dex_pc,
5100 SideEffects additional_side_effects = SideEffects::None())
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005101 : HTemplateInstruction(
5102 SideEffects::ArrayWriteOfType(expected_component_type).Union(
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005103 SideEffectsForArchRuntimeCalls(value->GetType())).Union(
5104 additional_side_effects),
5105 dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005106 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005107 needs_type_check_(value->GetType() == Primitive::kPrimNot),
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005108 value_can_be_null_(true),
5109 static_type_of_array_is_object_array_(false) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005110 SetRawInputAt(0, array);
5111 SetRawInputAt(1, index);
5112 SetRawInputAt(2, value);
5113 }
5114
Calin Juravle77520bc2015-01-12 18:45:46 +00005115 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005116 // We call a runtime method to throw ArrayStoreException.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005117 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005118 }
5119
Mingyao Yang81014cb2015-06-02 03:16:27 -07005120 // Can throw ArrayStoreException.
5121 bool CanThrow() const OVERRIDE { return needs_type_check_; }
5122
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005123 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005124 // TODO: Same as for ArrayGet.
5125 return false;
5126 }
5127
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005128 void ClearNeedsTypeCheck() {
5129 needs_type_check_ = false;
5130 }
5131
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005132 void ClearValueCanBeNull() {
5133 value_can_be_null_ = false;
5134 }
5135
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005136 void SetStaticTypeOfArrayIsObjectArray() {
5137 static_type_of_array_is_object_array_ = true;
5138 }
5139
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005140 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005141 bool NeedsTypeCheck() const { return needs_type_check_; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005142 bool StaticTypeOfArrayIsObjectArray() const { return static_type_of_array_is_object_array_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005143
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005144 HInstruction* GetArray() const { return InputAt(0); }
5145 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005146 HInstruction* GetValue() const { return InputAt(2); }
5147
5148 Primitive::Type GetComponentType() const {
5149 // The Dex format does not type floating point index operations. Since the
5150 // `expected_component_type_` is set during building and can therefore not
5151 // be correct, we also check what is the value type. If it is a floating
5152 // point type, we must use that type.
5153 Primitive::Type value_type = GetValue()->GetType();
5154 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5155 ? value_type
5156 : expected_component_type_;
5157 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005158
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005159 Primitive::Type GetRawExpectedComponentType() const {
5160 return expected_component_type_;
5161 }
5162
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005163 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5164 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5165 }
5166
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005167 DECLARE_INSTRUCTION(ArraySet);
5168
5169 private:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005170 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005171 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005172 bool value_can_be_null_;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005173 // Cached information for the reference_type_info_ so that codegen
5174 // does not need to inspect the static type.
5175 bool static_type_of_array_is_object_array_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005176
5177 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5178};
5179
5180class HArrayLength : public HExpression<1> {
5181 public:
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01005182 HArrayLength(HInstruction* array, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005183 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005184 // Note that arrays do not change length, so the instruction does not
5185 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005186 SetRawInputAt(0, array);
5187 }
5188
Calin Juravle77520bc2015-01-12 18:45:46 +00005189 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005190 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005191 return true;
5192 }
Calin Juravle641547a2015-04-21 22:08:51 +01005193 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5194 return obj == InputAt(0);
5195 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005196
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005197 DECLARE_INSTRUCTION(ArrayLength);
5198
5199 private:
5200 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5201};
5202
5203class HBoundsCheck : public HExpression<2> {
5204 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005205 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5206 // constructor.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005207 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005208 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005209 DCHECK(index->GetType() == Primitive::kPrimInt);
5210 SetRawInputAt(0, index);
5211 SetRawInputAt(1, length);
5212 }
5213
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005214 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005215 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005216 return true;
5217 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005218
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005219 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005220
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005221 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005222
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005223 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005224
5225 DECLARE_INSTRUCTION(BoundsCheck);
5226
5227 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005228 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5229};
5230
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005231class HSuspendCheck : public HTemplateInstruction<0> {
5232 public:
5233 explicit HSuspendCheck(uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005234 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005235
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005236 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005237 return true;
5238 }
5239
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005240 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5241 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005242
5243 DECLARE_INSTRUCTION(SuspendCheck);
5244
5245 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005246 // Only used for code generation, in order to share the same slow path between back edges
5247 // of a same loop.
5248 SlowPathCode* slow_path_;
5249
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005250 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5251};
5252
David Srbecky0cf44932015-12-09 14:09:59 +00005253// Pseudo-instruction which provides the native debugger with mapping information.
5254// It ensures that we can generate line number and local variables at this point.
5255class HNativeDebugInfo : public HTemplateInstruction<0> {
5256 public:
5257 explicit HNativeDebugInfo(uint32_t dex_pc)
5258 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5259
5260 bool NeedsEnvironment() const OVERRIDE {
5261 return true;
5262 }
5263
5264 DECLARE_INSTRUCTION(NativeDebugInfo);
5265
5266 private:
5267 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5268};
5269
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005270/**
5271 * Instruction to load a Class object.
5272 */
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005273class HLoadClass : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005274 public:
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005275 HLoadClass(HCurrentMethod* current_method,
5276 uint16_t type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005277 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005278 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005279 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005280 bool needs_access_check,
5281 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005282 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005283 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005284 dex_file_(dex_file),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005285 is_referrers_class_(is_referrers_class),
Calin Juravleb1498f62015-02-16 13:13:29 +00005286 generate_clinit_check_(false),
Calin Juravle98893e12015-10-02 21:05:03 +01005287 needs_access_check_(needs_access_check),
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005288 is_in_dex_cache_(is_in_dex_cache),
Calin Juravle2e768302015-07-28 14:41:11 +00005289 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005290 // Referrers class should not need access check. We never inline unverified
5291 // methods so we can't possibly end up in this situation.
5292 DCHECK(!is_referrers_class_ || !needs_access_check_);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005293 SetRawInputAt(0, current_method);
5294 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005295
5296 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005297
5298 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravlea9a306d2015-10-08 16:48:31 +01005299 // Note that we don't need to test for generate_clinit_check_.
5300 // Whether or not we need to generate the clinit check is processed in
5301 // prepare_for_register_allocator based on existing HInvokes and HClinitChecks.
Calin Juravle386062d2015-10-07 18:55:43 +01005302 return other->AsLoadClass()->type_index_ == type_index_ &&
5303 other->AsLoadClass()->needs_access_check_ == needs_access_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005304 }
5305
5306 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
5307
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005308 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005309 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005310 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005311
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005312 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005313 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005314 }
5315
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005316 bool MustGenerateClinitCheck() const {
5317 return generate_clinit_check_;
5318 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005319
Calin Juravle0ba218d2015-05-19 18:46:01 +01005320 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005321 // The entrypoint the code generator is going to call does not do
5322 // clinit of the class.
5323 DCHECK(!NeedsAccessCheck());
Calin Juravle0ba218d2015-05-19 18:46:01 +01005324 generate_clinit_check_ = generate_clinit_check;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005325 }
5326
5327 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005328 return MustGenerateClinitCheck() ||
5329 (!is_referrers_class_ && !is_in_dex_cache_) ||
5330 needs_access_check_;
Calin Juravle98893e12015-10-02 21:05:03 +01005331 }
5332
5333 bool NeedsAccessCheck() const {
5334 return needs_access_check_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005335 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005336
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005337 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005338 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005339 }
5340
Calin Juravleacf735c2015-02-12 15:25:22 +00005341 ReferenceTypeInfo GetLoadedClassRTI() {
5342 return loaded_class_rti_;
5343 }
5344
5345 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5346 // Make sure we only set exact types (the loaded class should never be merged).
5347 DCHECK(rti.IsExact());
5348 loaded_class_rti_ = rti;
5349 }
5350
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005351 const DexFile& GetDexFile() { return dex_file_; }
5352
Vladimir Markodc151b22015-10-15 18:02:30 +01005353 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return !is_referrers_class_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005354
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005355 static SideEffects SideEffectsForArchRuntimeCalls() {
5356 return SideEffects::CanTriggerGC();
5357 }
5358
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005359 bool IsInDexCache() const { return is_in_dex_cache_; }
5360
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005361 DECLARE_INSTRUCTION(LoadClass);
5362
5363 private:
5364 const uint16_t type_index_;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005365 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005366 const bool is_referrers_class_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005367 // Whether this instruction must generate the initialization check.
5368 // Used for code generation.
5369 bool generate_clinit_check_;
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005370 const bool needs_access_check_;
5371 const bool is_in_dex_cache_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005372
Calin Juravleacf735c2015-02-12 15:25:22 +00005373 ReferenceTypeInfo loaded_class_rti_;
5374
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005375 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5376};
5377
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005378class HLoadString : public HExpression<1> {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005379 public:
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005380 HLoadString(HCurrentMethod* current_method,
5381 uint32_t string_index,
5382 uint32_t dex_pc,
5383 bool is_in_dex_cache)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005384 : HExpression(Primitive::kPrimNot, SideEffectsForArchRuntimeCalls(), dex_pc),
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005385 string_index_(string_index),
5386 is_in_dex_cache_(is_in_dex_cache) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005387 SetRawInputAt(0, current_method);
5388 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005389
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005390 bool CanBeMoved() const OVERRIDE { return true; }
5391
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005392 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
5393 return other->AsLoadString()->string_index_ == string_index_;
5394 }
5395
5396 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
5397
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005398 uint32_t GetStringIndex() const { return string_index_; }
5399
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005400 // Will call the runtime if the string is not already in the dex cache.
5401 bool NeedsEnvironment() const OVERRIDE { return !IsInDexCache(); }
5402
Vladimir Markodc151b22015-10-15 18:02:30 +01005403 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE { return true; }
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005404 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005405 bool IsInDexCache() const { return is_in_dex_cache_; }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005406 bool CanThrow() const OVERRIDE { return !IsInDexCache(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005407
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005408 static SideEffects SideEffectsForArchRuntimeCalls() {
5409 return SideEffects::CanTriggerGC();
5410 }
5411
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005412 DECLARE_INSTRUCTION(LoadString);
5413
5414 private:
5415 const uint32_t string_index_;
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005416 const bool is_in_dex_cache_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005417
5418 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5419};
5420
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005421/**
5422 * Performs an initialization check on its Class object input.
5423 */
5424class HClinitCheck : public HExpression<1> {
5425 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005426 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005427 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005428 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005429 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5430 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005431 SetRawInputAt(0, constant);
5432 }
5433
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005434 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005435 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005436 return true;
5437 }
5438
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005439 bool NeedsEnvironment() const OVERRIDE {
5440 // May call runtime to initialize the class.
5441 return true;
5442 }
5443
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005444 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005445
5446 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5447
5448 DECLARE_INSTRUCTION(ClinitCheck);
5449
5450 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005451 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5452};
5453
5454class HStaticFieldGet : public HExpression<1> {
5455 public:
5456 HStaticFieldGet(HInstruction* cls,
5457 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005458 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005459 bool is_volatile,
5460 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005461 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005462 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005463 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005464 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005465 : HExpression(field_type,
5466 SideEffects::FieldReadOfType(field_type, is_volatile),
5467 dex_pc),
5468 field_info_(field_offset,
5469 field_type,
5470 is_volatile,
5471 field_idx,
5472 declaring_class_def_index,
5473 dex_file,
5474 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005475 SetRawInputAt(0, cls);
5476 }
5477
Calin Juravle52c48962014-12-16 17:02:57 +00005478
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005479 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005480
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005481 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00005482 HStaticFieldGet* other_get = other->AsStaticFieldGet();
5483 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005484 }
5485
5486 size_t ComputeHashCode() const OVERRIDE {
5487 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5488 }
5489
Calin Juravle52c48962014-12-16 17:02:57 +00005490 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005491 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5492 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005493 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005494
5495 DECLARE_INSTRUCTION(StaticFieldGet);
5496
5497 private:
5498 const FieldInfo field_info_;
5499
5500 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5501};
5502
5503class HStaticFieldSet : public HTemplateInstruction<2> {
5504 public:
5505 HStaticFieldSet(HInstruction* cls,
5506 HInstruction* value,
5507 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005508 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005509 bool is_volatile,
5510 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005511 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005512 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005513 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005514 uint32_t dex_pc)
Mingyao Yang8df69d42015-10-22 15:40:58 -07005515 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile),
5516 dex_pc),
5517 field_info_(field_offset,
5518 field_type,
5519 is_volatile,
5520 field_idx,
5521 declaring_class_def_index,
5522 dex_file,
5523 dex_cache),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005524 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005525 SetRawInputAt(0, cls);
5526 SetRawInputAt(1, value);
5527 }
5528
Calin Juravle52c48962014-12-16 17:02:57 +00005529 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005530 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5531 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005532 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005533
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005534 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005535 bool GetValueCanBeNull() const { return value_can_be_null_; }
5536 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005537
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005538 DECLARE_INSTRUCTION(StaticFieldSet);
5539
5540 private:
5541 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005542 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005543
5544 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
5545};
5546
Calin Juravlee460d1d2015-09-29 04:52:17 +01005547class HUnresolvedInstanceFieldGet : public HExpression<1> {
5548 public:
5549 HUnresolvedInstanceFieldGet(HInstruction* obj,
5550 Primitive::Type field_type,
5551 uint32_t field_index,
5552 uint32_t dex_pc)
5553 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5554 field_index_(field_index) {
5555 SetRawInputAt(0, obj);
5556 }
5557
5558 bool NeedsEnvironment() const OVERRIDE { return true; }
5559 bool CanThrow() const OVERRIDE { return true; }
5560
5561 Primitive::Type GetFieldType() const { return GetType(); }
5562 uint32_t GetFieldIndex() const { return field_index_; }
5563
5564 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
5565
5566 private:
5567 const uint32_t field_index_;
5568
5569 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
5570};
5571
5572class HUnresolvedInstanceFieldSet : public HTemplateInstruction<2> {
5573 public:
5574 HUnresolvedInstanceFieldSet(HInstruction* obj,
5575 HInstruction* value,
5576 Primitive::Type field_type,
5577 uint32_t field_index,
5578 uint32_t dex_pc)
5579 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5580 field_type_(field_type),
5581 field_index_(field_index) {
5582 DCHECK_EQ(field_type, value->GetType());
5583 SetRawInputAt(0, obj);
5584 SetRawInputAt(1, value);
5585 }
5586
5587 bool NeedsEnvironment() const OVERRIDE { return true; }
5588 bool CanThrow() const OVERRIDE { return true; }
5589
5590 Primitive::Type GetFieldType() const { return field_type_; }
5591 uint32_t GetFieldIndex() const { return field_index_; }
5592
5593 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
5594
5595 private:
5596 const Primitive::Type field_type_;
5597 const uint32_t field_index_;
5598
5599 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
5600};
5601
5602class HUnresolvedStaticFieldGet : public HExpression<0> {
5603 public:
5604 HUnresolvedStaticFieldGet(Primitive::Type field_type,
5605 uint32_t field_index,
5606 uint32_t dex_pc)
5607 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
5608 field_index_(field_index) {
5609 }
5610
5611 bool NeedsEnvironment() const OVERRIDE { return true; }
5612 bool CanThrow() const OVERRIDE { return true; }
5613
5614 Primitive::Type GetFieldType() const { return GetType(); }
5615 uint32_t GetFieldIndex() const { return field_index_; }
5616
5617 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
5618
5619 private:
5620 const uint32_t field_index_;
5621
5622 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
5623};
5624
5625class HUnresolvedStaticFieldSet : public HTemplateInstruction<1> {
5626 public:
5627 HUnresolvedStaticFieldSet(HInstruction* value,
5628 Primitive::Type field_type,
5629 uint32_t field_index,
5630 uint32_t dex_pc)
5631 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
5632 field_type_(field_type),
5633 field_index_(field_index) {
5634 DCHECK_EQ(field_type, value->GetType());
5635 SetRawInputAt(0, value);
5636 }
5637
5638 bool NeedsEnvironment() const OVERRIDE { return true; }
5639 bool CanThrow() const OVERRIDE { return true; }
5640
5641 Primitive::Type GetFieldType() const { return field_type_; }
5642 uint32_t GetFieldIndex() const { return field_index_; }
5643
5644 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
5645
5646 private:
5647 const Primitive::Type field_type_;
5648 const uint32_t field_index_;
5649
5650 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
5651};
5652
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005653// Implement the move-exception DEX instruction.
5654class HLoadException : public HExpression<0> {
5655 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005656 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
5657 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005658
David Brazdilbbd733e2015-08-18 17:48:17 +01005659 bool CanBeNull() const OVERRIDE { return false; }
5660
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005661 DECLARE_INSTRUCTION(LoadException);
5662
5663 private:
5664 DISALLOW_COPY_AND_ASSIGN(HLoadException);
5665};
5666
David Brazdilcb1c0552015-08-04 16:22:25 +01005667// Implicit part of move-exception which clears thread-local exception storage.
5668// Must not be removed because the runtime expects the TLS to get cleared.
5669class HClearException : public HTemplateInstruction<0> {
5670 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005671 explicit HClearException(uint32_t dex_pc = kNoDexPc)
5672 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01005673
5674 DECLARE_INSTRUCTION(ClearException);
5675
5676 private:
5677 DISALLOW_COPY_AND_ASSIGN(HClearException);
5678};
5679
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005680class HThrow : public HTemplateInstruction<1> {
5681 public:
5682 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005683 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005684 SetRawInputAt(0, exception);
5685 }
5686
5687 bool IsControlFlow() const OVERRIDE { return true; }
5688
5689 bool NeedsEnvironment() const OVERRIDE { return true; }
5690
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005691 bool CanThrow() const OVERRIDE { return true; }
5692
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005693
5694 DECLARE_INSTRUCTION(Throw);
5695
5696 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005697 DISALLOW_COPY_AND_ASSIGN(HThrow);
5698};
5699
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005700/**
5701 * Implementation strategies for the code generator of a HInstanceOf
5702 * or `HCheckCast`.
5703 */
5704enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01005705 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005706 kExactCheck, // Can do a single class compare.
5707 kClassHierarchyCheck, // Can just walk the super class chain.
5708 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
5709 kInterfaceCheck, // No optimization yet when checking against an interface.
5710 kArrayObjectCheck, // Can just check if the array is not primitive.
5711 kArrayCheck // No optimization yet when checking against a generic array.
5712};
5713
Roland Levillain86503782016-02-11 19:07:30 +00005714std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
5715
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005716class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005717 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005718 HInstanceOf(HInstruction* object,
5719 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005720 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005721 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005722 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005723 SideEffectsForArchRuntimeCalls(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005724 dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005725 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005726 must_do_null_check_(true) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005727 SetRawInputAt(0, object);
5728 SetRawInputAt(1, constant);
5729 }
5730
5731 bool CanBeMoved() const OVERRIDE { return true; }
5732
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005733 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005734 return true;
5735 }
5736
5737 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005738 return CanCallRuntime(check_kind_);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005739 }
5740
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005741 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
5742
5743 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005744
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005745 // Used only in code generation.
5746 bool MustDoNullCheck() const { return must_do_null_check_; }
5747 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
5748
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005749 static bool CanCallRuntime(TypeCheckKind check_kind) {
5750 // Mips currently does runtime calls for any other checks.
5751 return check_kind != TypeCheckKind::kExactCheck;
5752 }
5753
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005754 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005755 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005756 }
5757
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005758 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005759
5760 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005761 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005762 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005763
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005764 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
5765};
5766
Calin Juravleb1498f62015-02-16 13:13:29 +00005767class HBoundType : public HExpression<1> {
5768 public:
David Brazdilf5552582015-12-27 13:36:12 +00005769 HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005770 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
David Brazdilf5552582015-12-27 13:36:12 +00005771 upper_bound_(ReferenceTypeInfo::CreateInvalid()),
5772 upper_can_be_null_(true),
5773 can_be_null_(true) {
Calin Juravle61d544b2015-02-23 16:46:57 +00005774 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00005775 SetRawInputAt(0, input);
5776 }
5777
David Brazdilf5552582015-12-27 13:36:12 +00005778 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005779 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
5780 bool GetUpperCanBeNull() const { return upper_can_be_null_; }
David Brazdilf5552582015-12-27 13:36:12 +00005781 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00005782
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005783 void SetCanBeNull(bool can_be_null) {
5784 DCHECK(upper_can_be_null_ || !can_be_null);
5785 can_be_null_ = can_be_null;
Calin Juravleb1498f62015-02-16 13:13:29 +00005786 }
5787
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005788 bool CanBeNull() const OVERRIDE { return can_be_null_; }
5789
Calin Juravleb1498f62015-02-16 13:13:29 +00005790 DECLARE_INSTRUCTION(BoundType);
5791
5792 private:
5793 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005794 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
5795 // It is used to bound the type in cases like:
5796 // if (x instanceof ClassX) {
5797 // // uper_bound_ will be ClassX
5798 // }
David Brazdilf5552582015-12-27 13:36:12 +00005799 ReferenceTypeInfo upper_bound_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005800 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
5801 // is false then can_be_null_ cannot be true).
David Brazdilf5552582015-12-27 13:36:12 +00005802 bool upper_can_be_null_;
Calin Juravlea5ae3c32015-07-28 14:40:50 +00005803 bool can_be_null_;
Calin Juravleb1498f62015-02-16 13:13:29 +00005804
5805 DISALLOW_COPY_AND_ASSIGN(HBoundType);
5806};
5807
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005808class HCheckCast : public HTemplateInstruction<2> {
5809 public:
5810 HCheckCast(HInstruction* object,
5811 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005812 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005813 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005814 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005815 check_kind_(check_kind),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005816 must_do_null_check_(true) {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005817 SetRawInputAt(0, object);
5818 SetRawInputAt(1, constant);
5819 }
5820
5821 bool CanBeMoved() const OVERRIDE { return true; }
5822
5823 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5824 return true;
5825 }
5826
5827 bool NeedsEnvironment() const OVERRIDE {
5828 // Instruction may throw a CheckCastError.
5829 return true;
5830 }
5831
5832 bool CanThrow() const OVERRIDE { return true; }
5833
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005834 bool MustDoNullCheck() const { return must_do_null_check_; }
5835 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005836 TypeCheckKind GetTypeCheckKind() const { return check_kind_; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005837
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005838 bool IsExactCheck() const { return check_kind_ == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005839
5840 DECLARE_INSTRUCTION(CheckCast);
5841
5842 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005843 const TypeCheckKind check_kind_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005844 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005845
5846 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005847};
5848
Calin Juravle27df7582015-04-17 19:12:31 +01005849class HMemoryBarrier : public HTemplateInstruction<0> {
5850 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005851 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07005852 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005853 SideEffects::AllWritesAndReads(), dex_pc), // Assume write/read on all fields/arrays.
Calin Juravle27df7582015-04-17 19:12:31 +01005854 barrier_kind_(barrier_kind) {}
5855
5856 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
5857
5858 DECLARE_INSTRUCTION(MemoryBarrier);
5859
5860 private:
5861 const MemBarrierKind barrier_kind_;
5862
5863 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
5864};
5865
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005866class HMonitorOperation : public HTemplateInstruction<1> {
5867 public:
5868 enum OperationKind {
5869 kEnter,
5870 kExit,
5871 };
5872
5873 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005874 : HTemplateInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005875 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
5876 kind_(kind) {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005877 SetRawInputAt(0, object);
5878 }
5879
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005880 // Instruction may go into runtime, so we need an environment.
5881 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00005882
5883 bool CanThrow() const OVERRIDE {
5884 // Verifier guarantees that monitor-exit cannot throw.
5885 // This is important because it allows the HGraphBuilder to remove
5886 // a dead throw-catch loop generated for `synchronized` blocks/methods.
5887 return IsEnter();
5888 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005889
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005890
5891 bool IsEnter() const { return kind_ == kEnter; }
5892
5893 DECLARE_INSTRUCTION(MonitorOperation);
5894
Calin Juravle52c48962014-12-16 17:02:57 +00005895 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005896 const OperationKind kind_;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005897
5898 private:
5899 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
5900};
5901
David Brazdil74eb1b22015-12-14 11:44:01 +00005902class HSelect : public HExpression<3> {
5903 public:
5904 HSelect(HInstruction* condition,
5905 HInstruction* true_value,
5906 HInstruction* false_value,
5907 uint32_t dex_pc)
5908 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
5909 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
5910
5911 // First input must be `true_value` or `false_value` to allow codegens to
5912 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
5913 // that architectures which implement HSelect as a conditional move also
5914 // will not need to invert the condition.
5915 SetRawInputAt(0, false_value);
5916 SetRawInputAt(1, true_value);
5917 SetRawInputAt(2, condition);
5918 }
5919
5920 HInstruction* GetFalseValue() const { return InputAt(0); }
5921 HInstruction* GetTrueValue() const { return InputAt(1); }
5922 HInstruction* GetCondition() const { return InputAt(2); }
5923
5924 bool CanBeMoved() const OVERRIDE { return true; }
5925 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
5926
5927 bool CanBeNull() const OVERRIDE {
5928 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
5929 }
5930
5931 DECLARE_INSTRUCTION(Select);
5932
5933 private:
5934 DISALLOW_COPY_AND_ASSIGN(HSelect);
5935};
5936
Vladimir Markof9f64412015-09-02 14:05:49 +01005937class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005938 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01005939 MoveOperands(Location source,
5940 Location destination,
5941 Primitive::Type type,
5942 HInstruction* instruction)
5943 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005944
5945 Location GetSource() const { return source_; }
5946 Location GetDestination() const { return destination_; }
5947
5948 void SetSource(Location value) { source_ = value; }
5949 void SetDestination(Location value) { destination_ = value; }
5950
5951 // The parallel move resolver marks moves as "in-progress" by clearing the
5952 // destination (but not the source).
5953 Location MarkPending() {
5954 DCHECK(!IsPending());
5955 Location dest = destination_;
5956 destination_ = Location::NoLocation();
5957 return dest;
5958 }
5959
5960 void ClearPending(Location dest) {
5961 DCHECK(IsPending());
5962 destination_ = dest;
5963 }
5964
5965 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00005966 DCHECK(source_.IsValid() || destination_.IsInvalid());
5967 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005968 }
5969
5970 // True if this blocks a move from the given location.
5971 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08005972 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005973 }
5974
5975 // A move is redundant if it's been eliminated, if its source and
5976 // destination are the same, or if its destination is unneeded.
5977 bool IsRedundant() const {
5978 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
5979 }
5980
5981 // We clear both operands to indicate move that's been eliminated.
5982 void Eliminate() {
5983 source_ = destination_ = Location::NoLocation();
5984 }
5985
5986 bool IsEliminated() const {
5987 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
5988 return source_.IsInvalid();
5989 }
5990
Alexey Frunze4dda3372015-06-01 18:31:49 -07005991 Primitive::Type GetType() const { return type_; }
5992
Nicolas Geoffray90218252015-04-15 11:56:51 +01005993 bool Is64BitMove() const {
5994 return Primitive::Is64BitType(type_);
5995 }
5996
Nicolas Geoffray740475d2014-09-29 10:33:25 +01005997 HInstruction* GetInstruction() const { return instruction_; }
5998
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005999 private:
6000 Location source_;
6001 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006002 // The type this move is for.
6003 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006004 // The instruction this move is assocatied with. Null when this move is
6005 // for moving an input in the expected locations of user (including a phi user).
6006 // This is only used in debug mode, to ensure we do not connect interval siblings
6007 // in the same parallel move.
6008 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006009};
6010
Roland Levillainc9285912015-12-18 10:38:42 +00006011std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6012
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006013static constexpr size_t kDefaultNumberOfMoves = 4;
6014
6015class HParallelMove : public HTemplateInstruction<0> {
6016 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006017 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006018 : HTemplateInstruction(SideEffects::None(), dex_pc),
6019 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6020 moves_.reserve(kDefaultNumberOfMoves);
6021 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006022
Nicolas Geoffray90218252015-04-15 11:56:51 +01006023 void AddMove(Location source,
6024 Location destination,
6025 Primitive::Type type,
6026 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006027 DCHECK(source.IsValid());
6028 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006029 if (kIsDebugBuild) {
6030 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006031 for (const MoveOperands& move : moves_) {
6032 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006033 // Special case the situation where the move is for the spill slot
6034 // of the instruction.
6035 if ((GetPrevious() == instruction)
6036 || ((GetPrevious() == nullptr)
6037 && instruction->IsPhi()
6038 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006039 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006040 << "Doing parallel moves for the same instruction.";
6041 } else {
6042 DCHECK(false) << "Doing parallel moves for the same instruction.";
6043 }
6044 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006045 }
6046 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006047 for (const MoveOperands& move : moves_) {
6048 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006049 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006050 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006051 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006052 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006053 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006054 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006055 }
6056
Vladimir Marko225b6462015-09-28 12:17:40 +01006057 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006058 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006059 }
6060
Vladimir Marko225b6462015-09-28 12:17:40 +01006061 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006062
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006063 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006064
6065 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006066 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006067
6068 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6069};
6070
Mark Mendell0616ae02015-04-17 12:49:27 -04006071} // namespace art
6072
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006073#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6074#include "nodes_shared.h"
6075#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006076#ifdef ART_ENABLE_CODEGEN_arm
6077#include "nodes_arm.h"
6078#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006079#ifdef ART_ENABLE_CODEGEN_arm64
6080#include "nodes_arm64.h"
6081#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006082#ifdef ART_ENABLE_CODEGEN_x86
6083#include "nodes_x86.h"
6084#endif
6085
6086namespace art {
6087
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006088class HGraphVisitor : public ValueObject {
6089 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006090 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6091 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006092
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006093 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006094 virtual void VisitBasicBlock(HBasicBlock* block);
6095
Roland Levillain633021e2014-10-01 14:12:25 +01006096 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006097 void VisitInsertionOrder();
6098
Roland Levillain633021e2014-10-01 14:12:25 +01006099 // Visit the graph following dominator tree reverse post-order.
6100 void VisitReversePostOrder();
6101
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006102 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006103
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006104 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006105#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006106 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6107
6108 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6109
6110#undef DECLARE_VISIT_INSTRUCTION
6111
6112 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006113 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006114
6115 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6116};
6117
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006118class HGraphDelegateVisitor : public HGraphVisitor {
6119 public:
6120 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6121 virtual ~HGraphDelegateVisitor() {}
6122
6123 // Visit functions that delegate to to super class.
6124#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006125 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006126
6127 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6128
6129#undef DECLARE_VISIT_INSTRUCTION
6130
6131 private:
6132 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6133};
6134
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006135class HInsertionOrderIterator : public ValueObject {
6136 public:
6137 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
6138
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006139 bool Done() const { return index_ == graph_.GetBlocks().size(); }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006140 HBasicBlock* Current() const { return graph_.GetBlocks()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006141 void Advance() { ++index_; }
6142
6143 private:
6144 const HGraph& graph_;
6145 size_t index_;
6146
6147 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
6148};
6149
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006150class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006151 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00006152 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
6153 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006154 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006155 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006156
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006157 bool Done() const { return index_ == graph_.GetReversePostOrder().size(); }
6158 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006159 void Advance() { ++index_; }
6160
6161 private:
6162 const HGraph& graph_;
6163 size_t index_;
6164
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006165 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006166};
6167
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006168class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006169 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006170 explicit HPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006171 : graph_(graph), index_(graph_.GetReversePostOrder().size()) {
David Brazdil10f56cb2015-03-24 18:49:14 +00006172 // Check that reverse post order of the graph has been built.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006173 DCHECK(!graph.GetReversePostOrder().empty());
David Brazdil10f56cb2015-03-24 18:49:14 +00006174 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006175
6176 bool Done() const { return index_ == 0; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006177 HBasicBlock* Current() const { return graph_.GetReversePostOrder()[index_ - 1u]; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006178 void Advance() { --index_; }
6179
6180 private:
6181 const HGraph& graph_;
6182 size_t index_;
6183
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01006184 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01006185};
6186
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006187class HLinearPostOrderIterator : public ValueObject {
6188 public:
6189 explicit HLinearPostOrderIterator(const HGraph& graph)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006190 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().size()) {}
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006191
6192 bool Done() const { return index_ == 0; }
6193
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006194 HBasicBlock* Current() const { return order_[index_ - 1u]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006195
6196 void Advance() {
6197 --index_;
6198 DCHECK_GE(index_, 0U);
6199 }
6200
6201 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006202 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006203 size_t index_;
6204
6205 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
6206};
6207
6208class HLinearOrderIterator : public ValueObject {
6209 public:
6210 explicit HLinearOrderIterator(const HGraph& graph)
6211 : order_(graph.GetLinearOrder()), index_(0) {}
6212
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006213 bool Done() const { return index_ == order_.size(); }
6214 HBasicBlock* Current() const { return order_[index_]; }
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006215 void Advance() { ++index_; }
6216
6217 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006218 const ArenaVector<HBasicBlock*>& order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01006219 size_t index_;
6220
6221 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
6222};
6223
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006224// Iterator over the blocks that art part of the loop. Includes blocks part
6225// of an inner loop. The order in which the blocks are iterated is on their
6226// block id.
6227class HBlocksInLoopIterator : public ValueObject {
6228 public:
6229 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6230 : blocks_in_loop_(info.GetBlocks()),
6231 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6232 index_(0) {
6233 if (!blocks_in_loop_.IsBitSet(index_)) {
6234 Advance();
6235 }
6236 }
6237
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006238 bool Done() const { return index_ == blocks_.size(); }
6239 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006240 void Advance() {
6241 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006242 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006243 if (blocks_in_loop_.IsBitSet(index_)) {
6244 break;
6245 }
6246 }
6247 }
6248
6249 private:
6250 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006251 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006252 size_t index_;
6253
6254 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6255};
6256
Mingyao Yang3584bce2015-05-19 16:01:59 -07006257// Iterator over the blocks that art part of the loop. Includes blocks part
6258// of an inner loop. The order in which the blocks are iterated is reverse
6259// post order.
6260class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6261 public:
6262 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6263 : blocks_in_loop_(info.GetBlocks()),
6264 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6265 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006266 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006267 Advance();
6268 }
6269 }
6270
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006271 bool Done() const { return index_ == blocks_.size(); }
6272 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006273 void Advance() {
6274 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006275 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6276 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006277 break;
6278 }
6279 }
6280 }
6281
6282 private:
6283 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006284 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006285 size_t index_;
6286
6287 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6288};
6289
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006290inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006291 if (constant->IsIntConstant()) {
6292 return constant->AsIntConstant()->GetValue();
6293 } else if (constant->IsLongConstant()) {
6294 return constant->AsLongConstant()->GetValue();
6295 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006296 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006297 return 0;
6298 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006299}
6300
Vladimir Marko58155012015-08-19 12:49:41 +00006301inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
6302 // For the purposes of the compiler, the dex files must actually be the same object
6303 // if we want to safely treat them as the same. This is especially important for JIT
6304 // as custom class loaders can open the same underlying file (or memory) multiple
6305 // times and provide different class resolution but no two class loaders should ever
6306 // use the same DexFile object - doing so is an unsupported hack that can lead to
6307 // all sorts of weird failures.
6308 return &lhs == &rhs;
6309}
6310
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006311#define INSTRUCTION_TYPE_CHECK(type, super) \
6312 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6313 inline const H##type* HInstruction::As##type() const { \
6314 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6315 } \
6316 inline H##type* HInstruction::As##type() { \
6317 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6318 }
6319
6320 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6321#undef INSTRUCTION_TYPE_CHECK
6322
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00006323class SwitchTable : public ValueObject {
6324 public:
6325 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse)
6326 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) {
6327 int32_t table_offset = instruction.VRegB_31t();
6328 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset;
6329 if (sparse) {
6330 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
6331 } else {
6332 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
6333 }
6334 num_entries_ = table[1];
6335 values_ = reinterpret_cast<const int32_t*>(&table[2]);
6336 }
6337
6338 uint16_t GetNumEntries() const {
6339 return num_entries_;
6340 }
6341
6342 void CheckIndex(size_t index) const {
6343 if (sparse_) {
6344 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6345 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_));
6346 } else {
6347 // In a packed table, we have the starting key and num_entries_ values.
6348 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_));
6349 }
6350 }
6351
6352 int32_t GetEntryAt(size_t index) const {
6353 CheckIndex(index);
6354 return values_[index];
6355 }
6356
6357 uint32_t GetDexPcForIndex(size_t index) const {
6358 CheckIndex(index);
6359 return dex_pc_ +
6360 (reinterpret_cast<const int16_t*>(values_ + index) -
6361 reinterpret_cast<const int16_t*>(&instruction_));
6362 }
6363
6364 // Index of the first value in the table.
6365 size_t GetFirstValueIndex() const {
6366 if (sparse_) {
6367 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order.
6368 return num_entries_;
6369 } else {
6370 // In a packed table, we have the starting key and num_entries_ values.
6371 return 1;
6372 }
6373 }
6374
6375 private:
6376 const Instruction& instruction_;
6377 const uint32_t dex_pc_;
6378
6379 // Whether this is a sparse-switch table (or a packed-switch one).
6380 const bool sparse_;
6381
6382 // This can't be const as it needs to be computed off of the given instruction, and complicated
6383 // expressions in the initializer list seemed very ugly.
6384 uint16_t num_entries_;
6385
6386 const int32_t* values_;
6387
6388 DISALLOW_COPY_AND_ASSIGN(SwitchTable);
6389};
6390
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006391// Create space in `blocks` for adding `number_of_new_blocks` entries
6392// starting at location `at`. Blocks after `at` are moved accordingly.
6393inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6394 size_t number_of_new_blocks,
6395 size_t after) {
6396 DCHECK_LT(after, blocks->size());
6397 size_t old_size = blocks->size();
6398 size_t new_size = old_size + number_of_new_blocks;
6399 blocks->resize(new_size);
6400 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6401}
6402
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006403} // namespace art
6404
6405#endif // ART_COMPILER_OPTIMIZING_NODES_H_